var currentPlayer=false;
var playerFlash;
var playerSilverlight;
var isLive = false;
var liveFrontImage = false;
var currentVideoId = false;

var isEmbedded = false;

function xAddEventListener(e,eT,eL,cap)
{
  if(!(e=xGetElementById(e)))return;
  eT=eT.toLowerCase();
  if(e.addEventListener)e.addEventListener(eT,eL,cap||false);
  else if(e.attachEvent)e.attachEvent('on'+eT,eL);
  else {
    var o=e['on'+eT];
    e['on'+eT]=typeof o=='function' ? function(v){o(v);eL(v);} : eL;
  }
}

function xGetElementById(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}

var isBigSize = false;
function toggleScreenSize() {
	isBigSize = !isBigSize;

	if(flashIsInitialized) playerFlash.setScreenSize(isBigSize ? "big" : "small");
	if(silverlightIsInitialized) playerSilverlight.setScreenSize(isBigSize ? "big" : "small");
	
	if(isBigSize) {
		$("#containerPlayer,#ply,#plySL").css({width:"960px",height:"540px"});
		$("#containerMenuLevel1,#containerMenuLevel2,#containerArticles,#containerTools").css({display:"none"});
	} else {
		$("#containerPlayer,#ply,#plySL").css({width:"540px",height:"304px"});
		$("#containerMenuLevel1,#containerMenuLevel2,#containerArticles,#containerTools").css({display:"block"});
	}
}

function getRealTop(el){
	yPos = document.getElementById(el).offsetTop;
	tempEl = document.getElementById(el).offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}

function getRealLeft(el){
	xPos = document.getElementById(el).offsetLeft;
	tempEl = document.getElementById(el).offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}

var windowVisible = false;
function hideWindow() {
	var o = document.getElementById("popup");
	$(o).animate({opacity:0.01,height:1},600,"swing",function(){this.style.display="none"; windowVisible=false;});
}
function showWindow(url, width, height, topPos, leftPos) {
	var o = document.getElementById("popup");
	var cb = function(){
		windowVisible = true;
		var flashWidth = 962;
		var flashHeight = 542;
		if(!width) width = 500;
		if(!height) height = 300;
		$(o).load(function(){
			if(topPos) o.style.top = topPos + "px";
			else o.style.top = Math.floor((flashHeight - height)/2) + "px";
	
			if(leftPos) o.style.left = leftPos + "px";
			else o.style.left = Math.floor((flashWidth - width)/2) + "px";
	
			o.style.width = width + "px";
			o.style.height = height + "px";
			$(o).css({opacity:0});
			o.style.display = "block";	
			var h = height;
			$(o).animate({opacity:0.95,height:h},600,"swing");
			$(o).unbind("load");
		});
		o.src = url;
	}

	if(windowVisible)
	{
		$(o).animate({opacity:0.01,height:1},300,"swing",function(){this.style.display="none"; windowVisible=false; cb();});
	}
	else
	{
		cb();
	}
}

function resizeWindow(newWidth,newHeight,ms,newLeft,newTop,newOpacity)
{
	var o = document.getElementById("popup");
	var flashWidth = 962;
	var flashHeight = 542;
	if(!ms) ms=600;

	if(!newTop) newTop = Math.floor((flashHeight - newHeight)/2) + "px";
	if(!newLeft) newLeft = Math.floor((flashWidth - newWidth)/2) + "px";
	
	if(!newOpacity)
		$(o).animate({height:newHeight,width:newWidth,left:newLeft,top:newTop},ms,"swing");
	else
		$(o).animate({height:newHeight,width:newWidth,left:newLeft,top:newTop,opacity:newOpacity},ms,"swing");
}

function showEmbed(lang) {
	if (!lang) lang = "no";
	var currentId = currentPlayer.getCurrentVideo();
	showWindow("/popups/embed.php?videoId=" + currentId + "&lang=" + lang, 500, 180);
}

function showTipAFriend(lang) {
	if (!lang) lang = "no";
	showWindow("/popups/tipafriend.php?videoId=" + currentPlayer.getCurrentVideo() + "&lang=" + lang, 500, 330);
}

function showRSS(lang)
{
	if (!lang) lang = "no";
	showWindow("/popups/rss.php?q=" + escape(carouselParams) + "&lang=" + lang, 500, 170);
}

