var myTimer = {};
MT = {};

var defaultPage = "";
var currentPage = "";
var currentElement = "";
var canSwap = true;
MT.clickAndShow = 
{
	init: function()
	{
		$(".galleryLink").each( function() {

			var el = $(this).parents(".galleryBoxInner:eq(0)").attr("id");
			var show = $(this).attr("href");
			var $div = $(show); 
			var height = $div.height(); 
			$div.hide().css({ height : 0 });
			
			$(this).parents(".galleryBoxInner:eq(0)").bind (
				"click",
				function(){
					var $hideDiv;
					//alert(currentPage + ", " + show);
					if((currentPage != show) && canSwap)
					{
						$('#' + el).parent().addClass("selected");
						canSwap = false;

						if(currentPage != "")
						{
							$hideDiv = $(currentPage);		
							$('#' + currentElement).parent().removeClass("selected");
							$hideDiv.animate({ height: 0 }, { duration: 300, easing: "easeOutQuad", complete: function () {
								$hideDiv.hide();
								$div.show().animate({ height : height }, { duration: 300, easing: "easeOutQuad", complete: function() {
									currentPage = show;
									currentElement = el;
									canSwap = true;
								}})
							} });
						}
						else
						{
							// first one...	
							$div.show().animate({ height : height }, { duration: 300, easing: "easeOutQuad", complete: function() {
								currentPage = show;
								currentElement = el;
								canSwap = true;
							}})
						}
					}
					return false;
				}
			);
		});
	}
};


var doHover = false;
var fadeToOpacity = 0.5;
MT.hoverBox = 
{
	init: function ()
	{
		$(".hoverBoxToggle").show();
		
		doHover = true;
		$(".hoverBoxLink[rel]").each( function() {

			$(this).bind(
				"click",
				function() { return false; }
			);

			var rel = $(this).attr("rel");
			var show = $(this).attr("href");
			var $img = $(this).find('img').eq(0);
			var alt = $img.attr("alt");
			var $div = $(show);
			//var height = $div.height();

			//$div.hide().css("opacity","0");
			$div.hide();
			
			//$div.hide().css({ height : 0 });

			// set position based on class
/*			$div.css("position","absolute");
			$div.css("width","300px");
			$div.css("top","30px"); */
			
			$div.addClass("hoverInfo");
			$div.css("top",$(this).parents(".hoverBox:eq(0)").position().top);
			if($(this).parents(".hoverBox:eq(0)").hasClass("col1")) { $div.css("left","186px"); }
			if($(this).parents(".hoverBox:eq(0)").hasClass("col2")) { $div.css("left","372px"); }
			if($(this).parents(".hoverBox:eq(0)").hasClass("col3")) { $div.css("right","372px"); }
			if($(this).parents(".hoverBox:eq(0)").hasClass("col4")) { $div.css("right","186px"); }

			$(this).parents(".hoverBox:eq(0)").hover (
				function() {

					if(doHover)
					{
						$.clearTimer(myTimer);
						$(this).css("z-index","0").animate({opacity:1}, 0);
						$img.attr("alt",""); // remove alt text as it interferes with popup text
						
						// show content box for this item
						$div.show().css("z-index","999");//.animate({opacity:1}, 0);
						
						// fade other boxes with the same rel
						if(rel!="")
							$("[rel=" + rel + "]").each( function() {
								if($(this).attr("href") != show)
									if($(this).parents(".hoverBox:eq(0)").css("opacity") > fadeToOpacity) { $(this).parents(".hoverBox:eq(0)").css("z-index","0").animate({opacity:fadeToOpacity},0); }
						});
					}
				},
				function() {
					if(doHover)
					{
						// hide content box for this item
						//$div.show().animate({opacity:0},{ duration:0, complete: function(){ $div.hide(); }});
						$div.hide();
						$div.css("z-index","0");
						$img.attr("alt",alt); // restore alt text
	
						// restore other boxes with the same rel
						if(rel!="")
							myTimer = $.timer(300, function(){
								$("[rel=" + rel + "]").each( function() {
									if($(this).attr("href") != show)
										if($(this).parents(".hoverBox:eq(0)").css("opacity") < 1) { $(this).parents(".hoverBox:eq(0)").animate({opacity:1},200); }
							});
						});
					}
				}
			);
		});
	},
	toggle: function(e,group,showtext,hidetext) {
		if(doHover)
		{
			doHover = false;
			$(e).text(hidetext);
			$(e).addClass("hoverBoxHide");
			$("[rel=" + group + "]").each( function() {
				var show = $(this).attr("href");
				var $div = $(show);
	
				$div.show();
				$div.removeClass("hoverInfo");
				$div.css("left","auto");
				$div.css("right","auto");
				$div.css("top","0");
			}); 
		}
		else
		{
			doHover = true;
			$(e).text(showtext);
			$(e).removeClass("hoverBoxHide");
			$("[rel=" + group + "]").each( function() {
				var show = $(this).attr("href");
				var $div = $(show);
	
				$div.hide();
				$div.addClass("hoverInfo");
				$div.css("top",$(this).parents(".hoverBox:eq(0)").position().top);
				if($(this).parents(".hoverBox:eq(0)").hasClass("col1")) { $div.css("left","186px"); }
				if($(this).parents(".hoverBox:eq(0)").hasClass("col2")) { $div.css("left","372px"); }
				if($(this).parents(".hoverBox:eq(0)").hasClass("col3")) { $div.css("right","372px"); }
				if($(this).parents(".hoverBox:eq(0)").hasClass("col4")) { $div.css("right","186px"); }
			}); 
		}
	}
}


