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","181px"); }
			if($(this).parents(".hoverBox:eq(0)").hasClass("col2")) { $div.css("left","362px"); }
			if($(this).parents(".hoverBox:eq(0)").hasClass("col3")) { $div.css("right","362px"); }
			if($(this).parents(".hoverBox:eq(0)").hasClass("col4")) { $div.css("right","181px"); }

			$(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","181px"); }
				if($(this).parents(".hoverBox:eq(0)").hasClass("col2")) { $div.css("left","362px"); }
				if($(this).parents(".hoverBox:eq(0)").hasClass("col3")) { $div.css("right","362px"); }
				if($(this).parents(".hoverBox:eq(0)").hasClass("col4")) { $div.css("right","181px"); }
			}); 
		}
	}
}

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;
}

$(document).ready(function(){

	// search watermark
    $("#zoom_query").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Search report"
        }).removeClass("watermarkOn").val("");
    });
    $("#zoom_query").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermarkOn").val("Search report");
    });
	$("#zoom_query").addClass("watermarkOn").val("Search report");
	
	// search highlight
	highlight();


	// adjust height of info boxes
	var maxHeight = 0;
	$('.matchHeight').each(function(){
		var thisHeight = $(this).height();
		if (thisHeight > maxHeight) { maxHeight = thisHeight; }
	});
	$('.matchHeight').height(maxHeight);

	

	$('.show-table[href]').each(function(){
		var miniTableHide = $(this).attr('href').split('#');
		$('#' + miniTableHide[1]).hide();
	});

	// Toggle show/hide mini tables
	$('.show-table[href]').click(function(){
		
		var parentLink = $(this);
		var show = $(this).attr('href').split('#');
				
		$('#' + show[1]).slideToggle('slow', function(){
			if($(parentLink).attr('class')=='show-table') {
				$(parentLink).removeClass('show-table');	
				$(parentLink).addClass('hide-table');	
				$(parentLink).html('Hide data table');
			} else {
				$(parentLink).removeClass('hide-table');
				$(parentLink).addClass('show-table');
				$(parentLink).html('Show data table');
			}
		});
				
		return false;
	});


	// Audit details hide
	$('#audit-details').hide();
	
   // Match all link elements with href attributes within the content div
   $('#audited a[href]').qtip(
   {
      content: $('#audit-details'),
	  position: {
		  corner: { target: 'bottomRight', tooltip: 'topRight' },
		  adjust: { x: -2, y:0 }
	  },
	  hide: { when: 'mouseout', fixed: true },
	  style: {
		  background: '#efefef',
		  padding: 6,
		  border: {
			  width: 2,
			  radius: 2,
			  color: '#666666'
		  }
	  }
   });
   
   // committee icons
   $('ul.committee-icons a img[alt]').qtip(
   {
	  position: {
		  corner: { target: 'bottomMiddle', tooltip: 'topMiddle' }
	  },
	  hide: { when: 'mouseout', fixed: true },
	  style: {
		  background: '#D7D8EF',
		  padding: 6,
		  border: {
			  radius: 2,
			  color: '#212298'
		  },
		  tip: true
	  },
	  tip: 'topLeft'
   });
   
   // 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");  
	  }  
	);
	
	/* 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;
			}
		);
	});
	
	// external links
	$("a[rel='external']").addClass("external").click(function() { window.open($(this).attr("href")); return false; });
	
	// show biographies
	$("a[class='hoverBoxToggle']").click(function() {
		MT.hoverBox.toggle(this,'hoverBoxGroup','Show biographies','Hide biographies');
		return false;
	});
	
	// tracking pdf and xls
	$("a[href*=.pdf]").click( function() {
		pageTracker._trackEvent("Downloads", "PDF", $(this).attr("href"));
	});
	
	$("a[href*=.xls]").click( function() {
		pageTracker._trackEvent("Downloads", "XLS", $(this).attr("href"));
	});
	
	
});
