        $(document).ready(function() {    
        
        $("body").append('<div id="floatwindow" class="corner"></div>');
        
              // vlas. funkcie
        jQuery.fn.extend({
           player: function(url) {
              $(this).flash({
                id: "myytplayer",
                src: url+"&enablejsapi=1&playerapiid=ytplayer",
                height: 290,
                width: 350,
                wmode: "transparent",
                  flashvars: {
                autoplay: 1,allowScriptAccess: "always" }
              });
          },
            playing: function() {
              $("div#yplayer").fadeIn("slow");
              $("div#yplayer div.controls").remove();              
              $("div#yplayer h3").remove();
              $('div.pvid:first').addClass("activev");
              var index = $("div.pvid").index($("div.activev")[0])+1;
           $("div#yplayer").prepend('<h3><a class="close" href="#" title="zatvoriť">[X]</a><a href="#" class="skip">Preskoč tento track</a><span>Video '+index+'/'+$('div.pvid').length+'</span></h3>');
              $("div#yplayer").append('<div class="controls"><div class="sound"><a href="#" class="mutem"><img src="/uploads/images/css/minus.gif" alt="stíšiť" /></a><span class="volume">80</span><a href="#" class="mutep"><img src="/uploads/images/css/plus.gif" alt="zosilniť" /></a></div><div class="duration"><span class="durations"></span><span class="duratione"></span></div><div class="cleaner"></div></div>');
              $("div#yplayer div.object").text("").playertv('http://www.youtube.com/apiplayer?');                            
            },            
            playertv: function(link) {
              $(this).html('<object height="290" width="350" type="application/x-shockwave-flash" id="myytplayer" data="'+link+'&amp;enablejsapi=1&amp;playerapiid=myytplayer"><param name="allowScriptAccess" value="always"/><param value="'+link+'&amp;enablejsapi=1&amp;playerapiid=myytplayer" name="movie" /></object>');                           
            },         
            iplayer: function(title,link) {
              $(this).html('<h3><a title="zatvoriť" href="#" class="close">[X]</a></h3><object width="300" height="80"><param name="movie" value="'+link+'"></param><param name="wmode" value="transparent"></param><embed src="'+link+'" type="application/x-shockwave-flash" width="300" height="110" wmode="transparent"></embed></object>');                           
            }                         
         });
             	
      	 $('#youtubeAll a.playlist-all').click(function(){  
         $('div.videos').removeClass("activev").removeClass("disabled").removeClass("pvid");         
            $('div.videos').addClass("pvid");
            $(this).playing();            
            $("#4videopagination").children("a:eq(0)").click();               
      	   return false;
         });    
         
         $('#youtubeAll a.playlist-selected').click(function(){         
          if($("input.vplay:checked").length < 1) {
            alert("Nie sú vybrané žiadne videa v zozname.");            
            return false;
          }
         
         $('div.videos').removeClass("activev").removeClass("disabled").removeClass("pvid");
         $("input.vplay:checked").parents("div.videos").addClass("pvid");
         
         $(this).playing();
          
           var index1 = $("div.videos").index($("div.activev")[0]);
           var page = parseInt(index1/4);
          $("#4videopagination").children("a:eq("+page+")").click();        
         
         return false;
        });  
         
         $('div.videos a').click(function(){
         
         $(this).attr({target:''});   

         if($("div#yplayer div.object").html().length > 0 && $("div.activev").length > 0 && $("div.pvid").length > 0) {         
         prehravam = confirm("Momentálne ti hrá playlist automaticky, spustením samostatného videa zrušíš súčasný playlist. Chceš pokračovať?");
         
         if(prehravam == false) {
          return false; 
          }else{
          $('div.videos').removeClass("activev").removeClass("disabled").removeClass("pvid");            
          }
         }
         
         
              
            $('div.videos').removeClass("activev").removeClass("disabled").removeClass("pvid");            
            $("div#yplayer h3").remove();  
            $("div#yplayer div.controls").remove();             
            $(this).parents("div.videos:first").addClass("activev");
            $("div#yplayer").fadeIn("slow");                       
            $("div#yplayer").prepend('<h3><span>'+$(this).attr("title")+'</span></h3>');            
            $("div#yplayer H3").prepend('<a class="close" href="#" title="zatvoriť">[X]</a>');            
            $("div#yplayer div.object").html("").player('http://www.youtube.com/v/'+$(this).attr("rel"));                                            
          return false;
         });
         
             
        $("div#yplayer .close").livequery("click",function(){    
          $('div.videos').removeClass("activev").removeClass("disabled").removeClass("pvid");          
          $("div#yplayer h3").remove();
          $("div#yplayer div.controls").remove(); 
          $("div#yplayer div.object").html("");                
          $("div#yplayer").fadeOut("slow");
            return false;
         });   
               
         $("div#yplayer .skip").livequery("click",function(){        
            skip();          
            return false;
         });    
         
          $("div#yplayer .pause").livequery("click",function(){
          pause();
            return false;
         });
         
         $("div#yplayer .play").livequery("click",function(){
         play();
            return false;
         });
         
         $("ul.tracklist li a").livequery("click",function(e){         
          $('#floatwindow').load($(this).attr("href")+" #lyrics",'',function(){            
            var ptop = e.pageY-$(this).height()-10;
            var pleft = (e.pageX-$(this).width()-20);
             $(this).prepend('<h3 id="handle"><a class="close" href="#" title="zatvoriť">[X]</a>Presunúť okno</h3>').show("slow").css({top:ptop,left:pleft}).jqDrag('#handle'); 
                  
          });
                     
            return false;
         });
         
         $("div#floatwindow h3 a.close").livequery("click",function(e){
          $(this).parent("h3").parent("div#floatwindow").hide();          
          return false;
         });
          //$('#floatwindow').Draggable({snapDistance: 10}); 
         
         $("div#yplayer .mutem").livequery("click",function(){
         var volume = parseFloat($("span.volume").text());
          if((volume-10) > 0)
            setVolume(volume-10);
          else
            setVolume(0);          
            return false;
         });
         
         $("div#yplayer .mutep").livequery("click",function(){
          var volume = parseFloat($("span.volume").text());
          if((volume+10) < 100)
            setVolume(volume+10);
          else
            setVolume(100);          
            return false;
         });
         
         
        $("#4videopagination").pagination($("div.videos").length, {					
					items_per_page: 4,
					num_display_entries: 15,
					prev_show_always:false,
		next_show_always:false,
		 callback: pageselectCallback
                });
                
      // set first videos
      for(var i = 0; i < 4; i++) {                
                  $("div.videos:eq("+i+")").show();
       }   
       
      $("#5audiopagination").pagination($("div.audios").length, {					
					items_per_page: 5,
					prev_show_always:false,
		next_show_always:false,
		 callback: pageselectCallback
            });
    // set first audio
      for(var i = 0; i < 5; i++) {                
                  $("div.audios:eq("+i+")").show();
       }   
       
      $("#5imeempaginate").pagination($("div.imeem").length, {					
					items_per_page: 5,
					prev_show_always:false,
		next_show_always:false,
		 callback: pageselectCallback
            });
    // set first audio
      for(var i = 0; i < 5; i++) {                
                  $("div.imeem:eq("+i+")").show();
       }     
       
      
      $("div.imeem a").click(function(){
      var title = $(this).children('span').text();
      var link = $(this).attr("rel");      
      $("div#iplayer").iplayer(title,link);
      $("div.imeem").removeClass("activei");
      $(this).parent("div.imeem").addClass("activei");   
        return false;
      });
      
     $("div#iplayer h3 a.close").livequery("click",function(){
        $("div.imeem").removeClass("activei");    
        $("div#iplayer").html("");        
        return false;
      });
       
      $("#abc a").click(function(){
        if($(this).attr("title") != "all") {
          swap_letter($(this).attr("title"));
        }else{
          swap_letter("");
        }
        return false;
      });

      $('.artist-name').quicksearch({
        position: 'before',
        attached: 'div.artists',
        labelText: 'Hľadaj',  
        loaderImg: '/uploads/images/css/ico_loading.gif',
        inputText: '',
        loaderText: '',
        delay: 1000,  
        focusOnLoad: false
      })
      
      
      $("ul.unit-rating LI A").livequery("click",function(){ 
      $("ul.unit-rating").empty().html('<div class="loading"></div>');    
         $("#container").load($(this).attr("href")+" #ratingblock","",function(){
            
         });       
         return false;
       });    
       
         $(".hv").hover(function() {
            $(this).addClass("hv-hover");
          },
          function() {
            $(this).removeClass("hv-hover");
          }       
       )     
       
       
    })
    // end document ready

  
  function swap_letter(letter) {
  $('.artist-name').each(function(index) {
    var paragraph = this;
    var node = this;
    while (node.childNodes.length) {
      node = node.firstChild;
    }
    var text = node.nodeValue;
    var first_letter = text.substr(0,1);    
   
  var str = first_letter;
  var patt1 = new RegExp(letter);
  var match = patt1.test(str);
    
    if ( match ) {
      //node.nodeValue = text.slice(1);
    /*  $('<img />')
          .attr('src','/images/alphabet/' + first_letter.toLowerCase() + '.gif')
          .attr('alt',first_letter)
          .addClass('fancy-letter')
          .prependTo( paragraph );*/
      paragraph.style.display = "block";
    }else{
      paragraph.style.display = "none";
    }
  });
}      
        
        
       	function pageselectCallback(page_id,event){                 
          	var first,limit;       	
          	var classDiv = event.prev("div").attr("class");
          	number = parseInt(event.attr("id"));
          	class_a = classDiv.split(" ");               
            classDiv = class_a[0]; 	
          	
             if($("div."+classDiv).length > ((page_id*number)+number)) {
              first = ((page_id)*number);
              limit = ((page_id)*number + number);
             }else{
              first = (page_id)*number;
              limit = $("div."+classDiv).length;             
             }              
              $("div."+classDiv).hide();
               for(var i = first; i < limit; i++) {                
                  $("div."+classDiv+":eq("+i+")").show();
               }
                    
          return false;    					
        }
  
  function onYouTubePlayerReady(playerId) {
      
    ytplayer = document.getElementById(playerId);  
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
    ytplayer.addEventListener("onError", "onPlayerError");
    setVolume(80);        
    loadNewVideo($('div.pvid:first a').attr("rel"),0);
    play();
      setInterval(function() {
     
          $("span.durations").text(getCurrentTime());
        
        },
        250   
      );       
    }

  function onytplayerStateChange(newState) {  
      // ak sa zmenil stav na koniec
      if(newState == 3) $("span.duratione").html(' / '+$('div.activev .date strong').text()); 
         
   if(newState == 0) {
             
        $('div.activev').nextAll("div.pvid:first").addClass("activev");
        $('div.activev:first').removeClass("activev").addClass("disabled");
        var index = $("div.pvid").index($("div.activev")[0]);
        //var page = ($("a.selected").text()-1);
        var index1 = $("div.videos").index($("div.activev")[0]);
          var page = parseInt(index1/4);
        $("div#yplayer h3 span").text("Video "+(index+1)+"/"+$("div.pvid").length);        
   
        $("span.duratione").text($('div.activev .date strong').text());
        
        loadNewVideo($("div.activev div.video a.perex-image").attr("rel"),0);        
        
        
        $("#4videopagination").children("a:eq("+page+")").click(); 
        
   }   
}

