
var top=-1, left=-1, right=-1, bottom=-1;
var g_baseIcon = null;
var g_marker = new Array();
var g_orgs = new Array();
var g_Latitude = -122.1419;
var g_Longitude = 37.4419;
var g_Zoom = 4;

function Organization(id,name,address,address2,city,state,zip,phone,lng,lat,desc,hours,visit) {
	this.EntityId = id;
	this.Name = name;
	this.Address1 = address;	
	this.Address2 = address2;
	this.City = city;
	this.State = state;
	this.Zip = zip;
	this.Phone = phone;
	this.Longitude = lng;
	this.Latitude = lat;	
	this.Description = desc;	
	this.Hours = hours;
	this.Visit = visit;
}

/*
g_orgs[g_orgs.length] = new Organization("Test Vineyard","I am testing this with fake data... ",-122.1419, 37.4419);
g_orgs[g_orgs.length] = new Organization("Another Test Vineyard","I am testing this with more fake data... ",-122.1219, 37.4619);
g_orgs[g_orgs.length] = new Organization("Yet Another Test Vineyard","I am testing this with even more fake data... ",-122.1619, 37.4219);
*/

function highlight(i) {
	g_marker[i].openInfoWindow(createHtmlForOverlay(i));
	/*
	for (var h=0;h<g_orgs.length;h++) {
		if (i==h) {
			$('result'+h).className = "highlight";
		} else {
			$('result'+h).className = "";
		}
	}
	*/		
}

function truncate(s,word_count) {
	if (!s || s == "") return "";
	var words = s.split(" ");
	var output = new Array();
	for (var i=0; i<word_count; i++) {
		output.push(words[i]);
	}
	return output.join(" ") + "...";
}

function createHtmlForList(i) {
	var letter = "";
	if (i < 26) {
		letter = String.fromCharCode("A".charCodeAt(0) + i);
	}
	var image = "images/map/markerList/marker" + letter + ".png";
	
	var children = [];
	var subchildren = [];
	children.push("<li id=\"result" + i + "\">");
	children.push("<img src=\"" + image + "\" width=\"12\" height=\"22\" style=\"float:left;margin:0 4px 4px 0\">");
	children.push("<h2 style=\"margin:4px 0\">" + g_orgs[i].Name + "</h2>");
	children.push(createHtmlForImages(i,1));	
	children.push("<p class=\"list_desc\" id=\"desc" + i + "\">");
	children.push(truncate(g_orgs[i].Description,15));
	children.push("<a href=\"moreDetails.aspx?id=" + g_orgs[i].EntityId + "\">More details</a>");
	children.push("</p>");
	children.push("</li>");
	return children.join("\n");
	
	/*
	children.push(Builder.node('img',{src:image,width:12,height:22,style:'float:left;margin:0 4px 4px 0'}));
	children.push(Builder.node('h2',{style:'margin:4px 0'},g_orgs[i].Name));	
	var images = createHtmlForImages(i,1);
	if (images && images.length >0) {
		children.push(images[0]);
	}
	//subchildren.push(truncate(g_orgs[i].Description,15));
	//subchildren.push(Builder.node('a',{ href:'moreDetails.aspx?id=' + g_orgs[i].EntityId }, 'More details'));
	//children.push(Builder.node('p',{class:'list_desc',id:'desc'+i},subchildren));
	// children.push(Builder.node('p',g_orgs[i].Hours));
	return Builder.node('li',{id:'result'+i},children);
	*/
}

