
//Jquery Functions
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

jQuery(function( $ ){
				
	$(window).load(function () {
		this.surveyHeight = $("#surveyWrapperContent").height();
		$("#loading").height(this.surveyHeight);
		//alert("#surveyWrapperContent height = " + this.surveyHeight);	   
		this.ltimer = setInterval ('$(fadeLoading());', 2000);
		fadeLoading = function(){	
			$("#loading").fadeOut("fast"); 
			clearInterval(this.ltimer);
	}
		$("#processing").height(this.surveyHeight);
		//alert("#surveyWrapperContent height = " + this.surveyHeight);	   
		this.ptimer = setInterval ('$(fadeProcessing());', 2000);
		fadeProcessing = function(){	
			$("#processing").fadeOut("fast"); 
			clearInterval(this.ptimer);
	}
	});	
	
	
	$(document).ready(function(){							   
	// Set class on snav
	$("#snav li:first").addClass("first");
	
	//Set table row stuff :)			
	//$("table.underlined tr:last").addClass("noborder");
	$('#results table tbody tr:odd').addClass('odd');
	$('#results table tbody tr:even').addClass('even');		
	
	
	/* MENU SYSTEM */
	//Give odd rows a different color in mnav drop menu
	$('#nav li ul li:odd').addClass('navodd');
	//Remove border on last li elements
	$("#nav li ul").each(function(i){
		$('li:last', this).addClass('noBrd');
	});
	//Hover
    $('#nav ul li').hover(function () {
		this.ulWidth = $('ul', this).width();
		var posLeft = $(this).offset(); 
		$('ul', this).css("left", posLeft.left-2);			
		$('ul', this).width(this.ulWidth);			
		$('li a', this).width(this.ulWidth-19);		
        $('ul', this).fadeIn("fast");	
      },
      function () {
	  	obj = this;
	  	$('ul', this).fadeOut("fast", function(){ 
	
		});
      }
    );
	
	/* NAV SELECTOR */
	$("#nav ul li#mnav"+section).addClass("active")
	
	
	
		
	$(function() {
        var expand   = function(){ $(this).width(600) }
        var contract = function(){ if (!this.noHide) $(this).width(400) }
        var focus    = function(){ this.noHide = true }
        var blur     = function(){ this.noHide = false; contract.call(this) }
		//$('#hospitalSelect')
        $('#ctl00_ContentPlaceHolder1_MainContentControl1_ctl00_ddHospitalName')
                .hover(expand, contract)
                .focus(focus)
                .click(focus)
                .blur(blur)
                .change(blur)
	});
	
	//Remove line from last CTA and tables
	$(".cta:last").addClass("cta-last");
	
	$("table.borders tr:last-child td").each(function(i){
			$(this).css("border-bottom","0px solid #ffffff")
	});
	$("table.borders tr td:last-child").addClass("nobrdright");
	
	//Accordian Nav				   
	$(".accordion-nav h2:first").addClass("active");
	$(".accordion-nav h2:first").addClass("first");
	$(".accordion-nav div:first").show();
	$(".accordion-nav h2").click(function(){
		$(this).next("div").slideToggle("fast")
		.siblings("div:visible").slideUp("fast");
		$(this).toggleClass("active");
		$(this).siblings("h2").removeClass("active");
	});
	
			
	//FAQ Nav, used for FAQ, Time Commitments and People
	$(".faq-accordion-nav h3:first").addClass("first");
	$(".faq-accordion-nav div").hide();
	$(".faq-accordion-nav h3").click(function(){
		$(this).next("div").slideToggle("fast")
		//.siblings("div:visible").slideUp("fast");
		$(this).toggleClass("active");
		//$(this).siblings("h3").removeClass("active");
	});
	
	//Show/Hide Committee Descriptions
	$(".committee:first").addClass("last");
	$(".committee .committee-content").hide();
	$(".committee p.more").click(function(){
		$(this).next(".committee-content").slideToggle("fast")
		.siblings(".committee-content:visible").slideUp("fast");
		$(this).toggleClass("active");
		$(this).siblings("p.more").removeClass("active");		
	});
						

	thehash = document.location.hash;
	//alert(thehash);
	$(thehash).next("h3").next("div").slideToggle("fast");
	$(thehash).next("h3").toggleClass("active");
	
	
	//Show/Hide Help Menus
		//Search Help
		$(".help").hide();
		$(".search legend").click(function(){
			$(this).next("div").slideToggle("fast");
		});	
		
		//Survey Help
		$(".contentblock .help").hide();
		$(".contentblock span.section-title").live("click", function(){
			$(this).parent().parent().parent(".contentblock").children(".help").slideToggle("fast");
		});
			  
		$(".helpBtn").live("click",function(){
			$(this).parent("p").next("div").slideToggle("fast");
		});
		
		$(".help .close").live("click",function(){
			$(this).parent("div").slideToggle("fast");
		});

	
	// Yoav Added May 21,2009
	
		$(".detailshelp").click(function(){
				$(this).next("div").slideToggle("fast");
		});
	// End added May 21,2009
	
	
	//Tooltip actions
	
	$(".noteClose").click(function(){
		$(this).parent("div").slideUp("fast");
	});
	
	$("#one a").hover(function(){		
		var pos = $(this).offset();            
		var eWidth = $(this).outerWidth();        
		var theHeight = $("#onetooltip").height();  		
		var left = (pos.left + 15) + "px";        
		var top = pos.top - theHeight+ "px";
		//alert(theHeight);
		$("#onetooltip").css( {
				position: 'absolute',             
				zIndex: 5000,                
				left: left,                 
				top: top        
		} );
		this.timer = setInterval ('$(poptip());', 600);
		poptip = function(){	
		//alert(poptip);
		$("#onetooltip").fadeIn("fast"); }},
		function(){	
			$("#onetooltip").fadeOut("fast"); 
			clearInterval(this.timer);
			}
		);
	
	$("#two a").hover(function(){		
		var pos = $(this).offset();            
		var eWidth = $(this).outerWidth();        
		var theHeight = $("#twotooltip").height();  		
		var left = (pos.left + 15) + "px";        
		var top = pos.top - theHeight+ "px";
		//alert(theHeight);
		$("#twotooltip").css( {
				position: 'absolute',             
				zIndex: 5000,                
				left: left,                 
				top: top        
		} );
		this.timer = setInterval ('$(poptip());', 600);
		poptip = function(){	
		//alert(poptip);
		$("#twotooltip").fadeIn("fast"); }},
		function(){	
			$("#twotooltip").fadeOut("fast"); 
			clearInterval(this.timer);
			}
		);
	
	$("#three a").hover(function(){		
		var pos = $(this).offset();            
		var eWidth = $(this).outerWidth();        
		var theHeight = $("#threetooltip").height();  		
		var left = (pos.left + 15) + "px";        
		var top = pos.top - theHeight+ "px";
		//alert(theHeight);
		$("#threetooltip").css( {
				position: 'absolute',             
				zIndex: 5000,                
				left: left,                 
				top: top        
		} );
		this.timer = setInterval ('$(poptip());', 600);
		poptip = function(){	
		//alert(poptip);
		$("#threetooltip").fadeIn("fast"); }},
		function(){	
			$("#threetooltip").fadeOut("fast"); 
			clearInterval(this.timer);
			}
		);
	
	$("#four a").hover(function(){		
		var pos = $(this).offset();            
		var eWidth = $(this).outerWidth();        
		var theHeight = $("#fourtooltip").height();  		
		var left = (pos.left + 15) + "px";        
		var top = pos.top - theHeight+ "px";
		//alert(theHeight);
		$("#fourtooltip").css( {
				position: 'absolute',             
				zIndex: 5000,                
				left: left,                 
				top: top        
		} );
		this.timer = setInterval ('$(poptip());', 600);
		poptip = function(){	
		//alert(poptip);
		$("#fourtooltip").fadeIn("fast"); }},
		function(){	
			$("#fourtooltip").fadeOut("fast"); 
			clearInterval(this.timer);
			}
		);
	
	$("#five a").hover(function(){		
		var pos = $(this).offset();            
		var eWidth = $(this).outerWidth();        
		var theHeight = $("#fivetooltip").height();  		
		var left = (pos.left + 15) + "px";        
		var top = pos.top - theHeight+ "px";
		//alert(theHeight);
		$("#fivetooltip").css( {
				position: 'absolute',             
				zIndex: 5000,                
				left: left,                 
				top: top        
		} );
		this.timer = setInterval ('$(poptip());', 600);
		poptip = function(){	
		//alert(poptip);
		$("#fivetooltip").fadeIn("fast"); }},
		function(){	
			$("#fivetooltip").fadeOut("fast"); 
			clearInterval(this.timer);
			}
		);
	
	//http://gsgd.co.uk/sandbox/jquery.easing.php, borrowed from jQuery easing plugin
	$.easing.elasout = function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	$('div.pane').scrollTo( 0 );//reset all scrollable panes to (0,0)
	$.scrollTo( 0 );//reset the screen to (0,0)
	
	
	//COMMENTED OUT -->BACK TO TOP AND TABLE OF CONTENTS	
	//Back to Top
	$('.backToTop a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 1200, { easing:'elasout' });
		return false;
	});	
	
	/*Table of Contents
	$('#toc a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 1200, { easing:'elasout' });
	});
	*/
	
	});
});

