$( function()
{
	$("ul#nav li a").css("zoom", 1);
	
	Cufon.replace("#hero");
	Cufon.replace("div.article h2");
	Cufon.replace("ul#nav > li > a",
	{
		hover:
		{
			color: "#ffffff"
		}
	});
	
	$("ul.thumbs").eq(0).children("li").children("a").attr("rel", "prettyPhoto").prettyPhoto({ theme: 'dark_rounded'});
	$("ul.thumbs").eq(1).children("li").children("a").attr("rel", "prettyPhoto").prettyPhoto({ theme: 'dark_rounded'});
	$("ul.gallery").children("li").children("a[id!='noPretty']").attr("rel", "prettyPhoto[gallery]").prettyPhoto({ theme: 'dark_rounded'});
	
	$("ul#nav li:not(:animated)").hover( function()
	{
		$(this).addClass("current").children("ul").slideDown(100);
	}, function()
	{
		$(this).removeClass("current").children("ul").fadeOut(150, function()
		{
			
		});
		
		var timer = setInterval( function()
		{
			Cufon.refresh("ul#nav > li > a");
			clearInterval(timer);
		}, 4);
	});

	function px2int(value)
	{
		if (value != '' && typeof value == 'string')
			return parseInt(value.replace("px", ""));
		else if (typeof value == 'number')
			return value;
		else
			return 0;
	}
		
	// shop box config
	var sb =
	{
		speed: 400,
		wrapper_class: "shop-box-wrapper",
		visible: 1,
		scroll: 1,
		
		data:
		{
			items: 0,
			index: 0,
			items_container: Array(),
			pages: 0,
			margin: 0,
			padding: 0,
			scroll_size: 0,
			item_width: 0
		},
		
		tag:
		{
			container: "div.shop-box ul.items",
			item: "div.shop-box ul.items li",
			next: "ul.shop-nav li.next",
			prev: "ul.shop-nav li.prev",
			wrapper: "div.shop-box div." + this.wrapper_class
		}
	};
	
	sb.data.items = $(sb.tag.item).length;
	sb.data.item_width = px2int($(sb.tag.item).css("width"));
	sb.data.margin = px2int($(sb.tag.item).css("margin-left")) + px2int($(sb.tag.item).css("margin-right"));
	sb.data.padding = px2int($(sb.tag.container).css("padding-left")) + px2int($(sb.tag.container).css("padding-right"));
	sb.data.scroll_size = (sb.data.item_width + sb.data.margin) * sb.scroll;
	sb.data.pages = Math.ceil(sb.data.items/sb.visible);

	$(sb.tag.item).each( function()
	{
		sb.data.items_container.push($(this).html());
	});
	
	$(sb.tag.container).wrap( $("<div />").addClass(sb.wrapper_class).css(
	{
		"overflow": "hidden",
		"position": "relative",
		"height": "300px"
	}));
	
	$(sb.tag.container).css(
	{
		"position": "absolute",
		"width": ((sb.data.item_width + sb.data.margin) * sb.data.items) + sb.data.padding
	});
	
	if (sb.data.items > 1)
	{
		$(sb.tag.item).remove();
		$(sb.tag.container).append($("<li />").html(sb.data.items_container[sb.data.index]));
		
		$(sb.tag.next).click( function()
		{
			if ($(sb.tag.item + ":not(:animated)"))
			{
				var next = 0;
				
				if (sb.data.index < sb.data.pages - 1)
				{
					next = sb.data.index + 1;
				} else
				{
					next = 0;
				}

				$(sb.tag.container).append($("<li />").html(sb.data.items_container[next]));
				
				$(sb.tag.container).animate({
					"left": "-=" + (sb.data.scroll_size * sb.scroll)
				}, sb.speed, function()
				{
					$(sb.tag.item).eq(0).remove();
					$(sb.tag.container).css("left", "0px");
					sb.data.index = next;
				});
			}
			
			return false;
		});
		
		$(sb.tag.prev).click( function()
		{
			if ($(sb.tag.item + ":not(:animated)"))
			{
				var prev = 0;
				
				if (sb.data.index > 0)
				{
					prev = sb.data.index - 1;
				} else
				{
					prev = sb.data.items - 1;
				}

				$(sb.tag.container).prepend($("<li />").html(sb.data.items_container[prev]));
				$(sb.tag.container).css("left", -(sb.data.scroll_size * sb.scroll) + "px");
				
				$(sb.tag.container).animate({
					"left": "+=" + (sb.data.scroll_size * sb.scroll)
				}, sb.speed, function()
				{
					$(sb.tag.item).eq(1).remove();
					sb.data.index = prev;
				});
			}
			
			return false;
		});
	} else
	{
		$(sb.tag.next + "," + sb.tag.prev).remove();
	}
	
	$("div.sub-box-b form").clone().addClass("newsletter-form").appendTo("body");
	
	$("form.newsletter-form").css(
	{
		"left": $(window).width()/2 - $("form.newsletter-form").width()/2,
		"top": "0px",
		"position": "absolute",
		"z-index": 1337
	}).before( $("<div />").attr("id", "newsletter-wrapper").css(
	{
		"position": "absolute",
		"z-index": 1336,
		"left": "0px",
		"top": $(window).scrollTop(),
		"width": $(window).width(),
		"height": $(window).height(),
		"background": "#000",
		"opacity": 0.8
	}).hide());
	
	$("div.sub-box-b form").remove();
	
	$("#newsletter-wrapper").click( function()
	{
		$(this).fadeOut();
		$("form.newsletter-form").fadeOut();
	});
	
	$("form.newsletter-form fieldset a.close").click( function()
	{
		$("#newsletter-wrapper").fadeOut();
		$("form.newsletter-form").fadeOut();
	});
	
	$("div.sub-box-b h2.newsletter-signup").click( function()
	{
		$("form.newsletter-form").css(
		{
			"top": $(window).scrollTop() + ($(window).height()/2 - $("form.newsletter-form").width()/2)
		}).fadeIn(500);
		$("#newsletter-wrapper").css(
		{
			"top": $(window).scrollTop()
		}).fadeIn(500);
		
		return false;
	});
	
	$(window).scroll( function()
	{
		$("form.newsletter-form:visible").css(
		{
			"top": $(window).scrollTop() + ($(window).height()/2 - $("form.newsletter-form").width()/2)
		});
		$("#newsletter-wrapper:visible").css(
		{
			"top": $(window).scrollTop()
		});
	});
});
