/* 
Title: GroundLogic.js
Author: First Flight Communication, Sweden
Updated: 2008-04-01
Version: 1.04
*/


// --- GLOBAL VARIABLES -------------------------------------

window.rich = false;
window.richmedia = false;
window.richnavigation = false;
window.richcontent = false;
window.richcampaign = false;

window.richajax = false;

window.richauthor = false;
window.richgrid = false;
window.richedit = false;

window.nocache = "?nocache=" + new Date();

window.richlang = "";

window.richlegend = "";


// --- DETECT OS & BROWSER -------------------------------------

function getUserAgent(s) { var ua = navigator.userAgent.toLowerCase(); return ua.indexOf(s) + 1; };

var win, mac, msie, ie6, ie7, ie9, firefox, safari, firefox4 = false;

if (getUserAgent("win")) win = true;
if (getUserAgent("mac") || getUserAgent("ppc")) mac = true;
if ($.browser.msie) msie = true;
if ($.browser.msie && parseFloat($.browser.version) <= 6) ie6 = true;
if ($.browser.msie && parseFloat($.browser.version) >= 7) ie7 = true;
if ($.browser.msie && parseFloat($.browser.version) >= 9) ie9 = true;
if ($.browser.mozilla) firefox = true;
if ($.browser.safari) safari = true;
if(firefox && $.browser.version >= 2) firefox4 = true;


// --- GET FLASH VERSION -------------------------------------

var getFlash = deconcept.SWFObjectUtil.getPlayerVersion;


// --- GET QUERY VARIABLES -------------------------------------

function getQuery(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) return pair[1];
	}
}

var qrich = getQuery("rich");
var qrichmedia = getQuery("richmedia");
var qrichnavigation = getQuery("richnavigation");
var qrichcontent = getQuery("richcontent");
var qrichcampaign = getQuery("richcampaign");
var qrichajax = getQuery("richajax");
var qauthor = getQuery("author");
var qgrid = getQuery("grid");
var qedit = getQuery("edit");
var qcookie = getQuery("cookie");
var qreset = getQuery("reset");


// --- SET/GET COOKIE VARIABLES -------------------------------------

