var highlighted; //GLOBAL relates to var where, memorizes highlighted row
var mapRequest;
var mapCallback = false;

function tableEvents(firstLoad) { //must be reset each time data is loaded (by sorting)
	var where; //kind of like highlighted, but local to this function
	$(".sidebar tr:not(.red)").hover(function() {
		$(this).addClass("quickhighlight");
	}, function() {
		$(this).removeClass("quickhighlight");
	});
	$("#property-list tr").click(function() {
		var which = this.className;
		$("#"+highlighted).removeClass("highlighted");
		where = highlighted = this.id;
		$("#"+highlighted).addClass("highlighted");
		$("#property-image").removeClass("noimage");
		$("#property-image").load("inc/image.php?im=image1&i="+where, function() {verifyImage();}); //also loads initial info/images here
		$("#specs").load("inc/info-sold.php?i="+where, function() {specEvents(where)});
		pageTracker._trackPageview('/inc/info-sold.php?i='+where);
	});
	if(firstLoad) {
		if(defaultTo != 0) {
			$("#P"+defaultTo).trigger("click");
		} else {
			where = $("#property-list tr:eq(0)")[0].id;
			highlighted = where;
		}
	} else {
		where = highlighted;
	}
	$("#"+highlighted).addClass("highlighted"); //rehighlight saved row each time
	$("#property-image").removeClass("noimage");
	$("#property-image").load("inc/image.php?im=image1&i="+where, function() {verifyImage();}); //also loads initial info/images here
	$("#specs").load("inc/info-sold.php?i="+where, function() {specEvents(where)});
}

function verifyImage() { //checks to see if an image exists, then removes loading class from div if not;
	if($("#property-image img").length==0) {
		$("#property-image").addClass("noimage");
	}
}

function specEvents(whe) { //callback function for load of specs to define events and make equal columns
	$("#lower-panel .sidebar").css("height", "auto");
	$("#specs").css("height", "auto");
	if($("#lower-panel .sidebar").height() < $("#specs").height()) {
		$("#lower-panel .sidebar").css("height", $("#specs").height()+"px");
	} else if($("#lower-panel .sidebar").height() > $("#specs").height()) {
		$("#specs").css("height", $("#lower-panel .sidebar").height()-2+"px");
	}
	$(".thumbnail").click(function() {
		$("#property-image").load("inc/image.php?im="+this.id+"&i="+whe, function() {verifyImage();});
		return false;
	});
	$("#map-single").click(function() {//map single property
		mapRequest="single";
		var myLat = $("#latitude").html();
		var myLong = $("#longitude").html();
		var myTitle = $("#my-title").html();
		var myDescription = "TYPE: "+$("#my-type").html()+'<br /><a id="lat:'+myLat+'" name="long:'+myLong+'" title="'+myTitle+'" rel="TYPE: '+$("#my-type").html()+'" class="birds-eye" href="#" onclick="zoomTo(this.id, this.name, this.title, this.rel)">Click to see location on map</a>';
		openMapSingle({lat: myLat, long: myLong, view: "birdseye"});
		//ChangeMapStyle("road");
		CreateLayer();
		AddPushpin(myTitle, myDescription);
		return false;
	});
	if(mapCallback) {
		$("#map-single").trigger("click");
		mapCallback=false;
	}
}
function loadUp() {
	$(".sidebar tr th:eq(3)").css("border-right", "none"); //cell-spacing hack (cuz I hate inline crap)
	
	$("#property-list").load("inc/property-list-sold.php?sort=price&order=up&year="+thisYear, function() {tableEvents(true)}); //callback function must set events each time
}

function openMapSingle(params) {
	$("#map-canvas").removeClass("inviz");
	$("#close-map").removeClass("inviz");
	$("#property-panel").addClass("inviz");
	$("#property-image").addClass("inviz");
	if(params.view == "birdseye") {
		GetMap(params.lat, params.long, 18, "o");
	} else {
		GetMap(params.lat, params.long, 15, "r");
	}
	//$("#map-canvas").load("inc/map.php?"+params+"&height=100&width=100");
	//more here
}

function closeMap() {
	$("#map-canvas").addClass("inviz");
	$("#close-map").addClass("inviz");
	$("#property-panel").removeClass("inviz");
	$("#property-image").removeClass("inviz");
}

var map = null;
var layer;
var shape;
var mapType = "";
function GetMap(thisLat, thisLong, zoom, maptype) {
	map = new VEMap('map-canvas');
	if(maptype != "o") {
		//map.SetDashboardSize(VEDashboardSize.Small);
		map.SetDashboardSize(VEDashboardSize.Large);
	} else {
		//map.SetDashboardSize(VEDashboardSize.Tiny);
		map.SetDashboardSize(VEDashboardSize.Large);
	}
	map.LoadMap(new VELatLong(thisLat, thisLong), zoom ,maptype ,false);
	map.onLoadMap = function(){
		//$(".MSVE_PoweredByLogo").hide(); 
		//setMapEvents();
		/*if(mapType == "birdseye") {
			ChangeMapStyle("birdseye");
			map.SetCenterAndZoom(new VELatLong(thisLat, thisLong), 18);
			mapType = "";
		}*/
	}
	CreateLayer();
}

