$(document).ready(function(){
	//Homepage main graphic rotator
	$('#splash').innerfade({ 
		animationtype: 'fade',
		containerheight: '390px',
		containerwidth: '908px',
		children: 'img',
		type: 'sequence',
		speed: 1000,
		timeout: 8000
	});
	
	//Sidebar h3 header accordian
	$('#sidebar h3').click(function(){
		var tmp = $(this);
		$(this).next().slideToggle('normal',function(){tmp.toggleClass('active');});
	});
	
	//Sidebar sub item accordian
	$('#sidebar .info a').click(function(){
		var tmp = $(this).parent();
		$('p',tmp).slideToggle('normal',function(){tmp.toggleClass('active');});
		return false;
	});

	//Colors tooltip w/ new plugin
	$('#sidebar ul.colors a').qtip({
		content: false,
		show: 'mouseover',
		hide: 'mouseout',
		position: {
			corner: { target: 'topLeft', tooltip: 'bottomRight' },
			adjust: { x: 5, y: 5 }
		},
		style: { 
			name: 'light',
			width: { min: 75, max: 200 },
			textAlign: 'center',
			tip: 'bottomRight',
			border: { color: '#31BDEF' }
		}
	});
	
	//Product category page - add sfhover class
	$('#prod-list li.product').hover(function(){
		$(this).addClass('sfhover');
	},function(){
		$(this).removeClass('sfhover');
	});
	
	//Products thumbnail functionality
	$('#product .thumbs a').live('click',function(){
		var orig = $(this).children('img').attr('src');
		newImg = orig.replace('thumb','normal');
		$('#product .featured-image img').attr('src',newImg);
		return false;
	});
	
	//Change the product colors, update sku & change selected drop-down
	$('#product .color-options a').click(function(){
		var color_id = $(this).attr('href').replace('#','');
		var color_name = $('img',this).attr('alt');
		var sku = $(this).attr('rel');
		var product_id = $('#product .featured-image').attr('id').replace('product','');
		$('select.color').val(color_name);
		$('input[name="code"]').val(sku);
		$.getJSON('/ajax/products.php',{sku_id:sku,color_id:color_id,product_id:product_id},function(data){
			// empty current placeholders
			$('ul.thumbs').empty();
			$.each(data, function(i,item){
				if (typeof(item)=="object")
				{
					if (i==1){
						$('#product .featured-image img').attr('src','/uploads/product-images/'+item.product_id+'/'+item.image);
						$('#product .featured-image p').text(item.color+" - "+sku);
					}
					var thumb = '<li><a href="#"><img src="/uploads/product-images/'+item.product_id+'/'+item.icon+'" alt="" /></a></li>';
					$(thumb).appendTo("ul.thumbs");
				}
			});
			$("p.quantity").text(data.qty_message);
			$('#cart input[name="quantity_max"]').val(data.qty);
			if(data.qty==0){$('#cart input.submit').hide();}else{$('#cart input.submit').show();}
		});
		return false;
	});
	$('select.color').change(function(){
		var sku = $('option:selected',this).attr('rel');
		$("a[rel='"+sku+"']").trigger('click');
	});
	
	//This is to slow down spam on all the forms.
	$('form:not(.foxycart)').append('<input type="hidden" name="secretsquirrel" value="ihatebots" />');
	
	//Create modal window for email newsletter signup
	var name = $("#enews-name"),
		email = $("#enews-email"),
		allFields = $([]).add(name).add(email),
		tips = $("#validateTips");

	function updateTips(t){	tips.text(t).effect("highlight",{},1500); }
	function checkLength(o,n,min,max) 
	{
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			return true;
		}
	}
	function checkRegexp(o,regexp,n) 
	{
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}

	$("#enews-dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 180,
		modal: true,
		buttons: {
			'Signup for Kijaro News': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(name,"name",3,40);
				bValid = bValid && checkLength(email,"email",6,80);
				bValid = bValid && checkRegexp(name,/^[0-9a-zA-Z\- ]+$/i,"Name may consist of a-z, A-Z, dashes and spaces.");
				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. enews@kijaro.com");

				if (bValid) {
					box = $(this);
					//Send off ajax style
					$.post("/ajax/enews_signup.php",{name: name.val(), email: email.val()},function(data){
						$('form',box).fadeOut('fast');
						updateTips(data);
						tips.fadeOut(1200,function(){box.dialog('close');});
						pageTracker._trackPageview("/email-signup/");
					});
				}
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
			tips.text("All form fields are required.");
		}
	});
	
	$('#cart-status a.enews').click(function(){
		$('#enews-dialog').dialog('open');
		return false;
	});
	
});

Cufon.replace('h1,h2,#sidebar h3,.form label');

