$(document).ready(function () {


	/*$("form#preview").validate({
   	 submitHandler: function(form) {
			$(form).ajaxSubmit();
   	 	//tb_show('Preview WallTweet','/home/preview?height=640&width=640')
   	}
	});*/
	
	$('form').validate({
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			 if (errors) {
	      	alert("Please check the form, and try again.");
	      }  
	   },
		 submitHandler: function(form) {
		 	hideme();
			this.form.submit();
		}
	});


    function zebra() {
			$(".faveslist li:odd").addClass('odd');
			$(".faveslist li").hover(
		      function () {
		        $(this).addClass('on');
		      }, 
		      function () {
		        $(this).removeClass('on');
		      }
		    );
	 }
	$(".faveslist li").livequery('click', function () {
		var tweet = $('span',this).html();
		var who = $('strong',this).html();
		
		$('#tw').val(tweet);
		$('#wh').val(who);
	});

	function hideme() {
		$('#placeorder').hide();
		$('#placeorder').next('span').show();
	}
			
	$("#styletable img").click(function () {
		$('#styletable img').each( function () { $(this).removeClass("on"); });
		
		var stid = $(this).attr('id');
		$('input#wtstyle').val(stid);
		$(this).addClass("on");
	});
	
	//show shipping form on dropdown change
	$("select#sameship").change( function(){
		var hg = $(this).val();
		
		$('#shippingform').slideToggle('fast', function() {
			$('#shippingform input, #shippingform select').each(function (){
				if(!$(this).hasClass('no')) {
					$(this).toggleClass('required');
				}
			});
		
				
			if (hg == 'no') {
				//$('#shippingform .required').parent('p').children('label').prepend('<span class="req">*</span>');

			} else {
				$('#shippingform span.req').remove();
			}
		});
		
		
		
	});
	
	// add * to required labels
	$('.required').livequery( function() { 
		$(this).parent('p').children('label').prepend('<span class="req">*</span>');
	});

	// update cart total
	$('#qtytable select').change( function () {
		var i = 0;
		itemtotal = new Array();
		$('#qtytable select').each(function () {
			var value = $(this).val();
			var args = value.split(',');
			itemtotal[i] = parseFloat(args[0])*parseFloat(args[1]);
			i++;
		});
		total = 0;
		for( var p in itemtotal ) {
			total = total+parseFloat(itemtotal[p]);
		}
		//alert(total);
		$('#carttotal').val(total);
		$('.carttotal span').html(total);
	});
	
	$("select#pname,select#qty,select#pmaterial").change( function(){
		cacl();
	});
	
	function cacl() {
		var name = $("select#pname").val();
		var qty = $("select#qty").val();
		var mat = $("select#pmaterial").val();
		productprice(name,qty,mat);
	}
	
	function productprice(name,qty,mat) {
		$.ajax({
			type: "GET",
			url: "/cart/figureprice/"+name+"/"+qty+"/"+mat,
			success: function (m) {
				ff = m.split(',');
				$('#pid').val(ff[0]);
				$('#pprice').val(ff[1]);
				$('.unitprice').html(ff[1]).fadeIn();
			}
		});
		
	
	}
	
	cacl();
	zebra();

	
	$('li#more button').click( function() { 
		var page = $('#favename').val();
		getmorefaves(parseFloat(page)+1);
	});
	
	function getmorefaves(page) {
		$.ajax({
			type: "GET",
			url: "/home/morefaves/"+page,
			success: function (m) {
				$('li#more').before(m);
				zebra();
				$('#favename').val(page);
				return false;
				
			}
		});
	}
	
	$('a#noimg').click( function() {
		noimg();
		return false;
	});
	function noimg() {
		$('form.hide').fadeIn();
		$('#tweetpreview img').fadeOut().remove(); 
		$('#tweetpreview p span').css("margin-left", "0px");
		return false;
	
	}
	


		
		$('#tsamples').cycle({ 
    fx:     'scrollDown', 
    easing: 'bounceout', 
    delay:  -4000,
    timeout: 7000,
    random: 1
});
	
});

function gotopayment() {
	tb_remove();
	window.location = "/checkout";
}
