var tabs = new Array();

if(typeof(console) == 'undefined'){
	console = {
		log: function(msg){
			alert(msg);
		}
	}
}

function error(msg,e){

	/* CW: Some helpful debug stuff
		- set document.debug to true and the browser will alert javascript errors along with stack traces (when available),
		- set document.logerrors to true and the browser will output these errors to the firebug console */
	document.debug = User.debug;
	//document.logerrors = true;
	if(e){
		var er = "Oop's\n\n"+msg+":\n\nName: "+e.name+"\nMessage: "+e.message;
		if(e.fileName && e.lineNumber){
			er += "\nFile: "+e.fileName+"\nLine: "+e.lineNumber;
		}
		er += "\n\nStack Trace:\n"+e.stack;
	}
	if(document.debug && er){
		if(document.logerrors){
			console.log(er);
		}else{
			alert(er);
		}
	}else{
		if(window.Content){
			Content.alert(msg, 0, "error");
		}else{
			alert(msg);
		}
	}
	if(!document.debug && er){
		reportError(er);
	}
}

function reportError(er){
	var lb = "";
	if(window.Content && window.Content.lightBox){
		lb = " Lightbox location: "+Content.lightBox.location.getLocation();
	}
	new Ajax.Request("/utility/error", { parameters: "e="+escape(er)+"&l="+escape(document.location.href+lb) });
}

function whoops(message){
	if(window.Content){
		Content.alert(message,15,'error');
	}
	if(window.Lightbox){
		Lightbox.close();
	}
}

/* Evaluates any scripts within a string */
function runScripts(scripts){
	var myScripts = scripts.extractScripts();

	try{
		if(myScripts.length > 0){
			if(myScripts.indexOf("Object") >= 0){
				myScripts.evalJSON(true);
			}
			else{
				var myReturnedValues = myScripts.map(function(script) {
				  return eval(script);
				});
			}
		}
	}
	catch(e){
		error("A fatal error occured when trying to initialise this page. Please contact BEAMTV support if this persists.",e);
	}
}

/* Fire anything that requires the page to be loaded */
document.observe("dom:loaded",
	function(){
		try{
			if(window.coreSite){
				Effect.DefaultOptions.fps = 50;

				/* Inserts a div to display the loading spinner when required */
				if(window.Content && window.Content.location){
					Content.location.routed = Content.lightBox.location.routed = routed;
				}

				if(window.toolTips){
					toolTips.initialise();
				}

				/* Ready's context menu's for use */
				if(window.contextMenu){
					contextMenu.initialise();
				}

				/* Setup the clipboard */
				if(window.Clipboard){
					Clipboard.configure();
				}

				if(window.User){
					User.initialise();
				}

				var snl = $("sub-navigation-list");
				if(snl){
					snl.select("a").each(
						function(el){
							tabs[el.id] = 1;
						}
					);
				}
				configureHistory();

				var Area = Content.location.getArea().capitalize();
				if(window[Area] && window[Area].initialise){
					window[Area].initialise();
				}else{
					initialise();
				}
				
				if(window.alertMessage){
					alertMessage();
				}
				if(window.Content){
					Content.loadScript("js/beamtv/account.js?"+window.modified_date);
				}
				if(window.submitFrame){
					window.submitFrame.location.href="/utility/uploading";
				}
			}
		}catch(e){
			error("A Fatal error occured when trying to initialise this page. Please contact BEAM support if this persists.",e);
		}
	}
);

/* Cookie
	- CW: Sets, Gets and Unsets cookies */
var Cookie = {
	set: function(name, value, days){
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+escape(value)+expires+"; path=/";
	},
	get: function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
		}
		return null;
	},
	unset: function(name){
		this.set(name,"",-1);
	}
}

/* Basket
	- CW: Fires up the shopping Basket */
var Basket = {
	show: function(){
		OpenWin("/account/basket",'basket','990','800','yes','yes');
	},
	updated: function(){
		Content.alert("Files awaiting payment in Basket");
	}
}

var Help = {
	show: function(){
		var Help = window.open("/help/index", "HelpWindow", "resizable=yes,scrollbars=yes,status=yes,width=1000,height=500");
	}
}

if(!window.User) var User = {};
Object.extend(User, {
	initialise: function(){
		var useJava = Cookie.get("useJavaUploader");
		useJava = (typeof(useJava) === "string")? useJava.evalJSON(true) : false;
		if(useJava == true){
			this.useJavaUpload();
		}else{
			this.useHttpUpload();
		}
	},
	useJavaUpload: function(){
		this.useJavaUploader = true;
		Cookie.set("useJavaUploader", true, 60);
	},
	useHttpUpload: function(){
		this.useJavaUploader = false;
		Cookie.set("useJavaUploader", false, 60);
	},
	fatalError: "A fatal error occured when trying to initialise this page. Please contact BEAMTV support if this persists."
});

/** Initialize all of our objects now. */
if(window.coreSite){
	window.historyStorage.init();
	window.dhtmlHistory.create();
}

function toggleFrame(){
	var f = $("submitFrame");
	if(f && f.hasClassName("debug")){
		f.removeClassName("debug");
	}else{
		f.addClassName("debug");
	}
}

var translate = function(key, set){
	if (typeof(JsonArgs) != "undefined") {
		var keys = JsonArgs;
		if (typeof(set) != "undefined" && set in JsonArgs) {
			keys = JsonArgs[set];
			if (typeof(keys[key]) == "string") {
				return keys[key];
			}
		}
	}
	return "Translated text could not be found";
}
