$(document).ready( function() {

    $('#country').change(function(){
        if($(this).val() == "Australia"){
            $('#home_country').show();
            $('#country_other').hide();
            $('#state_other').hide();
        }
        else if($(this).val() == "OTHER"){
            $('#home_country').hide();
            $('#country_other').show();
            $('#state_other').show();
        }        
        else{
            $('#home_country').hide();
            $('#state_other').show();
        }
    });
    $('#del_country').change(function(){
        if($(this).val() == "Australia"){
            $('#del_home_country').show();
            $('#del_country_other').hide();
        }
        else if($(this).val() == "Other"){
            $('#del_home_country').hide();
            $('#del_country_other').show();
            $('#del_state_other').show();
        }
        else{
            $('#del_home_country').hide();
            $('#del_country_other').show();
        }
    });

    // open external link in new tab/window
    // use rel="external" instead of target="_blank"
    $('a[rel="external"]').click( function() {
        this.target = "_blank";
    });
    
    //email replacement
    $("span.mailto").each(function(){
        exp = $(this).text().search(/\((.*?)\)/) != -1 ? new RegExp(/(.*?) \((.*?)\)/) : new RegExp(/.*/);
        match = exp.exec($(this).text());
        addr = match[1] ? match[1].replace(/ at /,"@").replace(/ dot /g,".") : match[0].replace(/ at /,"@").replace(/ dot /g,".");
        emaillink = match[2] ? match[2] : addr;
        subject = $(this).attr('title') ? "?subject="+$(this).attr('title').replace(/ /g,"%20") : "";
        $(this).after('<a href="mailto:'+addr+subject+'">'+ emaillink + '</a>');
        $(this).remove();
    });
	
	
		 
    //*advanced hide and show*/
    $('ul.showhideinfo li> div.showinfo').hide();
    $('ul.showhideinfo li a').click(function() {
        $(this).next('div').slideToggle('fast')
        .parent().siblings('li').find('div.showinfo:visible').slideUp('fast');
    });
	
	
	
	
    //product_nav
		
    $('ul#product_nav > li > ul, ul#product_nav > li > ul > li > ul').hide();
    $('ul#product_nav > li > a').hover(function() {
        $('ul#product_nav > li > ul:visible').hide();
        $('ul#product_nav > li > a').removeClass('active');
        $(this).toggleClass('active').next("ul").show();
				
    });
		
    $('ul#product_nav > li > ul > li > a').hover(function() {
        $('ul#product_nav > li > ul > li > ul:visible').hide();
        $('ul#product_nav > li > ul > li > a').removeClass('active');
        $(this).toggleClass('active').next("ul").show();
				
    });
				
				
		
    /*hide and show*/
    $('div.hidden:not(:eq(0))').hide()
    $('ul.visible li a').click(function() {
        var category = "#" + $(this).attr('rel');
        $('div.hidden').hide();
        $(category).fadeIn("slow");
        return false;
    });
			
    $("ul li a img").hover(function () {
        $(this).fadeTo("slow", 0.50);
        $(this).fadeTo("fast", 0.90);
    });
			
    $('form#checkout-form').validate({

        // tell the validation plugin to check the following inputs
        // documentation: http://docs.jquery.com/Plugins/Validation
        rules: {
            // BILLING
            first_name: "required",
            surname: "required",
            phone: {
                required: true,
                digits: true
            },
            email: {
                required: true,
                email: true
            },
            address1: "required",
            suburb: "required",
            postcode: {
                required: true
//                digits: true
            },
            state: "required",
            country: "required",

            // DELIVERY
            del_first_name: "required",
            del_surname: "required",
            del_phone: {
                required: true,
                digits: true
            },
            del_email: {
                required: true,
                email: true
            },
            del_address1: "required",
            del_suburb: "required",
            del_postcode: {
                required: true
//                digits: true
            },
            del_state: "required",
            del_country: "required"
        },
        messages: {

            // BILLING
            phone: {
                digits: "Please enter numbers only"
            },
            postcode: {
                digits: "Please enter numbers only"
            },

            // DELIVERY
            del_phone: {
                digits: "Please enter numbers only"
            },
            del_postcode: {
                digits: "Please enter numbers only"
            }
        },
        success: function(label) {
            // inform user their input is valid
            label.html(" ").addClass("correct");
        }

    });
		  
		
    $("#keywords:text").focus(function() {
        if( this.value == this.defaultValue ) {
            this.value = "";
        }
    }).blur(function() {
        if( !this.value.length ) {
            this.value = this.defaultValue;
        }
    })
		 
	
});