//Author: Fabien Sanglard (sanglardf@yahoo.fr)

//TYPE DEFINITIONS
var IPHONE     = 0;
var ANDROID    = 1;
var BLACKBERRY = 2;
var DESKTOP    = 3;

// Output the viewport HTML tag if necessary
var VIEW_PORT_HTML = '<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" >';
function SplashEngine_DoViewport()
{
	if (clientType != DESKTOP) {
		document.write(VIEW_PORT_HTML);
	}
}

// Output the css HTML tag if necessary
var cssArray = new Array(3);
cssArray[IPHONE] = 'groups/content/@ontcaapp/documents/element/ont04_041346.css';
cssArray[ANDROID] = 'groups/content/@ontcaapp/documents/element/ont04_041344.css';
cssArray[BLACKBERRY] = 'groups/content/@ontcaapp/documents/element/ont04_041345.css';

function SplashEngine_DoCssOutput()
{
	if (clientType != DESKTOP) {
		//alert("Test0=10:51");
		//alert(cssArray[clientType]);
		document.write('<link id="mobile" rel="stylesheet" media="all" type="text/css" href="'+cssArray[clientType]+'" />');
		//alert("Test2");
	}
}


/*
	This function is a port of what happen on server side for
	the Service Location Finder, the goal is to populate clientType
*/
var clientType = DESKTOP;
function SplashEngine_Init()
{
	var userAgent = navigator.userAgent;
	

	//We are using an if else structure, placing the most likely
	//higher so they return early.
	if (userAgent.indexOf("iPhone") != -1 )
	{
		clientType = IPHONE;
	}
	else 
	if (userAgent.indexOf("Android") != -1 )
	{
		clientType = ANDROID;
	}
	else
	if (userAgent.indexOf("BlackBerry") != -1 )
	{
		clientType = BLACKBERRY;

		// Because blackberry device have different capabilities we perform extra detection
		// (model type)
		// TODO
	}

	SplashEngine_DoViewport();
	SplashEngine_DoCssOutput();
}

SplashEngine_Init(); 

//IPHONE SPECIFIC TO HIDE NAVIGATION BAR
	var currentWidth =0;
	function updateLayout() 
	{
		if (window.innerWidth != currentWidth) 
		{
			
			currentWidth = window.innerWidth;
			
			var orient = (currentWidth == 320) ? "profile" : "landscape";
			
			//document.body.setAttribute("orient", orient);
			
			window.scrollTo(0, 1);
		}
	}

	if (clientType == IPHONE)
	{
		updateLayout();
		setInterval(updateLayout, 500);
			
		function hideAddressBar() { window.scrollTo(0, 1); }
		window.addEventListener('load', function() { setTimeout(hideAddressBar, 100); }, false);
	}
