$(document).ready(function(){

	//
	// accordion menu: http://www.webdesignerwall.com/tutorials/jquery-tutorials-for-designers/
	//
	$(".accordion .heading:first").addClass("active");
	$(".accordion .slide:not(:first)").hide();

	$(".accordion .heading").click(function(){
		$(this).next(".slide").slideToggle().siblings(".slide:visible").slideUp();
		$(this).toggleClass("active");
		$(this).siblings(".heading").removeClass("active");
	});

	$(".touraccordion h3:first").addClass("active");
	$(".touraccordion ul:not(:first)").hide();

	$(".touraccordion h3").click(function(){
		$(this).next("ul").slideToggle().siblings("ul:visible").slideUp();
		$(this).toggleClass("active");
		$(this).siblings("h3").removeClass("active");
	});

	$(".expandit .slide").hide();

	$(".expandit .heading").click(function(){
		$(this).next(".slide").slideToggle();
		$(this).toggleClass("active");
	});

	$(".pmtoggle .content").hide();

	$(".pmtoggle .heading").click(function(){
		$(this).next(".content").toggle();
		$(this).toggleClass("active");
	});

	$(".pmtoggle .showall").click(function(){
		$(".pmtoggle .content").show();
		$(".pmtoggle .heading").addClass("active");
	});

	$(".pmtoggle .hideall").click(function(){
		$(".pmtoggle .content").hide();
		$(".pmtoggle .heading").removeClass("active");
	});

	//$(".gmenu").fixedMenu();

	//$(".gmenu ul li:has(ul)").click(function(){
		//$(this).addClass("active");
		//$(this).hover(function(){
		//},function(){
			//$(this).removeClass("active");
		//});
	//});

	$(".gmenu ul li:has(ul)").hover(function(){
		$(this).addClass("active");
	},function(){
		$(this).removeClass("active");
	});

	$(".gtoggle .content").hide();

	$(".gtoggle .heading").click(function(){
		$(this).next(".content").toggle();
		//$(this).toggleClass("active");
		$(this).find("span").toggleClass("rightarrow downarrow");
	});

	$(".gaccordion .heading:first").addClass("active").find("span").toggleClass("rightarrow downarrow");
	$(".gaccordion .content:first").show();

	$(".gaccordion .heading").click(function(){
		$(this).next(".content").slideToggle().siblings(".content:visible").slideUp();
		$(this).toggleClass("active").find("span").toggleClass("rightarrow downarrow");
		$(this).siblings(".heading.active").removeClass("active").find("span").toggleClass("rightarrow downarrow");
	});

	//
	// set top margin for first h2 heading with dropshadow....
	//
	$(".h2dropshadow").first().css("margin-top", "0.5em");

	//
	// append Google +1 to first heading
	//
	$("h1.top").append("&nbsp;<g:plusone size='small' annotation='none'></g:plusone>");

	//
	// append LinkedIn link to byline
	//
	$("#byline").append("&nbsp;<script src='http://platform.linkedin.com/in.js' type='text/javascript'></script><script type='IN/MemberProfile' data-id='http://www.linkedin.com/in/hydrosolve' data-format='click'></script>").after("<script type='IN/Share'></script>");

	//
	// remove link in tour menu for the active page
	//
	var url = window.location.href;
	var items = $("ul.tourmenu li");
	var nitems = items.size()-1;
	items.each(function(i) {
		var link = $(this).find("a:first");
		if (url.search(link.attr("href")) >= 0) {
			var text = $(this).text();
			link.remove();
			$(this).text(text);
			var prev = "";
			if (i > 0) prev = $(this).prev().html();
			var home = "<a href='tour.htm'>Tour Home</a>";
			var next = "";
			if (i < nitems) next = $(this).next().html();
			var menu = "<table id='tournav'><td class='left'>" + prev + "</td><td class='center'>" + home + "</td><td class='right'>" + next + "</td></table>";
			$("#content").append(menu);
			return false;
		}
	});

	//
	// append feedback to content section
	//
	if (document.title.search("Search") == -1)
	{
		$("#content").append("<p class='small center'><img border='0' src='http://www.aqtesolv.com/images/comments2.gif' width='16' height='16'><b><i>Comments? Suggestions?</i></b> Please send your <a href='http://www.aqtesolv.com/contact.asp'>feedback</a>.&nbsp;<g:plusone size='small' annotation='none'></g:plusone></p>");
	}

	//
	// image magnifier: modified from http://justinfarmer.com/?p=14
	// modifications allow multiple images embedded in HTML tables
	//
	var oWidth;
	var oHeight;
	$("img.resize").each(function(i) {
		oHeight = $(this).height();
		oWidth = $(this).width();
		$(this).data("oheight", oHeight);
		$(this).data("owidth", oWidth);
		var td = $(this).closest("td");
		td.width(oWidth).height(oHeight + 2*td.closest("table").attr("cellpadding"));
		$(this).css("position", "absolute");
	});
	$(".tourfigure p").each(function(i) {
		$(this).first().css("margin-top", "0");
	});
	var magfactor;
	$("img.resize").hover(function(){
		$(this).css("z-index","1");
		var td = $(this).closest("td");
		oWidth = $(this).data("owidth");
		oHeight = $(this).data("oheight");
		var offset = $(this).offset();
		var docHeight = $(document).height();
		magfactor = Math.min(Math.max((docHeight - offset.top)/oHeight*0.99, 1), 1.75);
		$(this).stop()
		.animate({
			width: (oWidth * magfactor) + "px",
			height: (oHeight * magfactor) + "px"
		},'fast');
	},
	function(){
		$(this).css("z-index","auto");
		$(this).stop()
		.animate({
			width: oWidth +'px',
			height: oHeight +'px'
		},'fast');
	});

});

(function($){
    $.fn.fixedMenu=function(){
        return this.each(function(){
            var menu= $(this);
            menu.find('ul li > a').bind('click',function(){
            if ($(this).parent().hasClass('active')){
                $(this).parent().removeClass('active');
            }
            else{
                $(this).parent().parent().find('.active').removeClass('active');
                $(this).parent().addClass('active');
            }
            })
        });
    }
})(jQuery);

