// JavaScript Document
// Made by Leighton Whiting
// Used for BuyItNearby.com
    //<![CDATA[
	// Initialize Various Variables
	var map, geocoder, vID, vFrameContents, vNewTitle, tvFrameContents, dID, dtID, baseIcon, blueIcon, redIcon, vIcon;

    function load(startSearch) {
		 var publisherId = 181; // This Id Is for the Lat49 Ads in the bottom corner of map. Get your idea from www.lat49.com
		Lat49.initAds(publisherId); 
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
		geocoder = new GClientGeocoder();
		map.addControl(new GSmallMapControl());
		//map.enableScrollWheelZoom();
		//map.enableContinuousZoom();
		map.addControl(new GMapTypeControl());
		//Changes the icons for different types of auctions
		baseIcon = new GIcon();
		baseIcon.shadow = "markers/shadow50.png";
		baseIcon.iconSize = new GSize(30, 30);
		baseIcon.shadowSize = new GSize(30, 15);
		baseIcon.iconAnchor = new GPoint(10, 20);
		baseIcon.infoWindowAnchor = new GPoint(8, 10);
		baseIcon.infoShadowAnchor = new GPoint(10, 15);
		blueIcon = new GIcon(baseIcon);
  		blueIcon.image = "markers/blue.png";
		redIcon = new GIcon(baseIcon);
  		redIcon.image = "markers/red.png";
        showAddress(startSearch);
    //Sets default map to hybrid style
		map.setMapType(G_HYBRID_MAP);
      }
    }
	
	//This function is called when the user mouses over the auction item 
	function showInfo(i) {
		vID = "c"+i;
		dtID = "d"+i;
		var x=document.getElementById(vID);
		var latitude = x.getAttribute("latitude");
		var longitude = x.getAttribute("longitude");
		var vContent = x.innerHTML;
		var itemID = x.getAttribute("itemID");
		var vTitle = x.getAttribute("vTitle");
		var vLink = x.getAttribute("vLink");
		var vPrice = x.getAttribute("vPrice");
		var picURL = x.getAttribute("thumbURL");
		vNewTitle = "$"+vPrice+"-"+vTitle.substr(0,11)+"..";
		var point = new GLatLng(latitude,longitude);
		
		//Move the map to the point of the auction
		map.panTo(point, zoomLevel);
		vNewContent = vContent+"<div style='text-align: center; font-size: 12px;'>(Click for Details)</div>";
		map.openInfoWindowHtml(point,vNewContent);
	}
	
	//This function is called when the user clicks on the auction. It opens a new tab and calls the info for the auction via AJAX
function prepareTab(i) {
		vID = "c"+i;
		dID = "d"+i;
		var x=document.getElementById(vID);
		var itemID = x.getAttribute("itemID");
		var vContent = x.innerHTML;
		var vTitle = x.getAttribute("vTitle");
		var vPrice = x.getAttribute("vPrice");
		vNewTitle = "$"+vPrice+"-"+vTitle.substr(0,11)+"..";
		createNewTab('dhtmlgoodies_tabView1',vNewTitle,"<div id="+dID+"><img src=images/loading1.gif></div>",0,true);
		getSingleItem(itemID);
		return false;
}
	
	//This is the function that goes to the point on the map
	function gotoPoint(count) {
		vID = "c"+count;
		var x=document.getElementById(vID);
		var latitude = x.getAttribute("latitude");
		var longitude = x.getAttribute("longitude");
		var point = new GLatLng(latitude,longitude);
		map.panTo(point, zoomLevel);
	}
	
	//This is used during the startup of the script to populate the map with the points
	function showAddress(address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
		map.setCenter(new GLatLng(37.4419, -122.1419), 4);
      } else {
        map.setCenter(point, zoomLevel);
		serveAd(point.lat(), point.lng(), zoomLevel);
		
		GEvent.addListener(map, "zoomend", function() {
		var tempZoomLevel = map.getZoom();
		var tempPoint = new GLatLng;
		tempPoint = map.getCenter();
	serveAd(tempPoint.lat(), tempPoint.lng(), tempZoomLevel);
  });
		GEvent.addListener(map, "dragend", function() {
		var tempZoomLevel = map.getZoom();
		var tempPoint = new GLatLng;
		tempPoint = map.getCenter();
	serveAd(tempPoint.lat(), tempPoint.lng(), tempZoomLevel);
  });
  
  //This part runs through the array of items and gives the points a popup div that shows some basic info about that item.
		for (var j in eBayList)
	{
		vID = "c"+j;
		var x=document.getElementById(vID);
		var dtID = x.getAttribute("dID");
		var latitude = eBayList[j]['latitude']
		var longitude = eBayList[j]['longitude']
		var vContent = x.innerHTML;
		var itemID = eBayList[j]['itemID']
		var vTitle = eBayList[j]['vTitle']
		var vLink = eBayList[j]['vLink']
		var price = eBayList[j]['vPrice']
		var picURL = eBayList[j]['thumbURL']
		vIcon = blueIcon;
		if (parseFloat(eBayList[j]['timeLeftRaw']) < 6000) {vIcon = redIcon;}
		vNewTitle = "$"+price+"-"+vTitle.substr(0,10)+"..";
		var point = new GLatLng(latitude,longitude);
		vNewContent = "<div onclick=\"getSingleItem(\'"+itemID+"\');createNewTab(\'dhtmlgoodies_tabView1\',\'"+vNewTitle+"\',\'<div id="+dID+"><img src=images/loading1.gif></div></div>\',0,true);return false\">"+vContent+"<div style='text-align: center; font-size: 12px;'>(Click for Details)</div></div>";
		map.addOverlay(createMarker(point, vNewContent, vID));
		j += 1;
		}
      }
    }
  );
}