$(document).ready(function() {

	/* do search */
	var watermarkString = "Search report...";
    $("#zoom_query").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == watermarkString
        }).removeClass("watermarkOn").val("");
    });
    $("#zoom_query").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermarkOn").val(watermarkString);
    });
	$("#zoom_query").addClass("watermarkOn").val(watermarkString);
	$("#form_search").submit(function() {
		return($("#zoom_query").val()!=watermarkString);
	});
	highlight();
	
	
	/* add 'print this page' link to page tools - JS is required for it to work, so adding here will hide from non-JS users */
	$('.pageTools ul').prepend('<li class="print"><a href="javascript:void(0);" onclick="window.print(); return false;">Print page</a></li>');
	

	/* do hoverBox */
	MT.hoverBox.init();
	
	/* do clickAndShow */
	MT.clickAndShow.init();
	
	/* do clickable sections */
	$(".clickSection").click(function() {  
  		window.location = $(this).find("a.clickSectionLink:first").attr("href");  
	});

	$(".clickSection").hover(  
	  function() {  
		$(this).addClass("clickSectionHover");  
	  },  
	  function() {  
		$(this).removeClass("clickSectionHover");  
	  }  
	);
	
	
	/* test */
	//$("#test").load("test.html #test1");
	
	/* carousel */
	/* $(".gallery").infiniteCarousel(); */
	
	
	/* do sliders */
	$(".btnToggle").each(
	function(){

		$(this).show();
		var show = $(this).attr("href");
		var $div = $(show);
		var height = $div.height();
		$div.hide().css({ height : 0 });
		
		var originalText = $(this).text();
		var newText = $(this).attr("rel");

		$(this).bind (
			"click",
			function(){
				if ($div.is(':visible')) {
					if(newText!=""){ $(this).text(originalText); }
					$(this).removeClass("selected");
					$div.animate({ height: 0 }, { duration: 500, easing: "easeOutQuad", complete: function () {
						$div.hide();
					} });
				} else {
					if(newText!=""){ $(this).text(newText); }
					$(this).addClass("selected");
					$div.show().animate({ height : height }, { duration: 500, easing: "easeOutQuad" });
					//reSIFR();
				}
				return false;
			}
		);
	});
	

	
});


function MM_jumpMenu(targ,selObj,restore){ //v3.0
  if(selObj.options[selObj.selectedIndex].value!=''){
	  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	  if (restore) selObj.selectedIndex=0;
  }
}
function doFastTrack(objID) {
	var obj = MM_findObj(objID);
	if(obj[obj.selectedIndex].value!='') { MM_jumpMenu('parent',obj,0); }
	return false;
}