function showSearch(lang)
{
	if (!lang) lang = "no";
	showWindow("/popups/search.php?lang=" + lang, 500, 135);
}

function showFeedback(lang)
{
	if (!lang) lang = "no";
	showWindow("/popups/feedback.php?lang=" + lang, 500, 410);
}

function showFacebook(lang) {
	if (!lang) lang = "no";
	showWindow("/popups/facebook.php?videoId=" + getCurrentVideo() + "&lang=" + lang, 500, 135);
}

function showDownload(lang) {
	if (!lang) lang = "no";
	showWindow("/popups/download.php?videoId=" + getCurrentVideo() + "&lang=" + lang, 500, 150);
}

function showDownloadSilverlight() {
	showWindow("/popups/downloadSilverlight.php", 500, 150);
}

function showDownloadFlash() {
	showWindow("/popups/downloadFlash.php", 500, 150);
}

function showActions() {
	if(isEmbedded) {
//		showWindow("/popups/actions.php?videoId=" + getCurrentVideo(), 500, 270, 4, 15);
	} else {
		showWindow("/popups/actions.php?videoId=" + getCurrentVideo(), 500, 270);
	}
}



function executeSearch(q)
{
	carouselParams = "query=" + escape(q);
	loadArticlesToCarousel(SERIA_VARS["HTTP_ROOT"] + "/" + getVideoPath + carouselParams);
}

function videoCompleted() {
	if(isBigSize) {
		toggleScreenSize();
	}
	showActions();
}

window.onload = initializePlayers;

var isInitialized = false; 
var silverlightIsInitialized = "";
var flashIsInitialized = "";

function onSilverlightLoad(e) {
	silverlightIsInitialized = true;
}

function onSilverlightError(e) {
}


function initializePlayers() {
	if(silverlightIsInitialized === "" || flashIsInitialized === "") {
		setTimeout(initializePlayers, 400);
	} else {
		if(flashIsInitialized === true) playerFlash = findSWF("ply");
		if(silverlightIsInitialized === true) playerSilverlight = findSilverlight("plySL");
		isInitialized = true;
	}
}

function getCurrentVideo() {
	if(currentPlayer) return currentPlayer.getCurrentVideo();
	else return false;
}

function replay() {
	if(currentPlayer) {
		currentPlayer.stopVideo();
		currentPlayer.playVideo();
	}
}

function debugMessage(message) {
//	if(Math.random()*100 > 98) alert(message);
//alert(message);
}

function navigateTo(videoId) {
	currentVideoId = videoId;
	document.getElementById("liveFrontImage").style.display = "none";
	if(isInitialized) {
		if(currentPlayer) currentPlayer.stopVideo();
		$.getScript("/js/setCurrentPlayer.php?id=" + videoId, 
			function () {
				if(currentPlayer) currentPlayer.loadVideo(SERIA_VARS["HTTP_ROOT"] + "/api/getVideo.php?id=" + videoId);
			}
		);
	} else {
		setTimeout("navigateTo(" + videoId + ")", 400);
	}
}

var lastCarouselURLFlash = "";
var lastCarouselURLSilverlight = "";
function loadArticlesToCarousel(url) {
	if(isInitialized) {
		if(flashIsInitialized && url != lastCarouselURLFlash) {
			playerFlash.loadArticlesToCarousel(url);
			lastCarouselURLFlash = url;
		}
		if(silverlightIsInitialized && url != lastCarouselURLSilverlight) {
			playerSilverlight.loadArticlesToCarousel(url);
			lastCarouselURLSilverlight = url;
		}
	} else {
		setTimeout("loadArticlesToCarousel('" + url + "')", 100);
	}
}

setTimeout(checkFrontImage, 15000);

function checkFrontImage() {
	if (isLive && isEmbedded && isInitialized && currentVideoId) {
		$.getScript("/js/getFrontImage.php?id=" + currentVideoId, 
			function () {
				if(liveFrontImage) {
					document.getElementById("liveFrontImage").src = liveFrontImage;
					document.getElementById("liveFrontImage").style.display = "block";
				} else {
					document.getElementById("liveFrontImage").style.display = "none";
				}
			}
		);
	}
	setTimeout(checkFrontImage, 15000);
}

