/**
 * Global JavaScript functions for the Kodak Snow Stories campaign website.
 * Author: Dylan Smith
 **/
 
//==========[ GLOBAL VARIABLES ]===================================================================

var GLOBAL_TRACKER;
var TRACKING_SITEID					= "UA-3532974-6";
var GLOBAL_DEBUG					= false;
var GLOBAL_FLASH_CONTAINER			= "flash-content";
var GLOBAL_FLASH_CONTAINER_WIDTH	= "100%";
var GLOBAL_FLASH_CONTAINER_HEIGHT	= "576";
var GLOBAL_REQUIRED_VERSION			= "9";
var GLOBAL_EXPRESSINSTALL_PATH		= false;
var GLOBAL_PRIVACY_URL				= "http://www.kodak.com/eknec/PageQuerier.jhtml?pq-path=7797&CID=go&idhbx=privacy&pq-locale=en_GB";
var GLOBAL_MOBILE_URL				= "http://mdp.xs2theworld.com/kodak/external/rossignol/";
var GLOBAL_ENTRYFORM_URL			= "http://www.google.com";
var GLOBAL_LANG						= "en-GB";
var GLOBAL_GET						= {};

//==========[ EVENT HANDLERS ]=====================================================================

/**
 * Page load handler
 **/
window.onload = function()
{
	extractGetLine();
	
	// are we in debug mode?
	//var regex = /^.*\?debug$/ig;
	var loc = window.location || document.location;
	GLOBAL_DEBUG = GLOBAL_GET["debug"] ? true : false;
	
	// hide the download prompt if we have the right version
	document.getElementById('download-flash-prompt').style.display = (swfobject.hasFlashPlayerVersion(GLOBAL_REQUIRED_VERSION)) ? 'none' : 'block';
	
	// grab the language from the HTML
	GLOBAL_LANG = document.getElementsByTagName('body')[0].attributes.lang.value;	

	// init popup and tracking
	ContentWindow.init();
	initTracking();
	trackAction();	
	loadKernel();
}

//==========[ PUBLIC FUNCTIONS ]=====================================================================

function deb(s) {
	_deb.innerHTML += s + "<br>";
}

function loadKernel() {
	var flashvars = {
		fps:				"false", 		
		debugger_color:		"#ff4040", 
		debugger_trace:		"true", 
		debugger_warning:	"false", 
		debugger_error:		"false", 
		debugger_fatal:		"false",
		sid:				GLOBAL_GET["sid"],
		locale:				GLOBAL_GET["locale"]
	};
	
	if(GLOBAL_LANG.indexOf("-") != -1) flashvars.locale = GLOBAL_LANG;
	
	flashvars["debugger"] = GLOBAL_DEBUG ? "true" : "false";	//if this is uncommented it doesn't work in Google Chrome
	for (var i in GLOBAL_GET) {
		if (i.indexOf("debugger") == 0) flashvars[i] = GLOBAL_GET[i];
	}
	
	var params = {wmode: "opaque", menu: "false", scale: "false", bgcolor: "#000000"};
	var attributes = {};
	swfobject.embedSWF(
		"kernel.swf",
		GLOBAL_FLASH_CONTAINER,
		GLOBAL_FLASH_CONTAINER_WIDTH,
		GLOBAL_FLASH_CONTAINER_HEIGHT,
		GLOBAL_REQUIRED_VERSION,
		GLOBAL_EXPRESSINSTALL_PATH,
		flashvars,
		params,
		attributes
	);
}

/**
 * Launches the mobile download popup
 */
function launchMobileDownload(locale) {
	if (ContentWindow.initialised) {
		if (ContentWindow.isVisible()) ContentWindow.hide();
		ContentWindow.scrollbars(false);
		var url = GLOBAL_MOBILE_URL;
		if (locale) {
			var lang;
			lang = locale.substring(0,2).toLowerCase();
			if (lang == 'de' || lang == 'fr') url += lang;
		}
		ContentWindow.setUrl(url);
		ContentWindow.setSize(318, 332);
		ContentWindow.show();
	}
}

/**
 * Launches the UGC competition entry form
 **/
