var sid='illigal';
var hasRotated = false;
var lastPlayedId = false;
var displayDate = false;
var comId = 0;


//var playerUrl = "http://127.0.0.1/mediamaker_dk4/mediamaker.php";
var playerUrl = "http://infocast.dk/mediamaker_dk4/mediamaker.php";

function openNewsAlertWin() {
	window.location='maillist.php?sid='+sid;
}

function setInfo(_clip) {
	
	//postpend dato in front of titel if it should be shown
	if (displayDate) {
		dispTitel = _clip.oprettetDen + " " + _clip.titel; 
	} else {
		dispTitel =_clip.titel
	}
	
	
	//short titel Length so it won't break
	var titelLength = 55;
	if (dispTitel.length>titelLength) {
		dispTitel = dispTitel.substring(0, titelLength-3) + '...';
	}
	
	
	var infoHead = document.getElementById('infoHead');
	
	var header = document.createTextNode(dispTitel);
	infoHead.appendChild(header);
	
	var infoCont = document.getElementById('infoBody');
	
	
	//change [BR] to <BR> javascript way.
	info = _clip.info;
	while ((breakIndex = info.indexOf('[BR]'))!=-1) {
		nonBreakedInfo = info.substring(0,breakIndex);
		info =  info.substring(breakIndex+4,info.strlen);
		
		var txt = document.createTextNode(nonBreakedInfo);
		infoCont.appendChild(txt);
		
		infoCont.appendChild(document.createElement('<BR>'));
		
	}
	
	
	var txt = document.createTextNode(info);
	infoCont.appendChild(txt);

}

function clearInfo() {
	
	var head = document.getElementById('infoHead');
	
	while ( head.hasChildNodes() ) {
		head.removeChild(head.firstChild);
	}
	
	var body = document.getElementById('infoBody');
	
	while (body.hasChildNodes()) {
		body.removeChild(body.firstChild);
	}
	
}

function setTitle(_clip) {
	window.status=_clip.titel;
	
	var titelCont = document.getElementById('titelCont');
	var header = document.createTextNode(_clip.titel);
	 titelCont.appendChild(header);
}

function clearTitle() {
	var titel = document.getElementById('titelCont');
	
	while (titel.hasChildNodes() ) {
		titel.removeChild(titel.firstChild);
	}
	
}

function countShow(_clip) {
	html = '<iframe  style="display:none" src="counter.php?loc=countShow&id=' + _clip.id + '&sid=' + sid + '"></iframe>';
	document.getElementById('counterCont').innerHTML=html;
}


function openTipWin() {
	if (!lastPlayedId) {
		alert('Du skal vælge et klip før du kan tippe en ven');
	} else {
		window.location='tip.php?id='+ lastPlayedId;
	}
}

function runsCommercial() {
	if (document.player1) {
		try {
			var currentUrl = document.player1.currentMedia.sourceUrl
		
			if (clip.url !=  currentUrl) {
				return true;
			}
		} catch (Exception){}
	}
	
	return false;
		
	
}


var isSetComTitle = 0;
function infoPolle() {
	
	if (runsCommercial()) {
		var c = clipObj(comId, 'Reklame',' ', 'Reklame', true, ' ')
		
		if (isSetComTitle == 0 ) {
			setTitle(c);
			setInfo(c);
			isSetComTitle = 1;
			countShow(c);
		}
		setTimeout("infoPolle()",300);
		return;
	}

	if (document.player1) {
		clearInfo();
		clearTitle();
	
		setInfo(clip);
		setTitle(clip);
	
		//lastPlayedId til newsAlert
		lastPlayedId = clip.id;
	}
	
}



function countClip() {
	
	if (!runsCommercial()) {
		if (document.player1) {
			countShow(clip);
		}
	} else {
		setTimeout("countClip()",2000);
	}
}

function clipObj(_id, _titel, _url, _info, _isCommercial, _oprettetDen) {
	obj = new Object;
	
	obj.id = _id;
	obj.titel = _titel;
	obj.url = _url;
	obj.isCommercial = _isCommercial;
	obj.oprettetDen = _oprettetDen;
	obj.info =  _info;
	return obj;
	
}


/**
 * Shows an image in the player window.
 */
function showImage(_image) {
	
	canvas = document.getElementById('playerImageLayer');
	canvas.style.visibility='';
	player.style.visibility='hidden';
	canvas.innerHTML = "<img width="+player.width+" height="+player.height+" src="+_image+">";
}

function openfaq() {
	window.open("http://faq.xstream.dk/faq.htm","mm_faq","height=600, width=800, scrollbars=yes");
}

function openXstream() {
	window.open("http://www.xstream.dk","xstraemFaq","height=600, width=800, scrollbars=yes");
}

function openMediamaker( _width, _height, _parameter) {
	
	if (_parameter==undefined) {
		_parameter="";
	}
	
	if (popUp = window.open(playerUrl+_parameter, "popUp", "top=" + ((screen.availHeight/2) - (_height / 2)) + ",left=" + ((screen.availWidth/2) - (_width / 2)) + ",width="+_width+",height="+_height+",resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0")){
		popUp.focus();
	}
	return popUp;
	
}

