// All praise be to jQuery
$(function() {
	
// initialize scrollable  
    $("div.scrollable").scrollable({ 
        size: 3, 
        items: '#thumbs',   
        hoverClass: 'hover',
		next: 'div.next', 
		prev: 'div.prev'
    });   

	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	// Highlight current menu item
	$("#menu").find("a").each(function() {
		if ("/"+$(this).attr("href")==window.location.pathname || $(this).attr("href")==window.location.href) {
			$(this).addClass("active");
		}
	});

	// Confirm certain link clicks
	$(".confirm").click(function(e) {
		return(confirm("Really?"));
	});

	// Date Inputs
	$('.date_input').each(function() {
		$(this).date_input();
	});

	// Apply delete links
	$(".delete").click(attachAjaxDeleteEvent);

	// Focus on username in login form
	$("#loginForm").find("#user_name").focus();

	// Form validation
	$("form").submit(function() {
		var alertMessage = "";
		$(this).find(".required").each(function() {
			if ($(this).val()=="") {
				alertMessage += $(this).attr("rel")+" is required\n";
			}
		});
		if ($(this).find("#re_password").length!=0 && $(this).find("#re_password").val()!=$(this).find("#password").val()) {
			alertMessage += "Passwords do not match\n";
		}
		if ($(this).find("#re_password").length!=0 && $(this).find("#re_password").val()!=$(this).find("#password").val()) {
			alertMessage += "Passwords do not match\n";
		}
		if ($(this).find("input[name=paymentMethod]").length && $(this).find("input[name=paymentMethod]:checked").length==0) {
			alertMessage += "You must select a Payment Type\n";
		}
		if ($(this).find("input[name=postageOption]").length && $(this).find("input[name=postageOption]:checked").length==0) {
			alertMessage += "You must select a Postage Method\n";
		}
		if ($(this).find(".iAgree").length != $(this).find(".iAgree:checked").length) {
			alertMessage += "You must agree to our Terms and Conditions\n";
		}
		if ($(this).find("input[name=paymentMethod]:checked").val()=="Credit Card") {
			if ($("#cardType").val()=="") alertMessage += "Card Type is required\n";
			if ($("#cardNumber").val()=="") alertMessage += "Card Number is required\n";
			if ($("#cardExpiryMonth").val()=="") alertMessage += "Card Expiry Month is required\n";
			if ($("#cardExpiryYear").val()=="") alertMessage += "Card Expiry Year is required\n";
			if ($("#cardName").val()=="") alertMessage += "Name on Card is required\n";
		}
		if (alertMessage.length) {
			alert(alertMessage);
			return false;
		}
		return true;
	});

	$("#loginLink").click(function(e) {
		e.preventDefault();
		$("#loginForm").submit();
	});
	$("#newsletterSubscribeLink").click(function(e) {
		e.preventDefault();
		alert("Subscribe to newsletter here....");
	});

	// Cart quantity update
	$("#updateCart").click(function(e) {
		e.preventDefault();
		$("#cartForm").submit();
	});

	// Cart item removal
	$(".removeItemFromCart").click(function(e) {
		e.preventDefault();
		$($(this).siblings("input")[0]).val(0);
		$("#cartForm").submit();
	});

	// Checkout, accordian information display for Postage Options
	$("#postageOptions,#paymentOptions").find("input[type=radio]").each(function() {
		$(this).click(function() {
			$("#postageOptions,#paymentOptions").find("input[type=radio]").each(function() {
				$(this).parent().siblings("div").slideUp(200);
			});			
			$(this).parent().siblings("div").slideDown(200);
		});
	});
	$("#sameAsAbove").click(function() {
		if ($(this).attr('checked')) {
			//$("#deliveryAddressDetails").slideUp(200);
			$("input[type=text],select").each(function() {
				if ($(this).attr("rev")) {
					$(this).val($("input[name="+$(this).attr("rev")+"]").val());
				}
			});
		}
		else {
			//$("#deliveryAddressDetails").slideDown(200);
		}
	});
	
	// james gallery script?

	//slideshow 
	$("#main_img").cycle({
		fx: 'fade',
		before: onBefore
	});

	function onBefore(){
		$("#img_tag").html(this.alt);
	}
	
	//sliding thumb menu
	next_item = 1;
	scroll_left = 0;
	scroller = false;
	max_left=0;

	itemsWidth = 0;
	magicNumber = 25;

	//get the length of the list
	$(this).find(".item_div").each(function()
	{	
		itemsWidth += parseInt($(this).attr('rel')) + magicNumber;
	});

	//max distance to the left the list can go before going past last item
	max_left -= (itemsWidth-$('#smallim').width());

	$('.thumb').hover(function(){
		$(this).addClass('fade');
		$('#main_img').cycle({
		timeout: 0
		});
		
		var img = '<img src="images/gallery_photos/cropped/' + $(this).attr('rel');

		var portraits = 'no';

		if($(this).attr('rev')=='109'||$(this).attr('rev')=='110' || portraits=='no')
		{
			img += '"/>';
		}
		else
		{
			img += '"style="padding-left:121px;"/>';
		}
		$('#main_img').html(img);
		$('#img_tag').html($(this).attr('alt'));
		noSlide = true;
	},      
		function () {$(this).removeClass('fade');}
	);

	$("#leftArrow").hover(function(){
		scroller = true;

		if(scroll_left<=0 && scroller)
		{
			$('#items').css('left', scroll_left);
			scroll_left = scroll_left + 10;
			setTimeout('scrollLeft()', 50);
		}

	},
		function(){
		scroller = false;
	});

$("#rightArrow").hover(function(){
	scroller = true;
	
	if(scroll_left>=max_left && scroller)
	{
		$('#items').css('left', scroll_left);
		scroll_left = scroll_left - 10;
		setTimeout('scrollRight()', 50);
	}

},
	function(){
	scroller = false;
});

	$('.scrollPane').jScrollPane({showArrows:true, scrollbarWidth:11, scrollbarMargin: 20});  
}); 


