﻿(function($) {
    $.preloadCssImages = function() {
        var allImgs = []; //new array for all the image urls 
        var k = 0; //iterator for adding images
        var sheets = document.styleSheets; //array of stylesheets

        for (var i = 0; i < sheets.length; i++) {//loop through each stylesheet
            var cssPile = ''; //create large string of all css rules in sheet
            var csshref = (sheets[i].href) ? sheets[i].href : 'window.location.href';
            var baseURLarr = csshref.split('/'); //split href at / to make array
            baseURLarr.pop(); //remove file path from baseURL array
            var baseURL = baseURLarr.join('/'); //create base url for the images in this sheet (css file's dir)
            if (baseURL != "") baseURL += '/'; //tack on a / if needed
            if (document.styleSheets[i].cssRules) {//w3
                var thisSheetRules = document.styleSheets[i].cssRules; //w3
                for (var j = 0; j < thisSheetRules.length; j++) {
                    cssPile += thisSheetRules[j].cssText;
                }
            }
            else {
                cssPile += document.styleSheets[i].cssText;
            }

            //parse cssPile for image urls and load them into the DOM
            var imgUrls = cssPile.match(/[^\(]+\.(gif|jpg|jpeg|png)/g); //reg ex to get a string of between a "(" and a ".filename"
            if (imgUrls != null && imgUrls.length > 0 && imgUrls != '') {//loop array
                var arr = jQuery.makeArray(imgUrls); //create array from regex obj       
                jQuery(arr).each(function() {
                    allImgs[k] = new Image(); //new img obj
                    allImgs[k].src = (this[0] == '/' || this.match('http://')) ? this : baseURL + this;     //set src either absolute or rel to css dir
                    k++;
                });
            }
        }
        return allImgs;
    };
})(jQuery);


(function($) {
    $.fn.extend({
        checked: function(b) {
            if (b) return this.attr("checked", "checked");
            else return this.attr("checked", false);
        },
        validated: function(callback) {
            if (callback) $(document).bind("_validated", callback);
            else this.trigger("_validated");
            return this;
        }
    });
})(jQuery);

$(document).ready(function() {

    // preload CSS images
    $.preloadCssImages();

    // validated event
    //    var validate = Page_ClientValidate;
    //    Page_ClientValidate = function() {
    //        var v = validate();
    //        if (v) {
    //            $(document).validated();
    //            return true;
    //        } else return false;
    //    };

    // radio/checkbox label click
    $("input[type='radio'] + label, input[type='radio'] + span, input[type='checkbox'] + label").click(function() {
        $(this).prev("input[type='radio'], input[type='checkbox']").trigger("click", this);
    });

    // IE6 CSS hack
    if ($.browser.msie) {
        if ($.browser.version == "6.0") {
            $("input").each(function() {
                var x = $(this);
                x.addClass(x.attr("type"));
            });
        }
    }

    // image hover preload
    $("img[hover]").each(function() {
        var img = $(this);

        if (img.attr("onmouseover")) return;

        var src = img.attr("src");
        img.data("_src", src);

        var hover = img.attr("hover")
        var overImgSrc = null;

        if (hover == "true") {
            overImgSrc = src.replace(/([^\/]+)\.(gif|jpg)/i, "$1_over.$2");
        }

        if (overImgSrc) {
            var overImg = new Image();
            overImg.src = overImgSrc;
            overImg.onload = function() {
                img.data("_overSrc", overImgSrc);
            }
        }

        img.mouseover(function() {
            var img = $(this);
            var overSrc = img.data("_overSrc");
            if (overSrc)
                img.attr("src", overSrc);
        });
        img.mouseout(function() {
            var img = $(this);
            img.attr("src", img.data("_src"));
        });
    });

});