function launchEntryForm(locale) {
	if (ContentWindow.initialised) {
		if (ContentWindow.isVisible()) ContentWindow.hide();
		ContentWindow.scrollbars(false);
		ContentWindow.setUrl('http://snowstories.kodak.com/core/Default.aspx?lang=' + locale);
		ContentWindow.setSize(778, 315);
		ContentWindow.setPosition(104);
		ContentWindow.setOffset(-90,0);
		//ContentWindow.container.style.marginLeft = "-80px";
		ContentWindow.show();
	}
}

/**
 *	Launches an overlaying content window.
 */
function launchContentWindow(sUrl) {
	if (ContentWindow.initialised) {
		ContentWindow.scrollbars(false);
		ContentWindow.setUrl(sUrl);
		ContentWindow.show();
	}
}

/**
 * Opens the Privacy Policy page
 **/
function launchPrivacyPolicy() {
	trackAction('/footer/privacy/');
	centeredWindow(GLOBAL_PRIVACY_URL, "privacy");
}

/**
 * Sends tracking requests to Google Analytics.
 * If "path" is empty, it will default to the current url
 */
function trackAction(path) {
	if (!GLOBAL_TRACKER) initTracking();
	if (GLOBAL_TRACKER) {
		if (path) {
			GLOBAL_TRACKER._trackPageview(path);
		} else {
			GLOBAL_TRACKER._trackPageview();
		}
	}
}

/**
 * Traces out log messages to an HTML element with an ID of "tracer"
 **/
function tracer(msg) {
	var t = document.getElementById('tracer');
	var m = document.createElement('div');
	m.innerHTML = t.childNodes.length + ': ' + msg;
	m.style.backgroundColor = "#333333";
	m.style.marginBottom = "1px";
	m.style.padding = "4px";
	if (t.firstChild) {
		t.insertBefore(m, t.firstChild);
	} else {
		t.appendChild(m);
	}
}

/**
 * Closes the content window
 **/
function closeContentWindow() {
	if (ContentWindow.initialised) ContentWindow.hide();
}

//==========[ UTILITY METHODS ]====================================================================
/**
 * Set up Google Analytics tracking objects
 */
function initTracking() {
	GLOBAL_TRACKER = _gat._getTracker(TRACKING_SITEID);
	GLOBAL_TRACKER._initData();
}

/**
 * Centered window
 **/ 
var WINDOW_OPTIONS_LITE = {
	directories:	"no",
	location:		"yes",
	menubar:		"no",
	resizable:		"yes",
	scrollbars:		"yes",
	status:			"yes",
	toolbar:		"no"
}
var WINDOW_OPTIONS_FULL = {
	directories:	"yes",
	location:		"yes",
	menubar:		"yes",
	resizable:		"yes",
	scrollbars:		"yes",
	status:			"yes",
	toolbar:		"yes"
}
function centeredWindow(url, name, w, h, opts) {
	if (w == undefined) w = 800;
	if (h == undefined) h = 600;	
	var left = (screen.width - w)/2;
	var top  = (screen.height - h)/2;
	if (opts == undefined) opts = WINDOW_OPTIONS_FULL;
	var params = "width="+w+",height="+h+",top="+top+",left="+left;
	for (var i in opts) params += ","+i+"="+opts[i];
	newwin = window.open(url, name, params);
	if (window.focus) {newwin.focus()}
	return void(0);
}

function extractGetLine()
{	
	var sUrl = String(window.location || document.location);	
	
	var i = sUrl.indexOf("?");
	if( i !=-1 )
	{
		var aArgs = sUrl.substr(i + 1).split("&");
		for(i=0;i<aArgs.length;i++)
		{
			var a = aArgs[i].split("=");
			GLOBAL_GET[a[0]] = unescape(a[1]);
		}
	}
}

function getFlashMovie(movieName) 
{	
	var isIE = navigator.appName.indexOf("Microsoft") != -1;	
	return (isIE) ? window[movieName] : document[movieName];
}

function notify(type, body, id)
{	
	if(!id) getFlashMovie(GLOBAL_FLASH_CONTAINER).notify(type, body);
	else getFlashMovie(GLOBAL_FLASH_CONTAINER).notify(type, body, id);
}

//==========[ DEBUG METHODS ]====================================================================
function DEB(s)
{
	document.write(s + "<br>");
}

function DEB_R(o, prefix)
{
	for(var i in o)
	{
		document.write( prefix + "['" + i + "'] = '" + o[i] + "';<br>");
	}
}