// loader sync
var loaderDone_var 		= false;
var catDone_var 		= false;
var clipDone_var		= false;
var flvDone_var 		= false;
var controllsDone_var 	= false;

var screenSizeState = "small";
var old_status;
var theVideo;
var theCat;
var theClip;
var theVol		=	64;
var isLive;
var shouldAutostart;
var initBanner;
var initNamePath;
var loadInterval;
var wmpNewState_var;
var timer;
var firstCommercial_var = "yes";

// synkning av ladd
function loaderDone(showFirstCommercial_var,initBanner_var,namePath_var) {
	firstCommercial_var	=	showFirstCommercial_var;
	initBanner			=	initBanner_var;
	initNamePath		=	namePath_var;
	makeFlash("player_flv_v1_06.swf", "container_flv", "container_flv_swf", 480, 360, "opaque");
	makeFlash("player_menu_cat_v1_06.swf", "container_cat", "container_cat_swf", 560, 44, "opaque");
	makeFlash("player_menu_clip_v1_06.swf", "container_menu", "container_menu_swf", 144, 565, "opaque");
	makeFlash("player_controller_v1_06.swf", "container_controller", "container_controller_swf", 629, 37, "opaque");
	loadInterval = window.setInterval("allDone()",500);
	//alert("loader "+loaderDone_var);
}
function firstCommercial(){
	return firstCommercial_var;
}
function catDone(theCat_input,theClip_input,autostart_input) {
	theCat = theCat_input;
	theClip= theClip_input;
	shouldAutostart=autostart_input;
	catDone_var = true;
	//alert("cat "+catDone_var);
}
function clipDone() {
	clipDone_var = true;
	//alert("clip "+clipDone_var);
}

function flvDone() {
	flvDone_var = true;
	//alert("flv "+flvDone_var);
}

function controllsDone() {
	controllsDone_var = true;
	//alert("con "+controllsDone_var);
}

function allDone() {
	if(catDone_var && flvDone_var && clipDone_var) {
		window.clearInterval(loadInterval);
		if(!isIE){
			makeContainer();
		}
		showAll();
	}
}

function showAll() {
	deleteObject("container_loader");
	
	//hideObject("container_loader");
	document.getElementById("bg").style.top 						= "0px";
	document.getElementById("bg").style.left 						= "0px";

	document.getElementById("container_cat").style.top 				= "0px";
	document.getElementById("container_cat").style.left 			= "57px";

	document.getElementById("container_menu").style.top 			= "0px";
	document.getElementById("container_menu").style.left 			= "651px";

	document.getElementById("container_flv").style.top	 			= "55px";
	document.getElementById("container_flv").style.left 			= "165px";

	document.getElementById("container_controller").style.top		= "416px";
	document.getElementById("container_controller").style.left		= "16px";
	
	document.getElementById("container_related").style.top 			= "55px";
	document.getElementById("container_related").style.left			= "16px";
	
	document.getElementById("container_banner").style.top 			= "570px";
	document.getElementById("container_banner").style.left 			= "5px";

	document.getElementById("container_skyscraper").style.top 		= "5px";
	document.getElementById("container_skyscraper").style.left 		= "805px";

	document.getElementById("container_top").style.top 				= "452px";
	document.getElementById("container_top").style.left				= "16px";
	
	if(isIE){
		hideObject("mediacontainer");
	//document.qtcontainer.width 		= "480px";
	//	document.qtcontainer.height	 	= "360px";
	}else{
		

		//document.getElementById("mediacontainer").style.top 		= "55px";
		//document.getElementById("mediacontainer").style.left 		= "165px";
		/*
		if(isIE){
		}else{
			document.qtcontainer.width 		= "480px";
			document.qtcontainer.height	 	= "360px";
		}
		*/
	}
	
	if(initBanner!="no"){
		loadBanners(initBanner,initNamePath);
		initBanner	=	"no";
	}
	
	//alert(initBanner+"    "+initNamePath);
	changeCat(theCat,theClip,shouldAutostart);
}

