<!-- /* SENASTE UPPDATERADE: ver. 2006.03.15 12:10, last author: Jon */ -->
// Misc Mediaplayer control functions
var paused, running;
var playing = true;
var controllsDone_var = false;
var skyDone_var = false;
var topDone_var = false;
var loaderDone_var = false;
var menuDone_var = false;
var flvDone_var = false;
var theVideo;
var errorCounter;

function initPlayer() {
	showObject("container_controlls");
	showObject("container_top");
	showObject("container_right");
	showObject("ram_liten");
	hideObject("mediacontainer");
	showObject("container_flv");
}
// synkning av ladd
function controllsDone() {
	controllsDone_var = true;
	allDone();
}
function skyDone() {
	skyDone_var = true;
	allDone();
}
function loaderDone() {
	loaderDone_var = true;
	allDone();
}
function menuDone() {
	menuDone_var = true;
	allDone();
}
function topDone() {
	topDone_var = true;
	allDone();
}
function flvDone() {
	flvDone_var = true;
	allDone();
}
function allDone() {
	if(controllsDone_var && skyDone_var && topDone_var && loaderDone_var && menuDone_var && flvDone_var) {
		try {
			//alert("HEJ!");
			document.container_loader_swf.loadingDone();
			//document.container.loadingDone1();
			//document.container_top_swf.loadingDone3();
			//document.container_flv_swf.loadingDone5();
			initPlayer();
		}
		catch(e)
		{}
		return true;
	}
}
function initVideo() {
	document.getElementById("mediacontainer").style.top = "57px";
	hideVideo();
	showObject("container_flv");
}
function lc() {
	return theLC;
}

function hideObject (object) {
	document.getElementById(object).style.visibility = "hidden";
}
function showObject (object) {
	document.getElementById(object).style.visibility = "visible";
}

function hideLoader() {
	hideObject("container_loader");
}
function loadVideo(video) {
	theVideo = video;
	errorCounter = 0;
	loadVideoStep2();
}

function loadVideoStep2() {
	if(errorCounter < 3) {
		document.container_video.updateTimer(" ", "loading");
		hideObject("mediacontainer");
		/* Mediacontainer: */
		if(isWMP) {
	//		document.getElementById("mediacontainer").style.visibility = "visible"
			document.container_video.updateTimer('', 'freezing');
			window.wmcontainer.controls.stop();
			window.wmcontainer.URL = theVideo;	// HR BLIR DET SEGT!!!! DETTA OMMANDO!!!!! HUR GRA?
		} else
		{
			document.qtcontainer.Stop();
			document.qtcontainer.SetURL(theVideo);
		}
		running = false;
		paused = false;
		playing = false;
		hideVideo();
		playVideo();
	}
}
function loadLiveVideo(video) {
	if(isWMP) {
   		window.wmcontainer.controls.stop();
		window.wmcontainer.URL = video;	// HR BLIR DET SEGT!!!! DETTA OMMANDO!!!!! HUR GRA?
	} else
	{
		document.qtcontainer.SetURL(video);
	}
	showVideo();
	running = false;
	paused = false;
	firstPlay = true;
	playVideo();
}
function do_playpause_state() {
	if(!running && playpause_state == "play") {
       	playVideo();
	}else if(!paused && playpause_state == "pause") {
       	pauseVideo();
	}
}
function playVideo() {
	do_play = true;
	running = true;
	paused = false;
	playpause_state	= "play";
	previousBuffstat = -3;
	if(isWMP) {
       	window.wmcontainer.controls.play();
	} else
	{
		document.qtcontainer.Play();
	}
}
function pauseVideo() {
//alert("paused");
	playpause_state	= "pause";
	paused = true;
	running = false;
	if(isWMP) {
		window.wmcontainer.controls.pause();
	} else
	{
		document.qtcontainer.Stop();
	}
}
function smallVideo() {
	if(isWMP) {
		window.wmcontainer.width 	= smallW;
		window.wmcontainer.height 	= smallH;
	} else
	{
		document.qtcontainer.width = smallW;
		document.qtcontainer.height = smallH;
	}
	document.getElementById("container_controlls").style.top 	= "443px";
	document.getElementById("container_controlls").style.left 	= "275px";
	document.getElementById("container_controlls").style.width 	= "810px";
	document.getElementById("container_controlls").style.height	= "250px";
	showObject("container_div");
	showObject("ram_liten");
	hideObject("ram_stor");
	document.getElementById("mediacontainer").style.left	= small_pos_x;
	//hideObject("container_controlls");  // test
	
}
function bigVideo() {
		if(isWMP) {
		window.wmcontainer.width 	= bigW;
		window.wmcontainer.height 	= bigH;
	} else {
		document.qtcontainer.width = bigW;
		document.qtcontainer.height = bigH;
	}
	hideObject("container_div");
	document.getElementById("container_controlls").style.top 	= "642px";
	document.getElementById("container_controlls").style.left 	= "2px";
	document.getElementById("container_controlls").style.width 	= "1024px";
	document.getElementById("container_controlls").style.height	= "50px";
	hideObject("ram_liten");
	showObject("ram_stor");
	document.getElementById("mediacontainer").style.left	= big_pos_x;
}
function setVolume(vol) {
	unmute();
	user_set_volume	= vol;
	if(isWMP)  {
		window.wmcontainer.settings.volume = vol;
	} else {
		document.qtcontainer.SetVolume(vol*2.56);  // volymskala i QT 0-255
	}
}

