$(document).ready(function(){
	
	$("body").removeClass("no-js");
	
	if(($("div.navHolder").height()+100) >= $("div.miscContentHolder").height()){
		$("div.miscContentHolder").height($("div.navHolder").height());
	}
	
	/* :hover */
	$(function(event) {
	  $("img.hover").hover(function(event) {
			$(this).attr("src", $(this).attr("src").split(".").join("_on."));  // adds -hover to the name of the image
	  }, function(event) {
			$(this).stop(true,false); // prevents the creation of stacked actions
			$(this).attr("src", $(this).attr("src").split("_on.").join("."));  // removes -hover from the name of the image
	  });
	});
	
	 $("ul.langNav li").hover(function() {
		 $(this).find(".sub-menu").fadeIn();
	 },function(){
		 $(this).find(".sub-menu").fadeOut();
	 });
	 
	//testimonials
	$(".testimonial .testText:odd").css("float","right");
	$(".testimonial img:odd").css("float","left");
	
	/* subContent slide */
	$(".sectionHeader a, .sectionHeader h3, .subToggle").click(function(e) {
		$(".sectionHolder").slideToggle("slow");
		$(".sectionHeader").toggleClass("active");
		if($('.sectionHeader').length != 0){
		    $('html,body').animate({ 
		         scrollTop: $('.sectionHeader').offset().top 
		    }, 1000);
		}
		return false;
	});
	
	if(window.location.hash == "#bottom") {
		$(".sectionHolder").slideDown();
	} 
	
	//dimensions table styles
	$("table.dimensions tr").find(":nth-child(1)").addClass("tableMenu");
	$("table.dimensions tr").find(":nth-child(4)").addClass("tableMenu");	
	$("table.dimensions td").parent().parent().children(":nth-child(2)").addClass("tableMenu");
	
	//lightbox
	if ($("#feature-area img.lazy").length != 0) {
       $('#feature-area img.lazy').each(function(i){
           $(this).jail({
        	   event : "mouseover", 
        	   selector : "#subNavLi"+i, 
               effect: 'fadeIn',
               placeholder: '',
               speed : 500
           });
       });
	}
	
	$('#preowned img.lazy').each(function(i){
        $(this).jail({ 
            effect: 'fadeIn',
            placeholder: '',
            speed : 500
        });
    });
	
	//lightbox
	if ($("a[rel=fancybox], a.fancybox").length != 0) {
		
		$("a.fancybox").fancybox({
			'padding' : 5,
			'overlayShow' : true,
		    'speedIn' : 600,
		    'speedOut' : 500,
		    'transitionIn': 'elastic',
		    'transitionOut': 'elastic',
		    'easingIn' : 'easeOutBack',
		    'easingOut' : 'easeInBack',
		    'titlePosition' : 'inside',
		    'titleFormat' : function(title, currentArray, currentIndex, currentOpts) { 
		    	 $title = '';
		    	 if(currentIndex > 0){
	    		    $title += '<a href="#" onclick="$.fancybox.prev();return false;" id="fancybox-prev-btn"></a>';
	    		  }
	    		  if(currentIndex < currentArray.length - 1){
	    		    $title += '<a href="#" onclick="$.fancybox.next();return false;" id="fancybox-next-btn"></a>';
	    		  }
		    	 $title += '<span class="fancybox-title">' + title + '<span class="fancybox-title-count">(image ' + (currentIndex + 1)  + ' of ' + currentArray.length + ')</span></span><br class="clearBoth"/>';
		    	 return $title;
		    },
		    'onComplete' : function(currentArray, currentIndex, currentOpts){
		    	var currentUrl = currentArray[currentIndex].href,
		        link = '<a target="_blank" id="fancybox-external-link" title="Open in a new tab" href="' + currentUrl + '"></a>';
		    	$('#fancybox-close').before(link);
		    }
		});
	}
	
	$("a.external").attr("target","_blank");
	
	//form validate
	// validate contact form on keyup and submit
	if($("#contact").length != 0){
		$("#contact").validate({
			rules: {
				name: {
					required: true,
					minlength: 2
				},
				telephone: {
					digits: true
				},
				email: {
					required: true,
					email: true
				},
				brochure: { 
					required: true 
				}
			},
			messages: {
				name: {
					required: "Please enter your name",
					minlength: "Your name must consist of at least 2 characters"
				},
				telephone:  "Please enter only digits no spaces",
				email: "Please enter a valid email address",
				brochure: "Please select a brochure option"
			}
		});
	}
			
	//Address Hide
	$(".address").hide();
	
	$("#brochure").change(function() { 
        var brochure = $("#brochure").val(); 
        if (brochure == 'yes') {
        	$(".address").slideDown("slow");
        } else {
        	$(".address").slideUp("slow");
        }
    }); 

	
	//How did you hear about us?
	$(".research_specific").hide();
	
	$("#research").change(function() { 
        var research = $("#research").val(); 
        if (research == '') {
        	$(".research_specific").slideUp("slow");
        } else {
        	$(".research_specific").slideDown("slow");
        }
    })

    //Hide/Show saves in cookie
    var show = $("#shows ul li.show");   
		show.addClass("active");
		
	$(show).each(function(c){
		var cvalue = $.cookie('show' + c);
		if ( cvalue == 'closed' + c ) { 
		    $(this).find(".content").css({display:"none"});
		    $(this).toggleClass('active');
		}
		$(this).find("h2:first").prepend("<span class='icn'></span>");
	}); 

	$("#shows li h2 span").toggle(function(){
		var li = $(this).parents("li"),
			li_content = li.find(".content");
	    var isactive = li.hasClass("active") ? true : false;

	    var num = show.index(li);
	    var cookieName = 'show' + num;
	    var cookieValue = null;

	    if(isactive){
	        cookieValue = 'closed' + num;
	        li_content.slideUp(500);
	        li.removeClass('active');
	    }else{
	    	li_content.slideDown(500);
	        li.addClass("active");
	    }

	    $.cookie(cookieName, cookieValue, { path: '/', expires: 10 }); 

	},function(){
		var li = $(this).parents("li"),
		li_content = li.find(".content");
	    var isactive = li.hasClass("active") ? true : false;

	    var num = $(this).index(li);
	    var cookieName = 'show' + num;
	    var cookieValue = null;
		
	    if(isactive){
	        li_content.slideUp(500);
	        li.removeClass("active");        
	    }else{
	        cookieValue = 'closed' + num;
	        li_content.slideDown(500);
	        li.addClass('active'); 
	    }
	    $.cookie(cookieName, cookieValue, { path: '/', expires: 10 }); 
	});
});
