$(document).ready(function(){
	
	
	//Homepage product teasers functionality
	$('#home-subnav .content').css({display:'none'})

	$("#home-subnav div.title").click(function(){
		$(this)
			.parent("li:not(.selected)")
			.parent("ul")
			.find("li.selected h2").animate( { fontSize: "1.1667em"}, 500).animate( { backgroundColor: '#696f29'}, 250).end()
			.find("li.selected div.content").slideUp(500).end()
			.find("li.selected").removeClass("selected");
		$(this)
			.parent("li").addClass("selected")
			.find("h2").animate( { backgroundColor: '#adb73d', fontSize: "26px"}, 250).end() // fontSize: "2.1667em" causes bug so must use pixel value
			.find("div.content").slideDown(500);
	});
	$("#home-subnav div.title").triggerHandler("click");
	
	$('#home-subnav h2').hover(
		function() {
			$(this).animate( { backgroundColor: '#adb73d'}, 200).end();
		},
		function() {
			$(this).animate( { backgroundColor: '#696f29'}, 200).end();
		}
	);
	// FAQ page accordion
	
	$('#faq-question').accordion({
		//header: 'a.question',
		active: false,
		alwaysOpen: false,
		autoHeight: false,
		animated: 'slide'
	});
	
	
	//WAJ Detail tabs
	vTabs = $("#joinery-tabs .tabs").append("<ul></ul>");
	$("#joinery-tabs .cmhtml-section").each(function(i){
		vTitle = $(this).find("h2").addClass("hide").html();
		vTabId = "tab-" + i;
		$("ul", vTabs).append("<li><a href='#" + vTabId + "'>" + vTitle + "</a></li>");
		$(this).attr("id",vTabId).addClass("tab-content");
	});
	
	// W&J detail page tabs
	$("div.tabs ul").tabs();
	
	
	function onAfter(curr, next, opts) {
		var index = $(this).parent().children().index(this);
		$('#prev')[index == 0 ? 'hide' : 'show']();
		$('#next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
	}
	
	var startSlide = parseInt($(document).getUrlParam("startSlide")); 
	if (isNaN(startSlide)) {
		startSlide = 0;
	}

	if ($("#slideshow li").length > 1) {
		$("#slideshow")
			.append("<a id='prev' href='#'><img width='32' height='32' alt='Previous' src='"+contextPath+"images/slideshow-previous.gif'/></a>")
			.append("<a id='next' href='#'><img width='32' height='32' alt='Next' src='"+contextPath+"images/slideshow-next.gif'/></a>");
	}
	$('#slideshow ol.slides').cycle({
		fx: 'fade',
		speed: 'fast',
		timeout: 0,
		nowrap: 1,
		next: '#next',
		prev: '#prev',
		after: onAfter,
		startingSlide: startSlide
	});
	
	
	//Experts links
	$("#meet-our-experts li a").each(function(count){
		vHref = $(this).attr("href");
		vHref = vHref.substring(0,vHref.indexOf("#"));
		vHref = vHref + '&fragment=true&TB_iframe=true';
		$(this).attr("href",vHref);
	});
	
	
	//Fragment links
	$("#brochure a, #technical-drawings a, #send-photo a, #arrange-visit a, #gallery-overlay-link, #architects-and-professionals-drawings-list a").each(function(count){
		vHref = $(this).attr("href");
		vHref = vHref + '&fragment=true&TB_iframe=true';
		$(this).attr("href",vHref);
	});
	
	$("#fragment form").validate();
	
	
	/* Dynamic sub-menus
	 * 
	 * */ 	
	$("#nav ul ul").hide();
	$("#nav ul li.on ul").show();
	$("#nav > ul > li").each(function (i) {
		$(this).bind("mouseenter", function(e){
			$("#nav ul ul").hide();
				$(this).find('ul').show(); 
		});
	})
	/*
	$("#nav ul ul:visible").bind("mouseout", function(e){
		$("#nav ul ul:visible").hide();
	});
	*/
	$("#nav").bind("mouseleave", function(e){
		$("#nav ul ul").hide();
		$("#nav > ul > li.on ul").show();
	});
	
	
});