//Random Image Script
	
myImages=new Array();
myImages[0]='images/imgFeature1.jpg';
myImages[1]='images/imgFeature2.jpg';
myImages[2]='images/imgFeature3.jpg';
myImages[3]='images/imgFeature4.jpg';
myImages[4]='images/imgFeature5.jpg';
myImages[5]='images/imgFeature6.jpg';

function newImage()
{
	document.write('<img src="'+myImages[Math.round(Math.random()*6)]+'" border=0 />');
}



function popWindow(URL,name,widgets) {
	popwin = window.open(URL,name,widgets);
	popwin.focus();	
	popwin.opener = self;
	popwin.name = name;
	// wrap in an if statement; don't rename if window.name already exists
	window.name = "MainWin";			
}

//	window code used on detail page
	var totalHeight = screen.availHeight;
	var totalWidth = screen.availWidth;
	function popWin(URL,name) {
		if(totalHeight > 600){
			theHeight = 800;
			theWidth =  800;
		}else{
			theHeight = 800;
			theWidth =  800;
		}
		popwin = window.open(URL,name,'height='+theHeight+',width='+theWidth+',resizable=1');
		popwin.focus();
		popwin.opener = self;
		popwin.name = name;
		// wrap in an if statement; don't rename if window.name already exists
		window.name = "MainWin";
	}


