$(function(){
	function filterPath(string) {
		return string.replace(/^\//,'').replace(/(index|default).[a-zA-Z]{3,4}$/,'').replace(/\/$/,'');
	}
	var locationPath = filterPath(location.pathname);
	var scrollElem = scrollableElement('html', 'body');

	function scrollableElement(els) {
		for (var i = 0, argLength = arguments.length; i <argLength; i++) {
			var el = arguments[i],
			$scrollElement = $(el);
			if ($scrollElement.scrollTop()> 0) {
				return el;
			} else {
				$scrollElement.scrollTop(1);
				var isScrollable = $scrollElement.scrollTop()> 0;
				$scrollElement.scrollTop(0);
				if (isScrollable) {
					return el;
				}
			}
		}
		return [];
	}

	$('.search input').placeholder();
	
	$("#main input:not([type='hidden']), #main select, #main textarea").uniform();

	$("a[href$='.pdf'],a[href$='.doc'],a[href$='.dwt'],a[href$='.dwg']").addClass("doc");
	
	var photosH = $(".photos").height(),
		finishH = $(".finishes").height();
	
	if(photosH < finishH) {
		$(".photos").parents("#secondary").css({"padding-top": finishH + 12 + 'px'});
	} else {
		$(".photos").parents("#secondary").css({"padding-top": photosH + 12 + 'px'});
	}
	
	$(".photos #slideshow").slideshow();
	$(".finishes").addClass("hidden").hide();
	
	$("#flip").click(function(){
		var $finishes = $(".finishes"),
			$photos = $(".photos");
			
		if(!$photos.hasClass("hidden")) {
			$(this).text("See Product Images");
			$photos.fadeOut(500,function(){
				$(this).css('z-index',1);
				$(this).addClass("hidden");
			});
			$finishes.removeClass("hidden").fadeIn(250,function(){
				$(this).css('z-index',2);
			});
			$finishes.find("#slideshow").slideshow();
		} else {
			$(this).text("See Product Finishes");
			$finishes.fadeOut(500,function(){
				$(this).css('z-index',1);
				$(this).addClass("hidden");
			});
			$photos.removeClass("hidden").fadeIn(250,function(){
				$(this).css('z-index',2);
			});
		}
	});
	
	$('a').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}).addClass("ext");
	
	var negative = true;
	var prevAngle = 0;
	$(".timeline img").each( function() {
		var $parent = $(this).parent();
		
		var ph = $parent.find('p').height();
		var h = $(this).height() + 20;
		
		$parent.height(Math.max(ph,h));
		
		var angle = Math.floor( Math.random() * 11 );
		
		if(negative) {
			angle = '-'+angle;
			negative = false;
		} else {
			negative = true;
		}
		
		$(this).css({   
			'-webkit-transform': 'rotate('+angle+'deg)',
			'-moz-transform': 'rotate('+angle+'deg)',
			'-o-transform': 'rotate('+angle+'deg)',
			'transform': 'rotate('+angle+'deg)' 
		}).addClass('rotate');  
	});
	
	$(".timeline img").hover(function(){
		$(this).parent().find('img').css("zIndex", 0);
		$(this).css("zIndex", 1);
	});

	$('#faq .questions a, #faq ol li .top').each(function() {
		var thisPath = filterPath(this.pathname) || locationPath;

		if (  locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/,'') ) {
			var $target = $(this.hash), target = this.hash;
			if (target) {
				var targetOffset = $target.offset().top;
				$(this).click(function(event) {
					event.preventDefault();
					$(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
						location.hash = target;
						$("#faq ol li.selected").removeClass("selected");
						$(target).addClass("selected");
					});
				});
			}
		}
	});
	
	$("#sign-up").click(function(e){
		e.preventDefault();
		
		var $parent = $(this).parent();
		var h = $parent.parent().outerHeight(true);
		var thisH = $(this).outerHeight(true);
		
		if($(this).hasClass("open")) {
			$(this).removeClass("open");
			$parent.animate({"height": thisH + 'px'}, 500, function(){
				$parent.find("form").hide();
			});
			
		} else {
			$(this).addClass("open");
			$parent.animate({"height": h + 'px'}, 500);	
			$parent.find("form").show();
		}
		
	});
	
	$("#cancel").click(function(e){
		e.preventDefault();
		
		var $form = $(this).parents("form");
		
		$form[0].reset();
		
		$("#sign-up").trigger("click");
	});
	
	$("#technical-details #sidebar select.category").change(function(){
		type = $(this).parent().parent().attr('data-type');
		category = $(this).val();
		$(this).parent().parent().children('.selector + .selector').hide();
		$(this).parent().parent().children('#uniform-'+type+'-'+category).show();
	});
	
	$("#technical-details #sidebar .selector + .selector select").change(function() {
		window.location.href = $(this).val();
	});
	
	$(".filter select:not('#orderby')").change(function() {
		$(this).parents('form').submit();
	});
	
	$(".filter select").change(function() {
		$("#sort").val($('option:selected',this).attr("data-sort"));
		
		$(this).parents('form').submit();
	});
	
	mapVal = $('#map-type').val();
	$('#uniform-map-'+mapVal).show();
	
	productVal = $('#product-type').val();
	$('#uniform-product-'+productVal).show();
	
	mapWidth = $('#map-box img').attr('width') - 125;
	
	$('#map-box .points > li').each(function(){
		topVal = $(this).position().top;
		leftVal = $(this).position().left;
		if(topVal < 50){
			$(this).addClass('top');
		}
		if(leftVal < 70){
			$(this).addClass('left');
		}
		if(leftVal > mapWidth){
			$(this).addClass('right');		
		}
	});
	
	$('#map-box .points > li:not(.top) .content').each(function(){
		contentHeight = $(this).height();

		if(contentHeight < 63){
			difference = 63 - contentHeight;
			newTop = (-85 + difference);
			$(this).css({top: newTop+'px'});
		} else if(contentHeight > 63){
			difference = contentHeight - 63;
			newTop = (-85 - difference);
			$(this).css({top: newTop+'px'});		
		}
	});
	
	$('#map-box .points > li').click(function(){
		$this = $(this);
		window.setTimeout(function(){
			if($.browser.msie){
				$this.children('div').addClass('visible').show();
			} else {
				$this.children('div').addClass('visible').fadeIn('250');
			}
		}, 0);
	}).mouseleave(function(){
		if($(this).children('div').hasClass('visible')){
			$(this).addClass('open');
			window.setTimeout(function(){
				if($.browser.msie){		
					$('.open').removeClass('open').children('div.visible').removeClass('visible').hide();
				} else {
					$('.open').removeClass('open').children('div.visible').removeClass('visible').fadeOut('250');
				}
			}, 1000);
		}
	});
	
	
	// Form Validation 
	
	/*$.validator.addMethod("eeEmail",function(value) { 
		var regex = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix;
		return regex.test(value);
	}, 'Please enter a valid email address');*/
	

	$("#thanks").hide();
	
	$("#contact-form form").validate({
		rules: {
			"first_name": "required",
			"last_name": "required",
			"email": {
				required: true,
				email: true
	 		},
			"phone": "required"
		},
		errorElement: "em",
		onkeyup: false,
		onfocusout: false,
		highlight: function(element, errorClass, validClass) {
			$(element).parents("p").children("label").addClass(errorClass);
		},
		  unhighlight: function(element, errorClass, validClass) {
			$(element).parents("p").children("label").removeClass(errorClass);
		},
		submitHandler: function(form) {
		   	$(form).ajaxSubmit({
				target: '#thanks', 
				success: function() {
					$('#contact-form form').fadeOut(300,function(){
						$('#thanks').fadeIn(300);
					});
				}
			});
		}
	});
	
	// Lunch and Learn Sign up Form
	
	
	$("#sign-up-form form").validate({
		rules: {
			"first_name": "required",
			"last_name": "required",
			"email": {
				required: true,
				email: true
	 		},
			"phone": "required",
			"address": "required",
			"city": "required",
			"state": "required",
			"zip": "required"
		},
		errorElement: "em",
		onkeyup: false,
		onfocusout: false,
		highlight: function(element, errorClass, validClass) {
			$(element).parents("p").children("label").addClass(errorClass);
		},
		  unhighlight: function(element, errorClass, validClass) {
			$(element).parents("p").children("label").removeClass(errorClass);
		},
		submitHandler: function(form) {
		   	$(form).ajaxSubmit({
				target: '#thanks', 
				success: function() {
					$('#sign-up-form form').fadeOut(300,function(){
						$('#thanks').fadeIn(300);
					});
				}
			});
		}
	});
	
	// Pagination
	
	$("#pagination a:contains('>')").addClass("next");
	$("#pagination a:contains('<')").addClass("previous");
	
	
});