function setCookie(name,value,days) {
	var expires = "";
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(variable) {
	var variable = variable + "=";
	var vars = document.cookie.split(';');
	for (var i=0;i<vars.length;i++) {
		var c = vars[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(variable) == 0) return c.substring(variable.length,c.length);
	}
}

function delCookie(name) {
	setCookie(name,"",-1);
}

function checkCookie(ID) {
	return getCookie(ID);
}

function setStoreCookie(ID, VAR) {
	setCookie(ID, VAR, 0);
}

function resetCookies() {
	delCookie("rich");
	delCookie("richmedia");
	delCookie("richnavigation");
	delCookie("richcontent");
	delCookie("richcampaign");
	delCookie("richajax");
	delCookie("richedit");
	delCookie("richgrid");
	delCookie("richlegend");
	delCookie("richlegendforce");
	delCookie("richstate");
	delCookie("richauthor");
}

if (qrich) setCookie("rich", qrich, 0);
if (qrichmedia) setCookie("richmedia", qrichmedia, 0);
if (qrichnavigation) setCookie("richnavigation", qrichnavigation, 0);
if (qrichcontent) setCookie("richcontent", qrichcontent, 0);
if (qrichcampaign) setCookie("richcampaign", qrichcampaign, 0);
if (qrichajax) setCookie("richajax", qrichajax, 0);
if (qauthor) setCookie("richauthor", qauthor, 0);
if (qgrid) setCookie("richgrid", qgrid, 0);
if (qedit) setCookie("richedit", qedit, 0);

var crich = getCookie("rich");
var crichmedia = getCookie("richmedia");
var crichnavigation = getCookie("richnavigation");
var crichcontent = getCookie("richcontent");
var crichcampaign = getCookie("richcampaign");
var crichajax = getCookie("richajax");
var cauthor = getCookie("richauthor");
var cgrid = getCookie("richgrid");
var cedit = getCookie("richedit");

if (qcookie == "off" || qcookie == "delete" || qcookie == "del") {
	resetCookies();
}

if (qreset == "true" || qreset == true) {
	resetCookies();
}


// --- LEGEND -------------------------------------

function setLegend(VAR) {
	setCookie("richlegend", VAR, 0);
}

function getLegend() {
	return getCookie("richlegend");
}

function addLegend(VAR) {
	LEGEND = getLegend();
	setCookie("richlegend", LEGEND + VAR + "|", 0);
}

function delLegend() {
	delCookie("richlegend");
}

var crichlegend = getLegend();
if (crichlegend) richlegend = crichlegend;


// --- LEGEND -------------------------------------

function setLegendForce(VAR) {
	setCookie("richlegendforce", VAR, 0);
}

function getLegendForce(obj) {
	flashObject(obj+"_swf").returnLegendForce(getCookie("richlegendforce"));
}

function delLegendForce() {
	delCookie("richlegendforce");
}

function setRichState(VAR) {
	setCookie("richstate", VAR, 0);
}

//alert("richstate = " + getCookie("richstate"));


// --- CSS CLASSES -------------------------------------

function setStoreClass(ID, CLASS) {
	var DIV = "#"+ID;
	$(DIV).removeClass().addClass(CLASS);
	setCookie(ID, CLASS, 0);
}


// --- IMPORT STUFF -------------------------------------

function importJs(uri) { document.write('<script src="' + uri +'" type="text/javascript"></script>\n'); }
function importCss(uri) { document.write('<link href="' + uri +'" type="text/css" rel="stylesheet" />'); }


// --- SETUP RICH NAVIGATION / CONTENT -------------------------------------

function initRich() {
	var flashver = getFlash();
	if (flashver['major'] >= flashreq) {
		rich = true;
		richmedia = true;
		richnavigation = true;
		richcontent = true;
		richcampaign = true;
		if (qrich == "off" || crich == "off") rich = false;
		if (qrichmedia == "off" || crichmedia == "off") richmedia = false;
		if (qrichnavigation == "off" || crichnavigation == "off") richnavigation = false;
		if (qrichcontent == "off" || crichcontent == "off") richcontent = false;
		if (qrichcampaign == "off" || crichcampaign == "off") richcampaign = false;
	}
}


// --- SETUP AUTHOR -------------------------------------

if (qauthor == "on" || cauthor == "on") richauthor = true;
if (qgrid == "on" || cgrid == "on") richgrid = true;
if (qedit == "on" || cedit == "on") richedit = true;


function showHTMLgrid(obj, color) {
	$(obj).addClass('AuthorGrid');
}

function updateHTMLdebugger(obj, output) {
	var jqobject = "#AuthorInput_"+obj;
	$(jqobject).html(output);
}

function openHTMLdebugger(obj,z,x,y,w,h) {
	var dbgwin = jQuery(''
	+'<div id="AuthorDebug_'+obj+'" class="AuthorDebug" style="left:'+x+'px;top:'+y+'px;z-index:'+z+';">'
	+'<div id="AuthorOutput_'+obj+'" class="AuthorOutput" style="width:'+w+'px;height:'+h+'px;">'
	+'<div id="AuthorInput_'+obj+'" class="AuthorInput"></div>'
	+'<div id="AuthorControls_'+obj+'" class="AuthorControls"></div>'
	+'</div>'
	+'<div id="AuthorResizeSE_'+obj+'" class="AuthorResizeSE"></div>'
	+'</div>'
	+'').insertAfter('div:first');
	//.insertAfter('#RichAuthor');
	$("#AuthorOutput_"+obj).Resizable({ handlers:{ se:'#AuthorResizeSE_'+obj } });
	$("#AuthorDebug_"+obj).Draggable({ opacity:0.5, zIndex:1000 });
}

function hideHTMLdebugger(obj) {
	$("#AuthorDebug_"+obj).hide();
}

function showHTMLdebugger(obj) {
	$("#AuthorDebug_"+obj).show();
}

function addHTMLdebuggerCtrl(obj, cid, cname) {
	var dbgctrl = jQuery('<span><input id="AuthorCtrl_'+obj+'_'+cid+'" type="button" value="'+cname+'" /></span>').appendTo('#AuthorControls_'+obj);
	$("#AuthorCtrl_"+obj+"_"+cid).bind("click", function(){ callHTMLdebuggerCtrl(obj, cid); });
}

function callHTMLdebuggerCtrl(obj, cid) {
	flashObject(obj+"_swf").callHTMLdebuggerCtrl(cid);
}


// --- CALL FLASH OBJECT -------------------------------------

function flashObject(flashName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[flashName] : document[flashName];

}

// --- UPDATE SWF & DIV -------------------------------------

function updateSwfSize(obj,w,h) {
	if (w) {
		document.getElementById(obj).style.width = w + "px";
		document.getElementById(obj+"_swf").style.width = w + "px";
	}
	if (h) {
	
		if(ie9 && h == 80) {
			h = 565;
		}
	
		document.getElementById(obj).style.height = h + "px";
		document.getElementById(obj+"_swf").style.height = h + "px";
		
	}
}

function updateSwfIndex(obj,z) {
	if (z) {
		document.getElementById(obj).style.zIndex = z;
		//document.getElementById(obj+"_swf").style.zIndex = z;
	}
}


// --- GET URL LOCATION -------------------------------------

function getHTMLurl(url, author) {
	if (author) {
		window.alert("...href='"+unescape(url)+"'");
	} else {
		window.location.href = unescape(url);
	}
}

function reloadHTMLurl() {
	window.location.href = window.location.href;
}


// --- LOAD EXTERNAL HTML -------------------------------------

function insertContent(url, divid) {
	if ($("#"+divid).length < 1) document.write('<div id="' + divid +'"></div>\n');
	$("#"+divid).load(url, {limit: 25}, function(){
		// Callback
		window.fixScroll();
	});
}


