$(function(){
	// runs on DOM load
	icr.init();
});

function submitform()
		{
		document.icrsearch.submit();
		}

icr = {
	currInset:1,
	init:function(){
		profTools.tweakSearch("input.enhanceSearch");
		profTools.externaliseLinks("romeo");
		//icrgf.org
		
		if( document.getElementById("homeBottom") ){
			icr.setupHomeBoxes();
			icr.setupHomeInset();
		}
		
		if( document.getElementById("articleInset") ){
			
			icr.setupArticleInset();
		}
		
		if( $("div.bioContainer img#scientist").length > 0){
			icr.setupBios();
		}
		
		if( $("h3#director").length > 0){
			icr.setupDirectors();
		}
		
	},
	setupHomeBoxes:function(){
		$("div.colHilite a").each(function(){
			var ind = $("div.colHilite a").index(this);
			var wUrl = $("div#col"+(ind+1)+" a.thisOne").attr("href");
			$(this).attr("href", wUrl);
		});
		
		$("div#homeBottom div.homeCol").mouseover(function(){
			var ind = $("div#homeBottom div.homeCol").index(this);
			
			$("div.homeCol").removeClass("hilite");
			$("div#col"+(ind+1)).addClass("hilite");
			
			$("div.colHilite").hide();
			$("div#col"+(ind+1)+"hilite").show();
		});
		$("div#col2").mouseover();
	},
	setupHomeInset:function(){
		$("div#homeInset").append('<div id="controls"></div>');
		$("div#homeInset div#controls").load("fragments/insetcontrols.php div", {}, function(){
			icr.setupHomeInsetControls();
		});
		/* Uncomment this to add a number at the start of a title for the home features
		$("div#homeInset div.inset").each(function(i){
			var linkText = $(this).find("p a").text();
			$(this).find("p a").text((i+1)+". "+linkText);
		});
		*/
	},
	setupHomeInsetControls:function(){
		$("div#controls li#prev a").unbind("click").click(function(){
			icr.rotateHomeInset(-1);
			this.blur();
			return false;
		});
		$("div#controls li#next a").click(function(){
			icr.rotateHomeInset(1);
			this.blur();
			return false;
		});
	},
	rotateHomeInset:function(dir){
		var numInsets = $("div#homeInset div.inset").length;
		var newInset = icr.currInset + dir;
		if(newInset > numInsets){
			newInset = 1;
		}
		if(newInset < 1){
			newInset = numInsets;
		}
		
		$("div#homeInset div.inset").hide();
		$("div#homeInset div#inset"+newInset).show();
		
		//$("div#controls span#insetCurrent").text(newInset).css("background-position", "0 "+((1-newInset)*41)+"px");
		
		icr.currInset = newInset;
	},
	setupArticleInset:function(){
		$("div#articleInset").append('<div id="controls"></div>');
		
		$("div#articleInset div#controls").load("fragments/insetcontrols.php div", {}, function(){
			icr.setupArticleInsetControls();
		});
		/*  Uncomment this to add a number at the start of a title for the feature index
		$("div#articleInset div.inset").each(function(i){
			var linkText = $(this).find("p:first a").text();
			$(this).find("p:first a").text((i+1)+". "+linkText);
		});
		*/
	},
	setupArticleInsetControls:function(){
		$("div#controls li#prev a").unbind("click").click(function(){
			icr.rotateArticleInset(-1);
			this.blur();
			return false;
		});
		$("div#controls li#next a").click(function(){
			icr.rotateArticleInset(1);
			this.blur();
			return false;
		});
	},
	rotateArticleInset:function(dir){
		var numInsets = $("div#articleInset div.inset").length;
		var newInset = icr.currInset + dir;
		if(newInset > numInsets){
			newInset = 1;
		}
		if(newInset < 1){
			newInset = numInsets;
		}
		
		$("div#articleInset div.inset").hide();
		$("div#articleInset div#inset"+newInset).show();
		
		//$("div#controls span#insetCurrent").text(newInset).css("background-position", "0 "+((1-newInset)*41)+"px");
		
		icr.currInset = newInset;
	},
	setupBios: function(){
	// Hide all content
	$("div.bioContinuation").slideToggle();
			
			$("div.bioContainer").each(function(){
			$(this).addClass("closedBio");
			$(this).append('<a href="toggle" class="toggleLink closeLink">Close</a>');
			$(this).append('<a href="toggle" class="toggleLink openLink">See extended bio</a>');
			$(this).find("a.toggleLink").click(function(){
				icr.toggleBio($(this).parents(".bioContainer"));
				return false;
			});
		});
		
		$("div.bioContainer a.closeLink").click();
		
//		$("div.bioContainer a.closeLink:not(:first)").click();
	},
	setupDirectors: function(){
		
		$("h3.director").each(function(){
				$(this).addClass("closedBio");
				$(this).append('<a href="toggle" class="toggleLink closeLink">Close</a>');
				$(this).append('<a href="toggle" class="toggleLink openLink">See extended bio</a>');
				$(this).find("a.toggleLink").click(function(){
					icr.toggleBio($(this).parents(".bioContainer"));
					return false;
				});
			});
		$(wBio).find(".bioContinuationWide").slideToggle();
		$("div.bioContainerWide a.closeLink").click();
		$("div.bioExcerptWide a.closeLink").click();	

	},
	toggleBio:function(wBio){
		$(wBio).toggleClass("closedBio").toggleClass("openBio");
		$(wBio).find(".bioContinuationWide").slideToggle();
		$(wBio).find(".bioContinuation").slideToggle();
		}
	
}



















