var $j = jQuery.noConflict();

$j(document).ready(function() {
	$j(".javascript_notice").remove(); // If this is put as the id of anything, it will appear when javascript is turned off
	
	$j("input").not(".submit_button, input[type=submit], input[type=button]").each(function() {
	   var default_value = this.value;
	   $j(this).focus(function() {
	       if(this.value == default_value) {
	           this.value = '';
	       }
	   });
	   $j(this).blur(function() {
	       if(this.value == '') {
	           this.value = default_value;
	       }
	   });
	});
	
	var twitter_account = $j("#twitter_account").text();
	
	$j("#twitter_feed").tweetable({
		username: twitter_account,
		limit: 1,
		time: false
	});
	
	$j("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($j("#products_page_container").length) {
    	$j.setOptionsButtonEvent();
    	
    	$j("#pause_button").click(function() {
                   			$j.pause();
                        });
                        $j("#next_button").click(function() {
                    		$j.next();
                        });
                        
                        $j("#prev_button").click(function() {
                        	$j.prev();
                        });
                        
                    	$j("#first_button").click(function() {
                        	$j.first();
                        });
                        
                    	$j("#last_button").click(function() {
                        	$j.last();
                        });
    }
    
    // Category Page Slider
    
    $j("#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
	
	$j(".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 = $j("#product_category_list li .productdisplay").size();
	
	if(product_count <= 8) {
		$j("#slide_controls").remove();
	}
	
	if(product_count <=4) {
		$j("#product_category_list li, #product_category_slider").css({ "height" : "300px" });
	}
	
	if(product_count >4) {
		$j("#product_category_list li, #product_category_slider").css({ "height" : "620px" });
	}
	
	var i = 1;
	
	$j("#slide_controls").prepend('<p class="category_page_nav">Page ' + i + '</p>');
	
	$j("#nextBtn a").click(function() {
		i = i + 1;
		
		$j('.category_page_nav').remove();
		$j("#slide_controls").prepend('<p class="category_page_nav">Page ' + i + '</p>');
	});
	
	$j("#prevBtn a").click(function() {
		i = i - 1;
		
		$j('.category_page_nav').remove();
		$j("#slide_controls").prepend('<p class="category_page_nav">Page ' + i + '</p>');
	});
	
	$j("#terms_and_conditions a.thickbox").removeClass('thickbox');
	
	$j(".fancybox, #terms_and_conditions a").fancybox({
		'overlayOpacity' : .8,
		'titleShow' : false
	});
	
	// Contact Form Styling
	
	$j("#contact_submit").click(function() {
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$j/;
		
		var senderNameVal = $j("#contact_form #name").val();
		if(senderNameVal == '' || senderNameVal == ' ' || senderNameVal == 'Your name...') {
			$j("#contact_form #name").css({ "color" : "#f00" });
			hasError = true;
		}
		
		var emailFromVal = $j("#contact_form #email").val();
		if(emailFromVal == '') {
			$j("#contact_form #email").css({ "color" : "#f00" });
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {	
			$j("#contact_form #email").css({ "color" : "#f00" });
			hasError = true;
		}
		
		var subjectVal = $j("#contact_form #subject").val();
		if(subjectVal == '' || subjectVal == ' ' || subjectVal == 'Subject...') {
			$j("#contact_form #subject").css({ "color" : "#f00" });
			hasError = true;
		}
		
		var messageVal = $j("#contact_form #message").val();
		if(messageVal == '' || messageVal == ' ') {
			hasError = true;
		}
		
		
		if(hasError == true) {
			$j("#contact_submit").attr('disabled', 'disabled');
		}
	});
	
	$j("#contact_form input").click(function() { // This will only work if the form is filled out correctly, avoiding confusion
		$j("#contact_submit").removeAttr('disabled');
	});
	
	// Little javascript intervention for button styling on User Account Page
	
	$j("#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"
	});
});
