var displayTime = 5000;
var transitionTime = 2000;

$(document).ready(function() {
	$('.feat:first').css({'display' : 'block', 'z-index' : '3'}).addClass('cur_feat');
	$('#pager > ul > li:first > a').addClass('cur_pager');
	timer = setTimeout('rotateFeatures()', displayTime);
	
	setShoppingCartHeight();
	
	//cart.php functions 
	var cur;
	var curVal;
	
	$('.qty_input').each(function() {
		setTotals($(this));
	});
	$('.qty_input').focus(function() {
		cur = $(this);
		cur.css({'color' : '#0101c3'});
		curVal = cur.val();
	});
	$('.qty_input').keyup(function() {
		if(is_int(cur.val())) {
			setTotals(cur);
		} else {
			alert('You must input a positive whole number');
			cur.val(curVal);
		} 
	});
	$('.qty_input').blur(function() {
		cur.css({'color' : ''});
		if(is_int(cur.val())) {
			setTotals(cur);
		} else {
			cur.val(curVal);
		} 
	});
	$('#email_input').blur(function() {
		cur = $(this);
		updateEmail(cur);
	});
	$('#checkout_form').submit(function() {
		if(validateEmail()) {
			saveCart();
			return true;
		} else {
			return false;
		}
		
	});
});

function rotateFeatures() {
	var curFeat = $('.cur_feat');
	var nextFeat = curFeat.next('.feat');
	if(!nextFeat.length) {
		nextFeat = $('.feat:first');
	}
	
	nextFeat.css({'z-index' : '2', 'display' : 'block'});	
	curFeat.fadeOut(transitionTime, function() {
		curFeat.css({'z-index' : '1'}).removeClass('cur_feat');
		nextFeat.css({'z-index' : '3'}).addClass('cur_feat');
		timer = setTimeout('rotateFeatures()', displayTime);
	});
	
	setPager();
}

function setPager() { //circle buttons that correspond with each feature
	var curPager = $('.cur_pager');
	var nextPager = curPager.parent().next('li').children('a');
	if(!nextPager.length) {
		nextPager = $('#pager > ul > li:first > a');
	}
	curPager.removeClass('cur_pager');
	nextPager.addClass('cur_pager');
}

function setShoppingCartHeight() {
	var shoppingCartInfo = $('#shopping_cart_info');
	var pageContent = $('#page_content');
	
	if(shoppingCartInfo.length) {
		shoppingCartInfo.height(pageContent.height() - 55);
	}
}

// cart.php functions
function is_int(value){
	if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
		if(value>0) {
			return true;
		}
	} else {
		return false;
	}
}
function setTotals(cur) {
	if(cur.length) {
		var name = cur.attr('name');
		var id = name.substr(5);
		var qty = $('#input' + id).val();
		var output = $('#output' + id);
		var price = $('#price' + id).html();
		var totalPrice = (price * qty).toFixed(2);
		var copies = qty>1 ? "copies" : "copy";
		
		$('#copies' + id).html(copies);		
		$.post("includes/ajax_update_quantity.php", {item_id : id, quantity : qty}, function() {
			//alert(data);
		
			resetCheckoutButton();
		});
		
		output.html(totalPrice);
		setSubtotal();
	}
}
function setSubtotal() {
	var subtotal = 0;
	
	$('.output').each(function() {
		var val = $(this).html();
		if(val > 0) {
			subtotal += parseFloat(val);
		}
		
	});
	$('#subtotal').html(subtotal.toFixed(2))
	$('#order_total').html(subtotal.toFixed(2))
}
function resetCheckoutButton() {
	$.post("includes/ajax_checkout_button.php", {}, function(data) {
		if(data.length) {
			$('#checkout_info').html(data);
		}
	});
}
function updateEmail(cur) {
	var emailAdr = cur.val();
	$.post("includes/ajax_update_email.php", {email : emailAdr}); 
}
function validateEmail() {
	var emailInput = $('#email_input');
	if(emailInput.val().length) {
		return true;
	} else {
		alert("You must enter an email address.");
		emailInput.css({'border' : 'solid 1px #c00', 'background-color' : '#facbcb'});
		emailInput.focus();
		return false;
	}
}
function saveCart() {
	//$.post("includes/ajax_save_cart.php", {});
}