//This is used during the startup to make the markers
function createMarker(point, vNewContent, vID) {
  markerOptions = { icon:vIcon };
  var marker = new GMarker(point, markerOptions);
  var x=document.getElementById(vID);
  GEvent.addListener(marker, "click", function() {
	var vCount = x.getAttribute("vCount");
    prepareTab(vCount);
  });
  GEvent.addListener(marker, "mouseover", function() {
		var popUpBoxId=document.getElementById('popUpBox');
		popUpBoxId.innerHTML = vNewContent;
ShowContent('popUpBox'); return true;
  });
  GEvent.addListener(marker, "mouseout", function() {
HideContent('popUpBox'); return true;
  });
  return marker;
}

//Also used to make the markers
function addMarker(latitude, longitude, vContent) {
	//alert(window.location.host);
	alert(latitude);
	var point = new GLatLng(latitude,longitude);
    map.addOverlay(createMarker(point, vContent));
}

//This is a part of the sort function
function sortByPrice(a, b) {
    var x = a.vPrice;
    var y = b.vPrice;
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function sortByDistance(a, b) {
    var x = a.vDistance;
    var y = b.vDistance;
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function sortByTitle(a, b) {
    var x = a.vTitle.toLowerCase();
    var y = b.vTitle.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function sortByEndTime(a, b) {
    var x = a.timeLeftRaw.toLowerCase();
    var y = b.timeLeftRaw.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	}

//This part has to do with alignment and the popup divs
var cX = 0; var cY = 0; var rX = 0; var rY = 0;
function UpdateCursorPosition(e){ cX = e.pageX; cY = e.pageY;}
function UpdateCursorPositionDocAll(e){ cX = event.clientX; cY = event.clientY;}
if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }
function AssignPosition(d) {
if(self.pageYOffset) {
rX = self.pageXOffset;
rY = self.pageYOffset;
}
else if(document.documentElement && document.documentElement.scrollTop) {
rX = document.documentElement.scrollLeft;
rY = document.documentElement.scrollTop;
}
else if(document.body) {
rX = document.body.scrollLeft;
rY = document.body.scrollTop;
}
if(document.all) {
cX += rX;
cY += rY;
}
d.style.left = (cX-240) + "px";
d.style.top = (cY+30) + "px";
}
function HideContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
AssignPosition(dd);
dd.style.display = "block";
}
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
AssignPosition(dd);
if(dd.style.display == "none") { dd.style.display = "block"; }
else { dd.style.display = "none"; }
}

//This is used for the lat49 ads
 function serveAd(lat, lon, zoomlevel)
{
    Lat49.updateAdByLatLon('adcontainer',lat,lon,zoomlevel)
}

    //]]>
