var Foo = {
	slideFx : '', 
	fadeFx : '',
	currentLink : '',
	labelVisible : true,
	map : '',
	frameHeight : 480
}


window.addEvent('load', function(){ initializeGmap(); });
window.addEvent('unload', function(){ GUnload(); });
window.addEvent('domready', function(){ setup(); });


function setup(){

	Foo.slideFx = new Fx.Scroll('slider', {
		wait: false,
		duration: 600,
		transition: Fx.Transitions.Quad.easeInOut,
		onComplete:setCurrent
	});
	Foo.fadeFx = new Fx.Style('location', 'opacity', {duration:600});

	$$('#nav a').addEvent('click', function(e){ 
		slideContent(this, e);
	});	
	
	//set label
	if($('loc-name').value!=''){
		var name = $('loc-name').value.replace('near', '');
		if(name.length > 33) name = name.substr(0, 32)+' &hellip;';
		var msg = 'Somewhere near ' + name;
		$('location').getElement('span').setHTML(msg);
	}	
}

function slideContent(el, event){

	var e = new Event(event).stop();
	var section = el.getProperty('href').replace('#', '');
	Foo.currentLink = el;
	Foo.slideFx.toElement(section);

	//show/hide label
	if(section == 'about-me') Foo.labelVisible = true;
	else if(Foo.labelVisible){
		Foo.fadeFx.start(1, 0);
		Foo.labelVisible = false;
	}
}

function setCurrent(){
	$$('#nav li').removeClass('current');
	Foo.currentLink.getParent().addClass('current');
	
	document.title = "[Steve Squier] - " + Foo.currentLink.getProperty('title');
	
	if(Foo.labelVisible && $('location').getStyle('opacity')!=1) Foo.fadeFx.start(0, 1);
}

function initializeGmap() {
	if (GBrowserIsCompatible()) {
		Foo.map = new GMap2($("map_canvas"));
		//get live pos
		var lat = $('lat').value.toFloat();
		var lng = $('lng').value.toFloat();
		var latlng = new GLatLng(lat,lng);
		var pageHeight = window.getScrollHeight();
		var newPanHeight = (pageHeight / 2)-Foo.frameHeight;
		var moveBy = new GSize(0, -newPanHeight);
		
		Foo.map.setCenter(latlng, 9);
		Foo.map.panBy(moveBy); //pan map to an offset so centre appreas in frame
		//Foo.map.addOverlay(new GMarker(latlng)); //add marker
		//Foo.map.setMapType(G_PHYSICAL_MAP); 	  //(G_SATELLITE_MAP - photo) (G_PHYSICAL_MAP - terain) (G_HYBRID_MAP - photo with map over)
		
		//size the map_canvas on load for small height screens
		$('map_canvas').setStyle('height', pageHeight);
		$('layout').setStyle('height', pageHeight);
	}
}