/* ZestMark ClientSide Programming copyright Pohjolan Mylly */

var mouseX = 1;
var mouseY = 1;
var map = null;
var headerHeight = 33;
var resizeHook = null;
var setlan = 0;
var setlon = 0;
var recpoint = false;

document.onmousemove = getMouseXY;
google.load("jquery", "1.3.2");

//Run On Load
google.setOnLoadCallback(function() {					  
	//Run On Resize
	$(window).bind('resize', function() {
		elementsResize();
	});	
	
	//Init Items
	$('a#shareTwitter').click(function(){window.open('http://twitter.com/home?status=Wants to tell you about http://www.zestmark.fi/"','tweet','toolbar=0,status=0,width=626,height=436');return false;});	
	$('a#scrollUp').click(function(){scrollWindow(-200);return false;});	
	$('a#scrollDown').click(function(){scrollWindow(200);return false;});	
	$('#scrollBar').click(function(){zestBarToggle();return false;});
	$('[name=title]').val('');
	$('[name=desc]').val('');	
	$('[name=password]').val('');	

	resizeHook = $(window);
	elementsResize();		
	mapInit();

	//Navigation Actions
	$('a#naviCompany').click(function(){loadContent('company');return false;});	
	$('a#naviTraining').click(function(){loadContent('training');return false;});	
	$('a#naviContact').click(function(){loadContent('contact');return false;});	
	$('a#naviCommunity').click(function(){loadContent('community');return false;});		
	$('a#goHome').click(function(){loadContent('home');return false;});		
	$('a#goLang').click(function(){ $('#lang_fi').toggle('slow',function(){scrollShow();}); $('#lang_en').toggle('fast',function(){scrollShow();}); return false;});			
	$('#notePop').click(function(){$('#notePop').fadeOut('slow');return false;});	
	$('#markerInfo').click(function(){$('#markerInfo').fadeOut('slow');return false;});		
	$('#markerInfoCustom').click(function(){$('#markerInfoCustom').fadeOut('slow');return false;});			
	$('#maprec').click(function(){$('#maprec').css('background-image','url(http://www.zestmark.fi/wp-content/themes/zestmark/image/maprec1.png)');recpoint=true;return false;});	
	$('#submit').click(function(){recordMarker();return false;});	
	$('#cancel').click(function(){cancelMarker();return false;});	
	
	//Onload
	setTimeout("$('#notePop').fadeIn('slow');",1000);			
	loadContent('home');
	
	//Get ADMIN Map Markers
	$.getJSON('http://www.zestmark.fi/wp-content/themes/zestmark/markers.php?r='+Math.random(), function(data) {																	  
		for (i=0;i<=100000;i++) {
			if (data.markers[i][0]) {
				mapMarker(data.markers[i][0],data.markers[i][1],data.markers[i][2],data.markers[i][3],data.markers[i][4],null);
			} else { break; } 
		}
	});
	
	//Get GUEST Map Marpers
	$.getJSON('http://www.zestmark.fi/wp-content/themes/zestmark/markers.php?&g=1&r='+Math.random(), function(data) {																	  
		for (i=0;i<=100000;i++) {
			if (data.markers[i][0]) {
				mapMarker(data.markers[i][0],data.markers[i][1],data.markers[i][2],data.markers[i][3],data.markers[i][4],3);
			} else { break; } 
		}
		//Add Visitor to Map, Just for Fun
		if (google.loader.ClientLocation) {
			mapMarker(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude,'You Are Here','You Are Here',-1,1);
 			mapCenter(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude);
		}
	});	
});

function cancelMarker() {
	$('[name=title]').val('');
	$('[name=desc]').val('');	
	$('[name=password]').val('');	
	$('#addpoint').fadeOut('slow');
	setTimeout("$('#zestContainer').fadeIn('slow');",1000);		
	setTimeout("$('#markerrec').fadeOut('slow',function(){$('#markerrec').css('display','none');$('#markerrec').css('margin-top','-50px');});",2000);			
}

function recordMarker() {
	var rec_lat = escape(setlat);
	var rec_lon = escape(setlon);
	var rec_title = escape($('[name=title]').val());
	var rec_desc = escape($('[name=desc]').val());	
	var rec_password = escape($('[name=password]').val());		
	$('[name=title]').val('');
	$('[name=desc]').val('');	
	$('[name=password]').val('');	
	mapMarker(rec_lat,rec_lon,unescape(rec_title),unescape(rec_desc),-1,2);
	$('#addpoint').fadeOut('slow');
	$('#markerrec').css('display','none');
	$('#markerrec').css('margin-top','-50px');	
	setTimeout("$('#zestContainer').fadeIn('slow');",1000);	
	$('#zestNotify').load('http://www.zestmark.fi/wp-content/themes/zestmark/save.php?lat='+rec_lat+'&lon='+rec_lon+'&title='+rec_title+'&desc='+rec_desc+'&password='+rec_password,function(){ setTimeout("$('#zestNotify').slideDown('slow');",1500);setTimeout("$('#zestNotify').slideUp('slow');",5000);});	
}

