$(document).ready(function(){
  vocabViewComments='<a href="#" onclick="return false">Blog</a>';
  vocabViewText='<a href="#" onclick="return false">View Text</a>';
  vocabProdRemove='Remove from basket';
  $(".dbChannelNum > a").click(function() {
    myParameters=this.href;
    myParameters='/ajax-df-vidlist.php' + myParameters.substring(myParameters.indexOf('?'));
    myTarget = $(this).parent().parent().siblings(".dbVidCtnr");
    newHighlight=$(this).parent();

    if ((newHighlight.prev().html()=='...')||(newHighlight.next().html()=='...')) { return true; };

    $.get(myParameters, function(data){
      myTarget.html(data);
      newHighlight.siblings().removeClass("dbChannelCurrent");
      newHighlight.addClass("dbChannelCurrent");
    });
        
    return false;
  });
/* Video comment functions */
  vcVidValue=window.vcVidValue||'empty';
  if (vcVidValue!=='empty') {
    $("#vidViewChanger").html(vocabViewComments);
    $("#vidViewChanger").click(function() { switchToComments(); });
    vcOriginalVideoText='';
  }
/* End video comment functions */
/* Product functions */
  doAjaxProducts=window.doAjaxProducts||false;
  aPzb=window.aPzb||false;
  if (doAjaxProducts && aPzb) {
    aPzu=window.aPzu||'';
//    $(".pQty").css('border','2px blue solid');
    removeBasicClickFromProductButtons();
    addAjaxToQtyFields();
  }
/* End product functions */
  changeColHeights();
});

function switchToComments(stcMessage) {
  if ($("#vidViewChanger").html()===vocabViewComments) vcOriginalVideoText=$("#mainPageTextIn").html();
  $("#mainPageTextIn").html('<div id="vcCommentContainer">Loading comments...</div>');
  $.ajax({
    type: "GET",
    url: "/ajax-getcomments.php",
    data: "vid="+vcVidValue,
    success: function(data){
      if (typeof(stcMessage)!='undefined') stcMessage='<div class="vcMessage">'+stcMessage+'</div>';
      else stcMessage='';
      $("#vcCommentContainer").html(stcMessage+data);
      $("form#vcAddCommentForm").bind("submit", function(e) {
        postComment($("#vcAddCommentForm").val());
        return false;
      });
    },
    error: function() { alert('Sorry, the comments could not be loaded at this time.'); }
  });
  $("#vidViewChanger").html(vocabViewText);
  $("#vidViewChanger").unbind().click(function() { switchToText(); });
}
function switchToText() {
  if (vcOriginalVideoText=='') { //didn't save the text correctly, so try and get it now
    $.ajax({
      type: "GET",
      url: "/ajax-getVideoText.php",
      data: {
        vid:  vcVidValue
      },
      async: true,
      cache: true,
      success: function(ajaxVideoText) { vcOriginalVideoText=ajaxVideoText; }
    });
  }
  $("#mainPageTextIn").html(vcOriginalVideoText);
  $("#vidViewChanger").html(vocabViewComments);
  $("#vidViewChanger").unbind().click(function() { switchToComments(); });
}
function postComment(pcFormId) {
  $.ajax({
    type: "POST",
    url: "/ajax-postComment.php",
    data: {
      vid:  $("#vcInputVid").val(),
      code: $("#vcInputCode").val(),
      commentText: $("#vcInputText").val()
    },
    cache: false,
    success: function(msg) { switchToComments(msg); },
    error: function() { alert('Sorry, the comment was not posted. Please reload the page and try again.'); }
  });
}

function removeBasicClickFromProductButtons() {
  $(".pBtn").unbind().click(function(){
    if ($(this).val()===vocabProdRemove) {
       $ajaxData="action=add"+
               "&zu="+aPzu+
               "&zb="+aPzb+
               "&pr="+$(this).parent().parent().parent().parent().children('.pId').val()+
               "&qty=0"+
               "&co=123";
      $selfReference=$(this).parent().parent();
      $limit=$(this).parent().parent().parent().parent().children('.pLim').val()
      $selfReference.html('<div class="pBoxes">Removing...</div>');
      $.ajax({
        type: "GET",
        url: "/ajax-basket.php",
        data: $ajaxData,
        success: function(data){
          $("#basketCtnr").html(data);
          $selfReference.load('/ajax-product-inputs.php?lim='+$limit+'&qty=0', null, function() {
            removeBasicClickFromProductButtons();
            addAjaxToQtyFields();
          });
        },
        error: function() { alert('Sorry, there was a problem processing your order.'); }
      });     
    } else {
      alert("Just enter a new value in the box to the left and it will be added to your basket.");
      return false;
    }
  });
}
function addAjaxToQtyFields() {
  $(".pQty").change(function(){
    $ajaxData="action=add"+
             "&zu="+aPzu+
             "&zb="+aPzb+
             "&pr="+$(this).parent().parent().parent().parent().children('.pId').val()+
             "&qty="+$(this).val()+
             "&co=123";
    $selfReference=$(this).parent().parent();
    $selfReference.html('<div class="pBoxes">Updating...</div>');
    $.ajax({
      type: "GET",
      url: "/ajax-basket.php",
      data: $ajaxData,
      success: function(data){
        $("#basketCtnr").html(data);
        $selfReference.html(''+
'<div class="pBoxes"><input type="hidden" name="qty" value="0" />&nbsp;</div>'+
'<div class="pBoxes"><input class="pBtn" type="submit" value="'+vocabProdRemove+'" /></div>');
        removeBasicClickFromProductButtons();
      },
      error: function() { alert('Sorry, there was a problem processing your order.'); }
    });
  });
}
function changeColHeights() {
  leftHeight=$("#menuCtnr").height();
  if (leftHeight>(524+156)) {
    $("#detailBody").height(leftHeight-156); // sponsors box is 156 tall
    $(".mainPageText").height(leftHeight-($("#mainvideo").height())-($("#sidemenuOut").height()));
    $("#s2Ctnr").height(leftHeight+90); // header is 90 tall
  }
}