$(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(){ });
		$('#wetplateproject .gallerymodal').fadeOut(250, function(){  });
	    $('#fade').fadeOut(function() {
	        $('#fade').remove();
	    });
	    return false;
	});
	$('#languageselect a.no').click(function() {
	  alert('Coming soon');
	  return false;
	});
	
	// 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');
		}
	);
	
	// open/close gender nav for products on click
	$("ul.sub li span").click(
		function(){
			$("ul.gender").css("display", "none");
			if ($(this).siblings("ul.gender").css("display") == "none"){
				$(this).siblings("ul.gender").css("display", "inline-block");
			} else {
				$(this).siblings("ul.gender").css("display", "none");
			}
		}
	);
	
	// quickfinder list
	$("#quickfinder strong").click(
		function(){
			if ($("#quickfinder ul.qf").css("display") == "none"){
				$(this).addClass('active');
				$("#quickfinder ul.qf").stop(true,true).fadeIn(200);
			} else {
				$(this).removeClass('active');
				$("#quickfinder ul.qf").stop(true,true).fadeOut(200);
			}
		}
	);
	$("#quickfinder").hover(
		function(){ },
		function(){
			$("#quickfinder strong").removeClass('active');
			$("#quickfinder ul.qf").stop(true,true).fadeOut(200);
		}
	);
	
	// team landing nav hovers
	$("ul.teamlist li a, ul.teamphotos li a").hover(
		function(){
			var id = $(this).attr('id').substr(2);
			var name = "#tn"+id;
			var photo = "#tp"+id;
			$(name).addClass('active');
			$(photo).addClass('active');
		},
		function(){
			var id = $(this).attr('id').substr(2);
			var name = "#tn"+id;
			var photo = "#tp"+id;
			$(name).removeClass('active');
			$(photo).removeClass('active');
		}
	);
	
	// functional tech slide nav
	$("ul.slidenav a").click(
		function(){
			var slide = $(this).attr('href').substr(1);
			var newXPos = 0-(slide*960);
			$("#functionaltech-slides").stop(true,false).animate({"left":newXPos+"px"}, 500);
			return false;
		}
	);
	
	// function tech diagram buttons
	$("ul.diagram a").click(
		function(){
			var slide = $(this).attr('href');
			var feature = $(this).attr('href').substr(1);
			var set = $(this).parent().parent().parent().attr('id');
			var loop = "#"+set+" ul.diagram a";
			var target = "#"+set+" .details-slides .details";
			
			var newTopPos = 0-((feature-1)*560);
			$(target).stop(true,false).animate({"top":newTopPos+"px"}, 500);
			
			$(loop).each(function(index){
				if (slide == $(this).attr('href')){
					$(this).addClass('active');
				} else {
					$(this).removeClass('active');
				}
			});
			
			return false;
		}
	);
	
	$("#ftslide-microshield ul.diagram a, #ftslide-microshield .msdetails a").mouseover(
		function(){
			var feature = $(this).attr('href');
			$("#ftslide-microshield ul.diagram a, #ftslide-microshield .msdetails a").each(function(index){
				if (feature == $(this).attr('href')){
					$(this).addClass('active');
				} else {
					$(this).removeClass('active');
				}
			});
		}
	);
	
	$("#ftslide-microshield ul.diagram a, #ftslide-microshield .msdetails a").click(
		function(){
			return false;
		}
	);
	
	// show and hide the default text on form elements
	$("input, textarea").focus(
		function(){
			if ($(this).attr('placeholder') != ''){
				if ($(this).val() == $(this).attr('placeholder')){
					$(this).addClass('active');
					$(this).val('');
				}
			}
		}
	);
	$("input, textarea").blur(
		function(){
			if ($(this).attr('placeholder') != ''){
				if ($.trim($(this).val()) == ''){
					$(this).removeClass('active');
					$(this).val($(this).attr('placeholder'));
				}
			}
		}
	);
	
	// 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 ($scroller.hasClass('wetplate')){ displayMax = 9; }

			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');
						});
					}
				});
			} else {
				$scroller.find('.scroller-shell').css({'left':'0'});
			}
		});
	}
	
	// wet plate project
	$('#wetplateproject .imggallery ul.scroller-list a').click(function(){
		var galleryTotal = $(this).closest('ul').find('li').length-1;
		var index = $(this).parent('li').index();
		var scope = $(this).closest('.imggallery');
		var target = scope.find('.gallerymodal');
		var subtarget = target.find('ul li:eq('+index+')');
		
		$('body').append('<div id="fade"></div>');
		$('#fade').css({'z-index': '88888', 'filter' : 'alpha(opacity=80)'}).fadeIn(250);
		target.css({'z-index': '99999'}).fadeIn(250, function(){ target.css({'z-index': '99999'}); });
		target.find('ul li').removeClass('active').hide();
		
		if (target.find('ul li:eq('+index+') span').length > 0){
			var src = target.find('ul li:eq('+index+') span').attr('data-src');
			subtarget.append('<img src="'+src+'" alt="">');
			subtarget.find('span').remove();
		}
		
		if (index == 0){
			target.find('span.prev').addClass('disabled');
			target.find('span.next').removeClass('disabled');
		} else if (index == galleryTotal){
			target.find('span.next').addClass('disabled');
			target.find('span.prev').removeClass('disabled');
		} else {
			target.find('span.next').removeClass('disabled');
			target.find('span.prev').removeClass('disabled');
		}
		
		subtarget.addClass('active');
		subtarget.show();
		return false;
	});
	
	$('#wetplateproject .gallerymodal > span').click(function(){
		var target = $(this).closest('.gallerymodal');
		var galleryTotal = $(this).siblings('ul').find('li').length-1;
		var index = $(this).siblings('ul').find('li.active').index();
		var shift = index;
		
		if ($(this).hasClass('prev')){
			if (index == 0){
				shift = galleryTotal;
			} else {
				shift--;
			}
		} else if ($(this).hasClass('next')){
			if (index == galleryTotal){
				shift = 0;
			} else {
				shift++;
			}
		}
		
		if (shift == 0){
			target.find('span.prev').addClass('disabled');
			target.find('span.next').removeClass('disabled');
		} else if (shift == galleryTotal){
			target.find('span.next').addClass('disabled');
			target.find('span.prev').removeClass('disabled');
		} else {
			target.find('span.prev').removeClass('disabled');
			target.find('span.next').removeClass('disabled');
		}
		
		$(this).siblings('ul').find('li').each(function(){
			if ($(this).index() == shift){
				if ($(this).find('span').length > 0){
					var src = $(this).find('span').attr('data-src');
					$(this).append('<img src="'+src+'" alt="">');
					$(this).find('span').remove();
				}
				$(this).addClass('active').show();
			} else {
				$(this).removeClass('active').hide();
			}
		});
	});
});

