$(document).ready(function(){

//	var url = "http://maps.googleapis.com/maps/api/staticmap?size=640x640&scale=1&markers=icon:http://socialmix.me/assets/images/marker.png%7C224+West+20th+Street+NY%7C75+9th+Ave+NY%7C700+E+9th+St+NY&sensor=true";
//	var marker = "";
//	var static_map = 'url("'+url+'")';
//alert(url);
//$('#map_canvas').css('background',static_map);	

/*
	var pt = new google.maps.LatLng(41.878114,-87.629798);
	
	  var myOptions = {
      zoom: 16,
      center: pt,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

  
  
     var map = new google.maps.Map(document.getElementById("map_canvas"),  myOptions);


     var marker = new google.maps.Marker({
	 	position: pt,
	 	title: 'title',
	 	icon: 'http://socialmix.me/assets/images/marker.png',
         visible: true,
	 	map: map
	 });
	 
	 	var title = "Meet new people";
        var contentString = document.createElement("div");
        contentString.className = 'inner_infowindow';
		var title = "<a class='title' href='<?php echo base_url(); ?>events/get_event/x/x' target='_blank'>"+title+"</a>";		
        var link = "<a class='link' href='<?php echo base_url(); ?>events/get_event/x/x' target='_blank'>Check it Out</a>";        
        var owner = "Created by: <img height='40' width='40' src='/assets/images/people/default.png'></a>";
		var content = "<p>Anyone one interested in basketball want to meet up?</p>"
        contentString.innerHTML = title + content + owner;
		var boxText = document.createElement("div");
		boxText.className = 'infowindow';
		
		boxText.appendChild(contentString);
 
		var myOptions = {
			 content: boxText
			,disableAutoPan: false
			,maxWidth: 0
			,pixelOffset: new google.maps.Size(-140, 0)
			,zIndex: null
			,boxStyle: { 
			  opacity: 1
			 }
			,closeBoxMargin: "10px 2px 2px 2px"
			,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
			,infoBoxClearance: new google.maps.Size(1, 1)
			,isHidden: false
			,pane: "floatPane"
			,enableEventPropagation: false
		};
		
		title = "Do stuff you like";
        contentString = document.createElement("div");
        contentString.className = 'inner_infowindow';
		title = "<a class='title' href='<?php echo base_url(); ?>events/get_event/x/x' target='_blank'>"+title+"</a>";		
        link = "<a class='link' href='<?php echo base_url(); ?>events/get_event/x/x' target='_blank'>Check it Out</a>";        
        owner = "Created by: <img height='40' width='40' src='/assets/images/people/default.png'></a>";
		content = "<p>If you subscribe to softball, you will discover people who also like softball and softball events in your world.</p>"
        contentString.innerHTML = title + content + owner;
		boxText = document.createElement("div");
		boxText.className = 'infowindow';
		
		boxText.appendChild(contentString);
		
		var pt = new google.maps.LatLng(41.8775935, -87.6207929);
	
	     var marker2 = new google.maps.Marker({
		 	position: pt,
		 	title: 'title',
		 	icon: 'http://socialmix.me/assets/images/marker_softball.png',
	         visible: true,
		 	map: map
		 });
	 
		var myOptions2 = {
			 content: boxText
			,disableAutoPan: false
			,maxWidth: 0
			,pixelOffset: new google.maps.Size(-140, 0)
			,zIndex: null
			,boxStyle: { 
			  opacity: 1
			 }
			,closeBoxMargin: "10px 2px 2px 2px"
			,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
			,infoBoxClearance: new google.maps.Size(1, 1)
			,isHidden: false
			,pane: "floatPane"
			,enableEventPropagation: false
		};
 
		var ib = new InfoBox(myOptions);
		var ib2 = new InfoBox(myOptions2);
		
		
		ib.open(map, marker);
		ib2.open(map, marker2);
		
		*/

		
  	/*
      

function addMarker(static_map) {

	var pt = new google.maps.LatLng(40.7426973, -73.9982404);
	
 	var marker = new google.maps.Marker({
	 	position: pt,
	 	title: 'title',
	 	icon: 'assets/images/sm_marker.png',
	 	map: static_map
	 });
		
        var contentString = document.createElement("div");
        contentString.className = 'inner_infowindow';
		var title = "title";
        var link = "<a class='link' href='<?php echo base_url(); ?>events/get_event/"+event_id+"/"+source+"' target='_blank'>Check it Out</a>";
        
        contentString.innerHTML = title; //+ owner  + link;
                    
		var boxText = document.createElement("div");
                boxText.className = 'infowindow';
		//boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: #FFF; padding: 5px;";
		boxText.appendChild(contentString);
 
		var myOptions = {
			 content: boxText
			,disableAutoPan: false
			,maxWidth: 0
			,pixelOffset: new google.maps.Size(-140, 0)
			,zIndex: null
			,boxStyle: { 
			  opacity: 1
			 }
			,closeBoxMargin: "10px 2px 2px 2px"
			,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
			,infoBoxClearance: new google.maps.Size(1, 1)
			,isHidden: false
			,pane: "floatPane"
			,enableEventPropagation: false
		};
 
		google.maps.event.addListener(marker, "click", function (e) {
			ib.open(map, this);
		});
 
		var ib = new InfoBox(myOptions);

        
 }
 */

    $('#header a[title]').qtip({
            position: {
            	at: 'bottom center',
               my: 'top center' 
            }, 
            style: {
            	classes: 'ui-tooltip-shadow ui-tooltip-tipsy'
            }
	
	});
	
	$('.match h1[title]').qtip({
            position: {
            	at: 'bottom center',
               my: 'top center' 
            }, 
            style: {
            	classes: 'ui-tooltip-shadow ui-tooltip-tipsy'
            }
	
	});
            
	$(".tab_content").hide(); //Hide all content
	//$("ul.nav a:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content


	
	//On Click Event
	/*
	 * 	$(".services").hide(); 
	$("li.person_box").mouseover(function() {

		$(".services").hide(); //Hide all tab content

		var activeTab = $(this).find(".services"); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		return false;
	});
	*/
	
	//On Click Event 
	$("#error_message a").live('click',function(e) {
		$('#message_box').fadeOut();	
	});
	
	$("#page_message a.dismiss").click(function(e) {
		$.ajax({
	        type: "POST",
	        url: '/welcome/disable_page_message',
	        success: function(data) {
					$('#page_message').fadeOut();	
	        		 return false;
	        }
		});
	});
	
	$("#a.dismiss").click(function(e) {
		alert('v');
		$(this).parent().fadeOut();	
	});
	
	$("#person_tab a").click(function() {
		
		$("#person_tab a").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	
	$("#map_canvas").html('<img id="loading" src="/assets/images/loading.gif" />');
 
	$('#rsvp_form .button').click(function(e){
		var button = $(this);
		var params = 'rsvp='+button.attr('value');
	  	params += '&'+$('#rsvp_form').serialize();
		params += "&ajax=true";
		
	  $.post("/events/rsvp/", params ,function(data) {
        $('#rsvp_form .button').parent().find('.button').removeClass('selected');
		button.addClass('selected');
	   	//alert(data);
	 });
	})
	
	$(".post_button").click(function() {
		$('#modal').dialog({
		modal: true,
                title: 'Post an Event',
			'width'		: 600,
			'height'		: 500,
			buttons: {
					Cancel: function() {
						$( this ).dialog( "close" );
					},
					Post: function() {
						post();
					}
			},
			open: function(event, ui) { $('.ui-dialog-titlebar-close').hide(); }
        })
        
	$("#inner").append('<img id="loading" src="/assets/images/loading.gif" />');
    $('#inner').load('/events/post');
	return false;
	
});

function post() {
	 	var url = '/events/post/';
	   	var params = $('#post_form').serialize();
	  
	      $.ajax({
	          type: "POST",
	           url: url,
	           data: params,
	             success: function(data) {
						$('#modal').fadeOut(function() {
								$('#modal').dialog("close");
								 $('#header').after(data); 
						}); 
	            },
	            error: function(data) {
	            	  $('#header').after(data.responseText); 
				  }
	      }); 
        return false;
}

$("#video").click(function() {
	$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		: 680,
			'height'		: 495,
			'href'			: $("#video").attr('href').replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			   	 'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});

	return false;
});

$("#search_button").live('click',function(e) {
	e.preventDefault();
	var city = $("#city").val();
	var tag = $("#tag").val();
	var page = $(this).attr("name");
	var form_link = '/search/index/city/'+encodeURI(city)+'/tag/'+encodeURI(tag);
	window.location = form_link;
});

$("#header_search").live('submit',function(e) {
	e.preventDefault();
	var city = $("#location").val();
	var tag = $(this).find(':input').val();
	var form_link = '/search/index/city/'+encodeURI(city)+'/tag/'+encodeURI(tag);
	window.location = form_link;
});

  

$('.detailed').live('click', function(event) {
/*	window.location.replace($(this).find('a').attr('href')); */

});
$('.listItem').live('mouseover mouseout', function(event) {

  if (event.type == 'mouseover') {
      $(this).addClass("hover");
      $(this).find('#rsvp_status').show();
      $(this).find('#meta').show();
  } else {
    $(this).removeClass("hover");
    $(this).find('#rsvp_status').hide();
    $(this).find('#meta').hide();
  }
});


$('.detailed').live('mouseover mouseout', function(event) {
  if (event.type == 'mouseover') {
	/* $(this).find('.title').css("text-decoration", "underline");*/
  		if(!$(this).hasClass("cal_date")) {
      	$(this).addClass("hover");
      }
  } else {
	/* $(this).find('.title').css("text-decoration", "none"); */
    $(this).removeClass("hover");
  }
});

	

  
  $("li.category").click(function() {
var cat =$(this).attr('id');

var listitems = $('.listItem');

        for ( i = 0; i < listitems.length; i++ ) {
        
        if(cat == '') {listitems[i].style.display = 'block';} 
        else 
          if(listitems[i].getAttribute("name") != cat) {listitems[i].style.display = 'none';} else 
          {
          listitems[i].style.display = 'block';
          }

        }
    
 
  });



  $("#edit-event").click(function() {
        $( "#edit-form" ).dialog( "open" );
});

    

  $("#location a").click(function() {
    $(this).hide(); 
    $('#location_form').show();
  });
  
  $("#eventtab").click(function() {
    $('#list').html(ajax_load).load('get.php'); 
    /* $('#list').append('<div></div>').load('meetup.php'); */
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
  });

	$("#profiletabX").click(function() {
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
    
      $('#profile').show();
    $('#events').hide();
    $('#followers').hide();
    $('#following').hide();
  });
  
  $("#eventstab").click(function() {
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
    
      $('#events').show();
    $('#profile').hide();
    $('#followers').hide();
    $('#following').hide();
  }); 
    
  $("#followerstab").click(function() {
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
    
      $('#followers').show();
    $('#profile').hide();
    $('#events').hide();
    $('#following').hide();
  });
  
  $("#followingtab").click(function() {
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
    
      $('#following').show(); 
      $('#events').hide();  
    $('#profile').hide();
    $('#events').hide();
    $('#followers').hide();
  });
  
  $("#alltab").click(function() {
  //  $('#list').append("<div></div>").html(ajax_load).load('peoplelist.php'); 
    $('#list').append("<div></div>").load('get.php'); 
   // $('#list').appendChild().load('meetup.php'); 
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
  });

  $("#meetuptab").click(function() {
    $('#list').html(ajax_load).load('meetup.php'); 
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
  });
  
  
  $("#linkuptab").click(function() {
    $('#list').html(ajax_load).load('getPosts.php'); 
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
  });
    
  $("#peopletab").click(function() {
    $('#list').html(ajax_load).load('peoplelist.php'); 
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
  });
  
  $("#eventbritetab").click(function() {
    $('#list').html(ajax_load).load('eventbrite.php'); 
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
  });
  $("#dealstab").click(function() {
    $('#list').html(ajax_load).load('groupon.php'); 
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
  });
  
  $(".category").click(function() {
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
  });

  $('#view_options a').click(function() {
    var view = $(this).attr('id');
    //toggle link border
      $("#view_options").find('img').removeClass('selected');
      $(this).find('img').addClass('selected');
       $('.cal_date').toggle();
      
      //toggle link icon    
    $('#list_view ul li').toggleClass(function() {
      if ($(this).is('.gallery')) {
        $(this).removeClass('gallery');
        return 'detailed';
      } else if ($(this).is('.detailed')) {
       $(this).removeClass('detailed');
        return 'gallery';
      }
    });

        
    //toggle div display area
    if(view == 'map'){
      $("#list_view").css("display","none");
      $("#display_map").css("display","block");
      getCurrentLocation();

      } else {
        $("#display_map").css("display","none");
        $("#list_view").css("display","block");
      }
      

  });

  $("#order_button").live('click',function() {
    $("#orderby .button").toggleClass('active');
    $("#orderby ul").toggle();
  }); 
  
  //filter
  $("#option").live('click',function() {
    $("#filter_area").toggle();
    $(this).toggleClass('active');
    $(".filter_arrow").toggleClass('up');
  }); 
  
  $(".people a").live('click',function() {
   $("#list_view").html('<img id="loading" src="assets/images/loading.gif" />');
            var order = "desc";
            var criteria = $(this).attr("value");
            var field = criteria;
            
            if(field=='match'){field='common'};
            
            if($(this).hasClass("desc")) { 
              order = "asc"; 
              $(this).removeClass("desc");
              $(this).addClass("asc");
            } else {
              $(this).removeClass("desc");
              $(this).addClass("desc");
              order = "desc";
            }
            if($("#paginate").hasClass("first")) {$("#previous").addClass("disabled")};
            
            var params =  "page="+$(".currentpage").text();   
            params += "&sort="+field;
            params += "&order="+order;
            
            $.ajax({
              type: "GET",
               url: 'people/people/get_person_list/'+field+'/'+order,
                 success: function(data) {
                     $("#list_view").html(data);   
                     $("#orderby .button").removeClass('active');
                     $("#order_by").html("sort by "+criteria);
                     $("#orderby ul").hide();
                     $(".order_arrow").toggleClass('up');
                 }  
                 
               });
               return false;
  }); 
  
 	$('#location').bind('change', function () {       		
 		
			var location = encodeURIComponent($(this).val()); 
 		if($("#filter_area").attr('name') == 'people') {
       			var page = 'people';
       		};
                     
			$("#list_view").html('<img class="loading" id="loading" src="/assets/images/loading.gif" />');
   
            //get order
            if(page != 'people') {
            	var date = $("#filter_area .date .selected").attr('name');//get tag
            }
            var tag = $("#filter_area .tags .selected").attr('name');
			
			
             if(page != 'people') {
            	var url =   '/events/index/location/'+location+'/tag/'+tag+'/date/'+date+'/ajax/FALSE/'; 
            }   else {
            	var url =   '/people/index/location/'+location+'/tag/'+tag+'/ajax/FALSE/'; 
            }
            
          if (url) { // require a URL
              window.location = url; // redirect
          }
          return false;
      });
  
  
  $("#filter_area  a").live('click',function() {
       		if($("#filter_area").attr('name') == 'people') {
       			var page = 'people';
       		};
                     $(this).parent().parent().find('a').removeClass('selected'); 
                     $(this).addClass('selected');
                     
   $("#list_view").html('<img class="loading" id="loading" src="/assets/images/loading.gif" />');
   
            //get order
            if(page != 'people') {
            	var date = $("#filter_area .date .selected").attr('name');//get tag
            }
            var tag = $("#filter_area .tags .selected").attr('name');
			var location = $("#location").attr('value');
			
             if(page != 'people') {
            	var url =   '/events/index/location/'+location+'/tag/'+tag+'/date/'+date+'/ajax/FALSE/'; 
            }   else {
            	var url =   '/people/index/location/'+location+'/tag/'+tag+'/ajax/FALSE/'; 
            }
            
          //  alert(url);

			//wndow.open(url);
		window.open (url,'_self',false);
			/*
            $.ajax({
              type: "GET",
               url: url,
                 success: function(data) {
                     $("#list_view").html(data); 
                     var url = $("#paginate").find('a').attr('href');
                 }  
                 
               });
               return false;
               */
  });  
 
  
  $(".follow").live('click',function() {
			var user_id = $(this).attr('id');
			var element =  $(this);
			if($(this).find('button').hasClass('selected')) {
				var action = 'unfollow';
			} else {
				var action = 'follow';
			} 
			
			var url = '/person/follow/'+user_id+'/'+action;
			//alert(url);
            $.ajax({
              type: "GET",
               url: url,
                 success: function(data) {
                     alert(data);
                     if(action == 'follow') {
						 element.find('button').addClass('selected');
						 element.find('a').text('Unfollow');
						} else {
						 element.find('button').removeClass('selected');
						  element.find('a').text('Follow');
						}
						location.reload();
                     
                 }  
                 
               });
           
  });  
  

  $("#results a").live('click',function() {
            $("#next").removeClass("disabled");
            $("#previous").removeClass("disabled");
            if($("#paginate").hasClass("last")) {$("#next").addClass("disabled")} ;
            if($("#paginate").hasClass("first")) {$("#previous").addClass("disabled")};
            
            if($(this).hasClass("disabled")) { } else {
          /*  window.location.hash = $(this).attr("value"); */
            
            var hash = $(".currentpage").text();/* window.location.hash.substring(1); */
            $("#next").attr("value",++hash);
            hash = $(".currentpage").text();
            $("#previous").attr("value",--hash);
            var params = "page="+$(this).attr("value");
            $.ajax({
              type: "GET",
               url: 'get.php',
               data: params,
                 success: function(data) {

                 
                     $("#list_view").html(data);
                     return false;
                     
                     
                 }  
                 
               });
               return false;
            }
  });   
  
   if (!Modernizr.input.placeholder)
        {
                var placeholderText = $('#search').attr('placeholder');

                $('#search').attr('value',placeholderText);
                $('#search').addClass('placeholder');

                $('#search').focus(function() {
                        if( ($('#search').val() == placeholderText) )
                        {
                                $('#search').attr('value','');
                                $('#search').removeClass('placeholder');
                        }
                });

                $('#search').blur(function() {
                        if ( ($('#search').val() == placeholderText) || (($('#search').val() == '')) )
                        {
                                $('#search').addClass('placeholder');
                                $('#search').attr('value',placeholderText);
                        }
                });
        }


    }); 
