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


var playerUrl = "http://mediamaker.dk/hairtv/mediamaker.php";


function openNewsAlertWin(language) {
	popUp = window.open('maillist.php?sid='+sid+'&language='+language, "Tip","width=450,height=624,resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0");
	popUp.focus();
	return popUp;
}

function openNewsAlertWin2(language) {
	popUp = window.open('tilmeld_nyhedsbrev.php?sid='+sid+'&language='+language, "Tip","width=450,height=324,resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0");
	popUp.focus();
	return popUp;
}


function openTipWin(clip_id,language) {
	popUp = window.open('tip.php?id='+ clip_id+'&language='+language, "Tip","width=450,height=524,resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0");
	popUp.focus();
	return popUp;
}

function setInfo(_clip) {
	if (_clip.titel==null) 
		return null;

		
	//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 = 60;
	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');
	var lineBreak = document.createElement("br");
	
	//[br] skal erstattes med html linieskift

// følgende kode er udekommenteret da den ikke virker i tilstrækkelig grad. Hvis der er for mange linie skift går den i uendelig løkke
/*

	//hvis der ikke er nogle så udskriv htmlstreangen uden videre.
	if (_clip.info.indexOf('[br]')==-1) {

		var txt = document.createTextNode(_clip.info);
		infoCont.appendChild(txt);
	
	//eller erstat alle [br] med lineBreak variablen.
	} else {

		var temp = _clip.info;
		while (temp.indexOf('br')!=-1) {

			var beforeBR = _clip.info.substr(0,temp.indexOf('[br]'));

			var temp = _clip.info.substr(temp.indexOf('[br]')+4,temp.length);

			var txt = document.createTextNode(beforeBR);
			infoCont.appendChild(txt);
			infoCont.appendChild(lineBreak);
		}
		var txt = document.createTextNode(temp);
		infoCont.appendChild(txt);
	}
*/
// istedet er følgende indsat


	var temp = _clip.info;
	temp = temp.replace(/\[([^\[\]\|]*)\|([^\[\]\|]*)\]/gi,"<a style=\"color:orange\" href=\"http://$2\">$1</a>");

	//var txt = document.createTextNode(temp.replace("[br]", ' '));
	//infoCont.appendChild(txt);

	infoCont.innerHTML = temp.replace("[br]", " ");
}

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 openRelatedClips() {
	popUp = window.open('relatedclips.php', "relatedclips","width=900,height=760,resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0");
	popUp.focus();
	return popUp;
}

function runsCommercial() {
	try {
		var currentUrl = document.player1.currentMedia.sourceUrl
	} catch (Exception) { return false; }; //netscape skal altid returnere false da den ikke kan se om een reklame kører .

	if (clip.url !=  currentUrl) {
		return true;
	}
	
	return false;
		
	
}


var isSetComTitle = 0;
function infoPolle() {
	if (runsCommercial()) {
		var c = clipObj(comId, comTitle, ' ', comInfo, true, ' ');
		if (isSetComTitle == 0 ) {
			setTitle(c);
			setDato(c);
			setInfo(c);
			isSetComTitle = 1;
			countShow(c);
		}
		setTimeout("infoPolle()",300);
		return;
	}
	clearInfo();
	clearTitle();
	if(clip) {
		setInfo(clip);
		setTitle(clip);
		setAnbefal();
		//lastPlayedId til newsAlert
		lastPlayedId = clip.id;
	}
}

function setAnbefal(lang) {
	var anbefalCont = document.getElementById('tipLnk');
	anbefalCont.innerHTML = "<INPUT TYPE=\"image\" src=\"./images/lang/tip_"+lang+".gif\" id=\"tipLnk\" class=\"lnk\" OnClick=\"openTipWin();\" Onfocus=\"this.blur()\">";
}

function isNull(a) 
 {
      return typeof a == 'object' && !a;
 }

function countClip() {
	if (!runsCommercial()) {
		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;

}



function openfaq(lang) {
	if(lang == "da")
		window.open("http://faq.xstream.dk/faq.htm","mm_faq","height=600, width=800, scrollbars=yes");
	else
		window.open("http://www.xstream.dk/2/123/faq.html","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="";
	}
	
//	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=1,location=0,status=0,menubar=0");
	popUp = window.open(playerUrl+_parameter, "popUp", "top=0,left=" + ((screen.availWidth/2) - (_width / 2)) + ",width="+_width+",height="+_height+",resizable=0,toolbar=0,scrollbars=1,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');
	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 openWhenComRuns() {
	if (runsCommercial())
		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" position:absolut; top:0; left:0; 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;
	
}

function detectBrowser() {
  if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') ) {
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') ) {
      $browser = 'Netscape (Gecko/Netscape)';
    } else if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )   {
      $browser = 'Mozilla Firefox (Gecko/Firefox)';
    } else {
      $browser = 'Mozilla (Gecko/Mozilla)';
    }
  } else if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) {
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') )  {
      $browser = 'Opera (MSIE/Opera/Compatible)';
    } else {
      $browser = 'Internet Explorer (MSIE/Compatible)';
    }
  } else {
    $browser = 'Other browsers';
  }
  return $browser;
} //end func detect browser