function createHtmlForOverlay(i) {	
	var children = [];
	children.push("<div id=\"result_overlay" + i + "\" class=\"overlay\">");
	children.push("<h2><a href=\"moreDetails.aspx?id=" + g_orgs[i].EntityId + "\">" + g_orgs[i].Name + "</a></h2>");
	children.push("<p>" + g_orgs[i].Address1 + "</p>");
	children.push("<p>" + g_orgs[i].City + "</p>");
	children.push("<p>" + g_orgs[i].Phone + "</p>");
	children.push(createHtmlForImages(i,3));
	children.push("<br/>");
	children.push("<a target=\"_top\" href=\"moreDetails.aspx?id=" + g_orgs[i].EntityId + "\">More details</a>");
	children.push("</div>");
	return children.join("\n");
	
	/*
	children.push(Builder.node('h2',[
		Builder.node('a',{ href:'moreDetails.aspx?id=' + g_orgs[i].EntityId }, g_orgs[i].Name)
	]));
	children.push(Builder.node('p',g_orgs[i].Address1));
	children.push(Builder.node('p',g_orgs[i].City));
	children.push(Builder.node('p',g_orgs[i].Phone));
	var images = createHtmlForImages(i,4);
	for (var j=0; j<images.length; j++) {
		children.push(images[j]);	
	}
	children.push(Builder.node('br'));
	children.push(Builder.node('a',{ href:'moreDetails.aspx?id=' + g_orgs[i].EntityId }, 'More details'));			
	return Builder.node('div',{id:'result_overlay_'+i,className:'overlay'},children);
	*/
}

function createHtmlForImages(i,nImages) {	
	var ret = [];
	/*
	for (var j=0; j<nImages && j<g_orgs[i].images.length; j++) {
		ret[ret.length] = Builder.node('a',{ href:'moreDetails.aspx?id=' + g_orgs[i].EntityId },[
			Builder.node('img',{class:'thumb',src:'GetImage.aspx?obj=Org&ID='+g_orgs[i].images[j].ImageId, border:'0'})		
		]);			
	}
	return ret;	
	*/
	for (var j=0; j<nImages && j<g_orgs[i].images.length; j++) {
		ret[ret.length] = "<a target=\"_top\" href=\"moreDetails.aspx?id=" + g_orgs[i].EntityId + "\"><img class=\"thumb\" src=\"GetImage.aspx?obj=Org&ID=" + g_orgs[i].images[j].ImageId + "\" border=\"0\"></a>";
	}
	return ret.join("\n");
}

function init() {	
	// setList();
	resize();
	loadMap();		
	window.onresize = resize;
	Element.hide("loading");
	$("loaded").style.visibility = "visible";
}


function setList() {
	var sb = [];
	for (var i=0; i<g_orgs.length; i++) {
	
		sb[sb.length] = createHtmlForList(i);
	}	
	$('results').innerHTML = sb.join("");
	Behaviour.apply();
}

function resize() {
	var windowHeight = getWindowSize().height;	
	var nonMapHeight = Element.getDimensions("controls").height + Element.getDimensions("navigation").height + Element.getDimensions("footer").height + 8 + +50 + 64 + 10;
	var mapHeight = (windowHeight - nonMapHeight);
	for (var i=0; i<1000; i++) {/* damn ie */};
	mapHeight = mapHeight < 400 ? 400 : mapHeight;
	$("map").style.height = (mapHeight) + "px"; // min-height
	// $("bcol").style.height = (mapHeight + 74) + "px";	
	$("results").style.height = (mapHeight) + "px";	
}

function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	var o = new Object();
	o.height = myHeight;
	o.width = myWidth;
	return o;  
}

function loadMap() {    
	var map = new GMap2($("map"));	
	
	map.setCenter(new GLatLng(g_Latitude, g_Longitude), g_Zoom);
	
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());

	g_baseIcon = new GIcon();
	g_baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	g_baseIcon.iconSize = new GSize(20, 34);
	g_baseIcon.shadowSize = new GSize(37, 34);
	g_baseIcon.iconAnchor = new GPoint(9, 34);
	g_baseIcon.infoWindowAnchor = new GPoint(9, 2);
	g_baseIcon.infoShadowAnchor = new GPoint(18, 25);
	var eid;
	for (var i=0; i<g_orgs.length; i++) {
	var sColor
	if (i == 0 || g_orgs[i].TypeID != g_orgs[i-1].TypeID){
			var sGroup
				switch (g_orgs[i].TypeID){
					case 3: 
					eid = 1;
					break;
					case 5: 
					eid = 1;
					break;
					case 38: 
					eid = 1;
					break;
					default : 
					eid = 1;
					}
				}
	switch (g_orgs[i].TypeID){
			case 3: 
			sColor  = "_T"; //rest
			break;
			case 5: 
			sColor  = "_P"; //hotels
			break;
			case 38: 
			sColor  = "_G"; //bed breakfast
			break;
			default : 
			sColor  = "_R"; //wineries
			}
		var marker = createMarker(new GPoint(g_orgs[i].Longitude,g_orgs[i].Latitude), i,sColor,eid);
		map.addOverlay(marker);	
		eid = eid + 1;	
	}
	GEvent.addListener(map, 'click', function(overlay, point) {
		if (overlay) {			
		}
	})
}   

