/**
 * author: unknown (says Mike Nelson)
 * description: this is actually a bunch of google maps stuff, originally used for LocationsPress, but 
 * also used a little by TourPress
 */

jQuery(document).ready(function(){
	jQuery("#iconbar li").hover(
		function(){
			var iconName = jQuery(this).find("img").attr("src");
			var origen = iconName.split("x.")[0];
			jQuery(this).find("img").attr({src: "" + origen + ""});
			jQuery(this).find("span").attr({
				"style": 'display:inline'
			});
			jQuery(this).find("span").animate({opacity: 1, top: "-70"}, {queue:false, duration:400});
		}, 
		function(){
			var iconName = jQuery(this).find("img").attr("src");
			var origen = iconName.split("o.")[0];
			jQuery(this).find("img").attr({src: "" + origen + ""});
			jQuery(this).find("span").animate({opacity: 0, top: "-50"}, {queue:false, duration:400}, "linear",
				function(){
					jQuery(this).find("span").attr({"style": 'display:none'});			
				}
			);
		});
});



/* mapping polygons */

	function Decode(poly)
	{
		var i=-1,j=-1,k,l,q=poly.match(/[\_-\~]*[\?-\^]/g),w=0,x=0,y=0,z=1e-5;
		poly=[];
		if (q) for (;;)
		{
			if (!q[++i]) break;
			for (k=q[i].length,l=63,w=0;k--;l=95) w=(w<<5)+q[i].charCodeAt(k)-l;
			y+=(w<<31>>31)^(w>>1);
			if (!q[++i]) break;
			for (k=q[i].length,l=63,w=0;k--;l=95) w=(w<<5)+q[i].charCodeAt(k)-l;
			x+=(w<<31>>31)^(w>>1);
			poly[++j]={x:x*z,y:y*z};
		}
		return poly;
	}

