var mainSubmenuVisible = false;
var productSubmenuVisible = false;
var activeMainMenu;

var footerMinMargin = 40;
var footerMaxMargin = 100;
var footerMinHeight = 60;
var footerMaxHeight = 120;
var footerDurationIn = 400;
var footerEaseIn = 'easeOutCubic';
var footerDurationOut = 500;
var footerEaseOut = 'easeOutQuad';
var footerAnimate = true;
var footerSliding = false;
var boxVisible = false;
var winHeight = $(window).height();

var oldBlogContent;


$(document).ready(function() {

	setTimeout("repositionElements(true)", 300);

	if($('.body-scrollable').length > 0) {
		$('#body-image').css('position', 'fixed');
	}

	$(window).resize(function() {
		repositionElements(false);
	});
	
	$('.footer-img').hover(function(e) {
		showBox($(this).parent());
	}, function(e) {
		hideBox($(this).parent());
	});
	
	$('#footer-left').hover(function() {
		animateFooter('right');
	}, function() {
		animateFooter('stop');
	});
	$('#footer-right').hover(function() {
		animateFooter('left');
	}, function() {
		animateFooter('stop');
	});
	// HIDE LEFT ARROW INITIALLY
	$('#footer-left').css('background-position-y', -1000);
	
	$('#main-menu .menu-item').hover(function() {
		hideProductSubmenus();
		if(this != activeMainMenu) {
			hideMainSubmenus();
		}
		if($(this).children().length > 1) {
			activeMainMenu = this;
			showMainSubmenu(this);
		}
	}, function() {
		if(this != activeMainMenu) {
			hideMainSubmenus();
		}
	});
	
	$('#shop-this-look').hover(function(e) {
		hideMainSubmenus();
		if(!$(this).hasClass('active')) {
			$(this).addClass('active');
		} else {
			$(this).removeClass('active');
		}
		showProductSubmenu();
	},
	function(e){
		//setTimeout(hideProductSubmenus, 500);
	});
	
	$('.recent-item a').hover(function() {
		$('span', this).fadeIn('slow');
	}, function() {
		$('span', this).fadeOut('slow');
	});
	
	// FADE IN PRODUCT IMAGE
	if($("#body-image").length > 0){
		setTimeout(function() {
			$("#body-image").animate({opacity: 1}, 'slow');
		}, 800);
	}
	
	// FADE IN PRODUCT INFO
	if($("#product-info").length > 0){
		$("#product-info").fadeOut();
		$("#product-info").fadeIn(500);
	}
	
	// FADE IN PRODUCT/PRESS DOCK
	if($("#footer-items").length > 0){
		setTimeout(function(){ $("#footer-container").css('background', 'url(/images/footer-bg.png) repeat-x bottom left'); }, 10);
		$(".footer-img").each(function(index){
			$(this).css('margin-top', 160);
			$(this).delay(index * 200).animate({marginTop: footerMaxMargin}, 1000, 'easeOutBack');
		});
	}
	
	// FADE IN RECENTLY VIEWED
	if($("#recently-viewed").length > 0){
		$("#recently-viewed").hide();
		$("#recently-viewed").delay(500).fadeIn(1500);
	}
	
/*
	$('a.lightbox').lightBox({
		imageLoading: '/images/lightbox-ico-loading.gif',
		imageBtnClose: '/images/lightbox-btn-close.gif',
		imageBtnPrev: '/images/lightbox-btn-prev.gif',
		imageBtnNext: '/images/lightbox-btn-next.gif'}
	);
*/
	
	
	//BLOG LINKS
	$("a.blog-link").click(function(event){ showBlog(event); });
	
	//WHERE TO BUY LINKS
	$("a.wtb").hover(function(){
		$(this).data('t', $(this).attr('title'));
		$("#wtb-desc").text($(this).data('t'));
		$(this).attr('title', '');
	},
	function(){
		$("#wtb-desc").empty();
		$(this).attr('title', $(this).data('t'));
	});

});

