$(document).ready(function() {

  /*
   * Hover help
   */
  $(".help .items .item").hover(
    function() {
      $(this).addClass("highlight");  
    }, 
    function() {
      $(this).removeClass("highlight");
  });
  
  /*
   * Help lightbox
   */
  $(".help .items .item").click(function() {
    $.messageLightbox({
      text: $(this).find(".self .hidden").html(),
      header: $(this).find(".self .title").text(),
      number: $(this).find(".number").text()
    });
    return false;
  });
  $(".help .list a").click(function() {
    $.messageLightbox({
      text: $(this).parent("li").find(".hidden").html(),
      header: $(this).text()
    });
    return false;
  });
  
  /*
   * Hover video
   */
  $("#contentBlock .videosList .content .item").hover(
    function() {
      $(this).addClass("highlight");  
    }, 
    function() {
      $(this).removeClass("highlight");
  });
  
  /*
   * Email obfuscate
   */
  $('a.js-email').each(function() {
    $(this).attr('href', $(this).attr('href').replace('(zavinac)', '@'));
    $(this).text($(this).text().replace('(zavinac)', '@'));
  });
  $('span.js-email').each(function() {
    $(this).text($(this).text().replace('(zavinac)', '@'));
  });
  
  /*
   * Delete video
   */
  $(".myVideoList a.delete").click(function () {
    var conf = confirm("Opravdu chcete smazat toto videoalbum?");
    if(conf) {
      var item = $(this).parents(".item");
      var url = $(this).attr("href");
      $.ajax({
        type: 'GET',
        url: url,
        error: function(msg) {
          alert(msg);
        },
        success: function(results) {
          if(results == '1') {
            $(item).fadeOut("normal", function() { 
              $(this).remove();
              $(".myVideoList .item").removeClass("even").each(function(i) {
                if((i+1)%2==0) $(this).addClass("even");
              }); 
            });
  			  } else alert('Chyba při odstraňování videoalba. Zkuste akci opakovat později.');
        }
      });
    }
    return false;
	});
	
	/*
	 * Set video as private/ public
	 */
  $(".myVideoList input:checkbox").click(function () {
    var item = $(this).parents(".item");
    if($(this).attr('checked')) {
      var url = $(item).find("input[name='setPrivate']").val();
      $.ajax({
        type: 'GET',
        url: url,
        //data: 'album_id=' + $(this).parents(".item").find("input[name='album_id']").val(),
        error: function(msg) {
          alert(msg);
        },
        success: function(results) {
          if(results == '1') alert("Videoalbum bylo nastaveno jako privátní.")
  			  else {
  			    $(item).find("input:checkbox").attr("checked", "");
            alert('Videoalbum nebylo nalezeno. Zkuste akci opakovat později.');
          }
        }
      });
    } else if(!$(this).attr('checked')) {
      var url = $(item).find("input[name='setPublic']").val();
      $.ajax({
        type: 'GET',
        url: url,
        error: function(msg) {
          alert(msg);
        },
        success: function(results) {
          if(results == '1') alert("Videoalbum bylo nastaveno jako veřejné.")
  			  else {
  			    $(item).find("input:checkbox").attr("checked", "checked");
            alert('Videoalbum nebylo nalezeno. Zkuste akci opakovat později.');
          }
        }
      });
    }
	});
	
	/*
	 * Export video to dvd
	 */
  $(".myVideoList .export").click(function () {
    var url = $(this).attr("href");
    var button = $(this);
    $.ajax({
      type: 'GET',
      url: url,
      error: function(msg) {
        alert(msg);
      },
      success: function(results) {
        if(results == '0' || results == '') alert('Videoalbum nebylo nalezeno. Zkuste akci opakovat později.');
        else {
          $(button).hide();
          //alert("Vaše video je právě exportováno do DVD kvality. Na e-mail, uvedený ve vaší registraci, vám bude po jeho dokončení zaslána informace s linkem pro stažení hotového videa.");
          $.messageLightbox({
            text: "Vaše video je právě exportováno do DVD kvality. Na e-mail, uvedený ve vaší registraci, vám bude po jeho dokončení zaslána informace s linkem pro stažení hotového videa.",
            header: "Export videa do DVD"
          });
        }
      }
    });
    return false;
	});
	
	/*
	 * Select all text from link input
	 */
  $("#videoLink").click(function() {
    $(this).select();
  });
  
	/*
	 * JS click event link
	 */
  $(".bestVideosList .item").click(function() {
    document.location = $(this).find(".img a").attr("href");
    return false;
  });
  
  /*
   * Lightbox + video
   */
  $(".lightBoxTv").click(function() {
    $.videoLightbox({
      video: "/files/flv/tv_spot_09.flv",
      header: "Televizní spot"
    });
    return false;
  });   
  
});