function RegionInfo(shapetype,regionid,regionname,centrepointlat,centrepointlong,geoRssUrl,defaultZoomLevel,encodedString,levels,numlevels,colour,opacity){
	this.shapetype=shapetype;
	if(regionid){this.regionid=regionid;}
	if(regionname){this.regionname=regionname;}
	this.centrePoint=new google.maps.LatLng(centrepointlat,centrepointlong);
	this.geoRssUrl=geoRssUrl;
	this.shape=null;
	this.showRegion=false;
	this.defaultZoomLevel=defaultZoomLevel;
	this.encodedString=encodedString;
	this.levels=levels;
	this.numlevels=numlevels;
	this.colour=colour;
	this.zoomFactor=2;
	this.opacity=opacity;
	}

	RegionInfoArray = new Array();
	RegionInfoArray['vancouver-island']= new RegionInfo('polygon',17,'Vancouver Island',49.56797,-125.48583,",", 7, '{onuHxdqsWckfBom`GqgtAw~lEhzjEkivIxgnD}nr@~wnDkybHv_^}nr@fcMihC|cMren@~br@_wXzaZhrVlzK||x@yuN|byAg{f@nwnCg|jD~{~IallC`xdGivrCxh~F','IHIII?IIIIIIIIIII',11,'#5692d0',0.5);
	RegionInfoArray['vancouver-coast-and-mountains']= new RegionInfo('polygon',18,'Vancouver Coast And Mountains',50.40151,-123.68408,",", 7, 'ut`uHpaoeVpib@poaAqj@lwuF}xbAhnc@v`Cvn`GzoLhrVbe^khCv_a@tqGvvP||x@~wlCghjBnrVg|i@zgvBoquE?mhuOkhe@tcA}xH|re@grDh`]aukAihC{wAfrV}_`Ah|i@exWrwg@s_~B~d_@','PLMMNLJLPMEOPLHMLKKMP',18,'#339fc1',0.5);
	RegionInfoArray['thompson-okanagan']= new RegionInfo('polygon',19,'Thompson Okanagan',51.17934,-119.88281,",", 6, '{htdIrbbyU|suB_or@ub}@vn`GffqBCvdhBctcAdat@z~eBx~^}nr@filEjivI||lBcd~Epmv@pytAzgvBetjD?mv{O}ggIfl}A_``Az~eBsniF}nr@a~h@qihCiaLihCzhQria@ukk@ria@urNg`]_oh@fxv@tdD~vXkdc@|vXgm`@h|i@`qShdPscDria@}|b@~vX?ren@','ONOMMMOONNOOMONKOMLMKLJMKLLO',17,'#009bba',0.5);
	RegionInfoArray['kootenay-rockies']= new RegionInfo('polygon',20,'Kootenay Rockies',50.00773,-115.708,",", 6, '}cl_IpyhoUhaLhhC`~h@pihCrniF|nr@~_`A{~eB|ggIgl}A?q{zVeaZrqGisR|j_Acty@h`]g|Lria@s{L}vXolmBrmTkjcAfxv@jmD~re@_ax@pytAoyI_{Kcl_@ra{@k|YrmTwlLren@aldBfppAbgTh|i@ytv@ra{@wpf@hdP~oIpa{@stVren@w~SsmTidY|nr@','OKNOMOOKLLMLMNLKLKKMLMKLMLKO',17,'#967293',0.5);
	RegionInfoArray['cariboo-chilcotin-coast']= new RegionInfo('polygon',21,'Cariboo Chilcotin Coast',52.41582,-122.7832,",", 6, '{qzxHhdvkWuqoAwlsEhzjEkivI?kioFzi`AsytA?w~lEgilEkivIy~^|nr@eat@{~eBwdhBbtcAgfqBBk{]tngJreh@bypN?fi}LfrgA}nr@{ck@pk~L|vShoFdzQ~eBdlXga@jhQurqAjl`@}c|@~paAgnc@zdV}`l@l{f@kdPfjVi}LnuKiuf@','ONNLMOMMOMOMLNMOHJMKMLKILO',17,'#b28f51',0.5);
	RegionInfoArray['northern-bc']= new RegionInfo('polygon',22,'Northern British Columbia',57.46858,-125.68359,",", 5, '{htdIrbbyU|suB_or@ub}@vn`Gk{]tngJreh@bypN?fi}LfrgA}nr@ce^fi}LhiiDrhgIiiiDb`kFs_t@|j_AknwA|flAynwEdr}BccOwsfQozMhhC?gdPu|x@}flAavMamEiuXsia@owH`{K{uSrcAcvY|hRwqSscAc}UhhCqgRs{ZwrN?{}VhdPzqB|d_@ejMtia@cnT|re@ap[zbyAmob@|byA?hxv@os]rqGmcT~re@iw_@uqGitJ~j_AugVumTit}Bd`dC{ubBpuaBwfJidPady@fl}Ae~f@fl}Akix@htcAywGdxv@_s]hdP}s_@qqGc~e@zbyAp}XpuaB`eNftcAn`LidP|lPhdPydA|vXrr]?hjYh`]leA~nr@jdk@dhjB{eAhdPu|~@jdPgieBluhEu_[rmT{dPfxv@?_ncsB`dtd@jhCbaDkhCcaDjhCbaDkhCdwPidPjxPg|i@`hN|re@nlS_wXb|Pg`]','OOLNLONNOJMKOOKKJJNJJLILLJOKIJIKNKLLMLILMKKLMKOMHNLKLKMKOKMKKOONFJFJLMIO',17,'#c80000',0.25);

function drawPolygon(regionid, map){
	
	decodedString = new Array();
	
	decodedString = Decode(RegionInfoArray[regionid].encodedString);

	decodedStringB = new Array();
	for (var i=decodedString.length-1; i>=0; --i) {
		decodedStringB.push(
			new google.maps.LatLng(decodedString[i].y, decodedString[i].x)
		);
	}

	area = new google.maps.Polygon({
		paths: decodedStringB,
		strokeColor: RegionInfoArray[regionid].colour,
		strokeOpacity: 0.7,
		strokeWeight: 1,
		fillColor: RegionInfoArray[regionid].colour,
		fillOpacity: RegionInfoArray[regionid].opacity
	});

	area.setMap(map);

	}
	

/**
 * initializes a region on the map 'region-map'map'
 * @param region_slug
 */
function init_region(region_slug){	
	if(!mapInitialized){
		latlng = RegionInfoArray[region_slug].centrePoint;
		//init_map(latlng,5)
		initializeBasicMap(latlng,5);
		drawPolygon(region_slug, map);
		mapInitialized=true;
	}
}


