$(document).ready(function() {
	$(".javascript_notice").remove(); // If this is put as the id of anything, it will appear when javascript is turned off
	
	$("input").not(".submit_button, input[type=submit], input[type=button]").each(function() {
	   var default_value = this.value;
	   $(this).focus(function() {
	       if(this.value == default_value) {
	           this.value = '';
	       }
	   });
	   $(this).blur(function() {
	       if(this.value == '') {
	           this.value = default_value;
	       }
	   });
	});
	
	var twitter_account = $("#twitter_account").text();
	
	$("#twitter_feed").tweetable({
		username: twitter_account,
		limit: 1,
		time: false
	});
	
	$("ul#product_showcase").innerfade({
        speed: 1000,
        timeout: 5500,
        type: 'sequence',
        containerheight: 	'400px',
        slide_timer_on: 	'yes',
        slide_ui_parent: 	'product_showcase',
        slide_ui_text:		'portfolio-desc',
        pause_button_id: 	'pause',
        slide_nav_id:		'slide_nav'
    });
    
    if($("#products_page_container").length) {
    	$.setOptionsButtonEvent();
    	
    	$("#pause_button").click(function() {
                   			$.pause();
                        });
                        $("#next_button").click(function() {
                    		$.next();
                        });
                        
                        $("#prev_button").click(function() {
                        	$.prev();
                        });
                        
                    	$("#first_button").click(function() {
                        	$.first();
                        });
                        
                    	$("#last_button").click(function() {
                        	$.last();
                        });
    }
    
    // Category Page Slider
    
    $("#product_category_slider").easySlider({
    	speed: 600,
    	prevText : '',
    	nextText : '',
    	controlsBefore: '<div id="slide_controls">',
    	controlsAfter: '</div>'
	});
	
	// Applies background-color to category page slider for internet explorer
	
	$(".ie #product_showcase li h2, .ie #product_showcase li p, .ie #product_showcase li label, .ie #product_showcase li .wpsc_product_price, .ie #product_showcase li span.pricedisplay, .ie #product_showcase li .wpsc_loading_animation, .ie #product_showcase li .item_no_image span").css({
		"background-color" : "#fff"
	});
	
	// Find if less than 8 products, if true, delete slide nav
	
	var product_count = $("#product_category_list li .productdisplay").size();
	
	if(product_count <= 8) {
		$("#slide_controls").remove();
	}
	
	if(product_count <=4) {
		$("#product_category_list li, #product_category_slider").css({ "height" : "291px" });
	}
	
	var i = 1;
	
	$("#slide_controls").prepend('<p class="category_page_nav">Page ' + i + '</p>');
	
	$("#nextBtn a").click(function() {
		i = i + 1;
		
		$('.category_page_nav').remove();
		$("#slide_controls").prepend('<p class="category_page_nav">Page ' + i + '</p>');
	});
	
	$("#prevBtn a").click(function() {
		i = i - 1;
		
		$('.category_page_nav').remove();
		$("#slide_controls").prepend('<p class="category_page_nav">Page ' + i + '</p>');
	});
	
	$("#terms_and_conditions a.thickbox").removeClass('thickbox');
	
	$(".fancybox, #terms_and_conditions a").fancybox({
		'overlayOpacity' : .8,
		'titleShow' : false
	});
	
	// Contact Form Styling
	
	$("#contact_submit").click(function() {
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var senderNameVal = $("#contact_form #name").val();
		if(senderNameVal == '' || senderNameVal == ' ' || senderNameVal == 'Your name...') {
			$("#contact_form #name").css({ "color" : "#f00" });
			hasError = true;
		}
		
		var emailFromVal = $("#contact_form #email").val();
		if(emailFromVal == '') {
			$("#contact_form #email").css({ "color" : "#f00" });
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {	
			$("#contact_form #email").css({ "color" : "#f00" });
			hasError = true;
		}
		
		var subjectVal = $("#contact_form #subject").val();
		if(subjectVal == '' || subjectVal == ' ' || subjectVal == 'Subject...') {
			$("#contact_form #subject").css({ "color" : "#f00" });
			hasError = true;
		}
		
		var messageVal = $("#contact_form #message").val();
		if(messageVal == '' || messageVal == ' ') {
			hasError = true;
		}
		
		
		if(hasError == true) {
			$("#contact_submit").attr('disabled', 'disabled');
		}
	});
	
	$("#contact_form input").click(function() { // This will only work if the form is filled out correctly, avoiding confusion
		$("#contact_submit").removeAttr('disabled');
	});
	
	// Little javascript intervention for button styling on User Account Page
	
	$("#results form table input[type=submit]").css({
		"border" 		: 		"5px solid #f2f2f2",
		"color" 		: 		"#00cd00",
		"font-size" 	: 		"1.0em",
		"font-weight" 	: 		"900",
		"padding" 		:	 	"12px 20px",
		"width" 		: 		"130px"
	});
});
