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=false;
	if(e && document.debug){
		var er = "Whoops!\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.logerrors){
			console.log(er);
		}else{
			alert(er);
		}
	}else{
		alert(msg, "error");
	}
}


function playClip(href){
	Lightbox.open("");
	new Ajax.Request(href, {onSuccess: function(t){
		if(document.documentElement && document.documentElement.scrollTop){
			document.documentElement.scrollTop=0;
		}else if(document.body && document.body.scrollTop){
			document.body.scrollTop=0;
		}
		Lightbox.elInner.innerHTML = t.responseText;
		Lightbox.elInner.innerHTML.evalScripts();
		Lightbox.el.className="lightbox";
		setupLinks();
	}});
}

function setupLinks(){
	try{
		var links = document.body.getElementsByTagName("a");
		for(i=0;i<links.length;i++){
			if(links[i].rel == "lightbox" && !links[i].playListener){
				links[i].playListener = function(evt){
					Lightbox.open("");
					Lightbox.el.className = "loading";
					playClip(this.href);
					Event.stop(evt);
				}.bindAsEventListener(links[i]);
				Event.observe(links[i], "click", links[i].playListener);
			}
		}
	}catch(e){
		error("Error initialising reel", e);
	}
}

window.onload = function(){
	try{
		var location = document.location.toString();

		if(location.indexOf("file") != -1){
			playClip(location);
		}
		setupLinks();
	}catch(e){
		error("Error setting up page", e);
		//Seems that JavaScript has failed for some reason, thats ok! Its bulletproof!
	}
}
