$(function()
{
	$.preload('.background img', {
		base: '/images/backgrounds/',
		ext: '.jpg'
	});

	$('.background img').hide();
	
	var newheight = 0;
	var browserheight = $(window).height();
	newheight = browserheight-197;
	if (newheight >= 480)
	{
		var posX = Math.round((newheight/2)-20);
		$('.background').css({'min-height': newheight + 'px','background-position':'50% ' + posX + 'px'});
	}
	
	var i = 0;
	var init = 0;
	$(window).bind('load', function()
	{
		var init = setInterval("" + $(this).bgImage(i, init) + "", 500);
	});
	
	$.fn.loader = '<div class="loader"><img src="/images/standalone/loader.gif" alt="" /></div>';
	
	$.fn.device = {
		iPad: navigator.userAgent.match(/iPad/i) != null
	};
	
	$('#mouse-ico').hide();

	// Side panel navigation
	$('#side-panel li a').click(function()
	{
		$('#expand-side-panel').append($.fn.loader).find('.loader').css({'top': 150, 'left': 144, 'position':'absolute'});
		
		$('#side-panel').find('a').each(function()
		{
			if ($(this).hasClass('on'))
			{
				$(this).removeClass('on');
				Cufon.replace('.custom-light', { fontFamily: 'DINLight', hover: { color: '#ffffff' } });
				Cufon.now();
			}
		});
	
		var link = $(this).attr('href'),
			linkSplit = link.split('/'),
			linkURL = '/' + linkSplit[3] + '/' + linkSplit[4];
		if (link !== '#')
		{
			$(this).addClass('on');
			$('#side-panel ul#side-panel-navigation').stop().delay(200).animate({'left':'320px'}, 600, 'easeInOutExpo');
			
			$('#side-panel-content').hide();
			var $loader = $('.loader');
				$loader.animate({'opacity' : 1 }, (($.fn.device.iPad) ? 0 : 550), function () {
					$loader.remove();
					$('#side-panel-content').load(linkURL).fadeIn();
				});
			
			
			$('#expand-side-panel').stop().delay(200).animate({'left':'0px'}, 600, 'easeInOutExpo').addClass('expand');
		}
		return false;
	});

	$('.trigger').click(function()
	{
		var link = $(this).attr('href');
		var path = link.split('#');
		var body = $('body').attr('id');
		$('.mailing-list').addClass('on');
		$('#side-panel ul#side-panel-navigation').stop().delay(200).animate({'left':'320px'}, 600, 'easeInOutExpo');
		$('#side-panel-content').load(path[1]);
		$('#expand-side-panel').stop().delay(200).animate({'left':'0px'}, 600, 'easeInOutExpo').addClass('expand');
		if (body === 'home')
		{
			return false;
		}
	});
	
	$('.action, #action').click(function()
	{
		var link = $(this).attr('href');
		var path = link.split('#');
		var body = $('body').attr('id');
		$('.collection').addClass('on');
		$('#side-panel ul#side-panel-navigation').stop().delay(200).animate({'left':'320px'}, 600, 'easeInOutExpo');
		$('#side-panel-content').load(path[1]);
		$('#expand-side-panel').stop().delay(200).animate({'left':'0px'}, 600, 'easeInOutExpo').addClass('expand');
		if (body === 'home')
		{
			return false;
		}
	});
	
	$('.action1, #action2').click(function()
	{
		var link = $(this).attr('href');
		var path = link.split('#');
		var body = $('body').attr('id');
		$('.lgc').addClass('on');
		$('#side-panel ul#side-panel-navigation').stop().delay(200).animate({'left':'320px'}, 600, 'easeInOutExpo');
		$('#side-panel-content').load(path[1]);
		$('#expand-side-panel').stop().delay(200).animate({'left':'0px'}, 600, 'easeInOutExpo').addClass('expand');
		if (body === 'home')
		{
			return false;
		}
	});
	
	var pathname = window.location.hash;
	if (pathname === '#mailing_list/side_bar')
	{
		var link = pathname.split('#');
		$('.mailing-list').addClass('on');
		$('#side-panel ul#side-panel-navigation').stop().delay(200).animate({'left':'320px'}, 600, 'easeInOutExpo');
		$('#side-panel-content').load(link[1]);
		$('#expand-side-panel').stop().delay(200).animate({'left':'0px'}, 600, 'easeInOutExpo').addClass('expand');
	}
	if (pathname === '#collection/side_bar')
	{
		var link = pathname.split('#');
		$('.collection').addClass('on');
		$('#side-panel ul#side-panel-navigation').stop().delay(200).animate({'left':'320px'}, 600, 'easeInOutExpo');
		$('#side-panel-content').load(link[1]);
		$('#expand-side-panel').stop().delay(200).animate({'left':'0px'}, 600, 'easeInOutExpo').addClass('expand');
	}
	
	
	$('body').click(function(e)
	{
		if ($('#expand-side-panel').hasClass('expand'))
		{
			var $t = $(e.target);
			var search = $t.is('div#search');
			if (search === true)
			{
				return false;
			}
			if ($t.is('div:not(.expand)') || $t.is('img:not(.expand)') || $t.is('a:not(.expand)'))
			{
				$('#side-panel li a').removeClass('on');
				$('#side-panel ul#side-panel-navigation').stop().delay(200).animate({'left':'0px'}, 600, 'easeInOutExpo');
				$('#expand-side-panel').stop().delay(200).animate({'left':'-330px'}, 600, 'easeInOutExpo').removeClass('expand');
				Cufon.replace('.custom-light', { fontFamily: 'DINLight', hover: { color: '#ffffff' } });
				Cufon.now();
			}
		}
	});
	
	/*$('#container').waypoint(function(event, direction) {
		//$('.top').toggleClass('hidden', direction === "up");
	}, {
		offset: '-100%'
	}).find('#our-collection-heading').waypoint(function(event, direction) {
		$(this).parent().toggleClass('sticky', direction === "down");
		event.stopPropagation();
	});*/

	
	$('#video a').click(function()
	{
		$('.mask-container').stop().delay(200).animate({marginLeft: -640}, 600, 'easeInOutExpo');
		return false;
	});
	
	$('.close').click(function()
	{
		$('.mask-container').stop().delay(200).animate({marginLeft: 0}, 600, 'easeInOutExpo');
		return false;
	});
	
	$('#sizing-chart a').
		bind('expand', function(e, $sourceElement)
		{
			if ($sourceElement) 
			{
				$sourceElement.removeClass().addClass('collapse').css({'background-position':'0 -1086px'});
				$sourceElement.parent().stop().delay(200).animate({'height':'540px'}, 600, 'easeInOutExpo');
			}	
		}).
		bind('collapse', function(e, $sourceElement)
		{
			if ($sourceElement) 
			{
				$sourceElement.removeClass().addClass('expand').css({'background-position':'0 -1022px'});
				$sourceElement.parent().stop().delay(200).animate({'height':'63px'}, 600, 'easeInOutExpo');
			}
		});
	
	$('ul#category_nav a.custom-light').click(function()
	{
		if($(this).parent().hasClass('current')) 
		{
			$(this).siblings('ul').slideUp('fast',function() 
			{
				$(this).parent().removeClass('current');
			});
		}
		else 
		{
			$('ul#category_nav li.current ul').slideUp('fast',function() 
			{
				$(this).parent().removeClass('current');
			});
			$(this).siblings('ul').slideToggle('fast',function() 
			{
				$(this).parent().toggleClass('current');
			});
		}
		return false;
	});
	
	$('ul#category_nav').find('.on').each(function()
	{
		$(this).parent().parent().addClass('current');
	});
	
	$('#sizing-chart a').click(function(e)
	{
		var $t = $(e.target);
		if ($t.is('a:not(.disabled)')) 
		{
			$(this).trigger($t.attr('class'), [$t]);
		}
		return false;
	});
	
	$('.close').tipsy({gravity: 'e', fade: true});
	
	$("input[type$='text']").focus(function() 
	{
		if( this.value === this.defaultValue ) 
		{
			this.value = "";
		}
	}).blur(function() 
	{
		if( !this.value.length ) 
		{
			this.value = this.defaultValue;
		}
	});
	
	$("textarea").focus(function() 
	{
		if( this.value === this.defaultValue ) 
		{
			this.value = "";
		}
	}).blur(function() 
	{
		if( !this.value.length ) 
		{
			this.value = this.defaultValue;
		}
	});
	
	$.preloadCssImages();
});