function attachAjaxDeleteEvent(e) {
	e.preventDefault();
	var parentRow;
	//TODO: tr/li branch untested!
	if ($(this).parents("tr")[0]) {
		parentRow = $(this).parents("tr")[0];
	}
	else if ($(this).parents("li")[0]) {
		parentRow = $(this).parents("li")[0];
	}
	if (confirm("Really Delete?")) {
		$.get($(this).attr("href"), function(d) {
			if (d=="Success") {
				$(parentRow).hide();
			}
			else alert("UNEXPECTED RESPONSE FROM SERVER: "+d);
		});
	}
}

function attachAjaxReactivateEvent(e) {
	e.preventDefault();
	var parentRow;
	//TODO: tr/li branch untested!
	if ($(this).parents("tr")[0]) {
		parentRow = $(this).parents("tr")[0];
	}
	else if ($(this).parents("li")[0]) {
		parentRow = $(this).parents("li")[0];
	}
	if (confirm("Reactivate?")) {
		$.get($(this).attr("href"), function(d) {
			if (d=="Success") {
				$(parentRow).hide();
			}
			else alert("UNEXPECTED RESPONSE FROM SERVER: "+d);
		});
	}
}

function attachAjaxDialogEvents() {
	$(".closeDialog").click(function(e) {
		e.preventDefault();
		$("#ajaxDialog").fadeOut(500);
	});
}



function scrollLeft()
{
if(scroll_left<=0 && scroller)
	{
		$('#items').css('left', scroll_left);
		scroll_left = scroll_left + 10;
		setTimeout('scrollLeft()', 50);
	}

}

function scrollRight()
{
if(scroll_left>=max_left && scroller)
	{
		$('#items').css('left', scroll_left);
		scroll_left = scroll_left - 10;
		setTimeout('scrollRight()', 50);
	}

}
