/*
 * Slideshow Functions
 */
 
$(window).load(function()
{
	//this is intended to wait for all the images to load before running the slideshow
	init_slideshow()
})

init_slideshow = function()
{
	if ($('#slides').length) {
		$('#slides').cycle({
			fx:'fade',
			timeout:8000,
			pager:'#slide_navigation',
			after:update_slide_caption,
			before:fade_slide_caption
		});
	}
}

fade_slide_caption = function(next, previous)
{
	caption_container = $('#project_caption')
	caption_container.fadeOut('fast')
}

update_slide_caption = function(next, previous)
{
	caption_container = $('#project_caption')

	caption = $('span.slide_caption', previous)
	caption_container.fadeIn('fast')
	caption_container.html(caption.html())
	
}



/** new checkout **/

jQuery(function($) {
	
	/* cart page */
	var deleteButton = false;
	var prevValue;
	$("select.quantityUpdater").bind({
		change: function() {
			if ($(this).val() == 0 && deleteButton == false) {
				if(confirm("Are you sure you want to remove the item?")) {
					$("#cart").submit();
				} else {
					$(this).val(prevValue);
					return false;
				}
			} else {
				$("#cart").submit();
			}
		},
		focus: function(event) {
			prevValue = $(this).val();
			$(this).unbind(event);
		}
	});
	
	$("a.deleteLink").click(function(e) {
		if(confirm("Are you sure you want to remove the item?")) {
			deleteButton = true;
			$(this).closest(".table-row").find("select.quantityUpdater").val("0").change();
		}
		e.preventDefault();
	});
	
	/** Confirm Page **/
	
	$("a#login").click(function(e) {
		var url = this.href;
        var dialog = $('<div id="loginform" style="display:hidden"></div>').appendTo('body');
        
        dialog.load(
			url,
			function (responseText, textStatus, XMLHttpRequest) {
				dialog.dialog({
					height: 300,
					width:450,
					modal: true
				});
			}
		);
        e.preventDefault();
		return false;
    });
	
	
	$("form.ajax").live("submit", function(e) {
		var $f = $(this);
		$.post($f.attr("action"),$f.serialize(),
			function(data) {
				if(data.success) {
					$("#loginform").dialog("close");
					window.location.href= window.location;
				} else {
					$(".ajax-error").html(data.error).fadeIn();
				}
			},
			"json"
		);
		e.preventDefault();
		return false;
	});
	
	
	var cacheKiller = function() {
		return "time="+new Date().getTime();
	}
	
	var DDMCheckout = (function($) {
		return {
			init: function() {
				$(DDMCheckout.form).live("change", function() {
					DDMCheckout.updateData();
				}).change().submit(function() {
					$.ajaxSetup({async:false});
					DDMCheckout.updateData();
				});
			},
			
			form: $("form#confirm"),
			
			updateData: function() {
				$.post("/checkout/ajax_update?"+cacheKiller(), $(DDMCheckout.form).serialize(), function(data) {
					if(!$.isEmptyObject(data)) {
						$.each(data.callback, function(i,callback) {
							if($.isFunction(DDMCheckout[callback])) {
								DDMCheckout[callback]();
							}
						});
					};
				}, "json");
			},
			
			reloadPage: function() {
				window.location.href = window.location;
			},
			
			updateZone: function() {
				$("#zone").load("/checkout/ajax_zone?"+cacheKiller(), function() {
					var $field = $(this).find("select");
					$field.val($field.children().first());
					DDMCheckout.updateData();
				});
				
				
			},
			
			updateShipping: function() {
				$("#shipping").load("/checkout/ajax_shipping?"+cacheKiller());
			},
			
			updateSummary: function() {
				
				$("#summary").load("/checkout/ajax_summary?"+cacheKiller());
			},
			
			updatePayment: function() {
				$("#payment").load("/checkout/ajax_payment?"+cacheKiller());
			},
			
			updateCoupon: function() {
				$("#coupon-status").load("/checkout/ajax_coupon?"+cacheKiller());
			}
		}
		
	})(jQuery, this);
	
	DDMCheckout.init();
	
});

