(function($){
    $.fn.pause = function(duration, func){
        $(this).animate({
            dummy: 1
        }, duration, 'swing', func);
        return this;
    };
	//$('.preview_link').addClass('imageLink');
	$('#mc_unsub_link a').html('Unsubscribe');
	$('input[type=submit], button').addClass('submit');
	$('input[type=submit], button').css('width', 'auto');
	$('input[type=hidden]').css('background', 'none');
	$("label[for='mc_mv_EMAIL']").html('Email');

	$('.custom_gateway .paypal_certified').parent().parent().addClass('paypal_label');
	
	$('#shopping-cart h2').append('<span class="left cartcount">' + $('#shopping-cart .items .cartcount').text() + ' item(s)</span>');
	$('#shopping-cart h2 .title_text').addClass('left');
	$('.gocheckout').after('<div class="clear"></div>');
	
    $('.current_page_parent').addClass('current_page_item').removeClass('current_page_parent');
    $('.current_page_ancestor').addClass('current_page_item').removeClass('current_page_ancestor');
    $('.current_page_item > ul > li').addClass('current_page_parent');
    
//    $("ul.sf-menu").superfish({
//        pathClass: 'current_page_item',
//        delay: 2000
//    });
    
    $('input[type=submit]').mouseover(function(){
        $(this).css('color', '#70c3bc')
        $(this).css('cursor', 'pointer')
    });
    $('.submit').mouseout(function(){
        $(this).css('color', '#fff')
    });
    
    
    /* start search */
    $("ul.sf-menu li.page_item:last span").css({
        'background': 'none'
    });
    $("#s").focus(function(){
        if ($(this).val() == $(this)[0].title) {
            $(this).removeClass("blurred");
            $(this).val("");
        }
    });
    $("#s").blur(function(){
        if ($(this).val() == "") {
            $(this).addClass("blurred");
            $(this).val($(this)[0].title);
        }
    });
    $("#s").blur();
    $("#searchform .go").click(function(){
        if ($("#s").val() == "" || $("#s").val() != $("#s")[0].title) {
            $("#searchform").submit();
        }
        else {
            $(".search_message").fadeIn("slow", function(){
                $(this).pause(5000).fadeOut();
            });
            return false;
        }
    });
    /* end search */

    /* start form processing */
    $('form.loginform').submit(function(){
		// trim fields
		$(this).find('input.required').each(function(){$(this).val($.trim($(this).val()));});
		
		// highlight error fields
		$(this).find('input.required:text[value=""], input.required:checkbox[value=""], input.required:password[value=""]').addClass('error_field');
		$(this).find('input.required:text[value!=""], input.required:checkbox[value=""], input.required:password[value!=""]').removeClass('error_field');
		
		// focus first error field
		$(this).find('input.required[value=""]:first').focus();
		
		$global_error = '';
		$ok = true;
		
		// check if all fields are filled in
		if($(this).find('input.required:text[value=""], input.required:checkbox[value=""], input.required:password[value=""]').length != 0){
			$global_error += '<p>Please ensure all required fields are correctly filled in.</p>';
			$ok = false;
		}
		
		// does password fields exists and are equal
		if(($(this).find('#pass1').is(':visible') && $(this).find('#pass2').is(':visible')) && ($(this).find('#pass1').val() != $(this).find('#pass2').val())){
			$global_error += '<p>Please ensure both passwords are equal.</p>';
			$(this).find('#pass1').addClass('error_field');
			$(this).find('#pass2').addClass('error_field');
			$ok = false;
		}
		
		// is there a legal agreement and is it checked
		if($(this).find('#legal_agree').is(':visible') && $(this).find('#legal_agree:checked').length != 1) {
			$global_error += '<p>Please ensure you have read our privacy policy and terms and conditions before continuing.</p>';
			$ok = false;
		}
		
		// check for valid email
		if($(this).find('.email').is(':visible') && !isValidEmailAddress($(this).find('.email').val())) {
			$global_error += '<p>Please enter a valid email.</p>';
			$(".email").keyup();
			$ok = false;
		}
		
		// display errors
		if(!$ok && ($(this).attr('class').indexOf('showerror') >= 0)){
			if(!$(this).find('.error').is(':visible')) {
				$(this).prepend('<div style="line-height:20px;padding:5px 5px 10px 40px;background-position:5px 4px;" class="error hidden">' + $global_error + '</div>');
				$(this).find('.error').fadeIn('slow');
			} else {
				$(this).find('.error').html($global_error);
			}
		}
		
		return $ok;
	});
	
	// check for valid email
    $(".email").keyup(function(){
        if (isValidEmailAddress($(this).val())) {
            $(this).removeClass('error_field');
        } else {
            $(this).addClass('error_field');
        }
    });
    /* end form processing */
    
    /* start ie fixes and rounded corners */
    if ($.browser.msie) {
        //DD_roundies.addRule('.png');
    }
    //DD_roundies.addRule('.wp-caption', '10px 10px 10px 10px', !$.browser.msie);
    DD_roundies.addRule('input#s', '10px 10px 10px 10px', !$.browser.msie);
    /* start ie fixes and rounded corners */
})(jQuery);

/**
 * Check if user entered a valid email
 * 
 * @param {Object} emailAddress
 */
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

/**
 * Gets the specified querystring from the browser url
 *
 * @param {Object} key
 */
function qs(key, default_){
    if (default_ == null) 
        default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&]*)");
    //var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null) 
        return default_;
    else 
        return qs[1];
}