$(function()
{
    $("img[rollover]").each(function(event)
    {
        var original = $(this).attr('src');
        var rollover = $(this).attr('rollover');
        
        var preload = new Image();
        preload.src = rollover;
        
        $(this).mouseenter(function(event)
        {
            $(this).attr('src', rollover);
        });
        
        $(this).mouseleave(function(event)
        {
            $(this).attr('src', original);
        });
    });
    
    $(".toggleblock .trigger").click(function(event)
    {
        event.preventDefault();
        $(this).parents(".toggleblock:first").find(".hidden").slideToggle();
        $(this).parents(".toggleblock:first").find(".shown").slideToggle();
    });
    
    $(".formsubmit").click(function(event)
    {
        event.preventDefault();
        
        var form = $(this).attr('submit');
        
        if (form != null)
        {
            $("form[name="+form+"]").submit();
        }
        else
        {
            $(this).parents("form:first").submit();
        }
    });
    
    $("form:has(input[prompt])").each(function(event)
    {
        $(this).find("input[prompt]").each(function(event)
        {
            if ($(this).attr('value') == '')
            {
                $(this).attr('value', $(this).attr('prompt'));
            }
            
            $(this).focus(function(event)
            {
                if ($(this).attr('value') == $(this).attr('prompt'))
                {
                    $(this).attr('value', '');
                }
            });
        });
        
        $(this).submit(function(event)
        {
            $(this).find("input[prompt]").each(function(event)
            {
                if ($(this).attr('value') == $(this).attr('prompt'))
                {
                    $(this).attr('value', '');
                }
            });
            
            return true;
        });
    });
    
    $("a.coupon").click(function(event)
    {
        event.preventDefault();
        
        var width = 740;
        var height = 560;
        var top = (screen.width - width) / 2;
        var left = (screen.height - height) / 2;
        
        popup = window.open(this.href, 'coupons', 'toolbar=yes,location=yes,directories=no,menubar=yes,scrollbars=yes,width='+width+',height='+height+',resizable=no,top='+top+',left='+left);    
        popup.focus();
    });
    
    if ($(".datepicker").size() > 0)
    {
        $(".datepicker").datepick({ dateFormat: 'yy-mm-dd', showAnim: 'fadeIn' });
    }
});

function initDatePickerRange(first, second, min, max)
{
    $(function()
    {
        if (min != null) { $(first).datepick('option', 'minDate', min); }
        if (max != null) { $(second).datepick('option', 'maxDate', max); }
        
        $(first).datepick('option', 'maxDate', $(second).val());
        $(second).datepick('option', 'minDate', $(first).val());
        
        $(first).datepick('option', 'onSelect', function(string) { $(second).datepick('option', 'minDate', $(first).val()); });
        $(second).datepick('option', 'onSelect', function(string) { $(first).datepick('option', 'maxDate', $(second).val()); });
    });
}