function addComment() {
	var c = escape($('form #comment').val());
	var cg = escape($('form #commentgiver').val());	
	var cid = escape($('form #commentid').val());		
	$('#zestNotify').load('http://www.zestmark.fi/wp-content/themes/zestmark/save.php?comment=1&c='+c+'&cg='+cg+'&cid='+cid,function(){ setTimeout("$('#zestNotify').slideDown('slow');",1500);setTimeout("$('#zestNotify').slideUp('slow');",5000);});	
	loadMarker(cid);
}

function channelToggle(that) {
	$('#'+that.parentNode.id+' .channelContent').slideToggle('slow',function(){scrollShow();});
	$('#'+that.parentNode.id+' .channelToggleHide').slideToggle('slow');
	$('#'+that.parentNode.id+' .channelToggleShow').slideToggle('slow');
}

function scrollShow() {
	if($('#zestContent').height() < $('#zestWindow').height()) { $('a#scrollDown').fadeOut('fast'); $('a#scrollUp').fadeOut('fast'); } else 
	{ $('a#scrollDown').fadeIn('fast'); $('a#scrollUp').fadeIn('fast'); } 		
}

function loadContent(pageLoad) {
	$('#zestContent').fadeOut('slow',function(){$('#zestContent').css('marginTop',0);showLoad();$('#zestContent').load('http://www.zestmark.fi/wp-content/themes/zestmark/index.php?ap='+pageLoad,function(){$('#zestContent').fadeIn('slow',function(){elementsResize();FB.init('ceb8671044aae5353577e207d3c81faa');});});});	
	if (pageLoad == 'home') { $('#goHome').fadeOut('fast'); $('#goLang').fadeOut('fast'); } else { $('#goHome').fadeIn('fast'); $('#goLang').fadeIn('fast');} 
	if(parseInt($("#zestBar").css('marginLeft'))<0) { zestBarToggle(); }	
}

function loadMarker(pageLoad) {
	$('#zestContent').fadeOut('slow',function(){$('#zestContent').css('marginTop',0);showLoad();$('#zestContent').load('http://www.zestmark.fi/wp-content/themes/zestmark/index.php?marker='+pageLoad,function(){$('#zestContent').fadeIn('slow',function(){elementsResize();});});});	
	$('#goHome').fadeIn('fast');  
	$('#goLang').fadeOut('fast');  
	if(parseInt($("#zestBar").css('marginLeft'))<0) { zestBarToggle(); }	
}

function showLoad() {
	$('#zestContent').html('<h2 class="loading">Please Wait</h2><p>Compiling mashup data...</p>');	
	$('#zestContent').fadeIn('slow');	
}

function scrollWindow(ammount) {
		ammount = parseInt($('#zestContent').css('marginTop'))-ammount;
		if (ammount>0) { ammount=0; }
		if(ammount<-$('#zestContent').height()+100) { ammount=-$('#zestContent').height()+100; }
		$("#zestContent").animate({marginTop:ammount+'px'},'fast');
}

function elementsResize() {
	$('#zestContainer').css('height',resizeHook.height());	
	$('#zestBar').css('height',$('#zestContainer').height()-headerHeight);
	$('#zestMap').css('height',$('#zestContainer').height()-headerHeight);
	$('#zestWindow').css('height',$('#zestContainer').height()-(headerHeight+105));	
	if($('#zestContent').height() < $('#zestWindow').height()) { $('a#scrollDown').fadeOut('fast'); $('a#scrollUp').fadeOut('fast'); } else 
	{ $('a#scrollDown').fadeIn('fast'); $('a#scrollUp').fadeIn('fast'); } 	
}

function zestBarToggle() {
	if(parseInt($("#zestBar").css('marginLeft'))>-1) {
		//$("#zestBar").animate({top:'0px'},'slow');			
		//$("#zestHeader").animate({marginTop:'-33px'},'slow');
		$("#zestBar").animate({marginLeft:'-555px'},'slow');
		$('#scrollBar').fadeOut('slow',function(){$("#scrollBar").css('left','20px');$("#scrollBar").css('top','35px');$("#scrollBar").css('background-image','url(http://www.zestmark.fi/wp-content/themes/zestmark/image/scrollBar2.gif)');});		
		$('#scrollBar').fadeIn('slow');			
		//headerHeight = 0;
		elementsResize();
	} else {		
		//$("#zestBar").animate({top:'33px'},'slow');			
		//$("#zestHeader").animate({marginTop:'0px'},'slow');
		$('#scrollBar').fadeOut('slow',function(){$("#scrollBar").css('left','513px');$("#scrollBar").css('top','125px');$("#scrollBar").css('background-image','url(http://www.zestmark.fi/wp-content/themes/zestmark/image/scrollBar.gif)');});		
		$("#zestBar").animate({marginLeft:'0px'},'slow');			
		$('#scrollBar').fadeIn('slow');				
		//headerHeight = 33;	
		elementsResize(); 			
	}
}

