var wta = {};
wta.depasajo = {};

wta.depasajo.pageMover = function(){
	
	var $menulinks;
	var $pagesContainer;
	var $pages;
	var $closeLink;
	
	var pageActive = false;
	
	var MARGIN_TOP = 140;
	
	function _init(){
		$menulinks = $('#menutabs ul li a');
		$pagesContainer = $('#pagesContainer').animate({'height': 0}, 'fast');
		$pages = $('div.page').addClass('hidden');
		$closeLink = $('#close');
		_setupListeners();
	}
	
	
	function _activatePage(hash){
		$pagesContainer.animate({'height': _setPageHeight()}, 600, null, function(){ pageActive = true; });
	}
	function _closePage(){
		if(pageActive == true){
			$pagesContainer.animate({'height': 0}, 600, null, function(){ 
				$menulinks.removeClass('active');
				$pages.removeClass('visible').addClass('hidden');
				pageActive = false; 
			});
			
		}
		return false;
	}
	function _setPageHeight(){
		var h;
		if(($('#outerWrapper').height() - MARGIN_TOP) > 600){
			h = 600;
		} else {
			h = $('#outerWrapper').height() - MARGIN_TOP;
		}
		
		return h;
	}
	
	
	function _setupListeners()
	{
		$closeLink.bind('click', _closePage)
		$menulinks.bind('click', _onMenuClick);
		$(window).bind('resize', _onWindowResize);
	}
	function _onMenuClick(ev){
		var hash = ev.target.hash
		$menulinks.removeClass('active');
		$(this).addClass('active');
		if(pageActive == true)
		{
			$pagesContainer.animate({'height': 0}, 600, null, function(){ 
				$pages.removeClass('visible').addClass('hidden');
				$(hash).removeClass('hidden').addClass('visible');
				_activateMap(hash);
				_activatePage(hash);
			 });
		} else
		{
			$(hash).addClass('visible');
			_activateMap(hash);
			_activatePage(hash);
		}
		return false;
	}
	function _activateMap(hash)
	{
		if($(hash).is('#contacts')) { 
			if(!wta.depasajo.map.mapReady){
				wta.depasajo.map.startMapping();
			}
		}
	}
	function _onWindowResize(e)
	{
		if(pageActive) { $pagesContainer.animate({'height': _setPageHeight()}, 600); }
	}
	
	return {
		init: _init
	}
}();


wta.depasajo.pagesTabs = function(){
	function _init(){
		$('.tabs > ul').tabs(0);
	
	};
	
	return {
		init: _init
	}
}();

wta.depasajo.wineScroller = function(){
	function _init(){
	 	$('div.scrollpane').serialScroll({
			target:'div.scroller',
			items:'dl', // Selector to the items ( relative to the matched elements, '#sections' in this case )
			prev:'.prev a',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
			next:'.next a',// Selector to the 'next' button (absolute too)
			axis:'xy',// The default is 'y' scroll on both ways
			//navigation:'#navigation li a',
			duration:600 ,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
			force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
			
			//queue:false,// We scroll on both axes, scroll both at the same time.
			//event:'click',// On which event to react (click is the default, you probably won't need to specify it)
			//stop:false,// Each click will stop any previous animations of the target. (false by default)
			//lock:true, // Ignore events if already animating (true by default)
			start: 0, // On which element (index) to begin ( 0 is the default, redundant in this case )
			//cycle:true,// Cycle endlessly ( constant velocity, true is the default )
			step:2, // How many items to scroll each time ( 1 is the default, no need to specify )
			//jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
			//lazy:false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
			//interval:1000, // It's the number of milliseconds to automatically go to the next
			constant:false, // constant speed
			
			onBefore:function( e, elem, $pane, $items, pos ){
				/**
				* 'this' is the triggered element
				* e is the event object
				* elem is the element we'll be scrolling to
				* $pane is the element being scrolled
				* $items is the items collection at this moment
				* pos is the position of elem in the collection
				* if it returns false, the event will be ignored
				*/
				//those arguments with a $ are jqueryfied, elem isn't.
				e.preventDefault();
					if( this.blur )
					this.blur();
			},
			onAfter:function( elem ){
				//'this' is the element being scrolled ($pane) not jqueryfied
			}
		});
 	}
	
	return {
		init: _init
	}
}();

wta.depasajo.map = function(){
	
	var apiLoaded = false;
	var mapReady = false;
	var map, mapHolder, mapGeocoder, dpPoint;

	function _init()
	{
		google.load('maps','2', {'callback': function(){ apiLoaded = true; }});
		mapHolder = document.getElementById("map");
	}
	
	
	function _startMapping()
	{
		map = new google.maps.Map2(mapHolder);
		map.size = new google.maps.Size(530, 250);
		map.addControl(new google.maps.SmallMapControl());
		map.enableScrollWheelZoom();
		
		mapGeocoder = new google.maps.ClientGeocoder();
		mapGeocoder.getLatLng('Via Cesare da Sesto 7, Milano', _geoCodingComplete);
		
		mapReady = true;
	}
	
	function _geoCodingComplete(gPoint)
	{
		dPoint = gPoint;
		map.setCenter(gPoint, 16);
		map.addOverlay(new google.maps.Marker(dPoint))
	}
	
	return {
		mapReady: mapReady,
		init: _init,
		startMapping: _startMapping
	}
}();

// INIT FUNCTION 
$(document).ready(function(){
	wta.depasajo.pagesTabs.init();
	wta.depasajo.pageMover.init();
	wta.depasajo.wineScroller.init();
	wta.depasajo.map.init();
})
