// Skapa kaka
function setCookie(c_name,value,expiredays){
	/*
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString());
	*/
	new Ajax.Request("ajax/setCookie.php", {
		method: 'post',
		parameters: {c_name: c_name, value: value, expiredays: expiredays},
		onSuccess: function(transport){
			//alert(transport.responseText);
			//alert("Cookie sparad.");
		}
	});
}
function getCookie(c_name){
	if(document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if(c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
    }
  }
	return "";
}

function sync(sekund){
	// Spara kaka var 10:e sekund
	if(Math.round(sekund/10)==(sekund/10)){
		setCookie("episodePosition",sekund,365);
	}
  text = "";
  if(sekund>=0&&sekund<22) text = "";
  if(sekund>=22&&sekund<28) text = "<img src='foto/stinaBacklund.jpg' style='float:right;' />Stina Backlund";
  if(sekund>=28&&sekund<34) text = "<img src='foto/magganJohansson.jpg' style='float:right;' />Maggan Johansson";
  if(sekund>=34&&sekund<40) text = "Stina Backlund";
  if(sekund>=40&&sekund<50) text = "Sync slut.";
/*
  if(sekund>=0&&sekund<23) text = "Signaturmelodi";
  if(sekund>=23&&sekund<28) text = "<img src='foto/stinaBacklund.jpg' style='float:right;' />Stina Backlund";
  if(sekund>=28&&sekund<34) text = "<img src='foto/magganJohansson.jpg' style='float:right;' />Maggan Johansson";
  if(sekund>=34&&sekund<40) text = "Stina Backlund";
  if(sekund>=40&&sekund<50) text = "Sync slut.";
*/
  $('episodeContent').innerHTML = "<div style='float:left;width:730px;'>"+text+"</div>";
  $('sekund').innerHTML = time(Math.round(sekund*1000))+" //&nbsp;";
}

// FSCommand
var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Hantera alla FSCommand-meddelanden i en Flash-film.
function spelare2_DoFSCommand(command, args) {
	var spelare2Obj = isInternetExplorer ? document.all.spelare2 : document.spelare2;
	if(command=="sync") sync(Math.round(args/1000));
	if(command=="loadcomplete"){
		$('total').innerHTML = time(Math.round(args));
	}
	if(command=="play"){
		//alert("Play");
		$('total').style.display = "block";
		$('sekund').style.display = "block";
	}
	if(command=="pause"){
		$('total').style.display = "none";
		$('sekund').style.display = "none";
	}
}
// Koppling för Internet Explorer.
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub spelare2_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call spelare2_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}


function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}
function time(ms){
	var sec = Math.floor(ms/1000)
	ms = ms % 1000
	//t = three(ms)
	var min = Math.floor(sec/60)
	sec = sec % 60
	//t = two(sec) + ":" + t
	t = two(sec)
	var hr = Math.floor(min/60)
	min = min % 60
	t = two(min) + ":" + t
	var day = Math.floor(hr/60)
	hr = hr % 60
	//t = two(hr) + ":" + t
	//t = day + ":" + t
	return t
}