
jQuery.fn.clearForm = function(){
    return this.each(function(){
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input',this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};

jQuery(function (){

    /* view products by */
    $('#view-by a').click(function(event) {
        var link = $(this) ;
        $('#view-by a').removeClass('selected-view') ;
        link.addClass('selected-view') ;
        $('div.view').hide() ;
        $('div.product-description').hide() ;
        $('#' + link.attr('id') + '-view').fadeIn() ;
        event.preventDefault() ;
    })
   
    /* view a product */
    $('div.view a.product').click(function(event) {
        var link = $(this) ;
        var vis = $('#' + link.attr('id') + '-desc').is(':visible') ;
        $('div.product-description').hide() ;
        if (!vis) $('#' + link.attr('id') + '-desc').fadeIn() ;
        event.preventDefault() ;
    })

    /* contact form */
    $('form').clearForm();
    $('input[type="text"]:first').focus();
    $("#contact-form").validate({
        invalidHandler: function(e, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = errors == 1
                    ? 'Oops! There is a problem. (It has been highlighted)'
                    : 'Oops! There are ' + errors + ' problems.  (They have been highlighted)';
                $("div.error span").html(message);
                $("div.error").show();
            } else {
                    $("div.error").hide();
            }
        },
        rules: {
            name: "required",
            message: "required",
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            name: "(required)",
            message: "(required)",
            email: {
                required: "(required)",
                email: "(invalid)"
            }
        }
    });

    /* welcome slideshow */
    $('#banner-image').cycle({timeout: 3500});
    
});