function createMarker(point,id,sColor,eid) {
	//g_marker[id] = new GMarker(point);
	//var letter = "";
	//if (id < 26){
	//	letter = String.fromCharCode("A".charCodeAt(0) + id);
	//}
	var icon = new GIcon(g_baseIcon);
	//icon.image = "images/map/marker/marker" + letter + ".png";
	if (eid > 51) {
		var image = "images/map/markerlist/marker/" + sColor + ".png";
	}
	else
	{
	var image = "images/map/markerlist/marker/" + eid + sColor + ".png";
	}
	//icon.image  = "images/map/markerlist/marker/" + id + sColor + ".png";	
	icon.image = image;
	g_marker[id] = new GMarker(point, icon);
	GEvent.addListener(g_marker[id], 'click', function() {
		highlight(id);		
	});	
	return g_marker[id];
}

var page_rules = {
	'#changeSearchOptions' : function(element) {
		element.onclick = function() {
			Element.makePositioned('wizard');			
			$('wizard').style.display = "block";
			return false;
		}
	},
	
	'#updateSearchOptions' : function(element) {
		element.onclick = function() {
			submitForm();
		}
		},
	
	'#results li' : function(element) {
		element.onmouseover = function() {
			this.style.backgroundColor = "#edf8b4";
		}
		element.onmouseout = function() {
			this.style.backgroundColor = "";
		}
		element.onclick = function() {
			var id = this.id.substr("result".length);
			
			if (id >= 0 && id<g_orgs.length) {
				highlight(id);
			}
		}
	},
	
	//'#refineDropDown' : function(element) {
	//	element.onchange = function() {
	//		submitForm();
	//	}
	//},
	
	//'#showHotels' : function(element) {
	//	element.onclick = function() {
	//		submitForm();
	//	}
	//},
	
	//'#showRestaurants' : function(element) {
	//	element.onclick = function() {
	//		submitForm();
	//	}
	//},
	
	//'#showBedBreakfast' : function(element) {
	//	element.onclick = function() {
	//		submitForm();
	//	}
	//},
	
	'#showPublic2' : function(element) {
		element.onclick = function() {			
			submitForm();
		}
	},
	
	'#showPrivate2' : function(element) {
		element.onclick = function() {
			submitForm();
		}
	}
	
}

function submitForm() {
	
	// URL format
	// http://.../searchResults.aspx?method=0&searchValue=1&region=1&chkPrivate=on&refineDropDown=1
	
	// prototype and asp.net not playing nice made me do this. 
	var sb = new Array();
	
	sb[sb.length] = "method=" + $("refineMethod").value;
	sb[sb.length] = "searchValue=" + $("refineDropDown").options[$("refineDropDown").selectedIndex].value;
	sb[sb.length] = "region=" + $("refineRegion").value;
	sb[sb.length] = "showPrivate=" + ($("showPrivate2").checked ? "on" : "off");
	sb[sb.length] = "showPublic=" + ($("showPublic2").checked ? "on" : "off");
	sb[sb.length] = "refineDropDown=" + $("refineDropDown").options[$("refineDropDown").selectedIndex].value;
	sb[sb.length] = "showHotels=" + ($("showHotels").checked ? "on" : "off");
	sb[sb.length] = "showRestaurants=" + ($("showRestaurants").checked ? "on" : "off");
	sb[sb.length] = "showBedBreakfast=" + ($("showBedBreakfast").checked ? "on" : "off");
			
	document.location = 'searchResults.aspx?' + sb.join("&");
}

Behaviour.addLoadEvent(init);
Behaviour.register(page_rules);
    

