$(document).ready(function(){
	var defaultValues = ['Model #','Enter Search Term','Enter your email address','Enter Model Number'];
	
	$.fn.search = function() 
	{	
		return this.focus(function() 
		{	
			if( $.inArray(this.value, defaultValues) > -1 ) 
			{
				this.value = "";
				$(this).css('color','#000');
			}
		}).blur(function() 
		{
			if( !this.value.length ) 
			{
				this.value = this.defaultValue;
				$(this).css('color','#aeaeae');
			}
			else if( $.inArray(this.value, defaultValues) < 0 ) 
			{
				$(this).css('color','#000');
			}
		});
	};
	$(".search input[name=model]").search();
	$(".search input[name=s]").search();
	$(".join input[name=email]").search();
	$("#search input[name=model]").search();
	
	$(".browse select[name=model_id]").change(function(){
		window.location = $(this).val();
	});
	
	$("#register_form").validate({
		errorPlacement: function(error,element) {
			error.insertAfter(element);
		}
	});
	
	/**
	 * Handles the Home page carousel.  
	 **/
	$("#carousel #home-page").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		auto: 7500,
		speed:2000,
		visible: 1,
		btnGo:[".numbers #list_item_1",".numbers #list_item_2",".numbers #list_item_3",".numbers #list_item_4",".numbers #list_item_5",".numbers #list_item_6",".numbers #list_item_7",".numbers #list_item_8",".numbers #list_item_9",".numbers #list_item_10"],
		beforeStart: function() {
			$('.numbers span').removeClass('hover');
		},
		afterEnd: function(a) {
			$(".numbers #list_" + $(a).attr("id")).addClass('hover');
		}
	});
	
	$('.print a, .button-print').click(function(){
		window.print();
	});
	
	$('.button-back').click(function(){
		$("#register_form").attr('action', '/registration/info');
		$("#register_form").submit();
	});
	
	$(':date').dateinput();
	
	$(".tag").change(function(){
		$("#tags").submit();
	});
	
	$('#signup').submit(function() {
		// update user interface
		$('#response').html('Adding email address...');
		
		// Prepare query string and send AJAX request
		$.ajax({
			url: '/store-address.php',
			data: 'ajax=true&email=' + escape($('#mail_email').val()),
			success: function(msg) {
				$('#response').html(msg);
			}
		});
	
		return false;
	});
	
	/**
	 *  Product page js
	 **/	
	$(".image-thumbs a").click(function(){
		var clicked_id = $(this).attr('id');
		var show_id = clicked_id.replace('thumb','medium');
		$(".image-large li").hide();
		$('#' + show_id).show();
		return false;
	});
	
	$(".related-vids").colorbox({
		rel:'related-vids',
		current: "({current} of {total})",
		inline:true
	});
	
	$(".featured-vids").colorbox({
		rel:'featured-vids',
		current: "({current} of {total})",
		inline:true
	});
	
	$(".product-photos").colorbox({
		rel:'product-photos',
		current: "({current} of {total})"
	});
	
	$(".order-a-part-colorbox").colorbox({
		rel:'order-a-part-colorbox'
	});
	
});
