$(document).ready(function() {

    function addMega() {
        $(this).addClass("expanded");
        $("div#topmenu div#right").addClass("expanded");
        $(this).children("div.subnav").addClass("subnav-visible");
    }

    function removeMega() {
        $(this).removeClass("expanded");
        $("div#topmenu div#right").removeClass("expanded");
        $(this).children("div.subnav").removeClass("subnav-visible");
    }

    var megaConfig = {
        interval: 50,
        sensitivity: 1,
        over: addMega,
        timeout: 50,
        out: removeMega
    };

    $("li.subnav").hoverIntent(megaConfig)
    
    $.fn.clearDefault = function() {
        return this.each(function() {
            var default_value = $(this).val();
            $(this).focus(function() {
                if ($(this).val() == default_value) {
                    $(this).addClass("bright");
                    $(this).removeClass("vague");
                    $(this).val("");
                }
            });
            $(this).blur(function() {
                if ($(this).val() == "") {
                    $(this).addClass("vague");
                    $(this).removeClass("bright");
                    $(this).val(default_value);
                }
            });
        });
    };

    $("a.more").each(function() {
        $(this).bind(
            "click",
            function() {
                $("#1_hidden").slideDown("slow");
                $(this).hide();
                return false;
            }
        );
    });

    $("a.less").each(function() {
        $(this).bind(
            "click",
            function() {
                $("#1_hidden").slideUp("fast");
                $("#1_more").show();
                return false;
            }
        );
    });

    LoadAllImages();

    /*
    if ($.browser.msie) {  
    jQuery(function() {     
    $('form button[type=submit]').click(function() {      
    // Handle on the button clicked      
    var obj = $(this);      
    // Disable all buttons (keeps their values from being submitted)      
    $('button').attr('disabled', 'disabled');       
    // Get the text label of the button      
    var label = obj.text();       
    // Set it to an empty string so IE will read the real value attribute      
    obj.text('');      
    // Create a hidden field with the name and value of the button      
    obj.after($("<input/>").attr('type', 'hidden').attr('name', obj.attr('name')).val(obj.attr('value')));       
    // Restore the button's text label      
    obj.text(label);
    // Submit the form
    obj.closest('form').submit();
    });
    });
    }
    */
});


/**
* Form text box hints.
*
* This plug-in will allow you to set a 'hint' on a text box or
* textarea.  The hint will only display when there is no value
* that the user has typed in, or that is default in the form.
*
* You can define the hint value, either as an option passed to
* the plug-in or by altering the default values.  You can also
* set the hint class name in the same way.
*
* Examples of use:
*
*     $('form *').textboxhint();
*
*     $('.date').textboxhint({
*         hint: 'YYYY-MM-DD'
*     });
*
*     $.fn.textboxhint.defaults.hint = 'Enter some text';
*     $('textarea').textboxhint({ classname: 'blurred' });
*
* @copyright Copyright (c) 2009,
*            Andrew Collington, andy@amnuts.com
* @license New BSD License
*/
(function($) {
    $.fn.textboxhint = function(userOptions) {
        var options = $.extend({}, $.fn.textboxhint.defaults, userOptions);
        return $(this).filter(':text,textarea').each(function() {
            if ($(this).val() == '') {
                $(this).focus(function() {
                    if ($(this).attr('typedValue') == '') {
                        $(this).removeClass(options.classname).val('');
                    }
                }).blur(function() {
                    $(this).attr('typedValue', $(this).val());
                    if ($(this).val() == '') {
                        $(this).addClass(options.classname).val(options.hint);
                    }
                }).blur();
            }
        });
    };

    $.fn.textboxhint.defaults = {
        hint: 'Please enter a value',
        classname: 'hint'
    };
})(jQuery);


gLoadSpinnerUrl = '/content/images/ajax-loader.gif';
gFailImage = '/content/images/ajax-loader.gif';


function LoadImage(pSelector, pCallback) {
    var loader = $(pSelector);
    loader.html('<img src="' + gLoadSpinnerUrl + '"/>');
    LoadThisImage($(img), loader, pCallback);
}

function LoadThisImage(loader, pCallback) {
    image_src = loader.attr('src');
    image_alt = loader.attr('alt');
    img = new Image();
    $(img).hide();
    $(img).load(function() {
        cb_js = loader.get(0).getAttribute('onload');
        onload_cb = function() {
            eval(cb_js);
        }

        loader.html(this);
        loader.removeClass('loadable-image');
        loader.removeAttr('src');
        loader.removeAttr('onload');

        $(this).show();

        if (onload_cb) {
            onload_cb($(this));
        }
        if (pCallback) {
            cb = pCallback;
            cb($(this));
        }
    })
  .error(function() { $(this).attr('src', gFailImage).show(); })
  .attr('src', image_src)
  .show();
}

function LoadAllImages() {
    $('.loadable-image').each(function() {
        var loader = $(this);
        loader.html('<img src="' + gLoadSpinnerUrl + '"/>');
        LoadThisImage(loader);
    });
}


$.fn.infiniteCarousel = function() {

    function repeat(str, num) {
        if (isNaN(num))
            num = 0;
        return new Array(num + 1).join(str);
    }

    return this.each(function() {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),

            singleWidth = $single.outerWidth(),
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect

        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);

        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;

            $wrapper.filter(':not(:animated)').animate({
                scrollLeft: '+=' + left
            }, 400, function() {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                }

                currentPage = page;
            });

            return false;
        }



        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function() {
            return gotoPage(currentPage - 1);
        });

        $('a.forward', this).click(function() {
            return gotoPage(currentPage + 1);
        });

        // create a public interface to move to a specific page
        $(this).bind('goto', function(event, page) {
            gotoPage(page);
        });

        // create a public interface to move to a specific page
        $(this).bind('gotoNext', function(event) {
            gotoPage(currentPage + 1);
        });
    });
};