function loadRek(inputContent) {
//alert("loadRek");
	window.clearInterval(timer);
	if(screenSizeState=="big"){
		bigVideo();
	}		
	if(is_mac && is_moz){
		showForRek();
	}else{
		if(isIE){
			document.wmcontainer.controls.pause();
			hideObject("mediacontainer");
		}
		showObject("container_flv");
	}
	if(isIE){
		document.container_flv_swf.loadFlv(inputContent,"isIE",theVol);
	}else{
		document.qtcontainer.Stop();
		document.container_flv_swf.loadFlv(inputContent,"notIE",theVol);
	}
	playing=false;
	document.container_controller_swf.resetControls();
	//alert("loadRek");
}
function loadRelated(inputLink){
	if(isIE){
		frames['related'].location.href=inputLink;
	}else{
		document.getElementById("related").src=inputLink;
	}
	
}
function loadVideoStep2(inputVideo,inputLive,inputVol) {
//alert(inputVideo);
	//theVol 		=	inputVol;
	//isLive		=	inputLive;
	theVideo	=	inputVideo;
	if(isIE) {
		window.wmcontainer.URL = theVideo;	// HR BLIR DET SEGT!!!! DETTA OMMANDO!!!!! HUR GRA?
		window.wmcontainer.controls.play();
	} else{
		setTimeout("loadVideoStep3();",1000);
		//document.qtcontainer.SetURL(theVideo);
		//document.qtcontainer.Play();
	}
	//setVolume(theVol);
	startInterval();
}
function loadVideoStep3() {
	document.qtcontainer.SetURL(theVideo);
	document.qtcontainer.Play();
}

function loadBanners(movieid,namepath){
//alert(movieid +"   "+namepath);
	if(movieid=="noad"){
		if(isIE) {
			frames['banner'].location.href="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_bottom.jsp?subcat="+namepath;
			frames['skyscraper'].location.href="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_right.jsp?subcat="+namepath;
		}else{
			document.getElementById("banner").src="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_bottom.jsp?subcat="+namepath;
			document.getElementById("skyscraper").src="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_right.jsp?subcat="+namepath;
		}
	}else if(movieid=="no"){
		if(isIE) {
			frames['banner'].location.href="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_bottom.jsp";
			frames['skyscraper'].location.href="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_bottom.jsp";
		}else{
			document.getElementById("banner").src="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_bottom.jsp";
			document.getElementById("skyscraper").src="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_bottom.jsp";
		}
	}else{
		if(isIE){
			frames['banner'].location.href="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_bottom.jsp?subcat="+namepath+"&movieid="+movieid;
			frames['skyscraper'].location.href="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_right.jsp?subcat="+namepath+"&movieid="+movieid;
		}else{
			document.getElementById("banner").src="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_bottom.jsp?subcat="+namepath+"&movieid="+movieid;
			document.getElementById("skyscraper").src="http://wwwc.aftonbladet.se/special/webbtv/jsp/webbtv_ad_right.jsp?subcat="+namepath+"&movieid="+movieid;
		}
		
	}
}

function resetControls() {
	document.container_controller_swf.resetControls();
}
function restartControls() {
	document.container_controller_swf.restartControls();
}
function rekInfo(time) {
	document.container_controller_swf.rekPlaying(time);
}
function changeCat(theCatXml,theClipXml,shouldAutostart_input) {
	document.container_menu_swf.loadMenuXML(theCatXml,theClipXml,shouldAutostart_input);	
}
function playRelated(theClip){
	document.container_cat_swf.loadExtXML(theClip);
}
function playTop(theTopClip){
	changeCat("null",theTopClip,"yes");
}
function pauseVideo() {
	if(isIE) {
		window.wmcontainer.controls.pause();
	} else{
		document.qtcontainer.Stop();
	}
}
function playVideo() {
	if(isIE) {
     	window.wmcontainer.controls.play();
	} else{
		document.qtcontainer.Play();
	}
}
function stopVideo() {
	//window.clearInterval(timer);
	if(isIE) {
		window.wmcontainer.controls.stop();
		window.wmcontainer.URL = "";
		
	} else {
		//document.qtcontainer.SetURL("");
		document.qtcontainer.SetTime(0);
		document.qtcontainer.Stop();
		
	}
	document.container_controller_swf.resetControls("stop");
}

function setVolume(vol) {
	theVol	=	vol;
	if(isNaN(theVol)){
		theVol	=	64;
	}
	if(isIE)  {
		window.wmcontainer.settings.volume = theVol;
	} else {
		document.qtcontainer.SetVolume(Math.floor(theVol*2.56));  // volymskala i QT 0-255
	}
	//document.container_flv_swf.setVol(theVol);
}

