jQuery.preloadImage = function(imageToBePreloaded) {
    preloadedImages[lastPreloadedImageIndex] = new Image();
    preloadedImages[lastPreloadedImageIndex].src = imageToBePreloaded;
    lastPreloadedImageIndex++;
};

jQuery.preloadImages = function(imagesToBePreloaded) {
    for(var i = 0; i < imagesToBePreloaded.length; i++) {
        jQuery.preloadImage(imagesToBePreloaded[i]);
    }
};

jQuery.isImagePreloaded = function(img) {
    // During the onload event, IE correctly identifies any images that
    // weren’t downloaded as not complete. Others should too. Gecko-based
    // browsers act like NS4 in that they report this incorrectly.
    if (!img.complete) {
        return false;
    }

    // However, they do have two very useful properties: naturalWidth and
    // naturalHeight. These give the true size of the image. If it failed
    // to load, either of these should be zero.
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    // No other way of checking: assume it’s ok.
    return true;
}

jQuery.dontShowIncompatibleBrowserPopup = function() {
    DONT_SHOW_INCOMPATIBLE_BROWSER_POPUP_PERIOD = 1*24*60*60;
    if(jQuery('#no-more-incompatible-browser-popup').is(':checked') == true) {
        jQuery.createCookie('incompatible_browser_popup', 'false', DONT_SHOW_INCOMPATIBLE_BROWSER_POPUP_PERIOD);
    }
};

jQuery.incompatibleBrowser = function() {
/*@cc_on
   /*@if (@_jscript_version <= 5.6)
    if(jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 7) {
        if(jQuery.readCookie('incompatible_browser_popup') == 'false') {
            return;
        }

        jQuery("div.incompatible-browser-wrapper").animate({"top": "+=1100px", "left": "-=0px"}, 0);
        jQuery("div.incompatible-browser").html('\
            <div class="incompatible-browser-content">\
                Oops! It seems your browser is out of date. Please click on one of the links below to either update your current browser, or to install different web browser.<br /> <br />\
                <img alt="Firefox" src="'+IMAGES_URL+'browsers/firefox.png" /> <span class="link"><a href="http:\/\/www.mozilla.com/firefox/">Mozilla Firefox (2.0+)</a></span><br />\
                <img alt="Google Chrome" src="'+IMAGES_URL+'browsers/chrome.png" /> <span class="link"><a href="http:\/\/www.google.com/chrome/">Google Chrome (1+)</a></span><br />\
                <img alt="Safari" src="'+IMAGES_URL+'browsers/safari.png" /> <span class="link"><a href="http:\/\/www.apple.com/safari/download/">Safari (3+)</a></span><br />\
                <img alt="Opera" src="'+IMAGES_URL+'browsers/opera.png" /> <span class="link"><a href="http:\/\/www.opera.com/">Opera (9+)</a></span><br />\
                <img alt="Internet Explorer" src="'+IMAGES_URL+'browsers/ie.png" /> <span class="link"><a href="http:\/\/www.microsoft.com/windows/internet-explorer/">Microsoft Internet Explorer (7+)</a></span><br /> <br /> <br />\
                <input id="no-more-incompatible-browser-popup" type="checkbox" name="no-more-incompatible-browser-popup" value="true" /> Don\'t show this information anymore<br />\
                <div class="popup-buttons"><input type="button" value=" &nbsp; Close &nbsp; " onclick="jQuery.dontShowIncompatibleBrowserPopup(); jQuery(\'div.incompatible-browser-wrapper\').hide();" /></div>\
            </div>');
    }
   @else @*/
   /*@end
@*/
};

jQuery.createCookie = function(name, value, periodInSeconds) {
    var date = new Date();
    date.setTime(date.getTime()+(periodInSeconds*1000));
    var expires = "; expires="+date.toGMTString();

    document.cookie = name+"="+escape(value)+expires+"; path=/";
};

jQuery.readCookie = function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');

    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while(c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }

        if(c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length,c.length);
        }
    }

    return null;
};

jQuery.getTotalDonationAmount = function() {
    var amount = 0;
    var partialAmount = 0;

    jQuery(".product-options .product-custom-option").each(function(){ 
        partialAmount = Math.max(0, parseInt('0'+jQuery(this).val().substring(0,7), 10));
        amount += partialAmount;

        if(partialAmount == 0) { partialAmount = ''; }

        jQuery(this).val(partialAmount);
    });

    return amount;
}

jQuery.checkTotalDonationAmount = function() {
    if(jQuery.getTotalDonationAmount() < 10) {
        jQuery("#add-to-cart-box-hint").slideDown("fast");
        jQuery(".add-to-cart-box .form-button").attr("disabled", true);
    }
    else {
        jQuery("#add-to-cart-box-hint").slideUp("fast");
        jQuery(".add-to-cart-box .form-button").attr("disabled", false);
    }
}

jQuery.updateDonationProductPrice = function() {
    jQuery.checkTotalDonationAmount();
    jQuery(".product-info-box .price-box .price").html(CURRENCY_SYMBOL+jQuery.getTotalDonationAmount()+'.00');
}

jQuery.setUpDonationProductPage = function() {
    if(donationProductPage != true) {
        return;
    }

    opConfig.reloadPrice = function() {
        jQuery.updateDonationProductPrice();
    }

    jQuery(".qty-box").hide();
    jQuery(".product-options-bottom .price-box").before('<div id="add-to-cart-box-hint" style="color:red; text-align:right; display:none;"><b>Total donation amount must be at least '+CURRENCY_SYMBOL+'10.00</b></div>');
    jQuery(".product-options .product-custom-option").width('120px');
    jQuery(".product-options .product-custom-option").before('<i>Please enter the amount you wish to donate:</i><br/>'+CURRENCY_SYMBOL);
    jQuery(".product-options .product-custom-option").after(' .00');
    jQuery.updateDonationProductPrice();

    jQuery(".product-options .product-custom-option").change(function(){ jQuery.updateDonationProductPrice() });
    jQuery(".product-options .product-custom-option").keyup(function(){ jQuery.updateDonationProductPrice() });
}

