function findSWF(movieName) {
        if(window[movieName]) {
                return window[movieName];
        } else {
                return document[movieName];
        }
//      if (navigator.appName.indexOf("Microsoft")!= -1) {
//        return window[movieName];
//      } else {
//        return document[movieName];
//      }
}

function findSilverlight(movieName) {

        return document.getElementById(movieName).content.js;
}

var flashObj=false;
function playerReady(obj) {
        flashObj = obj;
//      flashId = obj['id'];
//      var version = obj['version'];
//      var client = obj['client'];
}

var currentPlayer=false;
var playerFlash;
var playerSilverlight;

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;
}


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 (true) {
			if(topPos) o.style.marginTop = topPos + "px";
			else o.style.marginTop = Math.floor((flashHeight - height)/2) + "px";

			if(leftPos) o.style.marginLeft = leftPos + "px";
			else o.style.marginLeft = Math.floor((flashWidth - width)/2) + "px";
} else {

			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() {
	var currentId = currentPlayer.getCurrentVideo();
	showWindow("/popups/embed.php?videoId=" + currentId, 500, 180);
}

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

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

function showSearch()
{
	showWindow("/popups/search.php", 500, 135);
}

function showFeedback()
{
	showWindow("/popups/feedback.php", 500, 410);
}

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

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

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

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

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



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

function playerStatus(status) {
	if(status == "play") {
		hidePlayButton();
	}
}

function videoCompleted() {
	showActions();
}
window.onload = initializePlayers;

var isInitialized = false; 
var flashIsInitialized = "";


function initializePlayers() {
	if(flashIsInitialized === "") {
		setTimeout(initializePlayers, 400);
	} else {
		if(flashIsInitialized === false) {
		} else {
			playerFlash = findSWF("ply");
			if(playerFlash === undefined) {
				alert("error code 59283058");
			} else {
				isInitialized = true;
			}
		}
	}
}

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

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

function debugMessage(message) {
//	alert(message);
}

function playVideo() {
	if(isInitialized) {
		currentPlayer.playVideo();
		hidePlayButton();
		if(windowVisible)
			hideWindow();
	}
}

function hidePlayButton() {
	document.getElementById("playButton").style.display = "none";
}

function showPlayButton() {
	document.getElementById("playButton").style.display = "block";
}

function navigateTo(videoId, autoStart) {
	if (document.getElementById("frontPoster")) document.getElementById("frontPoster").style.display = "none";
	
	if(!autoStart) autoStart = false;
	if(windowVisible)
		hideWindow();
	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);
					if(autoStart) {
						playVideo();
					} else {
						showPlayButton();
					}
				}
			}
		);
	} else {
		setTimeout("navigateTo(" + videoId + ")", 400);
	}
}

function stateChanged(state) {

//alert(state);
	var o = document.getElementById('bufferingIcon');
	if((state == "loading" || state == "buffering") && o) {
		o.style.display = "block";
	} else {
		o.style.display = "none";
	}
}