function closeWin(){
	window.close()
}

function printPage(lang) {
  if (window.print) {
    window.print() 
  } else if (lang == "fr/") {
    alert("Sorry, your browser doesn't support this feature. [FR] \nUse the print option on your browser to print this page"); 
  } else if (lang == "en/") {
    alert("Sorry, your browser doesn't support this feature. \nUse the print option on your browser to print this page"); 
  } else {
  	alert("Sorry, your browser doesn't support this feature. \nUse the print option on your browser to print this page"); 
  }
}

/**************************************************************************************
show and hide a div
**************************************************************************************/
function getStyleObject(strId) {
	//alert(strId);
	if (document.getElementById && document.getElementById(strId)) {
		return document.getElementById(strId).style;
	} else if (document.all && document.all(strId)) {
		return document.all(strId).style;
	} else {
		return false;
	}
}
	
function fnShow(strDivId) {
	//alert(strDivId);
	var objStyle = document.getElementById(strDivId);
	//alert(objStyle);
	if(objStyle){	
		objStyle.style.display = "block"; 
	}
}

function fnHide(strDivId) {
	var objStyle = document.getElementById(strDivId);
	objStyle.style.display = "none"
}

function fnHideByClass(className){
	classCollection=new Array()
	var inc=0
	
	var alltags = document.getElementsByTagName("*")

	for (i=0; i<alltags.length; i++){
		if (alltags[i].className==className)
		classCollection[inc++]=alltags[i]
	}

	for (i=0; i<classCollection.length; i++){
		classCollection[i].style.display = "none" 	
	}
	
}

function fnShowBoxPanel(){
		if(document.getElementById('searchBox').style.display != 'block'){
			//show searchPanel
			fnShow('searchBox');
			fnHide('searchBoxCollectionBtn');
		}else{
			//hide searchPanel
			fnHide('searchBox');
			fnShow('searchBoxCollectionBtn');
		}	
	}
	
function fnShowPanel(){
		if(document.getElementById('searchBox').style.display != 'block'){
			//show searchPanel
			fnShow('searchBox');
			fnHide('searchCollectionBtn');
		}else{
			//hide searchPanel
			fnHide('searchBox');
			fnShow('searchCollectionBtn');
		}	
	}
	
function fnShowBoxPanelFr(){
		if(document.getElementById('searchBoxFr').style.display != 'block'){
			//show searchPanel
			fnShow('searchBoxFr');
			fnHide('searchBoxCollectionBtn');
		}else{
			//hide searchPanel
			fnHide('searchBoxFr');
			fnShow('searchBoxCollectionBtn');
		}	
	}	
	
function fnShowPanelFr(){
		if(document.getElementById('searchBoxFr').style.display != 'block'){
			//show searchPanel
			fnShow('searchBoxFr');
			fnHide('searchCollectionBtn');
		}else{
			//hide searchPanel
			fnHide('searchBoxFr');
			fnShow('searchCollectionBtn');
		}	
	}	
	
