﻿$(document).ready(function() {
    
    // Add fading effect to product list images on hover
    $('#productCategories .category, .products .product img, #product .thumbs li').hover(
        function(){
            $(this).fadeTo('fast',0.5);
        },
        function(){
            $(this).fadeTo('fast',1,function(){
                try{$(this).get(0).style.removeAttribute('filter');}catch(err){} // Fix cleartype bug in IE
            });
        }
    );

    // Add lightbox to all links with class "lightbox"
    // Loads content from the href attribute into the lightbox
    var lightbox = new Lightbox();
    $('a.lightbox').click(function(){
        lightbox.open($(this));
        return false;
    })

    // Toggle display of the tip a friend form on the product page
    $('#product #tipafriend .tip a').click(function(){
        $('#tipafriend form').slideToggle('fast');
        return false;
    });

    // Add hints to various form elements
    // Shows a hint in input and select elements that disappears when you enter the field 
    $('#searchForm .text input').hint({hint:'Finn et produkt:'});
    $('#newsletter .text input').hint({hint:'Din epostadresse:'});
    $('#productComments input.title').hint({hint:'Overskrift'});
    $('#productComments textarea.comment').hint({hint:'Skriv din kommentar...'});
    
    // Validatious configuration
    v2.Fieldset.prototype.failureClass = 'error';
    v2.Field.prototype.instantWhenValidated = false;
    v2.Form.prototype.scrollToFirstWhenFail = false; // Disable jump to first field that has a validation error
    v2.Validator.prefix = 'val-';
    v2.Validator.add({
        name: 'hint', 
        message: '${field} er påkrevet',
        fn: function( field, value, params ) {
            elements = field.getElements();
            for( el in elements ) {
                if( /(^|\s)hint(\s|$)/.test( elements[el].className ) ) {
                    return false;
                }
            }
            return true;
        }
    });
    v2.Form.prototype.onSuccess = function () {
        var checkout = $('form#checkout-payment'); 
        if( checkout.length ) {
            checkout.find('.section').fadeTo('fast',0.2);
            checkout.find('.waiting').show('fast');
            $('body').addClass('hideSelects');
        }
    }
    
    // Make columns equal height
    $('#footer .content > div').equalHeights();
    $('#productFilter .col').equalHeights();
    $('#product .footer > div').equalHeights();
    $('.section-row .section').equalHeights();

    // Add dropdown menu functions to mainmenu
    $('#mainMenu').dropdownMenu();
    
	// Display billing address form if checkbox is not checked
	var billingAddress = $('#checkout-customerinfo .billing-address');
	var billingAddressContent = billingAddress.find('.content');
	var billingAddressCheckbox = billingAddress.find('#billing-same');
	if( billingAddressCheckbox.length ) 
		{
		if( billingAddressCheckbox[0].checked ) 
			{
			billingAddressContent.show();
			}
		billingAddressCheckbox.click(function()
			{
			if( !this.checked )
				{billingAddressContent.slideUp('fast');}
			 else
				{billingAddressContent.slideDown('fast');} 
			});
		}

	var giftwrap = $('#checkout-delivery .giftwrap');
	var giftwrapContent = giftwrap.find('.content');
	var giftwarpCheckbox =giftwrap.find('#giftwrap');
	if (giftwarpCheckbox.length)
		{
		if (giftwarpCheckbox[0].checked)
			{
			giftwrapContent.show();
			}
		giftwarpCheckbox.click(function()
			{
        		if( !this.checked ) 
				{ giftwrapContent.slideUp('fast'); }
				 else 
				{ giftwrapContent.slideDown('fast'); } 
			});
		}
    
    $('#comments .showall a').click(function(){
        $('#comments .morecomments').slideToggle('fast');
        return false;
    });
    
    $('#productComments .star').rating();
});