// function declarations

(function($){
    $.searchFuncs = {
        searchFocus: function(){
            if($(this).val()==='Search...'){
                $(this).val('');
            }
        },
        searchBlur: function(){
            if($(this).val()===''){
                $(this).val('Search...');
            }
        },
        checkForm: function(e){
            e.preventDefault();
            var searchTerm = $('input#searchInput').val()
            
            if(searchTerm == '' || searchTerm == 'Search...')
            {
                alert('Please enter a search term into the box.');
            }else{
                window.location.href="http://www.stirlingpramcentre.co.uk/cart.php?m=search_results&search=" + searchTerm;
            }
        }
    };
    
    $.animations = {
        doHeader: function(){
            $('#header-area').hide().fadeIn(1000);
        }
    };
    
    $.columns = {
        maxHeight: 0,
        equalize: function(elements){    
            $.each(elements, function(i, item){
                var height = $(item).height();
                if(height > $.columns.maxHeight)
                {
                    $.columns.maxHeight = height;
                }
            })
            
            $.each(elements, function(i, item){
                $(this).height($.columns.maxHeight);
            })   
        }
    };
    
    $.ie6 = {
        check: function(){
            return ($.browser.msie && $.browser.version.substring(0,1)==6);
        }
    };
    
}(jQuery))

// attach function declarations to their event handlers
$(function(){
    // animate header on homepage only
    if($("body.homepage").length && !$.browser.msie){
        $.animations.doHeader();
    }
    
    // search functions
    $('#searchInput').focus($.searchFuncs.searchFocus);
    $('#searchInput').blur($.searchFuncs.searchBlur);
    $("form#search-form").submit($.searchFuncs.checkForm);
    
    // equalize related product heights
    if($('.relatedItem').length)
    {
        $.columns.equalize($('.relatedItem'));
    }
    
    // ie6 .png fix (plugin checks browser version but I'm checking it here again to prevent the overhead of the plugin being invoked for everyone)
    if($.ie6.check())
    {
        $('body').supersleight({shim: 'images/design/clear.gif'});
    }
});
