$(function() {

    /**
     * Remove default value of inputs on focus
     */
    $('#newsletter-signup-form input[type=text], #newsletter-signup-form input[type=password], #newsletter-signup-form textarea').focus(function() {
        var defaultVal = $.data(this, 'defaultValue');
        
        if ( typeof(defaultVal) == 'undefined' ) {
             $.data(this, 'defaultValue', $(this).val());
             $(this).val('');
        } else if ( defaultVal == $(this).val() ) {
            $(this).val('');
        }
    }).blur(function() {
        var defaultVal = $.data(this, 'defaultValue');
        if ($(this).val() == '') {
            $(this).val(defaultVal);
        }
    });
       
    
    /**
     * Style form elements
     */
    $('select, input:checkbox, input:radio, input:file').uniform();
    
    
    /**
     * Footer map colorbox
     */
    $('.googleMaps a').colorbox({
        width: 640,
        height: 480,
        iframe: true
    });
    
    
    /**
     * Normal link colorbox
     */
    $('.colorbox').colorbox({
        maxWidth: '90%',
        maxHeight: '80%'
    });
    
    
    /**
     * Footer logos hover effect
     */
    $('footer .levelTwoLogos a').hover(function() {
        $(this).css('opacity', 0.7);
    }, function() {
        $(this).css('opacity', 1);
    });
    
    
    /**
     * Product pagination
     */
    $('.productPager a').click(function() {
        var idx = $(this).index();
        var myhref = $(this).attr('href');
        
        if ($(this).hasClass('active')) { return false; }
        
        $('.productPager').find('a').removeClass('active');
        $('.productPager a[href=' + myhref + ']').addClass('active');
        
        $('.products').find('ul').removeClass('visible');
        $('.products').find('ul:eq(' + idx + ')').addClass('visible');
        
        return false;
    });
    
    
    /**
     * Override default search form action
     */
    $('select[name="manufacturer"]').change(function() {
        var url_parts = window.location.href.split('/');
        var text = $(this).find(':selected').text().toLowerCase();
        $(this).parents('form').attr('action', 'http://' + url_parts[2] + '/manufacturers/' + $(this).val());
        $(this).parents('form').submit();
    });
    
    $('form.search select[name!=manufacturer]').change(function() {
        $(this).parents('form').submit();
    });
    
    
    /**
     * Testimonials cycle
     */
    $('#sidebar-quotes').cycle({
        fx: 'none',
        timeout: 4000,
        slideExpr: 'blockquote'
    });
    
    
    /**
     * Product Detail Colorbox
     */
    $('.thumbs a, #specs-photos a').colorbox({
        slideshow: true,
        slideshowSpeed: 6000,
        maxWidth: '90%',
        maxHeight: '90%'
    });
    
    
    /**
     * Form Validation
     */
    $('aside .contactForm').validate({
        rules: {
            contact_name: {
                required: true
            },
            contact_email: {
                required: true,
                email: true
            },
            contact_phone: {
                required: true,
                digits: true,
                rangelength: [10,10]
            },
            form_comments: {
                required: true
            }
        }
    });
    
    
    /**
     * Infield Labels
     */
    $('aside label').inFieldLabels();
    
    
    /**
     * Media Gallery lightboxes
     */
    $('.image-lightbox').click(function() {
        var target = $(this).attr('data-target');
        $('a[rel="' + target + '"]:eq(0)').trigger('click');
        return false;
    });
    
    $('.image-slides a').colorbox({
        slideshow: true,
        slideshowAuto: false,
        innerWidth: 640,
        innerHeight: 480,
        initialWidth: 80,
        initialHeight: 80
    });
    
    $('.video-lightbox').click(function() {
        var target = $(this).attr('data-target');
        $('a[rel="' + target + '"]:eq(0)').trigger('click');
        return false;
    });
    
    $('.video-slides a').colorbox({
        innnerWidth: 640,
        innerHeight: 480,
        initialWidth: 80,
        initialHeight: 80,
        slideshow: true,
        slideshowAuto: false,
        slideshowStart: ''
    });
});