// home hero slideshow
function homeSlidesInit(){	
	var heroSlideInterval;
	if ($('#homehero .set div').length > 0){
			if (delayedHomeSlides.length > 0){
			for (var i=0; i<delayedHomeSlides.length; i++){
				$('#homehero .set').append(delayedHomeSlides[i]);
			}
			var photonavtarget = $('#homehero .container');
			var photoobjtarget = $('#homehero .set');
			var photosWidth = $('#homehero .set div').length*960;
			$('#homehero .set').width(photosWidth);
			$('#homehero .container').after('<span class="prev disabled">Previous</span><span class="next">Next</span>');
			var heroSlideInterval = setInterval(heroSlideAdvance, 5000);
			
			// project detail thumbnail paging nav
			$("#homehero > span").click(
				function(){
					var btnTarget = $(this);
					if (!($(this).hasClass('working'))){
						
						clearInterval(heroSlideInterval);
						heroSlideInterval = setInterval(heroSlideAdvance, 5000);
						
						if ($(this).hasClass('prev')){
							var direction = 'prev';
						} else if ($(this).hasClass('next')){
							var direction = 'next';
						}
						var shellTarget = photonavtarget;
						var shellWidth = shellTarget.width();
						var shellLeft = 0;
						var listTarget = photoobjtarget;
						var listWidth = listTarget.width();
						
						var listLeft = parseInt(listTarget.css('left'));
						var newListPos = listLeft;
						if (direction == 'next'){
							if ($(this).hasClass('disabled')){ newListPos = shellLeft; }
							else if (newListPos < (shellLeft-(listWidth-shellWidth))){ newListPos = shellLeft-(listWidth-shellWidth); }
							else { newListPos = listLeft-shellWidth; }
						} else if (direction == 'prev'){
							if ($(this).hasClass('disabled')){ newListPos = shellLeft-(listWidth-shellWidth); }
							else if (newListPos > shellLeft){ newListPos = shellLeft; }
							else { newListPos = listLeft+shellWidth; }
						}
						btnTarget.siblings('span').addClass('working');
						btnTarget.addClass('working');
						listTarget.stop(true,false).animate({"left": newListPos+"px"}, 750, function(){
							var newlistLeft = parseInt(listTarget.css('left'));

							if (direction == 'next'){
								if (newlistLeft+listWidth <= shellLeft+shellWidth){
									btnTarget.addClass('disabled');
								} else {
									btnTarget.removeClass('disabled');
								}
							} else if (direction == 'prev'){
								if (newlistLeft < shellLeft){
									btnTarget.removeClass('disabled');
								} else {
									btnTarget.addClass('disabled');
								}
							}
							btnTarget.siblings('span').removeClass('disabled').removeClass('working');
							btnTarget.removeClass('working');
							
							// reset disabled status on loop around
							if (direction == 'next' && newListPos == shellLeft){
								btnTarget.siblings('span').addClass('disabled');
							} else if (direction == 'prev' && newListPos == shellLeft-(listWidth-shellWidth)){
								btnTarget.siblings('span').addClass('disabled');
							}
						});
					}
					return false;
				}
			);
		}
	}
}
function heroSlideAdvance(){
	$("#homehero > span.next").click();
}