function do_mute_state() {
	if(isWMP) {
		window.wmcontainer.settings.mute = mute_state;
	} else {
		document.qtcontainer.SetMute(mute_state);//something
	}
}
function mute() {
	mute_state = true;
	do_mute_state();
}

function unmute() {
	mute_state = false;
	do_mute_state();
}
function startInterval(){
	if(timer == undefined) {
		var timer = window.setInterval("counterUpdate()",1000);
	}
}

// hide video container. Obsolete?	
function hideVideo(){
//alert("hidden");
	pauseVideo();
	playpause_state	= "pause";
	do_play = false;
	hideObject("mediacontainer");
}
function showVideo(){
	showObject("mediacontainer"); 
}
//user clicks the video
function videoClick() {
	//code here
}

//called at end of every clip
function videoEnd() {
	hideObject("mediacontainer");
	showObject("container_flv");
	smallVideo();
	if(!isIE) {
		var currentClip = document.qtcontainer.GetURL();
		if(currentClip.indexOf("qtif") == -1) {
			document.container_video.smallVideoX();
			document.container.mediaEnd();
		} 
	} else {
		document.container_video.smallVideoX();
		document.container.mediaEnd();
	}
}

// used to update the timer. Called utomatically every second.
function counterUpdate() {
	if(isWMP) {
//	var bufStat = wmcontainer.network.bufferingProgress;
		try {
			var bufStat = parseInt(window.wmcontainer.network.bufferingProgress);
			var reply, status;

			bufStat = Math.max(bufStat, previousBuffstat);
			bufStat = Math.min(bufStat, 100);
			previousBuffstat	= Math.max(bufStat, previousBuffstat + 3);
			if (bufStat < 100 && bufStat > 0) {
				reply	= "Buffrar:" + bufStat + "%";
				status	= "buffering";
			} else {
				if (!playing) {
					hideObject("container_flv");
					showObject("mediacontainer");
					//playVideo();
					playing = true;
				}
				do_mute_state();
				do_playpause_state();
				document.container_video.updateSoundIcon(mute_state);
		
				previousBuffstat	= 0;
				if(window.wmcontainer.currentMedia != null && do_play){
					if(window.wmcontainer.currentMedia.DurationString != "00:00") {
					//if(document.getElementById("mediacontainer").style.visibility != "visible")
						//window.container.bgAnimAct("fadeOut");
					reply = window.wmcontainer.controls.currentPositionString + " / " + wmcontainer.currentMedia.DurationString;
						status = "playing";
					} else {
						reply = "initiering";
						status = "playing";	// ??????????????????????????????????????????
					}
				}
			}
			document.container_video.updateTimer(reply, status);
		}
		catch(e)
		{
		}
	} else {	// problem i IE!!! krs ev fr tidigt, br annars fungera.
		try {
		var qtstatus = document.qtcontainer.GetPluginStatus();
		switch(qtstatus) {
			case "Waiting":
			case "Loading":
			reply	= "Buffrar";
			status	= "buffering";
			break;
			case "Playable":
			case "Complete":
			if (!playing) {
				hideObject("container_flv");
				showObject("mediacontainer");
				playVideo();
				playing = true;
			}
			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);
			reply =  toBiDigit(D1.getMinutes()) + ":" + toBiDigit(D1.getSeconds()) + " / " + toBiDigit(D2.getMinutes()) + ":" + toBiDigit(D2.getSeconds());
			status = "playing";
			do_mute_state();
			do_playpause_state();
			document.container_video.updateSoundIcon(mute_state);
			if (reply.indexOf("12:19") +1) reply = 'Initierar';
			break;
			case "Complete, 404: Not Found":
			reply = "Filen finns inte";
			status = "Error";
			showObject("container_flv");
			hideObject("mediacontainer");
			//if(qtstatus.indexOf("Error") != -1) {
			//	alert("QT Error");
			//} else {
				//videoEnd();
			//}
			break;
			default:
				//alert(qtstatus);
				if(qtstatus.indexOf("Error") != -1) {
					errorCounter++;
					loadVideoStep2();
				}
			break;
		}
		document.container_video.updateTimer(reply, status);
	}
	catch(e) 
	{
	}
	} 
}
//small helper function
function toBiDigit(num) {
	if(num < 10) {
		return "0" + num;
	} else {
		return num;
	}
}
// For windows IE QT combo
function forceQuickTime() {
	window.location.replace('index.html?forceqt=true');
}