function showBox(element) {
	if(element.hasClass('footer-press')){
		$.cookie('footer-press', null);
		$.cookie('footer-press', $('#footer-items-slide').css('left'));
	} else {
		$.cookie('footer-products', null);
		$.cookie('footer-products', $('#footer-items-slide').css('left'));
	}
	boxVisible = true;
	$('#footer-container').css('z-index', 30);
	$('#view-all').css('z-index', 0);
	element.children('.footer-img').stop().animate({height: footerMaxHeight, 'margin-top': footerMinMargin}, footerDurationIn, footerEaseIn, function() { $('#footer-container').css('z-index', 30); });
	element.children('.footer-title').stop().show().animate({opacity: 1}, footerDurationIn * 1.5);
}

function hideBox(element) {
	$(element).children('.footer-img').stop().animate({height: footerMinHeight, 'margin-top': footerMaxMargin}, footerDurationOut, footerEaseOut, function() { boxVisible = false; });
	$(element).children('.footer-title').stop().show().animate({opacity: 0}, footerDurationOut * 1.5, function() {
		$(element).children('.footer-title').hide();
		if(!boxVisible) {
			$('#footer-container').css('z-index', 10);
			$('#view-all').css('z-index', 10);
		}
	});
}

function animateFooter(action) {
	if(action != "stop" && !footerSliding) {
		footerAnimate = true;
		footerSliding = true;
		animateFooterSlide(action);
	} else {
		footerAnimate = false;
		footerSliding = false;
	}
}


function animateFooterSlide(action) {
	var speed = 2;
	if ( $.browser.msie )
		speed = 10;
	if ( $.browser.mozilla )
		speed = 5;
		
	var delay = 1;
	var position = $('#footer-items-slide').position();
	if(action == 'left' && position.left >= -($('#footer-items-slide').width() - $('#footer-items').width())) {
		$('#footer-left').css('background-position-y', '100%');
		if($.browser.mozilla)
			$('#footer-left').css('background-position', '100% 100%');
		$('#footer-items-slide').css('left', position.left*1 - speed);
	} else if(action == 'right' && position.left <= 0) {
		$('#footer-right').css('background-position-y', '100%');
		if($.browser.mozilla)
			$('#footer-right').css('background-position', '100% 100%');
		$('#footer-items-slide').css('left', position.left*1 + speed);
	} else {
		if(action == "right"){
			$('#footer-left').css('background-position-y', -1000);
			if($.browser.mozilla)
				$('#footer-left').css('background-position', '-1000px 0');
		} else {
			$('#footer-right').css('background-position-y', -1000);
			if($.browser.mozilla)
				$('#footer-right').css('background-position', '-1000px 0');
		}
	}
	if(footerAnimate) {
		setTimeout(function() {
			footerSliding = false;
			animateFooterSlide(action);
		}, delay);
	}
}