function zoomTo(lat, long, titletext, reltext) {
	//$(".birds-eye").click(function() {
		//alert("hello");
		//var lat = this.id;
		lat = lat.replace(/lat:/, "");
		//var long = this.name;
		long = long.replace(/long:/, "");
		//var zoo = 18;
		if(mapRequest == "single") {
			mapType="road";
			GetMap(lat, long, 15, "r");
			//ChangeMapStyle("birdseye");
			CreateLayer();
			AddPushpin(titletext, reltext+'<br /><a class="map-view" href="#" onclick="zoomAway()">Click to return to birds-eye view</a>');
		} else {
			mapType="birdseye";
			GetMap(lat, long, 18, "o");
			//ChangeMapStyle("birdseye");
			CreateLayer();
			AddPushpin(titletext, reltext+'<br /><a class="map-view" href="#" onclick="zoomAway()">Click to return to map</a>');
		}

		//ChangeMapStyle("birdseye");
		//map.SetCenterAndZoom(new VELatLong(lat, long), zoom);
		//$(this).removeClass("birds-eye");
		//$(this).addClass("map-view");
		//$(this).html("Click to switch to map view");
	//});
	/*$(".map-view").click(function() {
		ChangeMapStyle("road");
		$(this).addClass("birds-eye");
		$(this).removeClass("map-view");
		$(this).html("Click to switch to bird's eye view");
	});*/
}
function zoomAway() {
	if(mapRequest=="single") {
		var thisTitle = $(".VE_Pushpin_Popup_Title").html();
		$("td.title").each(function() {
			if(this.innerHTML == '<a href="#highlight">'+thisTitle+'</a>') {
				mapCallback=true;
				$(this).parents("tr").trigger("click");
			}
		});
		//$("#map-single").trigger("click");
	} else {
		$("#map").trigger("click");
	}
}
/*function setMapEvents() {
	alert("hello");
	$(".ero").hover(function() {
		$(".birds-eye").click(function() {
			
			var lat = this.id;
			lat = lat.replace(/lat:/, "");
			var long = this.name;
			long = long.replace(/long:/, "");
			//var zoo = 18;
			mapType="birdseye";
			GetMap(lat, long, 18);
			//ChangeMapStyle("birdseye");
			//map.SetCenterAndZoom(new VELatLong(lat, long), zoom);
			$(this).removeClass("birds-eye");
			$(this).addClass("map-view");
			$(this).html("Click to switch to map view");
		});
		$(".map-view").click(function() {
			ChangeMapStyle("road");
			$(this).addClass("birds-eye");
			$(this).removeClass("map-view");
			$(this).html("Click to switch to bird's eye view");
		});
	}, function() {
		$(".birds-eye").unbind("click");
		$(".map-view").unbind("click");
	});
		
}*/

/*function ChangeMapStyle(thisStyle, that, lat, long, zoom) {//function not currently being used because of issues (haven't found way to make callback)
	//var s=map.GetMapStyle();
	if (thisStyle=="road") {
	   map.SetMapStyle(VEMapStyle.Road);
	}
	if (thisStyle=="hybrid") {
	   map.SetMapStyle(VEMapStyle.Hybrid);
	}
	if (thisStyle=="birdseye") {
	   map.SetMapStyle(VEMapStyle.Birdseye);
	   map.SetCenterAndZoom(new VELatLong(lat, long), zoom);
	}
	if (thisStyle=="birdseyehybrid") {
	   map.SetMapStyle(VEMapStyle.BirdseyeHybrid);
	}
}*/

function CreateLayer() {
	layer = new VEShapeLayer();
	map.AddShapeLayer(layer);
}

function AddPushpin(thisTitle, thisDescription, theLat, theLong) {
	if(theLat != null && theLong != null) {
		shape = new VEShape(VEShapeType.Pushpin, new VELatLong(theLat, theLong));
	} else {
		shape = new VEShape(VEShapeType.Pushpin, map.GetCenter());
	}
	shape.SetTitle(thisTitle);
	shape.SetDescription(thisDescription);
	layer.AddShape(shape);
}

$(document).ready(function() {
	loadUp();
	$(".sidebar th a").click(function() {
		if(this.id == "year") {return false;}
		if(this.className == "up") {
			this.className="down"; //only sort table by column here
			$("#property-list").load("inc/property-list-sold.php?sort="+this.id+"&year="+thisYear, function() {tableEvents()});//this.id contains data for sort
		} else if(this.className == "down") {
			this.className="up"; //else sort by title, since both are up
			$("#property-list").load("inc/property-list-sold.php?order=up&sort="+this.id+"&year="+thisYear, function() {tableEvents()});
		} else {
			$(".sidebar th a").removeClass("up");
			$(".sidebar th a").removeClass("down");
			if(this.id=="type") {
				this.className="up";
				$("#property-list").load("inc/property-list-sold.php?order=up&sort="+this.id+"&year="+thisYear, function() {tableEvents()});
			} else {
				this.className="down";
				$("#property-list").load("inc/property-list-sold.php?sort="+this.id+"&year="+thisYear, function() {tableEvents()});
			}
		}
		var thisId = this.id;
		var thisClass = this.className;
		$(".sidebar th a").each(function() {
			if(this.id!=thisId && this.className == thisClass) { //check the other sort column and change it, if needed.
				if(this.className == "down") {
					this.className="up";
				}
			}
		});
		return false;
	});
	$("#yearnav a").click(function() {
		thisYear = this.innerHTML;
		$("#yearnav a").removeClass("selected");
		$(this).addClass("selected");
		loadUp();
	});
	$("#yearnav a").eq(0).trigger("click");
	
	$("#close-map").click(function(){
		closeMap();
	});
	$("#agents-login").click(function() {
		$("#agents-login").unbind("click");
		$.getScript("/js/thickbox.js", function(){
			$("#agents-login").trigger("click");
		});
		return false;
	});
});