function sureDelete(_id,_charName) {
	if(confirm("Er du sikker på du vil slette " + _charName +" !")) {
	 	window.location='?sLoc=del&id=' + _id;
	} else {
 		return false;
	}
}

function disableKey( _key) {
	if (	window.event ) {
		if (window.event.keyCode==_key) {
			window.event.keyCode=0;
			window.event.returnValue=false;
		}
	}
}

function cutTofit( _size, object ) {
	if (object.value.length >_size) {
		object.value=object.value.substring(0,_size);
	}
}

function blinkIt() {
 if (!document.all) return;
 else {
   for(i=0;i<document.all.tags('blink').length;i++){
      s=document.all.tags('blink')[i];
      s.style.visibility=(s.style.visibility=='visible')?'hidden':'visible';
   }
 }
}

function checkClick(pos) {
	posx=pos.x;
	posy=pos.y;
	
	var player = document.getElementById('playerCont');
	if (player) {
		var xStart = player.getAttribute('offsetLeft');
		var yStart = player.getAttribute('offsetTop');
		var width  = player.getAttribute('offsetWidth');
		var height = player.getAttribute('offsetHeight');
		
		if (runsCommercial() && posx > xStart && posx < xStart+width && posy > yStart && posy < yStart + height )
			videoClick();
	}
}

function mouseEventHandler(mEvent) {
	obj = new Object;
	posx = 0;
	posy = 0;
	
	
  // Internet Explorer
  if (mEvent.srcElement)
  {
    
		posx = mEvent.clientX + document.body.scrollLeft;
		posy = mEvent.clientY + document.body.scrollTop;
   
    
  }
  // Netscape and Firefox
  else if (mEvent.target)
  {
		posx = mEvent.pageX;
		posy = mEvent.pageY;   
  	
  }
  obj.x =  posx;
  obj.y =  posy;
  
  checkClick(obj);
}


var hasCounted = 0;
function videoClick() {
	
	if (runsCommercial()) {
		if (!isPlaying()) {
			return;
		}
	
		if (hasCounted == 0) {
			talAnnonce();
			hasCounted = 1;
		}
			
		visAnnonce();
	}
		
}

function talAnnonce()  {
	if (comId>0) {
		html = '<iframe style="display:none" src="counter.php?loc=countClick&id=' + comId + '"></iframe>';
		document.getElementById('counterDiv').innerHTML=html;
	}
}

function isPlaying() {
	if (document.player1.playState==-1)
	
		return false;
	return true;
}

// if the current clip is of type radio
// hide player and show a picture

function switchBackgroundImage() {
	
	if ( !runsCommercial() ) {		
		hidePlayer();
		scaleScreener();
		showImage();
		centerImage();
		
	} else {
		showPlayer();
		hideImage();
		
		
	}
	
	if (screener != null ) {
		setTimeout("switchBackgroundImage()",500);
	} 
}

function centerImage() {
	
	//center the image using padding
	var widthOffset = (document.player1.width - screenerWidth) / 2;
	var heightOffset = (document.player1.height - screenerHeight) / 2;
	
	
	canvas = document.getElementById('screenerImg');
	
	if (canvas.height<540) {
		document.getElementById('screenerImg').style.top= heightOffset;
		document.getElementById('screenerImg').style.left = widthOffset;
		
	}
}

function showImage() {
	
	canvas = document.getElementById('screenerImg');
	canvas.src = screener.src;
	canvas.width= screenerWidth;
	canvas.height= screenerHeight;
	
}
function hideImage() {
	
	canvas = document.getElementById('screenerImg');
	canvas.src = screener.src;
	canvas.width= 0;
	canvas.height= 0;	
}

function hidePlayer() {
	
	if (document.player1.style.visibility != 'hidden' ) {
		document.player1.style.visibility='hidden';
	}
}


function showPlayer() {
	
	if (player1.style.visibility != '' ) {
		player1.style.visibility='';
	}
}

function restorePlayer() {
	if (document.player1.width == 0 ) {
		document.player1.width = playerWidth;
		document.player1.height = playerHeight;
	}
}

// hvis billedet er mindre en 320*240, rezise billedet op til 320*240 uden at skalere det.
// hvis billedet er større en 320*240,  rezise billedet op til 512*384 uden at skalere det.
// standarg jpg skal dog altid skalers til max størrlese.

function scaleScreener() {
	
	if ( screenerWidth<=320 && screenerHeight<=240 && screener.src.indexOf('standard.jpg')==-1) {
		
		wScale = 320/screenerWidth;
		hScale = 240/screenerHeight;
		
	} else {
		wScale = 512/screenerWidth;
		hScale = 384/screenerHeight;
		
	}
	if (wScale < hScale) {
		scale	= wScale;
	} else {
		scale	= hScale;
	}
	screenerWidth = screenerWidth*scale;
	screenerHeight = screenerHeight*scale;
	
}