$.fn.imagesLoaded = function(callback)
{
	// mit license. paul irish. 2010.
	// webkit fix from Oren Solomianik. thx!

	var elems = this.filter('img'), len = elems.length;
	elems.bind('load', function()
	{
		if(--len <= 0)
		{
			callback.call(elems,this);
		}
	}).each(function()
	{
		// cached images don't fire load sometimes, so we reset src.
		if(this.complete || this.complete === undefined)
		{
			var src = this.src;
			// webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
			// data uri bypasses webkit log warning (thx doug jones)
			this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
			this.src = src;
		}
	});

	return this;
};

function centerProductImages(all)
{
	$("#content .product span.image img").imagesLoaded(function()
	{
		$("#content .product span.image img").each(function()
		{
			var height1 = $(this).height();
			var height2 = $(this).parent().height();
			var top = parseInt((height2 - height1) / 2, 10);
			$(this).css("marginTop", top + "px");
		});
	});
}

function setupAjaxForm(id)
{
	$("#" + id).submit(function()
	{
		$(this).find(".button").attr("disabled", true);

		var textboxes = $(this).find(".text");
		var checkboxes = $(this).find("input[type=checkbox]");
		var selectboxes = $(this).find("select");
		var options = { submit: 1 };

		for(var i = 0; i < textboxes.size(); i++)
		{
			var element = textboxes.get(i);
			options[$(element).attr("name")] = $(element).val();
		}

		for(var i = 0; i < selectboxes.size(); i++)
		{
			var element = selectboxes.get(i);
			options[$(element).attr("name")] = $(element).val();
		}

		for(var i = 0; i < checkboxes.size(); i++)
		{
			var element = checkboxes.get(i);

			if(!$(element).attr("checked"))
			{
				continue;
			}

			var name = $(element).attr("name");
			var pos = name.indexOf("[");

			if(pos != -1)
			{
				name = name.substr(0, pos);

				if(options[name] == undefined)
				{
					options[name] = [ ];
				}

				options[name].push($(element).val());
			}
			else
			{
				options[name] = $(element).val();
			}
		}

		$.post($(this).attr("action"), options, function(data)
		{
			if(data)
			{
				$("#" + id).find(".button").attr("disabled", false);
				$("#contact-message").html(data).show();
			}
			else
			{
				$("#" + id).fadeOut(300, function()
				{
					$("#" + id + "-success").fadeIn(300);
					$("#" + id).find(".button").attr("disabled", false);
				});
			}
		});

		return false;
	});
}

$(document).ready(function()
{
	setupAjaxForm("contact-form");

	$("#content.home .sidebar img").imagesLoaded(function()
	{
		var height1 = $("#content.home .left.sidebar").height();
		var height2 = $("#content.home .right.sidebar").height();
		var height = Math.max(height1, height2);
		$("#content.home .main").css("min-height", height + "px");
	});

	$("#menu li").hover(function()
	{
		$(this).find(".submenu").fadeIn(300);
	}, function()
	{
		$(this).find(".submenu").fadeOut(300);
	});

	if($(".carousel").size() > 0)
	{
		$(".container").jCarouselLite(
		{
			btnNext: ".next",
			btnPrev: ".prev",
			circular: true,
			visible: 5
		});
	}

	if($(".banners .container").size() > 0)
	{
		$(".banners .container").jCarouselLite(
		{
			circular: true,
			visible: 1,
			auto: 5000,
			speed: 1000
		});
	}

	$("#content .left.sidebar ul li ul.visible").addClass("visible2");

	$("#top .search .text, #bottom .column form .text").focus(function()
	{
		if(!$(this).hasClass("clicked"))
		{
			$(this).addClass("clicked").val("");
		}
	});

	$("#content .left.sidebar ul li a.link").click(function(event)
	{
		event.preventDefault();

		var submenu = $(this).parent().find("ul");

		if(submenu.hasClass("visible2"))
		{
			submenu.removeClass("visible2").slideUp(500);
		}
		else
		{
			submenu.addClass("ignore");

			$("#content .left.sidebar ul li ul").each(function()
			{
				if(this == submenu.get(0))
				{
					$(this).addClass("visible2").slideDown(500);
				}
				else
				{
					$(this).removeClass("visible2").slideUp(500);
				}
			});
		}
	});

	if($("#content .main.prod .images").size() > 0)
	{
		$("#content .main.prod .images .enlarge").lightBox();
	}

	$("#content .toggle-reviews").click(function(event)
	{
		event.preventDefault();

		if($(this).hasClass("visible2"))
		{
			$(this).removeClass("visible2");
			$("#content .review-intro").slideDown();
			$("#content .reviews").slideUp();
		}
		else
		{
			$(this).addClass("visible2");
			$("#content .review-intro").slideUp();
			$("#content .reviews").slideDown();
		}
	});

	/*$("#content .product span.image img").each(function()
	{
		var height1 = $(this).height();
		var height2 = $(this).parent().height();
		var top = parseInt((height2 - height1) / 2, 10);
		$(this).css("marginTop", top + "px");
	});*/

	centerProductImages();

	/*var height1 = $("#content.home .sidebar.left").height();
	var height2 = $("#content.home .sidebar.right").height();
	var height3 = $("#content.home .main").height();

	var height = Math.max(height1, height2, height3);

	$("#content.home .main").css("minHeight", height + "px");*/
});