function fnShowExhPanel(){
		if(document.getElementById('searchBoxExhibition').style.display != 'block'){
			//show searchPanel
			fnShow('searchBoxExhibition');
			fnHide('searchCollectionBtn');
		}else{
			//hide searchPanel
			fnHide('searchBoxExhibition');
			fnShow('searchCollectionBtn');
		}	
	}
function fnShowExhPanelFr(){
		if(document.getElementById('searchBoxExhibitionFr').style.display != 'block'){
			//show searchPanel
			fnShow('searchBoxExhibitionFr');
			fnHide('searchCollectionBtn');
		}else{
			//hide searchPanel
			fnHide('searchBoxExhibitionFr');
			fnShow('searchCollectionBtn');
		}	
	}	
function toggleImg(imgId, imgFileName, srcFolder) {
	// *** Note: srcFolder is optional. It can be included if the image to be swapped is not located in /images/ 
	// (i.e. /images/en)
	var argLength = arguments.length;
	var imgFolder = "/images/";	
	if (argLength > 2) {
		imgFolder = imgFolder + srcFolder + '/';
	}
	var strFileExtension = ".gif";
	//alert(imgFolder + imgFileName + strFileExtension);
	document.getElementById(imgId).src = imgFolder + imgFileName + strFileExtension;
}

function getElementbyClass(className){
	classCollection=new Array()
	var inc=0
	var alltags = document.getElementsByTagName("*")
	for (i=0; i<alltags.length; i++){
		if (alltags[i].className==className)
		classCollection[inc++]=alltags[i]
	}
}

function fnShowHide(strDivId,linkDivId) {
	var objStyle = document.getElementById(strDivId);
	
	var  hideLinkStyle = 'hide' + strDivId;

	hideLinkStyle = document.getElementById(hideLinkStyle);
	
	var showLinkStyle = 'show' + strDivId;
	showLinkStyle = document.getElementById(showLinkStyle);
	
	if(objStyle){
		objStyle.style.display = "block"; 	
	}
	
	
		hideLinkStyle.style.display = "inline"; 
		showLinkStyle.style.display = "none"; 
	
	
}

function fnHideShow(strDivId,linkDivId) {

	var objStyle = document.getElementById(strDivId);
	var  hideLinkStyle = 'hide' + strDivId;
	//alert(hideLinkStyle);
	hideLinkStyle = document.getElementById(hideLinkStyle);
	var showLinkStyle = 'show' + strDivId;
	showLinkStyle = document.getElementById(showLinkStyle);
	
	if(!objStyle){
		objStyle.style.display = "block";
	}else{
		objStyle.style.display = "none"; 	
	}
	
	
		hideLinkStyle.style.display = "none"; 
		showLinkStyle.style.display = "inline"; 
	
	
}

function fnChangeClass(elementId,className){
	var currTabElem = document.getElementById(elementId); 

	currTabElem.setAttribute("class",className); 
	currTabElem.setAttribute("className",className); 

}

function slideShow(lang) {
	// set up list of images
	slidesArray = new Array()
	
	slidesArray[0]="/images/corporate/"+lang+"/imgHomeBanner1.jpg"
	slidesArray[1]="/images/corporate/"+lang+"/imgHomeBanner2.jpg"
	slidesArray[2]="/images/corporate/"+lang+"/imgHomeBanner3.jpg"
	slidesArray[3]="/images/corporate/"+lang+"/imgHomeBanner4.jpg"
	// load image
	document.images.homeBanner.src=slidesArray[i]
	
	// increment counter
	i=i+1;
	
	// when at the end clear the interval
	if(i==4) { 
	i=0; 
	/*window.clearInterval(interval);*/
	}
}

function resizeImage(imgName){
	theImage = imgName;
	var newHeight = "85";
	var newWidth = "85";
	imageHeight = theImage.height;
	imageWidth = theImage.width;
	alert( imageHeight + ' ' + imageWidth);
	if(imageHeight > imageWidth){
		newWidth = imageWidth * newHeight/imageHeight;
		newWidth = Math.round(newWidth);	
		alert('h ' + newHeight + ' ' + newWidth);
	}else{
		
		newHeight = imageHeight * newWidth/imageWidth;
		newHeight = Math.round(newHeight);	
		alert('w ' + newHeight + ' ' + newWidth);
	}
	
	
	theImage.height = newHeight;
	theImage.width = newWidth;			
}