function repositionElements(first) {
    var h = $(window).height();
	var w = $(window).width();
	
    var h_min = 640;
    var w_min = 1100;
	var menu_top_min = 50;
	var menu_top = ((h - h_min) / 2) + menu_top_min;
	var img_max = 1920;
	var img_ratio = 0.61;
	var c_left_min = 300;
	var c_left = ((w - w_min) / 4) + c_left_min;
	var ce_left_min = 870;
	var ce_left = ((w - w_min) / 2) + ce_left_min;
	
	var screen_ratio;
	var scrollbar_allowance = 20;
	
	if(h < h_min && w < w_min) {
		screen_ratio = w_min / h_min;
//		screen_ratio = (w + scrollbar_allowance) / (h + scrollbar_allowance);
	} else if(h < h_min) {
		screen_ratio = w / (h + scrollbar_allowance);
	} else if(w < w_min) {
		screen_ratio = (w + scrollbar_allowance) / h
	} else {
		screen_ratio = w / h;
	}
	
	if(h > h_min) {
		$('#body-wrapper').css('height', h);
		$('#kain-menu').css('top', menu_top);
	} else {
		$('#body-wrapper').css('height', h_min);
		$('#kain-menu').css('top', menu_top_min);
	}
	
	if($('#content-area.blog').length > 0) {
		var contentHeight = $('#content-area').height() + $('#content-area').offset()['top'] + 100;
		if(contentHeight > $('#body-wrapper').height()) {
			$('#body-wrapper').css('height', contentHeight + 'px');
		}
	}

	if(w > w_min) {
		$('#body-wrapper').css('width', w);
		if ($('#content-area').hasClass('community')) {
		 	$('#content-area').css('width', (w - 380));
		} else {
			$('#content-area').css('left', c_left);
		}
		$('#extra-content').css('left', ce_left);
	} else {
		$('#body-wrapper').css('width', w_min);
		if ($('#content-area').hasClass('community')) {
			
		} else {
			$('#content-area').css('left', c_left_min);
		}
		$('#extra-content').css('left', ce_left_min);
	}
	
	$('#kain-menu').animate({opacity: 1}, 500);
	$('#content-area').animate({opacity: 1}, 500);

	var ratio_timeout = 1;
	if(first) {
		ratio_timeout = 500;
	}
	
	if(first || (h >= h_min && w >= w_min)) {
		setTimeout(function() {
				$('.body-img img').each(function() {
					resizeBackgroundImage(screen_ratio, this);
				});
				
				if($('#body-image').length > 0){
					$('#body-image img').each(function() {
						resizeBackgroundImage(screen_ratio, this);
					});
				}
				if($('#alt-body-image').length > 0){
					$('#alt-body-image img').each(function() {
						resizeBackgroundImage(screen_ratio, this);
					});
				}
			}, ratio_timeout);
	}
//	$('#body-image').css('height', $('#body-image').width() * img_ratio);
		

	var content_top = $('#kain-menu').css('top').replace("px", "") * 1;
	$('#content-area').css('top', content_top + "px");
	$('#extra-content').css('top', content_top + "px");
	
	$('#footer-items').css('width', w - 62);
	$('#footer-items-slide').css('width', ($('#footer-items-slide').children().length * 195) + 'px');
	
	if($('#footer-items-slide').length > 0){
		if($('.footer-press').length > 0){
			if($.cookie('footer-press') != null){
				$('#footer-items-slide').css('left', $.cookie('footer-press'));
				if(parseInt($.cookie('footer-press'), 10) < 0){
					$('#footer-left').css('background-position-y', '100%');
					if($.browser.mozilla)
						$('#footer-left').css('background-position', '100% 100%');
				}
			}
		} else {
			if($.cookie('footer-products') != null){
				$('#footer-items-slide').css('left', $.cookie('footer-products'));
				if(parseInt($.cookie('footer-products'), 10) < 0){
					$('#footer-left').css('background-position-y', '100%');
					if($.browser.mozilla)
						$('#footer-left').css('background-position', '100% 100%');
				}
			}
		}
	}
}

function resizeBackgroundImage(screen_ratio, element) {
	var w = $(window).width();
	var current_ratio = $(element).width() / $(element).height();

	if(current_ratio > screen_ratio) {
		$(element).css({'height': '100%', 'width': ''});
		$(element).css('margin-left', "-" + (($(element).width() - w) / 2) + "px");
	} else {
		$(element).css({'height': '', 'width': '100%', 'margin-left': '0px'});
	}
}

function showMainSubmenu(e) {
	var parent = $(e);
	var element;
	var html;
	var top = 0;
	var left = 0;

	element = $('.submenu', e);
	html = element.html();

	if(element.length != null && html != null) {
		parent.addClass('active');
		var position = parent.offset();
		var submenu = $('#kain-submenu');
		submenu.html('<ul>' + element.html() + '</ul>');
		submenu.css('top', position.top);
		var newleft = position.left + element.width();
		submenu.css('left', (position.left + (element.width() - 20)));
		submenu.stop().animate({left: newleft, opacity: 1}, 'slow');
//		submenu.css('left', position.left + element.width());
		mainSubmenuVisible = true;
	}
}

function hideMainSubmenus() {
	$('#kain-submenu').stop().animate({opacity: 0}, 'slow').html('');
	activeMainMenu = '';
	$('.menu-item').each(function() {
		if($(this).hasClass('active')) {
			$(this).removeClass('active');
		}
	});
}

