$(document).ready(function(){
	// fadeout flash messages on click
	$('.flash_bad').click(function(){
		//alert('click() called');
		//$(this).fadeOut();
		$(this).hide("blind");
	return false;
	});

	// fade out good flash messages after 3 seconds
	//$('.flash_good').animate({opacity: 1.0}, 3000).fadeOut();
	$('.flash_good').animate({opacity: 1.0}, 3000).hide("blind");
	
	$('.togglePrint').click(
		function() {
			//alert($(this).is(':checked'));
			checkMe();
		}
	);
	$('.hidden').hide();
	
	$('[class^=toggle-item]').hide();
	$('[class^=link]').click(function() {
		var $this = $(this);
		var x = $this.attr("className");
		$('.toggle-item-' + x).toggle('blind');
		return false;
	});
});

function checkMe() {
	var box = $('.togglePrint').is(':checked');
	if(box) {
		$('.hidden').show("blind");
	} else {
		$('.hidden').hide("blind");
	}
}

/*$('#selectedImage img').each(function() {
	$(this).cjObjectScaler({
		method: "fit",
		fade: 550
	});
});*/

// ajax functionality for custom forms
$.ajaxSetup({
	cache: false
});
var ajax_load = '<img src="/img/loading.gif" alt="loading..." />';
// minus one from cart - ajax
var minusUrl = '/LithographsOrders/minusOne';
$('.minus').click(function() {
	// don't allow less than 1! -- alert them?
	//alert('minus');
	var selected = this.id;
	var selID = selected.match(/[\d\.]+/g); ///[\d\.]+/g
	var quantity = $('#quantity' + selID).val();
	if(quantity > 1) {
		//alert('#Print' + selID + 'Count');
		$('#quantity' + selID).val(quantity - 1);	// minus 1 from value
		$('#Print' + selID + 'Count').val(quantity - 1);	// minus 1 from value
		$('#total' + selID)
			.html(ajax_load)
			.load(minusUrl + '/' + selID, null, function(responseText){
				//$('#total' + selID).effect("highlight", {}, 3000);
			});
	}
});

// plus one to cart - ajax
var plusUrl = '/LithographsOrders/addOne';
$('.plus').click(function() {
	//console.log( "clicked on", jQuery(this), "which has id ", jQuery(this).attr('id') );
	//alert('plus');
	var selected = this.id;
	var selID = selected.match(/[\d\.]+/g);
	var quantity = $('#quantity' + selID).val();
	//alert($('#quantity' + selID).val());
	$('#quantity' + selID).val(Number(quantity) + 1);
	$('#Print' + selID + 'Count').val(Number(quantity) + 1);
	$('#total' + selID)
		.html(ajax_load)
		.load(plusUrl + '/' + selID);
		/*.load(plusUrl + '/' + selID, null, function(responseText){
			alert('load complete');
		});*/
});

$('.quantity').focusin(function() {
	alert('focus');
	var selected = this.id;
	var selID = selected.match(/[\d\.]+/g);
	var oldQuantity = $('#quantity' + selID).val();
	$('.quantity').data('oldQ', oldQuantity);
	//alert(oldQuantity);
});
function doMath() {
	var newValue = $('.quantity').data('newQ');
	var oldValue = $('.quantity').data('oldQ');
	//alert($('.quantity').data('selectedID'));
	var selID = $('.quantity').data('selectedID');
	if(newValue < 1) {
		//alert('less than one');
	} else if(isNaN(newValue)) {
		//alert('not a number!');
	} else if(newValue < oldValue) {
		$('#Print' + selID + 'Count').val(newValue);
		$('#total' + selID)
			.html(ajax_load)
			.load('/LithographsOrders/addN' + '/' + selID + '/' + newValue, function(responseText){
				//$('#total' + selID).effect("highlight", {}, 3000);
			});
	} else if(newValue > oldValue) {
		$('#Print' + selID + 'Count').val(newValue);
		$('#total' + selID)
			.html(ajax_load)
			.load('/LithographsOrders/addN' + '/' + selID + '/' + newValue, function(responseText){
				//$('#total' + selID).effect("highlight", {}, 3000);
			});
	} else if(newValue == oldValue) {
		$('#Print' + selID + 'Count').val(newValue);
		$('#total' + selID)
			.html(ajax_load)
			.load('/LithographsOrders/addN' + '/' + selID + '/' + newValue, function(responseText){
				//$('#total' + selID).effect("highlight", {}, 3000);
			});
	}
}
$('.quantity')
	.data('timeout', null)
	.keyup(function(e){
    	var selected = this.id;
		var selID = selected.match(/[\d\.]+/g);
		var newQ = $('#quantity' + selID).val();
    	if(newQ == 0 && newQ != '') {
    		$('#quantity' + selID).val($('.quantity').data('oldQ'));
    		//alert('you entered 0');
    	}
    	if((e.which < 48 || e.which > 57) && (e.which < 96 || e.which > 105) && (e.which != 8)) {
    		$('#quantity' + selID).val($('.quantity').data('oldQ'));
    		//alert('you entered a non number: ' + e.which);
    	}
    	$('.quantity').data('newQ', newQ);
    	$('.quantity').data('selectedID', selID);
    	clearTimeout($(this).data('timeout'));						// restarts the timer
    	$(this.id).data('timeout', setTimeout('doMath()', 2000));
});