var isInit = false;
var playerThumb;
var link;
var file;
var hd_ = true;

function hideVideoThumb()
{
  if(document.getElementById("divplayer_thumb").style.zIndex == "255")
  {
    document.getElementById("divplayer_thumb_loading").style.visibility = "hidden";
    document.getElementById("divplayer_thumb").style.visibility = "hidden";
    document.getElementById("divplayer_thumb").style.zIndex = "-255";
  }
}

function initPlayer(width,height)
{
  var so = new SWFObject('player.swf','player_thumb',width,height,'9','#000');
    so.addParam('allowfullscreen','false');
    so.addParam('allowscriptaccess','always');
    so.addVariable('stretching','uniform');
    so.addVariable('file',file);
    so.addVariable('provider','video');
    so.addVariable('controlbar','none');
    so.addVariable('icons','false');
    so.addVariable('autostart','true');
    so.addVariable('repeat','always');
    so.addVariable('playerready','ready');
  so.write('divplayer_thumb');
  isInit = true;
}

function zviditelniNahled(){
  document.getElementById("divplayer_thumb").style.visibility = "visible";
  document.getElementById("divplayer_thumb").style.zIndex = "255";
}

function zneviditelniNahled(){
  document.getElementById("divplayer_thumb").style.visibility = "hidden";
  document.getElementById("divplayer_thumb").style.zIndex = "-255";
}

function navigateTo(obj)
{
  if(link != "" && !obj.state)
  {
    gotoLink();
  }
}

function gotoLink()
{
  window.location = link;
}

function loadThumb(debug)
{
  if(playerThumb)
  {
    playerThumb.sendEvent("LOAD",file);
    playerThumb.addViewListener("PLAY", "navigateTo");
  }
  else
  {
    setTimeout('loadThumb()',750);
  }
}

function showVideoThumb(el,id,uid,video_link,hd,debug)
{
  hd_ = hd;
  link = video_link;
  if(hd){
    file   = 'http://www.nahotinky.cz/files_hd/1/' + uid + '/pre_video.flv';
  }else{
    file   = 'http://www.nahotinky.cz/files/1/' + uid + '/pre_video.flv';
  }
  
  var divThumb = document.getElementById("divplayer_thumb");
  var divThumbLoading = document.getElementById("divplayer_thumb_loading");
  
  if (divThumb.style.visibility=="" || divThumb.style.visibility=="hidden")
  {
    zneviditelniNahled();
    divThumb.style.visibility = "visible";
  }
  
  var tmp    = findPositionWithScrolling(el);
  var left   = tmp[0];
  var top    = tmp[1];
  var width  = el.offsetWidth;
  var height = el.offsetHeight;
  
  divThumbLoading.style.left = left + "px";
  divThumbLoading.style.top = top + "px";
  divThumbLoading.style.width = width + "px";
  divThumbLoading.style.height = height + "px";   
  divThumbLoading.style.visibility = "visible";

  if(!isInit){
    initPlayer(width,height,file);
    loadThumb(debug);
  }else{
    loadThumb(debug);
  }

  divThumb.style.left = left + "px";
  divThumb.style.top = top + "px";
  divThumb.style.width = width + "px";
  divThumb.style.height = height + "px";    
  
  zneviditelniNahled();
  setTimeout('zviditelniNahled()',2250);
}

function findPositionWithScrolling( oElement ) {
  function getNextAncestor( oElement ) {
    var actualStyle;
    if( window.getComputedStyle ) {
      actualStyle = getComputedStyle(oElement,null).position;
    } else if( oElement.currentStyle ) {
      actualStyle = oElement.currentStyle.position;
    } else {
      //fallback for browsers with low support - only reliable for inline styles
      actualStyle = oElement.style.position;
    }
    if( actualStyle == 'absolute' || actualStyle == 'fixed' ) {
      //the offsetParent of a fixed position element is null so it will stop
      return oElement.offsetParent;
    }
    return oElement.parentNode;
  }
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    var originalElement = oElement;
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    if( !originalElement.parentNode || !originalElement.style || typeof( originalElement.scrollTop ) == 'undefined' ) {
      //older browsers cannot check element scrolling
      return [ posX, posY ];
    }
    oElement = getNextAncestor(originalElement);
    while( oElement && oElement != document.body && oElement != document.documentElement ) {
      posX -= oElement.scrollLeft;
      posY -= oElement.scrollTop;
      oElement = getNextAncestor(oElement);
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}
