﻿$(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({
            triggerElement: $(this)
        });
        return false;
    })

    // Make the entire category div clickable
    // based on the first href found inside the div
    $('#productCategories .category, #product .thumbs li').clickable();
    
    $('#product .image img.zoom').each(function(){
        var img = $(this);
        var options = {
            image: img,
            lightbox: lightbox
        } 
        img.magnify(options);
        img.load(function(){
            if( $(this).hasClass('zoom') ) {
                $(this).magnify(options);
            } else {
                $(this).unbind('mouseenter');
                $(this).unbind('leave');
            }
        })
    });

    // Make the thumbnails under the product image clickable
    // Replaces the main product image with the one linked to in the thumbnail
    var productImage = $('#product .image img');
    $('#product .thumbs a').each(function(){
        var link = $(this);
        link.click(function(){
            if( productImage.attr('src') != link.attr('href') ) {
                if( link.hasClass('zoom') ) {
                    productImage.addClass('zoom');
                } else {
                    productImage.removeClass('zoom');
                }
                productImage.attr('src',link.attr('href'));
            }
            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');
        }
    }

   v2.Validator.reg('email', function(field, value, params) {
	// This doesn't compress... http://www.regular-expressions.info/email.html
	return /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(value.toLowerCase());
   });

    // 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');
    giftwrap.find('#giftwrap').click(function(){
        giftwrapContent = giftwrap.find('.content');
        if( !this.checked ) {
            giftwrapContent.slideUp('fast');
        } else {
            giftwrapContent.slideDown('fast');
        } 
    });
    
    $('#comments .showall a').click(function(){
        $('#comments .morecomments').slideToggle('fast');
        return false;
    });
    
    $('#productComments .star').rating();

/*
     * Added by Eric 10.juli.2010
    */
    
    var upadte_payment_details = function(input){
    	
    	var total_price  = 0;
    	var payment_method_price  = 0;
    	var delivery_method_price = 0;
    	var giftwrap_price = 0;
    	
    	
    	// Updating payments details in sidebar
    	var selected_input = $(input).attr('id');
    	var input_name = $(input).attr('name');
    	if(input_name == 'delivery_option' ){
    		var delivery_method = $(input).val();
    	    var delivery_method_price = $('#' + delivery_method + '_price').val();
    	    delivery_method_price = parseFloat(delivery_method_price); 
    	    $('#delivery_method').html(delivery_method);
    	    $('#delivery_method_price span').html(delivery_method_price);
    	    
    	} else {
    		var payment_method = $(input).val();
    	     payment_method_price = $('#' +selected_input+ '_price').val();
    	    payment_method_price = parseFloat(payment_method_price); 
    	    $('#payment_method').html(payment_method);
    	    $('#payment_method_price span').html(payment_method_price);
    	    var delivery_method_price = $('#delivery_price span').html();
    	    delivery_method_price = parseFloat(delivery_method_price);  
    	}
    	
    	var all_items_price = $('#all_items_price span').html();
    	all_items_price = parseFloat(all_items_price); 
    	
        if( $('#giftwrap_details').is(':visible')){
            giftwrap_price = $('#giftwrap_details dd span').html();
        }
    	total_price = payment_method_price + all_items_price + delivery_method_price + parseFloat(giftwrap_price);
    	$('#total_price span').html('kr '+ parseFloat(total_price) );
    }
    
     
        giftwrap.find('#isa_giftwrap').click(function(){
        giftwrapContent = giftwrap.find('.content_isa_gift');
        if( !this.checked ) {
            giftwrapContent.slideUp('fast');
        } else {
            giftwrapContent.slideDown('fast');
        } 
    });
    
    //levering metoder
    $('#checkout-delivery .delivery_option').change(function(){
        if( this.checked  ) {
        	upadte_payment_details(this);
        } 
    });
    
     
      $('#checkout-delivery span.read_more ').click(function(e){
      	e.stopPropagation();
      	$(this).parents("label").siblings(".levering_detail").slideToggle('fast');
   
    });
    
    // Hjelpetekst og feltet under må være synlig 
    if( $('#isa_giftwrap').is(':checked')){
        $('.content_isa_gift').show();
         
    }
    if( $('#giftwrap').is(':checked')){
         $('.giftwrap .content').show();
    }
    
    

    //payment
    var boxes = $('#checkout-payment .checkboxes');
    boxes.find('.payment_option').change(function(){
    	var parent_container = $(this).parents(".new_container");
        var betaling_detail = parent_container.find('.betaling_detail');
        if( !this.checked  ) {
            betaling_detail.slideUp('fast');
        } else {
        	$('.betaling_detail').hide('fast');
            betaling_detail.slideDown('slow');
            upadte_payment_details(this);
        } 
    });
    
    /* Lightbox effect for content */
    $('.melding_lightbox a.content_lightbox').click(function(){
        	$('#bill_preview').show();
        	return false;

    });
    
    $('.melding_lightbox a.content_lightbox_close').click(function(){
        	$('#bill_preview').hide();
        	return false;

    });
  
  
    
    /*
    * END added by Eric
    */

});