﻿  $(document).ready(function(){
    var player = isFlashAvailable();
    $("div[class^=YOUTUBE_VIDEO_]").each(function(){
      var vData = this.className.replace("YOUTUBE_VIDEO_", "").split("-");
      var vYTid = vData[0].replace("YTMINUS","-");
      var vWidth = 560;
      var vHeight = 340;
      var vQuality = vData[3];
      if (!isNaN(vData[1])){
        vWidth = vData[1];
      }
      if (!isNaN(vData[2])){
        vHeight = vData[2];
      }
      if (player){
        createYTPlayer(vYTid, vWidth, vHeight, vQuality);
      }else{
        $(".YOUTUBE_BOOKMARKS").hide();
        createYTLink(vYTid, vWidth, vHeight, vQuality);
      }
    });
    $(".YOUTUBE_RESIZER > a").bind("click", function(){
      this.blur();
    })
  });

  function createYTPlayer(ytId, width, height, quality){
    vpList[ytId] = new videoProxy(ytId, width, height, quality);
  }

  function createYTLink(ytId, width, height, quality){
/*
    var vid = "#YOUTUBE_VIDEO_"+ytId;
    var vDiv = $(vid);
    if (vDiv == null){
      return;
    }
    vDiv.removeAttr("style");
    var oLink = document.createElement("A");
    oLink.setAttribute("href", "http://www.youtube.com/watch?v=" + ytId);
    oLink.setAttribute("target", "_blank");
    var oImg = $("#YOUTUBE_VIDEO_"+ytId+" > img");
    oImg.wrap(oLink);
*/
  }

  function isFlashAvailable(){
    var browser = navigator.appName;
    if (browser != null && (browser.match("iPod") || browser.match("iPhone") /*|| browser.match("Netscape")*/)){
      return false;
    }
    return true;
  }


  var vpList = new Array();
  function onYouTubePlayerReady(playerid){
    var player = document.getElementById("YOUTUBE_VIDEO_" + playerid);
    player.addEventListener("onStateChange", "(function(state) { return vpList['" + playerid + "'].onStateChange(state);})");
  }


  function videoProxy(videoid, width, height, quality){
    this.VideoID = videoid;
    this.Width = parseInt(width);
    this.Height = parseInt(height);
    this.Quality = quality;
    this.ToolbarHeight = 25;
    // unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5)
    this.VideoState = -1
    this.SeekRequest = -1;
    
    var vUrl = "http://www.youtube.com/v/{ytId}?enablejsapi=1&version=3&fs=1&playerapiid=";
    var elementId = "YOUTUBE_VIDEO_"+videoid;
    vUrl = vUrl.replace("{ytId}", videoid);
    vUrl = vUrl + videoid;

    swfobject.embedSWF(vUrl, elementId, this.Width, (this.Height + this.ToolbarHeight), "8", null, null, {allowScriptAccess: "always",allowfullscreen: "true" }, null, function(){$("#"+elementId).parent().addClass("YouTubeVideo-Clip").parent().find(".YOUTUBE_RESIZER").show();}); //
    
    this.onStateChange = function (newState){
      if (this.VideoState == -1 && this.Quality != "default"){
        this.setQuality(this.Quality);
      }
      this.VideoState = newState;
      if ((this.VideoState == 0 || this.VideoState == 1 || this.VideoState == 2 || this.VideoState == 3) && this.SeekRequest > -1){
        youtubeGoto(this.SeekRequest, this.VideoID);
        this.SeekRequest = -1;
      }

    }

    this.setQuality = function(suggestedQuality){
      if (suggestedQuality == ""){
        //this.getPlayer().setPlaybackQuality(this.Quality);
        return;
      }
      if (suggestedQuality == "hd720"){
        this.setPlayerSize(898, 505);
      }else if (suggestedQuality == "hd1080"){
        this.setPlayerSize(1080, this.setPlayerHeight('widescreen', 1080));
      }else{
        this.setPlayerSize(this.Width, this.Height);
      }
/*
      if (this.getPlayer().getPlaybackQuality() != suggestedQuality){
        this.getPlayer().setPlaybackQuality(suggestedQuality);
      }
*/
    }

    this.getPlayer = function(){
      return document.getElementById(elementId);
    }

    this.setPlayerSize = function(playerWidth, playerHeight) {
      $("#page-column-right").css({overflow: "visible",zIndex:200});
      $("#page-column-left").css({overflow: "visible",zIndex:300});
      //this.getPlayer().setSize(playerWidth, playerHeight);
      //var oDims = new Array(this.Width, (this.Height + this.ToolbarHeight));
      var oEl	= $("#"+elementId);
      var oPar  = oEl.parent();
      if (document.getElementById(elementId).width != playerWidth){
        document.getElementById(elementId).width = playerWidth;
        document.getElementById(elementId).height = (playerHeight + this.ToolbarHeight);
	oPar.attr("style", "width:" + playerWidth + "px;height:" + (playerHeight + this.ToolbarHeight) + "px;");
        oPar.parent().find(".YOUTUBE_RESIZER > a").html("Zmenšit video").addClass("YOUTUBE_HD_ON");
      }else{
        document.getElementById(elementId).width = this.Width;
        document.getElementById(elementId).height = (this.Height + this.ToolbarHeight);
	oPar.attr("style", "width:" + this.Width +"px;height:" + (this.Height + this.ToolbarHeight)+"px;");
        oPar.parent().find(".YOUTUBE_RESIZER > a").html("Zvětšit video").removeClass("YOUTUBE_HD_ON");
      }
    }

    this.setPlayerHeight = function(aspectRatio, playerWidth) {
      if (aspectRatio == 'widescreen') {
        return ((playerWidth * 9) / 16);
      } else if (aspectRatio == 'standard') {
        return ((playerWidth * 3) / 4);
      }
    }

  }

  function youtubeControl(command, videoid, quality){
    if (vpList == null || vpList[videoid] == null){
      return;
    }
    var player = vpList[videoid].getPlayer();
    if (player == null){
      return;
    }
    if (command == "PLAY"){
      player.playVideo();
    }else if (command == "STOP"){
      player.stopVideo();
    }else if (command == "PAUSE"){
      player.pauseVideo();
    }else if (command == "QUALITY"){
      vpList[videoid].setQuality(quality);
    }
  }

  function youtubeGoto(seconds, videoid){
    if (vpList == null || vpList[videoid] == null){
      return;
    }
    var player = vpList[videoid].getPlayer();
    if (player == null){
      return;
    }
    if (vpList[videoid].VideoState == -1 || vpList[videoid].VideoState == 5){
      vpList[videoid].SeekRequest = seconds;
      player.playVideo();
    }
    if((seconds>-1) && (seconds < player.getDuration())){
      player.seekTo(seconds,true);
    }

  }
