$(document).ready(function(){
	
	// language selector
	$("a#selectlanguage").click(
		function(){
			var hostname = location.protocol+'//'+location.hostname;
			var url = location.href.replace(hostname,'');
			$('body').append('<div id="fade" onclick="recordOutboundLink(\'language-close\', \''+url+'\');"></div>');
			$('#fade').css({'z-index': '88888', 'filter' : 'alpha(opacity=80)'}).fadeIn(250);
			$('#languageselect').css({'z-index': '99999'}).fadeIn(250, function(){ $('#languageselect').css({'z-index': '99999'}); });
			return false;
		}
	);
	$('a.close, #fade').live('click', function(){
		$('#languageselect').fadeOut(250, function(){ });
	    $('#fade').fadeOut(function() {
	        $('#fade').remove();
	    });
	    return false;
	});
	$('#languageselect a.no').click(function() {
	  alert('Coming soon');
	  return false;
	});
	
	// fix and unfix the header based on scroll position
	$(window).scroll(
		function(){
			var shellScrollPosition = parseInt($('#shell').css('padding-top'));
			var sectionMarginOffset = 12;
			var headerHeight = 104;
			if ($("#shell").hasClass('midgap')){
				$('header').css({'position': 'fixed', 'top': '0px'});
				$('header').addClass('fixed');
				$("#shell").css({'margin-top': '548px'});
			} else {
				if ($(this).scrollTop() > shellScrollPosition && $('header').css('position') != 'fixed'){
					$('header').css({'position': 'fixed', 'top': '0px'});
					$('header').addClass('fixed');
					$('section:first').css({'margin-top': (headerHeight+sectionMarginOffset)+'px'});
				}
				if ($(this).scrollTop() <= shellScrollPosition && $('header').css('position') == 'fixed'){
					$('header').css({'position': 'relative'});
					$('header').removeClass('fixed');
					$('section:first').css({'margin-top': sectionMarginOffset+'px'});
				}
			}
		}
	);
	
	// image scroller
	if ($('.scroller').length > 0){
		$('.scroller').each(function(){
			$scroller = $(this);
			$list = $scroller.find('ul.scroller-list');
			$list.children('li:last-child').css({'margin-right':'0px'});
			var adNum = $list.children().length;
			var adWidth = $list.children('li:first-child').width();
			var adMargin = parseInt($list.children('li:first-child').css('margin-right'));
			var totalScrollWidth = (adNum*adWidth)+((adNum-1)*adMargin);
			$list.css({'width':totalScrollWidth+'px'});

			var type = "gallery";
			var displayMax = 6;

			if ($list.children().length > displayMax){
				$scroller.append('<div class="scroller-nav"><span class="prev disabled">Previous</span><span class="next">Next</span></div>');
				$scroller.find('.scroller-nav span').click(function(){
					if (!$(this).hasClass('working')){
						if ($(this).hasClass('next')){
							var direction = 'next';
						} else if ($(this).hasClass('prev')){
							var direction = 'prev';
						}

						var btnTarget = $(this);
						var shellTarget = btnTarget.parent().parent().find('.scroller-shell');
						var shellWidth = shellTarget.width();
						var shellLeft = 0;

						var listTarget = btnTarget.parent().parent().find('.scroller-list');
						var listWidth = listTarget.width();
						var listLeft = parseInt(listTarget.css('left'));

						var newListPos = listLeft;
						if (direction == 'next'){
							if ($(this).hasClass('disabled')){
								newListPos = 0;
							} else {
								newListPos = listLeft-(shellWidth+adMargin)+1;
							}
							if (newListPos < 0-(listWidth-shellWidth)){ newListPos = 0-(listWidth-shellWidth); }
						} else if (direction == 'prev'){
							if ($(this).hasClass('disabled')){
								newListPos = 0-(listWidth-shellWidth);
							} else {
								newListPos = listLeft+(shellWidth+adMargin)-1;
							}
							if (newListPos > 0){ newListPos = 0; }
						}

						btnTarget.siblings('span').addClass('working');
						btnTarget.addClass('working');

						listTarget.stop(true,false).animate({"left": newListPos+"px"}, 700, function(){
							var newListLeft = parseInt(listTarget.css('left'));

							if (direction == 'next'){
								if (newListLeft+listWidth <= shellLeft+shellWidth){
									btnTarget.addClass('disabled');
								} else {
									btnTarget.removeClass('disabled');
								}
								if (newListLeft == shellLeft){
									btnTarget.siblings('span.prev').addClass('disabled');
								} else {
									btnTarget.siblings('span.prev').removeClass('disabled');
								}
							} else if (direction == 'prev'){
								if (newListLeft < shellLeft){
									btnTarget.removeClass('disabled');
								} else {
									btnTarget.addClass('disabled');
								}
								if (newListLeft+listWidth <= shellLeft+shellWidth){
									btnTarget.siblings('span.next').addClass('disabled');
								} else {
									btnTarget.siblings('span.next').removeClass('disabled');
								}
							}
							btnTarget.siblings('span').removeClass('working');
							btnTarget.removeClass('working');
						});
					}
				});
			}
		});
	}
	
	// open main nav section with subnavs on hover
	/*
	$("ul.main li.sec").hover(
		function(){
			//$("ul.gender").css("display", "none"); // reset gender navs to hidden
			$(this).children(".sub").stop(true,true).fadeIn(200);
			$(this).children(".sub").siblings("span").addClass('active');
		},
		function(){
			$(this).children(".sub").stop(true,true).fadeOut(200);
			$(this).children(".sub").siblings("span").removeClass('active');
		}
	);
	*/
	$("ul.main > li.sec span").click(function(){
		$(this).parent().children(".sub").stop(true,true).fadeIn(200);
		$(this).parent().children(".sub").siblings("span").addClass('active');
	});
	
	// open/close gender nav for products on click
	$("ul.sub li span").click(
		function(){
			$("ul.type").css("display", "none");
			$("ul.sub li span").removeClass('active');
			if ($(this).siblings("ul.type").css("display") == "none"){
				$(this).siblings("ul.type").css("display", "inline-block");
				$(this).addClass('active');
			} else {
				$(this).siblings("ul.type").css("display", "none");
				$(this).removeClass('active');
			}
		}
	);
	
	// date nav sections open/close
	$("ul.datenav span").click(
		function(){
			$(this).siblings("ul").toggle("slow");
		}
	);
	
	// show and hide the default text on form elements
	$("input, textarea").live('focus',
		function(){
			if ($(this).attr('placeholder') != ''){
				if ($(this).val() == $(this).attr('placeholder')){
					$(this).addClass('active');
					$(this).val('');
				}
			}
		}
	);
	$("input, textarea").live('blur',
		function(){
			if ($(this).attr('placeholder') != ''){
				if ($.trim($(this).val()) == ''){
					$(this).removeClass('active');
					$(this).val($(this).attr('placeholder'));
				}
			}
		}
	);
	
	// show and hide elements of the fit table based on selected type
	$(".fittable-control a").live('click',
		function(){
			$(this).removeClass('active');
			$(".fittable-control a").removeClass('active');
			var type = $(this).attr('class');
			
			$(".fittable tbody").each(function(index){
				if ($(this).attr('class') == type){
					$(this).css("display", "table-row-group");
				} else {
					$(this).css("display", "none");
				}
			});
			
			$(".fittable-control a").each(function(index){
				if ($(this).attr('class') == type){
					$(this).addClass('active');
				}
			});
			return false;
		}
	);
	
	$('a.backtotop').click(function(){
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 500 );
		return false;
	});
	$('.fitsnav a').click(function(){
		var fit = $(this).attr('href').split('#');
		
		var destination = $('#fit-'+fit[1]).offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-116}, 500 );
		
		return false;
	});
	
	$(".teamnav-control a").hover(
		function(){
			var target = $(this).attr('class');
			$(".teamnav li").each(function(index){
				if ($(this).attr('class') == target){
					$(this).children("a").addClass('active');
				}
			});
		},
		function(){
			var target = $(this).attr('class');
			$(".teamnav li").each(function(index){
				if ($(this).attr('class') == target){
					$(this).children("a").removeClass('active');
				}
			});
		}
	);
	$(".teamnav a").hover(
		function(){
			var target = $(this).parent().attr('class');
			$(".teamnav-control a").each(function(index){
				if ($(this).hasClass(target)){
					$(this).addClass('active');
				}
			});
		},
		function(){
			var target = $(this).parent().attr('class');
			$(".teamnav-control a").each(function(index){
				if ($(this).hasClass(target)){
					$(this).removeClass('active');
				}
			});
		}
	);
	
	// FITS
	$('.fit-rotation').each(function(){
		for (var i=8; i>0; i--){
			$(this).append('<span class="r'+i+'"></span>');
		}
	});
	
	$('.fit-rotation span').live("hover",
		function(){
			var target = $(this).parent();
			if (target.hasClass("load")){
				target.removeClass("load").addClass("working");
				target.find('.loading').remove();
				target.append('<div class="loading">loading</div>');
				var img = $('<img style="display:none;">').attr('src', target.attr('data-src'));
				img.appendTo(target);
				$(img).load(function(){
					target.find('.loading').remove();
					target.css({'background-image':'url('+target.attr('data-src')+')'});
					target.removeClass("working").addClass("ready");
				});
			}
			if (target.hasClass("ready")){
				var viewNum = $(this).attr('class').substring(1);
				var xPos = 0-((viewNum-1)*320);
				target.css({'background-position':xPos+"px"});
			}
		},
		function(){
			// do nothing
		}
	);
	
  var submit_email = function() {
    var params = {
      url: this.action,
      type: 'POST',
      data: { email: this.email.value },
      context: this,
      dataType: 'text',
      success: function(result) {
        if(result == 'ok') {
          $(this).replaceWith('<p>Thanks for joining us.</p>');
        } else {
          alert(result);
        }
      }
    };
    $.ajax(params);
    return false;
  };
  $('form#emailsignupfooter').submit(submit_email);
  $('form#emailsignuptop').submit(submit_email);
});