function validateSearch(lang){
	keywords = window.document.forms[0].searchCollection.value;
	errorLog = "";	
	if (keywords.length == ""){
		if(lang.value == "fr"){
			errorLog = errorLog + "FR Please enter keywords to search. \n";
		}else{
			errorLog = errorLog + "Please enter keywords to search. \n";
		}
	}
	
	if (errorLog.length >= 1){
		alert(errorLog);
		errorLog = "";
		return false;
	} else {
		window.document.forms[0].submit;
		return true;
		return false;
	}
	
}

function validateExhSearch(lang){
	keywords = window.document.forms[0].searchExhibition.value;
	errorLog = "";	
	if (keywords.length == ""){
		if(lang.value == "fr"){
			errorLog = errorLog + "FR Please enter keywords to search. \n";
		}else{
			errorLog = errorLog + "Please enter keywords to search. \n";
		}
	}
	
	if (errorLog.length >= 1){
		alert(errorLog);
		errorLog = "";
		return false;
	} else {
		window.document.forms[0].submit;
		return true;
		return false;
	}
	
}

function validateAdvancedSearch(lang){

	keywords = window.document.forms[1].keywords.value;
	artifactTypeId  = document.getElementById('artifactTypeId').options[document.getElementById('artifactTypeId').selectedIndex].value;
	regionId = document.getElementById('regionId').options[document.getElementById('regionId').selectedIndex].value;
	peopleId = document.getElementById('peopleId').options[document.getElementById('peopleId').selectedIndex].value;
	materialId = document.getElementById('materialId').options[document.getElementById('materialId').selectedIndex].value;
	techniqueId = document.getElementById('techniqueId').options[document.getElementById('techniqueId').selectedIndex].value;
	mimsyId  = window.document.forms[1].mimsyId.value;
	//dimensions = window.document.forms[1].dimensions.value;
//	dimensions2  = window.document.forms[1].dimensions2.value;
//	colour = document.getElementById('colour').options[document.getElementById('colour').selectedIndex].value;
//	dateTypePeriod = window.document.forms[1].dateType[0].checked;
	dateTypeDateMade = window.document.forms[1].dateType[0].checked;
	dateTypeEra = window.document.forms[1].dateType[1].checked;
	//period = document.getElementById('period').options[document.getElementById('period').selectedIndex].value;
	dateMadeFrom = window.document.forms[1].dateMadeFrom.value;
	dateMadeTo = window.document.forms[1].dateMadeTo.value;
	era = document.getElementById('era').options[document.getElementById('era').selectedIndex].value;

	errorLog = "";	
	
	if( 
	   (keywords.length == "") &&
	   (artifactTypeId == 0) &&
	   (regionId == 0) &&
	   (peopleId == 0) && 
	   (materialId == 0) && 
	   (techniqueId == 0) && 
	   (mimsyId.length == "")&& 
	//   (dimensions.length == "" && dimensions2.length == "") &&
	//   (colour.length == "") && 
	//   ((dateTypePeriod && period.length == 0) || 
	   ((dateTypeDateMade && (dateMadeFrom == "" || dateMadeTo == "")) || 
	   (dateTypeEra && era.length == 0))	 
	){
	
	if(lang.value == "fr"){
			errorLog = errorLog + "FR Please enter an advanced search criteria. \n";
		}else{
			errorLog = errorLog + " Please enter an advanced search criteria. \n";
		}
	}
	
	if (errorLog.length >= 1){
		alert(errorLog);
		errorLog = "";
		return false;
	} else {
		window.document.forms[1].submit;
		return true;
		return false;
	}
	
}

function validateRefine(lang){
	dateMadeEarliest = window.document.forms[1].dateMadeEarliest.value;
	dateMadeLatest= window.document.forms[1].dateMadeLatest.value;
	
	errorLog = "";	
	
	if(dateMadeEarliest == "" || dateMadeLatest == ""){
	
	if(lang.value == "fr"){
			errorLog = errorLog + "FR Please enter date made criteria to refine your search. \n";
		}else{
			errorLog = errorLog + "Please enter date made criteria to refine your search. \n";
		}
	}
	
	if (errorLog.length >= 1){
		alert(errorLog);
		errorLog = "";
		return false;
	} else {
		window.document.forms[1].submit;
		return true;
		return false;
	}
	
}

function fnExpand(strDivId) {
	//alert(strDivId);
	var objStyle = document.getElementById(strDivId);
	//alert(objStyle);
	objStyle.style.display = "block"; 
}

function fnColapse(strDivId) {
	var objStyle = document.getElementById(strDivId);
	objStyle.style.display = "none"
}