/************************************************************************
 *
 *  Caliber
 *  $Id: mobile.js 439 2009-03-18 13:03:33Z tom $
 *  © 2008 upstruct berlin oslo
 *
 ***************************************************************************/

/*
///////////////////////////////////////////////////////////////////////////////////
//
//  Querysting 
//
///////////////////////////////////////////////////////////////////////////////////
*/
/**
* Returns a object of the vars pased in the querystring
* @param	none
* @return	void
**/
function getUrlParameters() {
	var arg = new Object();
	var href = document.location.href;

	if ( href.indexOf( "?") != -1){
		var params = href.split( "?")[1];
		var param = params.split("&");

		for (var i = 0; i < param.length; ++i){
			var name = param[i].split( "=")[0];
			var value = param[i].split( "=")[1];

			arg[name] = value;
		}
	}
	return arg;
}

/*
///////////////////////////////////////////////////////////////////////////////////
//
//  Cookie function
//
///////////////////////////////////////////////////////////////////////////////////
*/

/**
* Set a new cookie
**/
function setCookie(name, value, expires, path, domain, secure){
		
    var today = new Date();
	today.setTime( today.getTime() );
    if ( expires ){
    	expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name + "=" +escape( value ) +
    	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    	( ( path ) ? ";path=" + path : "" ) + 
    	( ( domain ) ? ";domain=" + domain : "" ) +
    	( ( secure ) ? ";secure" : "" );
			
};

/**
* Get a cookie
* @param	none
* @return	void
**/
function getCookie(name){
		
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ){
	    return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
		
};

/*
///////////////////////////////////////////////////////////////////////////////////
//
//  Stack controller class
//
///////////////////////////////////////////////////////////////////////////////////
*/
var StackControll = Class.create({
	initialize: function(){
		this.ORIENTATION = 'portrait';
		this.CURRENT = null;
		this.PARENT = null;
		this.LANDSCAPE_WIDTH = 480;
		this.PORTRAIT_WIDTH = 320;
		this.DURATION = 0.4;
		this.FADING = false;
		
		this.peEx();
	},
	
	/**
	 * Periodical Executer to check the layout of the iPhone.
	 **/
	peEx:function(){
		var controller = this;
		
		new PeriodicalExecuter(function(pe) {
			
			var layout = (window.innerWidth <= controller.PORTRAIT_WIDTH) ? "portrait" : "landscape";
			if(layout != controller.ORIENTATION){
				$$('.stack').each(function(element) {
					$(element).removeClassName(controller.ORIENTATION);
				});
				controller.ORIENTATION = layout;
				$$('.stack').each(function(element) {
					$(element).addClassName(controller.ORIENTATION);
				});

			}
		}, 1);
	},
	
	setOrientation:function(string){
		this.ORIENTATION = (string == 'portrait') ? "portrait" : "landscape";
	},
	setCurrent:function(string){
		this.CURRENT = string;
	},
	setParent:function(string){
		this.PARENT = string;
	},
	setDuration:function(string){
		this.PARENT = string;
	},
	setFadign:function(bool){
		this.FADING = (bool == false) ? false : true;
	},
	
	getOrientation:function(){
		return this.ORIENTATION;
	},
	getCurrent:function(){
		return this.CURRENT;
	},
	getParent:function(){
		return this.PARENT;
	},
	getDuration:function(){
		return this.DURATION;
	},
	getFading:function(){
		return this.FADING;
	},
	
	pushStack:function(string){
		this.PARENT = this.CURRENT;
		this.CURRENT = string;
	}
})

/*
///////////////////////////////////////////////////////////////////////////////////
//
//  Stack class
//
///////////////////////////////////////////////////////////////////////////////////
*/
var Stack = Class.create({
	/**
	 * Open a new stack
	 * @param  array of params, required are id, url and direction
	 **/
	initialize: function(params){
		BUSCUIT.pushStack(params.id);
		
		/* This is a ugly hack to get around the 'this scope' in the AJAX */
		var thisStack = this;
		
		new Ajax.Request(params.url,{
			onSuccess: function(transport){
				var content = transport.responseText.gsub(/height=".*?"/, '');
				if(params.direction == 'left'){
					thisStack.openLeft(params.id,content);
				} else {
					thisStack.openRight(params.id,content);
				}
		    },
		    parameters: {}
		});
	},
	
	/**
	 * Open a new stack to the left
	 * @param  id, content
	 **/
	openLeft: function(id,content){
		var xPos = (BUSCUIT.getOrientation() == 'portrait') ? '-312px' : '-472px';
		$('stacks').insert({top:'<div id="'+id+'" class="stack '+BUSCUIT.getOrientation()+'"></div>'});
		$('stacks').setStyle({left:xPos});
		$(id).update(content);
		if(BUSCUIT.getFading())
			new Effect.Opacity(id, { from: 0.0, to: 1.0, duration: BUSCUIT.getDuration() });	

		new Effect.Move($('stacks'), { x: 0, y: 0, duration: BUSCUIT.getDuration(), mode: 'absolute', afterFinish:
			function(){
				$(BUSCUIT.getParent()).remove();
				window.scrollTo(0,1);
			} 
		});
	},
	
	/**
	 * Open a new stack to the right
	 * @param  id, content
	 **/
	openRight: function(id,content){
		var xPos = (BUSCUIT.getOrientation() == 'portrait') ? -312 : -472;
		$('stacks').insert({bottom:'<div id="'+id+'" class="stack '+BUSCUIT.getOrientation()+'"></div>'});
		$(id).update(content);
		if(BUSCUIT.getFading())
			new Effect.Opacity(id, { from: 0.0, to: 1.0, duration: BUSCUIT.getDuration() });
		new Effect.Move($('stacks'), { x: xPos, y: 0, duration: BUSCUIT.getDuration(), mode: 'absolute', afterFinish:
			function(){
				$(BUSCUIT.getParent()).remove();
				$('stacks').setStyle({left:'0px'});
				window.scrollTo(0,1);
			} 
		});
	}

});

/**
 * Called when the window is loaded, any functions that need initialisation
 **/
function windowOnLoad(){
	BUSCUIT.setCurrent('start');
	BUSCUIT.setFadign(false);
	
	var urlParam = getUrlParameters();
	
	if(urlParam.setAsDefault == 'true'){
		setCookie('safariMobile','iphone','','','','');
	}
}

/**
 * Attach a global eventlisteners to the window object
 **/
Event.observe(window, 'load', windowOnLoad);
var BUSCUIT = new StackControll();


addEventListener('load', function(){ 
	setTimeout(hideSafari, 0); 
}, false);

function hideSafari() { 
	window.scrollTo(0, 1); 
}

/*
///////////////////////////////////////////////////////////////////////////////////
//
//  Handlers for the Menu
//
///////////////////////////////////////////////////////////////////////////////////
*/
function toggleMenu(element,id){
	if($(id).getStyle('display') == 'none'){
		$(id).setStyle({height:$('frame').getHeight()+'px'});
		$(element).update('Close');
	} else {
		$(element).update('Menu');
	}	
	Effect.toggle(id, 'blind');
}

function closeMenu(id){
	Effect.BlindUp(id);
}

/*
///////////////////////////////////////////////////////////////////////////////////
//
//  UI calls from the pages
//
///////////////////////////////////////////////////////////////////////////////////
*/
/**
 * Handlers on frontpage
 * @param  
 **/
function display_presentation(element){
	if(element)
		$(element).up().className = 'showLoading';

	new Stack({id:'presentation',url:'presentation.html',direction:'right'});	
	//pageTracker._trackPageview('/m/work');
}

function display_emplacements(element){
	if(element)
		$(element).up().className = 'showLoading';
	
	new Stack({id:'emplacements',url:'emplacements.html',direction:'right'});
	//pageTracker._trackPageview('/m/blog');
}

function display_locations(element){
	if(element)
		$(element).up().className = 'showLoading';
	
	new Stack({id:'locations',url:'locations.html',direction:'right'});
	//pageTracker._trackPageview('/m/about.html');
}

function display_situation(element){
	if(element)
		$(element).up().className = 'showLoading';
	
	new Stack({id:'situation',url:'situation',direction:'right'});
	//pageTracker._trackPageview('/m/contact');	
}

function display_reservation(element){
	if(element)
		$(element).up().className = 'showLoading';
	
	new Stack({id:'reservation',url:'reservation.html',direction:'right'});
	//pageTracker._trackPageview('/m/blog');
}
function display_contact(element){
	if(element)
		$(element).up().className = 'showLoading';
	
	new Stack({id:'contact',url:'contact.html',direction:'right'});
	//pageTracker._trackPageview('/m/blog');
}