function bigVideo() {
	if(screenSizeState=="small"){

		document.getElementById("container_related").style.visibility	= "hidden";
		document.getElementById("container_top").style.visibility		= "hidden";
		document.getElementById("container_controller").style.top 		= "528px";
		
		document.getElementById("mediacontainer").style.left 			= "16px";
		document.getElementById("mediacontainer").style.width 			= "629px";
		document.getElementById("mediacontainer").style.height 			= "473px";		
		
		if(isIE) {
			window.wmcontainer.width 	= 629;
			window.wmcontainer.height 	= 473;
		} else {
			document.qtcontainer.width = "629px";
			document.qtcontainer.height = "473px";
		}
		screenSizeState="big";
	}else if(screenSizeState=="big"){
		
		document.getElementById("mediacontainer").style.width 	= "480px";
		document.getElementById("mediacontainer").style.height 	= "360px";
		document.getElementById("mediacontainer").style.left 	= "165px";
		document.getElementById("container_related").style.visibility	= "visible";
		document.getElementById("container_top").style.visibility		= "visible";
		document.getElementById("container_controller").style.top 		= "416px";
		
		if(isIE) {
			window.wmcontainer.width 	= 480;
			window.wmcontainer.height 	= 360;			
		} else {
			document.qtcontainer.width  = "480px";
			document.qtcontainer.height = "360px";
		}
		screenSizeState="small";
	}

}
function showForRek(){
	document.qtcontainer.width 	= "1px";
	document.qtcontainer.height = "1px";
	
	document.getElementById("mediacontainer").style.top				= "1px"; 
	document.getElementById("mediacontainer").style.left			= "1px"; 
	document.getElementById("mediacontainer").style.width 			= "1px";
	document.getElementById("mediacontainer").style.height 			= "1px";
	showObject("container_flv");
}
function showForVideo(){
	hideObject("container_flv");
	
	document.getElementById("mediacontainer").style.top				= "55px"; 
	document.getElementById("mediacontainer").style.left			= "165px"; 
	document.getElementById("mediacontainer").style.width 			= "480px";
	document.getElementById("mediacontainer").style.height 			= "360px";
	
	document.qtcontainer.width 	= "480px";
	document.qtcontainer.height = "360px";
}
//scrub bar functionality here 

function gotoTime(time) {
	window.clearInterval(timer);
	time = Math.floor(time);
	if(isIE) {
		window.wmcontainer.controls.currentPosition = time;
	} else {
		document.qtcontainer.Stop();
		document.qtcontainer.SetTime(time);
		document.qtcontainer.Play();
	}
	startInterval();
}

function videoEnd() {
	if(screenSizeState=="big"){
		bigVideo();
	}
	document.container_menu_swf.mediaEnd();
}

function startInterval(){
	window.clearInterval(timer);
	timer = window.setInterval("counterUpdate()",1000);
	//alert("startInterval");
}

