$(document).ready(function() {

// TITLE BOX

  $("#title-box .dropdown-headings h3").click(
    function() {
      // Set Variables
      var content = $(this).next("div").html();
      var newid   = $(this).next("div").attr('id');
      if ($("#title-box #dropdown").attr('class') == 'hidden') {
        // First click
        $("#title-box #dropdown").html(content);
        $("#title-box #dropdown").fadeIn("fast");
        $("#title-box #dropdown").attr('class', newid);
      } else if (newid == $("#title-box #dropdown").attr('class')) {
        // Clicked same item
        $("#title-box #dropdown").fadeOut("fast", function() {
          $(this).attr('class', 'hidden');
        });
      } else {
        // Clicked another item
        $("#title-box #dropdown").fadeOut("fast", function() {
          $(this).html(content);
          $(this).attr('class', newid);
          $("#title-box #dropdown").fadeIn("fast");
        });
      }
    }
  );

// TAB BOX
  function tabclick (object) {
      // Set Variables
      var content = $(object).next("div").html();
      var newid   = $(object).next("div").attr('id');
      $("#tab-box .headings h3").attr('class', '');
      $(object).attr('class', 'selected');
      if ($("#tab-box #tab-content").attr('class') == 'hidden') {
        // First click
        $("#tab-box #tab-content").html(content);
        $("#tab-box #tab-content").slideDown("fast");
        $("#tab-box #tab-content").attr('class', newid);
      } else if (newid == $("#tav-box #tab-content").attr('class')) {
        // Clicked same item
        $("#tab-box #tab-content").slideUp("fast", function() {
          $(object).attr('class', 'hidden');
        });
      } else {
        // Clicked another item
        $("#tab-box #tab-content").slideUp("fast", function() {
          $(this).html(content);
          $(this).attr('class', newid);
          $("#tab-box #tab-content").slideDown("fast");
        });
      }
  }
  // Change on Tab Click
  $("#tab-box .headings h3").click(
    function() {
      tabclick($(this));
    }
  );
  // Auto-Load First Tab
  //tabclick($("#tab-box .headings h3:first"));

// ITEM LIST POPUPS

  // Hovering over caption
  $(".item .linkage").hover(
    function () {
      var height = $(this).siblings(".caption").height();
      $(this).siblings(".caption").stop().animate({top: ((220 - height) + 3)+"px"}, {queue: false, duration: 300});
    },
    function () {
      $(this).siblings(".caption").stop().animate({top: "179px"}, {queue: false, duration: 300});
    }
  );
  $(".item .caption").hover(
    function () {
      var height = $(this).height();
      $(this).stop().animate({top: ((220 - height) + 3)+"px"}, {queue: false, duration: 300});
    },
    function () {
      $(this).stop().animate({top: "179px"}, {queue: false, duration: 300});
    }
  );
  // Hovering over image
  $(".item .image img").hover(
    function () {
      var height = $(this).parent().parent().siblings(".caption").height();
      $(this).parent().parent().siblings(".caption").stop().animate({top: ((220 - height) + 3)+"px"}, {queue: false, duration: 300});
    },
    function () {
      $(this).parent().parent().siblings(".caption").stop().animate({top: "179px"}, {queue: false, duration: 300});
    }
  );
  // Quantity Selector
  $(".increment").click(
    function () {
      // Set Maximum
      if (parseInt($(this).siblings("input").attr('value')) >= parseInt($(this).siblings("input").attr('alt'))) {
        $(this).siblings("input").attr('value', parseInt($(this).siblings("input").attr('alt')));
      } else {
        $(this).siblings("input").attr('value', parseInt($(this).siblings("input").attr('value')) + 1);
      }
    }
  );
  $(".decrement").click(
    function () {
      // Disallow Zero or Negative
      if (parseInt($(this).siblings("input").attr('value')) <= 1) {
        $(this).siblings("input").attr('value', 0);
      } else {
        $(this).siblings("input").attr('value', parseInt($(this).siblings("input").attr('value')) - 1);
      }
    }
  );
  // Show quickbuy
  $(".item .quickbuy-on").click(
    function () {
      $(this).parent().parent(".buttons").siblings(".quickbuy").show();
      $(this).parent().parent(".buttons").hide();
    }
  );
  // On load, reset all quickbuy quantities to 0
  $("#right-col .item .caption .quickbuy form .quantity input").attr('value', 0);
  $("#right-col .page .purchasing .quantity input").attr('value', 0);

// FLAVOUR LIST POPUPS

  // Hovering over caption
  $(".flavour .caption").hover(
    function () {
      var height = $(this).height();
      $(this).stop().animate({top: ((94 - height))+"px"}, {queue: false, duration: 200});
    },
    function () {
      $(this).stop().animate({top: "101"}, {queue: false, duration: 200});
    }
  );
  // Hovering over image
  $(".flavour .image img").hover(
    function () {
      var height = $(this).parent().siblings(".caption").height();
      $(this).parent().siblings(".caption").stop().animate({top: ((94 - height))+"px"}, {queue: false, duration: 200});
    },
    function () {
      $(this).parent().siblings(".caption").stop().animate({top: "101px"}, {queue: false, duration: 200});
    }
  );

  // Checkout Card Type Selection
  $(".cardtypes label").click(
    function () {
      $(".cardtypes label").removeClass("selected");
      $(this).addClass("selected");
    }
  );

});

// search field ###############################################################################################/
function searchbox() {
  searchbox = document.getElementById('searchfield');
  if (searchbox.value == 'search') {
    searchbox.value = '';
  }
}

function blankcaption (object) {
  if (object.value == 'Enter keyword') {
    object.value = '';
  } else if (object.value == '') {
    object.value = 'Enter keyword';
  }
}