﻿//center element
jQuery.fn.centerScreen = function(opts) {
    return jQuery(this).each(
    function() {
        if (opts == null || opts.frame == null)
            opts = { frame: jQuery(window) };

        jQuery(this).css("top", (opts.frame.height() / 2) - (jQuery(this).height() / 2)).css("left", (opts.frame.width() / 2) - (jQuery(this).width() / 2));
    });
};
//accordian
jQuery.fn.accordianTrigger = function () {
    return this.each(function () {
        jQuery(this)
            .css("cursor", "pointer")
            .click(function (e) {
                e.preventDefault();
                var hash = jQuery(this).attr("href");
                if (hash == null || hash.length == 0) {
                    jQuery(this).accordianSelect();
                    return;
                }
                hash = hash.replace("#", "");                                
                jQuery("a[name='" + hash + "']").accordianSelect();
            });

    });
};
jQuery.fn.accordianSelect = function () {
    return this.each(function () {
        var accGroup = jQuery(this).hasClass("accGroup") ? this : jQuery(this).parents(".accGroup").first();
        if (jQuery(".accBody:visible", accGroup).length > 0)
            jQuery(".accBody:visible", accGroup).slideUp();
        else {
            jQuery(".accBody:visible").slideUp();
            jQuery(".accClose").hide();
            jQuery(".accBody", accGroup).show("slow",
                function () {
                    if (jQuery(".accClose", accGroup).length == 0)
                        jQuery(accGroup).scrollToElement();
                    else {
                        jQuery(accGroup).scrollToElement(); 
                        jQuery(".accClose", accGroup).show("slow", function () {
                            
                        })
                    }


                });

        }
    });
};

jQuery.fn.accordian = function () {
    return jQuery(this).each(function () {
        jQuery(".accBody", this).hide();
        var hash = document.location.hash;
        hash = hash.replace("#", "");
        if (hash != null && hash.length > 0)
            jQuery("a[name='" + hash + "']").accordianSelect();       
    });

};

//image mouseovers
jQuery.fn.imageMouseOver = function(opts) {
    return jQuery(this).each(function() {
        var defaults = { ext: "_o", useSibling: false };
        var settings = jQuery.extend(defaults, opts);
        var src = jQuery(this).attr("src");
        var mo = src;
        if (settings.useSibling) {

            if (jQuery(this).next("img.ui-mosrc").length > 0) {
                mo = jQuery(this).next("img.ui-mosrc").first().attr("src");
            }
        }
        else {
            var imageTypes = [".gif", ".jpg", ".jpeg", ".png"];
            for (var i = 0; i < imageTypes.length; i++) {
                if (src.indexOf(imageTypes[i]) > 0) {
                    mo = src.replace(imageTypes[i], settings.ext + imageTypes[i]);
                }
            }
        }
        if (mo != src) {
            jQuery.preLoadImages(mo);
            jQuery(this).hover(function() { jQuery(this).attr("src", mo); }, function() { jQuery(this).attr("src", src); });
        }
    });
}
var imageCache = [];
jQuery.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--; ) {
        var cacheImage = document.createElement('img');
        cacheImage.src = arguments[i];
        imageCache.push(cacheImage);
    }
};




jQuery(document).ready(function () {
    setTimeout("scrollWinners()", 5000);
});
function scrollWinners() {
    var index = jQuery("#winners_list div:visible").index();
    index++;
    if (index >= jQuery("#winners_list div").length)
        index = 0;


    jQuery("#winners_list div:visible").hide("slow", function () {
        jQuery("#winners_list div").eq(index).show("slow", function () { setTimeout("scrollWinners()", 5000); });
    });

};

jQuery.fn.scrollToElement = function () {
    return jQuery(this).each(function () {        
        var enabled = true;
        if (!enabled) return;
        var target_top = jQuery(this).offset().top;
        $('html, body').animate({ scrollTop: target_top });
        var i = 1;
    });
};
jQuery(document).ready(function () {
    jQuery(".download").click(function () {
        if (piwikTracker)
            piwikTracker.trackGoal(1);
    });
});
