var totalReviews = 0;
var currentReview = 0;

$(document).ready(function(){

	/* Selects all checkboxes */
	$('#select-all').click(function(){
		$("input[type='checkbox']").attr('checked', true);
		return false;
	});
	/* Selects all checkbox and submits the form */
	$('.buynow_all').click(function(){
		$("input[type='checkbox']").attr('checked', true);
		$("#form_phrasebooks").submit();
		return false;		
	});
	/* Validates the number of choices from the user */
	$('#buynow').click(function(){
		var total =  $("input:checked").length;
		if(total == 2)
		{
			alert("You have selected two books, select another one for FREE !");
			return false;
		}else if(total == 0)
		{
			alert("You must select at least one book.");
			return false;
		}
	});
	/* Shows the floating windows */
	$(function() {
		$("a[rel]").overlay({effect: 'apple'});
	});	
	/* Gets ajax response from contact submit*/
	//
	$('#send-contact').click(function(){
		$('#message-status').html('<img src="static/img/ajax-loader.gif">');
		$('#send-contact').hide();
		$.post("contact-us/", $("#contact-form").serialize(),
   			function(data){
				if(data == "<div class='correct'>Your message was sent. Thank you!</div>")
				{
					$("#contact-form")[0].reset();

				}
   				$('#send-contact').show();
				$('#message-status').html(data);
   			});		
		return false;
	});
	
	$('#preview_iphone').jqm({modal: true, trigger: '.show_iphone'});
	$('#preview_book').jqm({modal: true, trigger: '.show_book'});
	
	$('.s2').cycle({ 
	    fx:     'fade', 
	    speed:  'fast', 
	    timeout: 0, 
	    next:   '#next', 
	    prev:   '#prev' 
	});
	
	$('.s3').cycle({ 
	    fx:     'fade', 
	    speed:  'fast', 
	    timeout: 0, 
	    next:   '#next3', 
	    prev:   '#prev3' 
	});	


	totalReviews = $('.review').size() - 1;
	currentReview = 0;
	// shows a random review
	show_random_quote();
	
	$("#next_review").click(function(){
		show_random_quote();
		return false;
	});
});

function show_random_quote()
{
	// hides all reviews
	$(".review").hide();
	
	if( (currentReview + 1) <= totalReviews)
	{
		currentReview++;
	}else{
		currentReview = 0;
	}
	// shows the review
	$("#review_" + currentReview).fadeIn();
}
