﻿//¤¤¤ ON LOAD EVENTS ¤¤¤

$(document).ready(function () {

    //Load Roll overs effects
    RollHoverEffects();

    //Used in the product template
    VerticalCenteringForIE();

    //Forms - Validation [jquery.validity.pack.js]
    try { if (ReturnLanguage() == "fr") { $.extend($.validity.messages, { require: "Ce champ est requis.", phone: "Ce champ doit être de format ###-###-####", email: "Ce champ doit être un adresse courriel" }); } } catch (err) { }
    $("form").validity(function () {
        $(".textbox.require").require();
        $(".txtemail").require().match("email");
        $(".txttelephone").require();
    });

    //Forms - Add effect when you just sent an form request. 
    $(".form-confirmation").effect("pulsate", { times: 3 }, 1000);


    //FAQ Section
    $(".faq dd").animate({ height: 'toggle' }, 0);
    $(".faq dt").click(function () {
        $(this).next("dd").stop(true, true).animate({ height: 'toggle' });
        $(this).toggleClass('expended');
    });
    $(".faq dt").loadsequence({ delay: 0 });
	
	
    //Products Section
    $(".sidebar #ul").animate({ height: 'toggle' }, 0);
    $(".sidebar #a").click(function () {
        $(this).siblings("ul").stop(true, true).animate({ height: 'toggle' });
    });
    $(".sidebar .opened").children("ul").animate({ height: 'toggle' }, 0);


    //Make some input controls pretty. All those with the class .uniform [jquery.uniform.min.js]
    $(".uniform, .smalluniform").uniform();
    $(".smalluniform").siblings().addClass("spansmalluniform");
    $(".smalluniform").parent().addClass("selectorsmalluniform");

    //Add hint to Textboxes [jquery.hint.js]
    $(":text").hint();

    //Slideshow [jquery.cycle.all.min.js]
    $("#banner").cycle({ fx: 'fade' });


    //¤¤ NOW WEBSITE CUSTOM ¤¤¤¤¤¤¤¤¤¤

    //Just adding bling to the homepage
    $(".featured-box").loadsequence({ delay: 200 });
    $(".featured-box").mouseenter(function () { $(this).css({ 'border': '1px solid #999' }); })
    $(".featured-box").mouseleave(function () { $(this).css({ 'border': '1px solid #ddd' }); })

    //Footer Tabs
    $(".featured").fadeIn(1000);
    $(".btn-footer-tab").mouseenter(function () {
        $(".footer-tab").hide();
        $("." + $(this).attr('showtab')).stop(true, true).fadeIn(1000);
    });

    //Set numeric fields
    $(".numericonly").numeric();

	$("#searchResult *").highlight($("#ctl00_cphMain_txtSearch").val(), "highlight");
});

//Function to highligh the string, enclose it in a span and give it the proper class.
jQuery.fn.highlight = function (str, className) {
    var regex = new RegExp(str, "gi");
    return this.each(function () {
        $(this).contents().filter(function() {
            return this.nodeType == 3 && regex.test(this.nodeValue);
        }).replaceWith(function() {
            return (this.nodeValue || "").replace(regex, function(match) {
                return "<span class=\"" + className + "\">" + match + "</span>";
            });
        });
    });
};

function clearFileInputField() 
{
    $("#ctl00_cphMain_ucForm_fuFile").parent().children(".filename").text("");
}

function RollHoverEffects()
{
    //.simplehover - Rollover effect on links
    $("a.simplehover img, img.simplehover").mouseover(function() {
        $(this).attr("src", $(this).attr("src").replace("-off.", "-on."));
    }).mouseout(function() {
        $(this).attr("src", $(this).attr("src").replace("-on.", "-off."));
    });


    //.fadehover - Rollover effect on links and images with FADE
    $("a.fadehover img, img.fadehover").mouseover(function() {
        $(this).stop(true, true).fadeTo(0, 0.5, function() {
            $(this).attr("src", $(this).attr("src").replace("-off.", "-on.")).fadeTo(500, 1);
        });
    }).mouseout(function() {
        $(this).stop(true, true).fadeTo(50, 0.5, function() {
            $(this).attr("src", $(this).attr("src").replace("-on.", "-off.")).fadeTo(50, 1);
        });
    });


    //Effect on thumbnails images.
    $("a.imagehover img, img.imagehover").mouseover(function() {
        $(this).stop().fadeTo(0, 0.35);
        $(this).stop().fadeTo(500, 1);
    });


}


//¤¤¤ FUNCTIONS & UTILITIES ¤¤¤

function ReturnLanguage() {
    language = $(location).attr('href').substr($(location).attr('href').indexOf('/', 7) + 1, 2);
    return language;
}

function VerticalCenteringForIE() {
    $(".category-image").load(function() {
        $(this).css("top", ($(this).parent().height() - $(this).height()) / 2);
    }).each(function() {
        if (this.complete) $(this).trigger("load"); //Patch for IE cached images
    });
}

//parse URL to get values  - To retrieve example : var i = getUrlVars()["i"];
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
