$(document).ready(function(){

    //1237652875123871201293-12470234019274-023-4182-4018-23490182-34018-2390

	var numpics = $("#photoFlipper > img").length;
	
	setTimeout(function() {flipImage(numpics)}, 2000);
	
	$('#BB_BuyButtonForm').submit(function(){
		return validateAmount($('#item_price_1').val());
   	});
	
	$('#RecurringDonationForm').submit(function(){
		$("#item-1_maximum-charge").val($("#item-1_unit-price").val());
		if($("#item1-period").val() == "MONTHLY") {
			$("#item1-times").val($("#ddlHowLong").val()*12);
		} else if($("#item1-period").val() == "QUARTERLY") {
			$("#item1-times").val($("#ddlHowLong").val()*4);
		} else {
			$("#item1-times").val($("#ddlHowLong").val());
		}

	});

    $("#sidebar li>a ").hover(function(){
        $(this).stop().animate({marginLeft : "10px"},200,"swing");},
        function(){
        $(this).stop().animate({marginLeft : "0px"},200,"swing");}
    );

    $(".navigation>.alignleft a,.single_alignleft a").hover(function(){
        $(this).stop().animate({marginLeft : "10px"},200,"swing");},
        function(){
        $(this).stop().animate({marginLeft : "0px"},200,"swing");}
    );

    $(".navigation>.alignright a,.single_alignright a").hover(function(){
        $(this).stop().animate({marginRight : "10px"},200,"swing");},
        function(){
        $(this).stop().animate({marginRight : "0px"},200,"swing");}
    );
});


function flipImage(imageT) {
				var randomVertical = Math.random() * 100 - 50;
				var widestPoint = 200;
				var numImages = $("div.#photoFlipper > img").length;
				
				if ($("#pic-"+imageT).css('z-index') == numImages) {
					movingImage = $("#pic-"+imageT);
					
					// move out
					$("#pic-"+imageT).animate({left: "+=" + widestPoint, top: "+=" + randomVertical}, 300, null, function(){resetZIndex(movingImage);}).animate({left: "-=" + widestPoint, top: "-=" + randomVertical}, {duration: 300, complete: onPhotoFlipped});
				}
				var nextPic = (imageT - 1);
				if(nextPic < 1) {
					nextPic = numImages;
				}
				setTimeout(function(){flipImage(nextPic);}, 3000);
			}

function resetZIndex(movingImage) {			
	var movingID = movingImage.attr("id");
	
	// set z-index of moving image to bottom
	movingImage.css('z-index', 1);
	var numImages = $("#photoFlipper > img").length;
	// move others up
	for (i=0; i < numImages; ++i) {
		image = $('#photoFlipper > img.#pic-' + (i + 1));
		
		if (image.attr("id") != movingID) {
			image.css('z-index', parseInt(image.css('z-index')) + 1);

			if (parseInt(image.css('z-index')) == numImages) {
				regexp = image[0].id.match(/\-(\d+)$/);
				index = regexp[1];
			}
		}
	}
}	

function onPhotoFlipped() {
	
}

function validateAmount(amount){
	if(amount.match( /^[0-9]+(\.([0-9]+))?$/)){
		return true;
	}else{
		alert('You must enter a valid donation.');
		$('#item_price_1').focus();
		return false;
	}
}