var LS_Featured_Pages = 0;
var LS_Featured_Page = 1;
var LS_Featured_Image;
var LS_Featured_Image_Width = 576;
var LS_Featured_Image_Spacing = 15;
var LS_Featured_Excerpt;
var LS_Featured_Excerpt_Width = 495;

var LS_Results_Container;
var LS_Results_Pages;
var LS_Results_Page_Width;
var LS_Results_Page = 1;
var LS_Results_SearchQuery = "";
var LS_Order = "mostpopular";

var LS_Featured_Pages = 0;
var LS_Featured_Page = 1;
var LS_Featured_Image;
var LS_Featured_Image_Width = 576;
var LS_Featured_Image_Spacing = 15;
var LS_Featured_Excerpt;
var LS_Featured_Excerpt_Width = 495;

var LS_Pages, LS_Page_Width;
var LS_Page_Count = 0;
var LS_Page_Number = 1;
var LS_Item_Count = 0;
var LS_ItemsPerPage = 0;

var LS_Items = [];

var bFirstLoad = true
var bHideDialog = false;
var sLatestReviews = "";
var sSearchResults = "";

var objMap, objCenterPoint;
var aMarkers = [];

$j(function(){
	LS_Featured_Pages = $j("#ls_featured div.image div.container div").length;
	LS_Featured_Image = $j("#ls_featured div.image div.container");
	LS_Featured_Image_Item_Width = LS_Featured_Image_Width + LS_Featured_Image_Spacing;
	LS_Featured_Image.width((LS_Featured_Image_Item_Width * LS_Featured_Pages));
	
	LS_Featured_Excerpt = $j("#ls_featured div.excerpt div.container");
	LS_Featured_Excerpt_Item_Width = LS_Featured_Excerpt_Width;
	LS_Featured_Excerpt.width((LS_Featured_Excerpt_Item_Width * LS_Featured_Pages));
	
	var objImage = new Image();
	objImage.src = "/images/listings-dialog.gif";
	
	LS_Featured_Pages = $j("#ls_featured div.image div.container div").length;
	LS_Featured_Image = $j("#ls_featured div.image div.container");
	//LS_Featured_Image_Item_Width = LS_Featured_Image.width() + LS_Featured_Image_Spacing;
	LS_Featured_Image_Item_Width = LS_Featured_Image_Width + LS_Featured_Image_Spacing;
	LS_Featured_Image.width((LS_Featured_Image_Item_Width * LS_Featured_Pages));
	
	LS_Featured_Excerpt = $j("#ls_featured div.excerpt div.container");
	//LS_Featured_Excerpt_Item_Width = LS_Featured_Excerpt.width();
	LS_Featured_Excerpt_Item_Width = LS_Featured_Excerpt_Width;
	LS_Featured_Excerpt.width((LS_Featured_Excerpt_Item_Width * LS_Featured_Pages));
	
	if(GBrowserIsCompatible()){
		objMap = new GMap2(document.getElementById("ls_map"));
		objMap.addControl(new GLargeMapControl());
		objMap.addControl(new GOverviewMapControl());
		objCenterPoint = new GLatLng(43.67016874852075, -79.38682079315186);
		objMap.setCenter(objCenterPoint, 13);
	}
	
	LS_Update();
});

function AG_Vote(){
	$j("#ls_recommend").html("One moment please...");
	$j.post("/includes/listings-section-bars.php", {
		cmd : "recommend",
		artist_id : iArtistID
	},
	function(sResponse){
		try{
			var objResponse = eval(sResponse);
		}
		catch(e){}
		if(objResponse){
			if(objResponse.success){
				$j("#ls_recommend").html("Recommended! (" + objResponse.votes + ")");
			}
			else{
				$j("#ls_recommend").html("[Sorry, an error ocurred.]");
			}
		}
		else{
			$j("#ls_recommend").html("[Sorry, an error ocurred.]");
		}
	});	
}
	
function LS_Featured_Scroll(iStep){
	LS_Featured_Page += iStep;
	if(LS_Featured_Page < 1){
		LS_Featured_Page = 1;
		return;
	}
	else if(LS_Featured_Page > LS_Featured_Pages){
		LS_Featured_Page = 1;
	}
	LS_Featured_Image.animate({left: ((LS_Featured_Image_Item_Width) * (LS_Featured_Page - 1) * -1)});
	LS_Featured_Excerpt.animate({left: ((LS_Featured_Excerpt_Item_Width) * (LS_Featured_Page - 1) * -1)});
}

function LS_Update(){
	if(!bFirstLoad){
		$j("#ls_loading").css({
			left : Math.round(($j("#ls_content").width() / 2) - ($j("#ls_loading").width() / 2)) + "px",
			top : "20px"
		});
		$j("#ls_content").animate({opacity : 0.5}, "fast");
		$j("#ls_loading").fadeIn("fast");
	}
	$j.post("/includes/listings-section-bars.php", {
		cmd : "load-section",
		type : $j("#ls_type")[0].value,
		neighborhood : $j("#ls_neighborhood")[0].value,
		order: LS_Order
	}, function(sResponse){
		try{
			bFirstLoad = false;
			var objResponse = eval(sResponse);
			var LS_Items = objResponse.items;
			var aPoints = [];
			aMarkers = [];
			objMap.clearOverlays();
			
			$j("#ls_content").html(objResponse.html);
			LS_Pages_Prepare();
			
			for(i=0; i<LS_Items.length; i++){
				objListing = LS_Items[i];
				if(objListing.latitude == 0 || objListing.longitude == 0) continue;
				iPage = Math.ceil((i + 1) / LS_ItemsPerPage);
				objPoint = new GLatLng(objListing.latitude, objListing.longitude);
				objMarker = new GMarker(objPoint, {title:objListing.name});
				objListing.page = iPage;
				objMarker.info = objListing;
				objMap.addOverlay(objMarker);
				aMarkers.push(objMarker);
				GEvent.addListener(objMarker, "click", LS_Map_Marker_Click);
				if(iPage == 1) aPoints.push(objPoint);
				else objMarker.hide();
			}
			LS_FocusMap(aPoints);
			
			$j("#ls_content").animate({opacity : 1}, "fast");
			$j("#ls_loading").fadeOut("fast");
		}
		catch(e){
			alert(e);
		}
	});
}