function showProductSubmenu() {
	var parent = $('#shop-this-look');
	var element;
	var html;
	var top = 0;
	var left = 0;
	
	element = parent.children('ul.submenu');
	html = element.html();

	if(element.length != null && html != null) {
		parent.addClass('active');
		var position = parent.offset();
		var submenu = $('#product-submenu');
		submenu.html('<ul>' + element.html() + '</ul>');
		submenu.css('top', position.top - submenu.children('ul').height() + $('#shop-this-look').height() + 15);
		var newleft = position.left + parent.width() + 15;
		submenu.css('left', (position.left + parent.width()));
		submenu.stop().animate({left: newleft, opacity: 1}, 'slow');
//		submenu.css('left', position.left + parent.width() + 25);
		productSubmenuVisible = true;
		submenu.hover(function(e){
			//nothing
		},
		function(e){
			//UNCOMMENT TO AUTO-HIDE shop-this-look submenu when hover out
			//setTimeout(hideProductSubmenus, 700);
		});
	}
}

function hideProductSubmenus() {
	$('#product-submenu').html('');
	$('#shop-this-look').removeClass('active');
}

function enableProductFader(dur){
	window.setInterval(function(){ $("#alt-body-image").fadeToggle(dur); }, dur * 5);
}

function homepageFader(dur){
	var total = $(".body-img").size();
	$(".body-img").each( function(index, element){
		setTimeout(function(){
			//$("#slide_count").html("Image "+(index+1)+" of "+total);
			$(element).animate({opacity: 1}, dur, function(){
				$(".body-img[rel='home"+index+"']").animate({opacity: 0});
				if(index == total - 1){
					setTimeout(function(){
						$(".body-img[rel!='home1']").each(function(){
							$(this).animate({opacity: 0}, dur);
						});
						homepageFader(dur);
					}, dur * total);
				}
			});
		}, index * (dur * total));
	});
}

function displayFeatured(type, link, title, urlTitle){
	switch(type){
		case "image":
			location.href="/products/view/"+urlTitle;
			break;
		case "video":
			if($("#videoContainer").length < 1)
				$("#footer-container").after('<div id="videoContainer"><div id="vid-close"><img id="video-close" src="/images/lightbox-btn-close.gif" /></div><div class="video-js-box"></div></div>');
			else {
				$("#videoContainer").empty();
				$("#videoContainer").dialog("destroy");
				$("#videoContainer").html('<div id="vid-close"><img id="video-close" src="/images/lightbox-btn-close.gif" /></div><div class="video-js-box"></div>');
			}				
				
			$.get("/chunks/video_chunk/"+urlTitle, function(data){
				$("#video-close").bind('click', function(){
					$("#videoContainer").empty();
					$("#videoContainer").dialog("close");
				});
				
				$(".video-js-box").html(data);
				VideoJS.setupAllWhenReady();
				$("#videoContainer").dialog({
					width: 720,
					height: 450,
					modal: true,
					open: function(){
						$("video").css("height", 380+"px");
						$("video img").css("height", 380+"px");
						$(".ui-widget-overlay").bind('click', function(){
							$("#videoContainer").empty();
							$("#videoContainer").dialog("close");
						});
					}
				});
			});
			break;
	}
}

var blogBoxHeight = 0;
function showBlog(event){
	blogBoxHeight = $('#blog-box').height();
	event.preventDefault();
	oldBlogContent = $("#blog-box").html();
	var url = $(event.currentTarget).attr("href");
	$("#blog-box").load(url.replace("/blog/", "/blog/view/"), function(){
		$("#fbtw").fadeOut(500);
		$("#fb-box").slideUp(500, "easeInOutExpo");
		$("#twitter-box").slideUp(500, "easeInOutExpo");
		$("#blog-box").animate({width: 600, height: 400}, 800, "easeInOutExpo", function() { $("#blog-box").css('overflow-y', 'auto'); } );
	});
}

function hideBlog(){
	$("#fb-box").delay(250).slideDown(500, "easeInOutExpo");
	$("#twitter-box").delay(250).fadeIn(500, "easeInOutExpo");
	$("#fbtw").delay(250).fadeIn(500);
	$("#blog-box").animate({width: 200, height: blogBoxHeight}, 800, "easeInOutExpo", function(){
		$(this).empty().html(oldBlogContent);
		$("a.blog-link").click(function(event){ showBlog(event); });
		$("#blog-box").css('overflow-y', 'hidden');
	});
}