// Cookies 15/3 2006
function setCookie(name, value) {
	var expires = new Date();
	expires.setTime(expires.getTime() + 7 * 24 * 60 * 60 * 1000);  // expires in one week
	var curCookie = name + "=" + escape(value) +
      "; expires=" + expires.toGMTString() //+
	  //"; bandw=" + Math.floor(bandw);
 	document.cookie = curCookie;
}
function readCookie(name) {
	var dc = document.cookie;
  	var prefix = name + "=";
  	var begin = dc.indexOf("; " + prefix);
  	if (begin == -1) {
    	begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  	} else
    	begin += 2;
  	var end = document.cookie.indexOf(";", begin);
  	if (end == -1)
    	end = dc.length;
  	return unescape(dc.substring(begin + prefix.length, end));
}

function hideSwfontop(){
	//document.getElementById("swfontop_div").style.visibility = "hidden";
	//document.getElementById("mediacontainer").style.visibility = "visible"
}

// open a window with a form for mail -obsolete!
function externalVideo(clip) {
	//if(hasSafari()) {
		//alert(clip);
		//window.location.href = clip;
		//var popup = window.open(clip, "popup", "width=347,height=365,location=no,menubar=no,scrollbars=no,toolbar=no,status=no,resizable=no");
//	} else {
		var popup = window.open("extvid.html?clip=" + clip +"&format=wmv", "popup", "width=347,height=365,location=no,menubar=no,scrollbars=no,toolbar=no,status=no,resizable=no");
	//}
}

/*
 * This function parses '&'-separated name=value 
 * argument pairs from the query string of the URL. 
 * It stores the name=value pairs in 
 * properties of an object and returns that object.
 */
function getArgs(  ) {
    var args = new Object(  );
    var query = location.search.substring(1);     
//alert(query);
      // 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);
//alert(argname);
          // Extract the name
        var value = pairs[i].substring(pos+1);
//alert(value);
          // 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) {
	switch (newState){
    case 1:
//		wmcontainer_state	= "stopped";
//		window.document.container.SetVariable("wmcontainer_state", wmcontainer_state);
    break;

    case 2:
//		wmcontainer_state	= "paused";
//		window.document.container.SetVariable("wmcontainer_state", wmcontainer_state);
    break;

    case 3:
// 		wmcontainer_state	= "playing ("+wmcontainer.network.bitRate/8000+" kbps)";
//		window.document.container.SetVariable("wmcontainer_state", wmcontainer_state);
         
    break;

    case 6:
//		wmcontainer_state	= "buffering...";
//		window.document.container.SetVariable("wmcontainer_state", wmcontainer_state);
    break;

    case 7:
//		wmcontainer_state	= "waiting...";
//		window.document.container.SetVariable("wmcontainer_state", wmcontainer_state);
    break;

    case 8:
		videoEnd();
//		wmcontainer_state	= "media ended";
//		window.document.container.SetVariable("wmcontainer_state", wmcontainer_state);
    break;

    case 9:
//		wmcontainer_state	= "transitioning";
//		window.document.container.SetVariable("wmcontainer_state", wmcontainer_state);
    break;

    case 10:
//		wmcontainer_state	= "ready";
//		window.document.container.SetVariable("wmcontainer_state", wmcontainer_state);
    break;

    // Other cases go here.

    default:
//    doNothing
	}
}
// misc stuff
function getisWMP() {
	return isWMP;
}
function getSerialId() {
	incoming_vars = getArgs();
	return incoming_vars['serialid'];
}
function getStartCategory() {
	incoming_vars = getArgs();
	return incoming_vars['category'];
}
function alertThis(input) {
	alert(input);
}