function onPlayerError() {
  //alert("Nastala neočakávaná chyba. Klipy nemôžu byť prehrané.");
}

     // functions for the api calls
        function loadNewVideo(id, startSeconds) {
          if (ytplayer) {          
            ytplayer.loadVideoById(id, startSeconds);
          }
        }
 function play() { 
          if (ytplayer) {
            ytplayer.playVideo();
          }
        }
        
  function pause() {
          if (ytplayer) {
            ytplayer.pauseVideo();
          }
  }
  
  function skip() {  
          if (ytplayer) {     
        //  ytplayer.stopVideo(); 
        
          loadNewVideo($('div.activev').next("div.pvid:first").children("div.video").children("a").attr("rel"),0);        
          }
  }
  
  function setVolume(newVolume) {
  $("span.volume").text(newVolume);
          if (ytplayer) {
            ytplayer.setVolume(newVolume);
          }
   }
   
   
  function getVolume() {
          if (ytplayer) {
            return ytplayer.getVolume();
          }
  }
  
    function getCurrentTime() {
          if (ytplayer) {
          if(ytplayer.getCurrentTime() < 0) return;          
            var minutes = Math.floor(ytplayer.getCurrentTime()/60);
            var seconds = parseInt(ytplayer.getCurrentTime())-minutes*60;
            return (minutes<10?0+''+minutes:minutes)+':'+(seconds<10?0+''+seconds:seconds);
          }
        }
  function getDuration() {
          if (ytplayer) {
            return ytplayer.getDuration();
          }
        }