(function($)
{
	jQuery.fn.bgImage = function(i, init)
	{
		var thisimg = $('.background').children('img');
		var imgs = $('img').length;
		var dimensions = $(this).calcSize($(thisimg).width(), $(thisimg).height());
		if (i >= imgs) 
		{
			clearInterval(init);
		}
		$(this).bgTransition(thisimg, dimensions);
		i++;
		
		$(window).bind('resize', function() 
		{
			$(this).bgResize();
		});
	};
	
	jQuery.fn.bgTransition = function(thisimg, dimensions)
	{
		var newwidth = dimensions[0];
		var newheight = dimensions[1];
		var newleft = dimensions[2];
		var newtop = dimensions[3];

		$(thisimg).css('width', newwidth + 'px').css('height', newheight + 'px').css('left', newleft + 'px').fadeIn(500);
		$('.background').css({'min-height': newheight + 'px'});
		
		// Mouse drag and scrolling background
		$('.scroll').overscroll();
		$('.scroll').mousemove(function(e)
		{
			$('#mouse-ico').show();
			$('#mouse-ico').css({top: "40%", left: (e.pageX + 15) + "px"});
		}).mouseleave(function()
		{
			$('#mouse-ico').hide();
		});
		
		function hideDrag()
		{
			$('#mouse-ico').hide();
		}
	};

	jQuery.fn.bgResize = function()
	{
		var thisimg = $('.background img');
		
		var dimensions = $(this).calcSize($(thisimg).width(), $(thisimg).height());
		var newwidth = dimensions[0];
		var newheight = dimensions[1];
		var newleft = dimensions[2];
		var newtop = dimensions[3];

		if (newheight >= 720)
		{
			$(thisimg).css('width', newwidth + 'px');
			$(thisimg).css({'height': newheight + 'px'});
			$(thisimg).css('left', newleft + 'px');
			$('.background').css({'min-height': newheight + 'px'});
		}
	};

	jQuery.fn.calcSize = function(imgw, imgh)
	{
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		
		var ratio = imgh / imgw;

		var newheight = 0; var newwidth = 0;
		if ((browserheight / browserwidth) > ratio) 
		{
			newheight = browserheight-197;
			newwidth = Math.round((browserheight-197) / ratio);
		}
		else 
		{
			newheight = Math.round(browserwidth * ratio);
			newwidth = browserwidth;
		}
		
		// calculate new left and top position
		var newleft = Math.round((browserwidth - newwidth) / 2);
		var newtop = Math.round((browserheight - newheight) / 2);
		
		var rcarr = [newwidth, newheight, newleft, newtop];
		return rcarr;
	};
})(jQuery);

