/*  
* General
* ----------------------
*/
function outputFlashDebug( str ) { 
	/*
	if ( __debug ) {
		if ( !document.getElementById("swfDebug") ) {
			var divDebug = document.createElement("div");
			divDebug.setAttribute("id","swfDebug");
			document.body.appendChild(divDebug);
		}
		
		document.getElementById("swfDebug").innerHTML = "> " + str + "<br />" + document.getElementById("swfDebug").innerHTML;
		document.getElementById("swfDebug").style.visibility	= "visible";
	}
	*/
	return false;
}
function setDocumentTitle( str ) { 
	document.title = str;
}
/* 
* Flash size functions
* ----------------------
* Used for keeping the size by the flash
*/
var __swfMainWidth = 950;
var __swfMainHeight = 490;
var __jqueryInitiated = false;

function resizeFlashContent( width, height, contentWidth, contentHeight ) {
	/*
	width -> Stage.width
	height -> Stage.width
	*/
	if ( width && height && contentHeight ) {
		height = contentHeight;
		if ( width < browserSize().availWidth ) width = browserSize().availWidth;
		if ( height < browserSize().availHeight ) height = browserSize().availHeight;
	} else {
		width = __swfMainWidth;
		height = __swfMainHeight;
	}	
	
	resizeFlash("swfMain", width, height);
	__swfMainWidth = width;
	__swfMainHeight = height;
}

function resizeFlash(obj, w, h)
{
	
	if(__jqueryInitiated)
	{
		
		var windowHeight = $(window).height();
		var windowWidth = $(window).width();
		
		var flashHeight = parseInt(h);
		var flashWidth = parseInt(w);
		
		if(flashHeight > windowHeight)
			$('#swfMain').height(flashHeight);
		else
			$('#swfMain').css({height: '100%'});

		if(flashWidth > windowWidth)
			$('#swfMain').width(flashWidth);
		else
			$('#swfMain').css({width: '100%'});
	}
}


function browserSize(){
    var availHeight;
    var availWidth;
 
    if(typeof(window.innerWidth) == 'number'){
        availHeight = window.innerHeight;
        availWidth = window.innerWidth;
    }else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
        availHeight = document.documentElement.clientHeight;
        availWidth = document.documentElement.clientWidth;
    }else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
        availHeight = document.body.clientHeight;
        availWidth = document.body.clientWidth;
    }
    var o = new Object();
    o.availWidth = availWidth;
    o.availHeight = availHeight;
    return o;
}
/*
* Print
* --------------
*/
function openPrint(URL) {
	printWindow = window.open(URL,'winPrint','toolbar=yes,directories=yes,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=600');
	printWindow.focus();
}
/*
* Statehandler
* --------------
* Used for keeping states by the flash
*/
function stateChanged( state ) {
		if ( state == "null" ) {
			// if Flash sends "" it gets translated to "null" for some reason
			state = "";
		}
		window.location.hash = encodeURI(state);
		
		//alert( "stateChanged" );
}

function forceStateChange( state ) {
	
	if ( state == "null" ) {
		// if Flash sends "" it gets translated to "null" for some reason
		state = "";
	}
	//window.location.hash = encodeURI(state);
	
	document.getElementById("swfMain").idUpdatedFromJS(state);
}
	
function restoreState( ) {
	var state = decodeURI(window.location.hash.substring(1));
	document.getElementById("swfMain").restoreState(state);
}

function onSiteLoaded( ) {
	restoreState();
}
/*
* BrowserDetect
* --------------
* BrowserDetect.browser 
* BrowserDetect.version 
* BrowserDetect.OS 
*/
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


$(function () {
	onresize = resizeFlashContent;
	__jqueryInitiated = true;
	// Restore state
	onSiteLoaded();
});