function LS_SortBy(sOrder){
	if(LS_Order == sOrder) return;
	LS_Order = sOrder;
	if(sOrder == "recentlyadded"){
		$j("#ls_sortby_recentlyadded").addClass("selected");
		$j("#ls_sortby_mostpopular").removeClass("selected");
	}
	else if(sOrder == "mostpopular"){
		$j("#ls_sortby_mostpopular").addClass("selected");
		$j("#ls_sortby_recentlyadded").removeClass("selected");
	}
	LS_Update();
}


function LS_FocusMap(aPoints){
	if(aPoints.length == 0) return;
	var fSumX = 0, fSumY = 0;
	var iPoints = aPoints.length;
	for(i=0; i<iPoints; i++){
		fSumX += aPoints[i].lat();
		fSumY += aPoints[i].lng();
	}
	var fAverageX = fSumX / iPoints;
	var fAverageY = fSumY / iPoints;
	fSumX = 0;
	fSumY = 0;
	for(i=0; i<iPoints; i++){
		fSumX += Math.pow((aPoints[i].lat() - fAverageX), 2);
		fSumY += Math.pow((aPoints[i].lng() - fAverageY), 2);
	}
	var fDeviationX = Math.sqrt(fSumX / (iPoints - 1));
	var fDeviationY = Math.sqrt(fSumY / (iPoints - 1));
	var iDeviationRadius = Math.sqrt(Math.pow((fDeviationX * -1), 2) + Math.pow((fDeviationY * -1), 2)) * 10;
	if(iDeviationRadius > 1){
		iZoom = 11;
	}
	else if(iDeviationRadius > 0.5){
		iZoom = 12;
	}
	else if(iDeviationRadius > 0.3){
		iZoom = 13;
	}
	else if(iDeviationRadius > 0.2){
		iZoom = 14;
	}
	else{
		iZoom = 15;
	}
	if(iDeviationRadius > 0 && iDeviationRadius <= 5){
		if(objMap.getZoom() == iZoom){
			objMap.panTo(new GLatLng(fAverageX, fAverageY));
		}
		else{
			objMap.setCenter(new GLatLng(fAverageX, fAverageY), iZoom);
		}
	}
	else{
		objMap.setCenter(objCenterPoint, 12);
	}
}

function LS_Map_Marker_Click(){
	var objListing = this.info;
	var sHTML = "<div class=\"ls_map_infowindow\"><p style=\"font-size: 15px; font-weight: bold; margin-bottom: 3px;\"><strong><a href=\"" + objListing.url + "\" target=\"_blank\">" + objListing.name + "</a></strong></p>" +
	"<br>" + objListing.address + "<br>" + objListing.phone + "<p><a href=\"" + objListing.url + "\" target=\"_blank\">View Review</a> | <a href=\"/listings/facebook-faves.php?entryid=" + objListing.entryID + "\" target=\"_blank\">Add to Faves</a></p></div>";
	this.openInfoWindowHtml(sHTML);
}

function LS_Pages_Prepare(){
	LS_Pages = $j("#ls_main div.container");
	LS_Page_Count = $j("#ls_main div.container div.page").length;
	LS_Item_Count = $j("#ls_main div.container div.item").length;
	LS_ItemsPerPage = Math.ceil(LS_Item_Count / LS_Page_Count);
	LS_Page_Width = $j("#ls_main").width();
	LS_Pages.width(LS_Page_Width * LS_Page_Count);
	LS_Page_Number = 1;
}

function LS_Pages_Go2(iPage){
	if(iPage < 1 || iPage > LS_Page_Count) return;
	LS_Page_Number = iPage;
	LS_Pages.animate({left: ((LS_Page_Width * (LS_Page_Number - 1)) * -1)});
	$j("#ls_nav div").removeClass("selected");
	$j("#ls_nav div:nth-child(" + (iPage + 1) + ")").addClass("selected");
	
	var aPoints = [];
	for(i=0; i<aMarkers.length; i++){
		var objMarker = aMarkers[i];
		if(objMarker.info.page == iPage){
			objMarker.show();
			aPoints.push(new GLatLng(objMarker.info.latitude, objMarker.info.longitude));
		}
		else{
			objMarker.hide();
		}
	}
	LS_FocusMap(aPoints);
}

function LS_Pages_Previous(){
	LS_Pages_Go2(LS_Page_Number - 1);
}

function LS_Pages_Next(){
	LS_Pages_Go2(LS_Page_Number + 1);
}

function LS_Pages_Last(){
	LS_Pages_Go2(LS_Page_Count);
}