// All praise be to jQuery
$(function() {

	$('.photo').removeClass('hide');
	//menu
	$("ul.sf-menu").superfish({ 
			pathClass:  'current',
			autoArrows:false,
			dropShadows:false,
			delay:0
		}); 

	$('#menu ul li').hover(function()
	{	
		if(!$(this).hasClass('subMenu'))
		{
			$("#currentCollection").removeClass('greenTab');
			if($(this).attr('class') != 'png_fix')
			{
				$("#currentSub").removeClass('currentSub');
				$("#currentSub").hide();
				
			}
			$(this).addClass('greenTab');
		}
	},
	function()
	{
		if(!$(this).hasClass('subMenu'))
		{
			$(this).removeClass('greenTab');
			$("#currentCollection").addClass('greenTab');
			if($(this).attr('class') != 'png_fix greenTab')
			{
				$("#currentSub").addClass('currentSub');
			}
		}
	});
	
	//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 = 20;

	//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;
});
	// 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 (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();
	});

}); 


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);
	}

}