function counterUpdate() {
	var current,olc_current, duration, reply,old_replay, status,currentSec,durationSec;
	if(isIE) {
		try {
			if(wmpNewState_var==6){
				var bufStat = parseInt(window.wmcontainer.network.bufferingProgress);
				reply		= "Buffrar "+bufStat+"%";
				status		= "text";
				if(reply!=old_replay){
					old_replay=reply;
					document.container_controller_swf.updateTimer(reply,status,current,duration);
				}			
			}else if(wmpNewState_var==3){
				if (!playing) {
					hideObject("container_flv");
					showObject("mediacontainer");
					playing = true;
				}
				if(isLive=="yes"){
					status 		= "text";
					reply 		= "LIVE";
					document.container_controller_swf.updateTimer(reply,status,current,duration);
				}else{
					reply 		= "";
					currentSec	= window.wmcontainer.controls.currentPosition;
					durationSec	= window.wmcontainer.currentMedia.duration;
					current		= toBiDigit(Math.floor(window.wmcontainer.controls.currentPosition/60))+":"+toBiDigit(Math.floor(window.wmcontainer.controls.currentPosition)%60);	
					duration	= toBiDigit(Math.floor(window.wmcontainer.currentMedia.duration/60))+":"+toBiDigit(Math.floor(window.wmcontainer.currentMedia.duration)%60);
					
					status 		= "time";
					document.container_controller_swf.updateTimer(reply,status,current,duration,currentSec,durationSec);
				}
			}else if(wmpNewState_var==9){
				reply	="Initierar";
				status 	= "text";
				if(reply!=old_replay){
					old_replay = reply;
					document.container_controller_swf.updateTimer(reply,status,current,duration);
				}	
			}
		}
		catch(e){
		}
	} else{	// problem i IE!!! krs ev fr tidigt, br annars fungera.
	
		try {
			var qtstatus = document.qtcontainer.GetPluginStatus();
			if(qtstatus.indexOf("hand")!=-1){
				status 		= "text";
				reply 		= "Initierar";
				document.container_controller_swf.updateTimer(reply, status,current,duration,currentSec,durationSec);
			}else if(qtstatus.indexOf("Buff")!=-1){
				status 		= "text";
				reply 		= "Buffrar";
				document.container_controller_swf.updateTimer(reply, status,current,duration,currentSec,durationSec);
			}else if(qtstatus=="Complete"){
		
				if (!playing) {
					if(is_mac && is_moz){
						showForVideo();
					
					}else{
						hideObject("container_flv");
						showObject("mediacontainer");
					}
					playing = true;
				}
				if(isLive=="yes"){
					status 		= "text";
					reply 		= "LIVE";
				}else{
					var factor = document.qtcontainer.GetTimeScale();
					var pos = document.qtcontainer.GetTime();
					var len = document.qtcontainer.GetDuration();
					var D1 = new Date(pos/factor*1000);
					var D2 = new Date(len/factor*1000);
					currentSec	= document.qtcontainer.GetTime();
					durationSec	= document.qtcontainer.GetDuration();
					currentsec 	= (toBiDigit(D1.getMinutes())*60)+toBiDigit(D1.getSeconds());
					current 	= toBiDigit(D1.getMinutes()) + ":" + toBiDigit(D1.getSeconds());
					duration 	= toBiDigit(D2.getMinutes()) + ":" + toBiDigit(D2.getSeconds());
					reply 		=  toBiDigit(D1.getMinutes()) + ":" + toBiDigit(D1.getSeconds()) + " / " + toBiDigit(D2.getMinutes()) + ":" + toBiDigit(D2.getSeconds());
					status 		= "time";
					//alert(status+" | "+duration+" | "+current);
					if (reply.indexOf("12:19") +1){
						status	= "text";
						reply 	= "Initierar";
					}
				}
				document.container_controller_swf.updateTimer(reply, status,current,duration,currentSec,durationSec);
			}
		}
		catch(e){
		}
	} 
}

function toBiDigit(num) {
	if(num < 10) {
		return "0" + num;
	} else {
		return num;
	}
}
function getArgs(  ) {
    var args = new Object(  );
    var query = location.search.substring(1);     
      // Get query string
    var pairs = query.split("&");
     // Break at comma
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
          // Look for "name=value"
        if (pos == -1) continue;
          // If not found, skip
        var argname = pairs[i].substring(0,pos);
          // Extract the name
        var value = pairs[i].substring(pos+1);
          // Extract the value
        args[argname] = unescape(value);
         // Store as a property
       // In JavaScript 1.5, use decodeURIComponent(  ) 
       // instead of escape(  )
    }
    return args;     // Return the object
}

function wmpNewState(newState) {
	wmpNewState_var=newState;
	switch (newState){
    case 1:
		//wmcontainer_state	= "stopped";
		break;
    case 2:
		//wmcontainer_state	= "paused";
		break;
    case 3:
		// wmcontainer_state	= "playing ("+wmcontainer.network.bitRate/8000+" kbps)";
		break;
    case 6:
		//wmcontainer_state	= "buffering...";
		break;
    case 7:
		//wmcontainer_state	= "waiting...";
		break;
    case 8:
		//wmcontainer_state	= "media ended";	
		//videoEnd();
		break;
    case 9:
		//wmcontainer_state	= "transitioning";
		break;
    case 10:
//		wmcontainer_state	= "ready";
		break;

    default:
	}
}

// misc stuff
function getSerialId() {
	incoming_vars = getArgs();
	return incoming_vars['serialid'];
}
function getStartCategory() {
	incoming_vars = getArgs();
	return incoming_vars['category'];
}
function getClient() {
	incoming_vars = getArgs();
	return incoming_vars['client'];
}
function alertThis(input) {
	alert(input);
}
function hideObject(obj) {
	document.getElementById(obj).style.visibility = "hidden";
}
function showObject(obj) {
	document.getElementById(obj).style.visibility = "visible";
}
function deleteObject(obj) {
	document.getElementById(obj).innerHTML = "";
	document.getElementById(obj).style.visibility = "hidden";
}
