var videoTransition;

jQuery(function() {
  if(jQuery("#breadcrumb a[href=http://www.locotv.com.au/tv/channels/premium-2/]").length > 0) {
    jQuery("#featured-large").css("paddingLeft", "96px");
    jQuery("#control-panel").css("marginLeft", "26px");
  }

  // report a fault page
  if(jQuery(".url input").length > 0) {
    // fetch previous page past as query string
    var hu = window.location.search.substring(1);
    var ar = hu.split("=");
    // insert into textfield
    jQuery(".url input").attr("value", "http://" + document.domain + ar[1]); //.attr("disabled", "disabled");
  }
  
  jQuery("#content .video_previews ul li > a img").hover(
    function() {
      var a = jQuery(this).parent().parent().find(".hidden");
      jQuery("#content .video_previews ul li .hidden").not(a).fadeOut();
      a.stop(true, true).fadeIn();
    },
    function() {
      // do nothing
    });
    
    //jQuery(this).find(".hidden").stop().fadeOut();
    jQuery("#content .video_previews ul li .hidden").hover(
      function() {
        // do nothing
      },
      function() {
        jQuery(this).fadeOut();  
    });
    
  //jQuery(".video-link").hover(
  //function() {
  jQuery("#featured-large .play-button").stop(true, true).fadeTo('fast', 0.5, function() {});
  //},
  //function() {
  //  jQuery("#featured-large .play-button").stop().fadeTo('fast', 0, function() {});
  //});
  
  jQuery("#featured-large .play-button").hover(function(){
    jQuery("#featured-large .play-button").stop(true, true).fadeTo('fast', 1, function() {});
  }, function() {
    jQuery("#featured-large .play-button").stop(true, true).fadeTo('fast', 0.5, function() {});
  });
  
  jQuery("#featured-large img, #featured-large .play-button").not("#control-panel img").click(function() {
    // document.location = jQuery(this).parent().find("a").attr("href");
    var n = jQuery(".video-image img:visible").index();
    var href= jQuery("#featured-small li." + n).find("a").attr("href");
    
    document.location = href;
  });
  
  jQuery("#featured-small li a").click(function() {
    var currentClass = parseInt(jQuery(this).parent().attr("class"));
    //console.log(currentClass);
    goToBig(currentClass);
    
    return false;
  });
  
  jQuery(".hidden").click(function() {
    document.location = jQuery(this).parent().find("a").attr("href");
  });
  
  // hide the darkness
  jQuery(".lights-out").click(function() {
    jQuery("#darkness").fadeIn();
    jQuery(".lights-out span").html("Lights On");
    return false;
  });
  
  jQuery("#dark-login .logout").click(function() {
    jQuery("#dark-login").fadeOut();
    jQuery("#darkness").fadeOut();
    return false;
  });
  
  jQuery("#darkness:not(#dark-login), #darkness:not(#dark-register)").click(function() {
    jQuery("#dark-login").hide();
    jQuery("#dark-register").hide();
    jQuery(this).fadeOut();
    jQuery(".lights-out span").html("Lights Out");
    return false;
  });
  
  jQuery(".report-fault").click(function() {
  
  });
  
  // featured movies scroller
  if(jQuery("#featured-small").length > 0) {
    videoTransition = setTimeout(function() {
      // moveSmall();
      fadeBig();
    }, 6000);
  }
  
  jQuery(".login").click(function() {
    //jQuery("#dark-login").show();
    //jQuery("#darkness").fadeIn();
    
    return false;
  });

  // Register
  jQuery("#loco-register").click(function() {
    jQuery("#dark-register").show();
    jQuery("#darkness").fadeIn();
    
    return false;
  });
  
  jQuery(".down").click(function() {
    // stop current transitions
    window.clearTimeout(videoTransition);
    // move down one
    // fadeBig();
    moveSmall();
    
    return false;
  });
  
  jQuery(".synopsis").click(function() {
    if(jQuery("#synopsis:visible").length > 0) {
      jQuery("#synopsis").fadeOut();
    }
    else {
      jQuery("#synopsis").fadeIn();
    }
    
    return false;
  });
  
  // category descriptions
  jQuery("#category-list li").hover(function() {
    jQuery(this).find(".cat_description").stop(true, true).fadeIn('fast');
    jQuery(this).find(".glow").fadeIn();
  }, function() {
    jQuery(this).find(".cat_description").stop(true, true).fadeOut();
    jQuery(this).find(".glow").fadeOut();
  });
  
  jQuery(".tell-us a").click(function() {
    changeVote(this);
    
    return false;
  });
  
  if(jQuery(".ym_reg_form_pack_radio").length > 0) {
    jQuery(".ym_reg_form_pack_radio input:visible").first().attr("checked", "checked");
  }
});

// change mind on voting
function changeVote(m) {
  var val = 0;
  //var m = this;
  if(jQuery(m).hasClass('tell-great')) {
    val = 1;
  }
  
  // existing value
  if(jQuery(".clicked").length > 0) {
    if(jQuery(m).hasClass("clicked")) {
      // remove vote
      jQuery.ajax({
        type:'POST',
        data:{
          action:'change_like_dislike',
          id: jQuery('[name=post_id]').attr("value"),
          value: val,
          remove: 1
        },
        url: "http://" + document.domain + "/wp-admin/admin-ajax.php",
        success: function(value) {
          // mark as clicked
          jQuery(".clicked").removeClass('clicked');
        }
      });
    }
    else {
      jQuery.ajax({
        type:'POST',
        data:{
          action:'change_like_dislike',
          id: jQuery('[name=post_id]').attr("value"),
          value: val,
          remove: 0
        },
        url: "http://" + document.domain + "/wp-admin/admin-ajax.php",
        success: function(value) {
          // mark as clicked
          jQuery(".clicked").removeClass('clicked');
          jQuery(m).addClass('clicked');
        }
      });
    }
  }
  else { // new value
    jQuery.ajax({
      type:'POST',
      data:{
        action:'like_dislike',
        id: jQuery('[name=post_id]').attr("value"),
        value: val
      },
      url: "http://" + document.domain + "/wp-admin/admin-ajax.php",
      success: function(value) {
        // mark as clicked
        jQuery(m).addClass('clicked');          
      }
    });
  }
  
  return false;
}

function moveSmall() {
  jQuery("#featured-small").animate({marginTop: '-=87'}, 300, function() {
    jQuery("#featured-small li:first").appendTo("#featured-small");
    jQuery("#featured-small").css("marginTop", 0);
  });
}

function fadeBig() {
  moveSmall();

  var i = jQuery(".video-image img:visible");
  var j = i.next();
  if(i.next().length == 0) {
    j = jQuery(".video-image img:first");
  }
  
  var ei = jQuery(".video-excerpt .e:visible");
  var ej = ei.next();
  if(ei.next().length == 0) {
    ej = jQuery(".video-excerpt .e:first");
  }
  
  i.fadeOut('300');
  ei.fadeOut('300');
  j.fadeIn('300');
  ej.fadeIn('300');
  
  videoTransition = setTimeout(fadeBig, 6000);
}

function goToBig(currentClass) {
  // stop scroll
  window.clearTimeout(videoTransition);
  
  // load item
  jQuery(".video-image img:visible").fadeOut();
  jQuery(".video-excerpt .e:visible").fadeOut();
  
  jQuery(".video-image img").eq(currentClass).fadeIn();
  jQuery(".video-excerpt .e").eq(currentClass).fadeIn();
}