function mapCenter(lat,lon) {
	map.setCenter(new GLatLng(lat, lon));
}

function markerInfo(lat,lon,name,desc,id,markerType) {
	//Admin
	if(!markerType && id >= -1){
		loadMarker(id);
	}
	//Guest
	if(markerType == 3 && id >= -1){
		loadMarker(id);
	}
	//Visitor Location
	if(markerType == 1 && id <= 0){
		$('#markerInfoCustom').html('');
		$('#markerInfoCustom').fadeIn('slow');
	}	
	//Recently Added
	if(id <= 0 && markerType == 2){
		$('#markerInfo').html('<h4>Marker Recently Added</h4><p>The marker "'+name+'" was recently added and it might need approving. Refresh the page to see the current status. Please dont re-submit this marker.</p>');
		$('#markerInfo').fadeIn('slow');
	}	
}

function mapMarker(lat,lon,name,desc,id,markerType) {
	var point = new GLatLng(lat, lon);
	map.setCenter(point);
	
	if(markerType == 99){
		var marker = new GMarker(point);
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function() { markerInfo(lat,lon,name,desc,id,markerType); }); 		
	}
	
	//Basic Zestmark
	if(!markerType){
		var customIcon = new GIcon();
		customIcon.image = 'http://www.zestmark.fi/wp-content/themes/zestmark/image/mapicons/icon0.png';
		customIcon.shadow = '';
		customIcon.iconSize = new GSize(32,49);
		customIcon.shadowSize = new GSize(0,0);
		customIcon.iconAnchor = new GPoint(16,45);
		customIcon.infoWindowAnchor = new GPoint(16,24);
		markerOptions = { icon:customIcon };
		var marker = new GMarker(point, markerOptions);
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function() { markerInfo(lat,lon,name,desc,id,markerType); }); 	
	} 	
	
	//Custom Markers
	if(markerType){
		var customIcon = new GIcon();
		customIcon.image = 'http://www.zestmark.fi/wp-content/themes/zestmark/image/mapicons/icon'+markerType+'.png';
		customIcon.shadow = '';
		customIcon.iconSize = new GSize(32,49);
		customIcon.shadowSize = new GSize(0,0);
		customIcon.iconAnchor = new GPoint(16,45);
		customIcon.infoWindowAnchor = new GPoint(16,24);
		markerOptions = { icon:customIcon };
		var marker = new GMarker(point, markerOptions);
		map.addOverlay(marker);
		//Custom Overlay for Visitor Location
		if (markerType == 1) { GEvent.addListener(marker, "click", function() { markerInfo(lat,lon,name,desc,id,markerType); }); } else {
			GEvent.addListener(marker, "click", function() { markerInfo(lat,lon,name,desc,id,markerType); });
		}
	} 
};

function getCoordinate(overlay, point) {
  if (!overlay) {
	if (recpoint) { 
		$('#markerrec').css('top',mouseY-50);
		$('#markerrec').css('left',mouseX-16);	
		$('#markerrec').fadeIn('fast');
		$("#markerrec").animate({marginTop:'0px'},'fast');	
		setTimeout("$('#zestContainer').fadeOut('slow');",500);
		//$('#zestContainer').fadeOut('slow');
		$('#reccoord').html(point.lat()+' '+point.lng());
		setTimeout("$('#addpoint').fadeIn('slow');",2000);
		setlon = point.lng();
		setlat =  point.lat();
		recpoint=false;
		$('#maprec').css('background-image','url(http://www.zestmark.fi/wp-content/themes/zestmark/image/maprec.png)');
	}
  }
} 

function mapInit() {	
	map = new GMap2(document.getElementById("zestMap"));	
	map.setCenter(new GLatLng(60.16613,24.945831),3);  
	var topRight= new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,40));
  	map.addControl(new GSmallMapControl(), topRight);
    map.addControl(new GMapTypeControl());	
	GEvent.addListener(map, "click", getCoordinate);	
	map.enableScrollWheelZoom();	
}

function getMouseXY(e) {
	try {
		if (!e) var e = window.event||window.Event;
		if('undefined'!=typeof e.pageX) {
			mouseX = e.pageX;
			mouseY = e.pageY;
		} else {
			mouseX = e.clientX + document.body.scrollLeft;
			mouseY = e.clientY + document.body.scrollTop;
		}	
		$('#markerInfo').css('top',mouseY-126);
		$('#markerInfo').css('left',mouseX-147);
		$('#markerInfoCustom').css('top',mouseY-126);
		$('#markerInfoCustom').css('left',mouseX-147);		
	} catch(err) {
		/* Do Nothing And Wonder What's Happening */
	}
}
