$(function () {
  $('.bubbleInfo').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          top: -90,
          left: 40,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
});

$(function() {
    Bind();
    var zoomOpt = {
        zoomWidth:395,
        zoomHeight:397,
        title:"",
        showPreload:true
        };
    $(".productZoom").jqzoom(zoomOpt);
    var related = new Object;
    related.matchCount = $("#related .relItemContainer").length;
    related.recommendCount = $("#upsell .upsellItem").length;
    
    if (related.matchCount > 0 || related.recommendCount > 0) {
        if (related.matchCount == 0) {
            $("#otherItems").tabs({selected: 1});
        }
        else if (related.recommendCount == 0) {
            $("#otherItems").tabs({selected: 0});
        }
        else {
            $("#otherItems").tabs();
        }
    }
    else {
        $("#otherItems").hide();
    }
    $("#basketMsg").colorbox({ opacity:0.5, inline:true, href:"#basketMsg", open:true });
    $(".productQty").keyup(function() {
        var qty = $(this).val();
        qty = qty.replace(/,/gi, "");
        qty = qty.replace(/[^.0-9]/g, "");

        $(this).val(qty);
    });
});
function Bind() {
    $("#the_size").change(function() {
        if ($(this).val() != "") {
            $.ajax({
                type: "get",
                url: "/include/getProductColoursXML.asp",
                data: "i=" + $("#ProductID").val() + "&s=" + $(this).val(),
                dataType: "xml",
                success: function(xml) {
                    $(xml).find("data>colour").each(function() {
                        var colour = $(this);
                        if (colour.attr("available") == "0") {
                            $("#colourItem_" + colour.attr("id")).addClass("unavailable");
                            $("#qty_" + colour.attr("id")).attr("disabled", "disabled");
                            $("#colourItem_" + colour.attr("id") + " span").html(colour.attr("colour") + " (unavailable)");
                        }
                        else if (colour.attr("available") == "-1") {
                            $("#colourItem_" + colour.attr("id")).removeClass("unavailable");
                            $("#qty_" + colour.attr("id")).removeAttr("disabled");
                            $("#colourItem_" + colour.attr("id") + " span").html(colour.attr("colour") + " (temporarily unavailable)");
                        }
                        else {
                            $("#colourItem_" + colour.attr("id")).removeClass("unavailable");
                            $("#qty_" + colour.attr("id")).removeAttr("disabled");
                            $("#colourItem_" + colour.attr("id") + " span").html(colour.attr("colour"));
                        }
                    });
                }
            });
        }
    });
}
function checkRelatedColourSizes(pid, size) {
    if ($(size).val() != "") {
        $.ajax({
            type:"get",
            url: "/include/getProductColoursXML.asp",
            data: "i=" + pid + "&s=" + $(size).val(),
            dataType: "xml",
            success: function(xml) {
                $(xml).find("data>colour").each(function() {
                    var colour = $(this);
                    if (colour.attr("available") == false) {
                        $("#colourItem" + pid + "_" + colour.attr("id")).addClass("unavailable");
                        $("#qty" + pid + "_" + colour.attr("id")).attr("disabled", "disabled");
                    }
                    else {
                        $("#colourItem" + pid + "_" + colour.attr("id")).removeClass("unavailable");
                        $("#qty" + pid + "_" + colour.attr("id")).removeAttr("disabled");
                    }
                });
            }
        });
    }
}
function ValidateForm() {
    var error = "";
    var colorQty = 0;
    if ($("#the_size").val() == "") {
        error += "Please select a size";
    }
    $(".productQty").each(function() {
        if ($(this).val() != "") {
            colorQty += parseInt($(this).val());
        }
    });
    if (colorQty == 0) {
        error += "\r\nPlease select a colour";
    }
    
    if (error.length > 0) {
        alert(error);
        return false;
    }
    else {
        //$("#addToBasket").submit();
        return true;
    }
}
function ValidateRelatedForm(id) {
    var error = "";
    var colorQty = 0;
    if ($("#the_size_" + id).val() == "") {
        error += 'Please select a size';
    }
    
    $("[id*=qty" + id + "]").each(function() {
        if ($(this).val() != "") {
            colorQty += parseInt($(this).val());
        }
    });
    if (colorQty == 0) {
        if (error.length > 0)
            error += ' and colour';
        else
            error += 'Please select a colour';
    }
    
    if (error.length > 0) {
        alert(error);
        return false;
    }
    else {
        //$("#addRelated" + id).submit();
        return true;
    }
}
function gotoShop() {
    $.fn.colorbox.close();
    $('#colorbox').remove();
}
function gotoBasket() {
    window.location.href = "/shoppingcart.asp";
}
