

var DontClick;
var ChangeSectionURL;

function LoadFunction() {

new Asset.image('/images/template/icons/less.gif', {id: 'less'});
new Asset.image('/images/template/icons/less-highlighted.gif', {id: 'lessHighlighted'});
new Asset.image('/images/template/icons/more-highlighted.gif', {id: 'moreHighlighted'});
new Asset.image('/images/template/icons/spinner.gif', {id: 'spinner'});
	var x = document.getElementsByTagName('DIV');

	for (var i=0;i<x.length;i++) {
		if(x[i].id == 'Initial_list_Content') {
			x[i].onclick = function () {if(DivToChange.parentNode.id == 'SectionMain') {inSection = 'yes';} else {inSection = 'no';}SectionorList = 'list'; DivToChange = this.parentNode;ToggleDetails(DivToChange,SectionorList,inSection)}
			x[i].onmouseover = function () {SectionorList = 'list';DivToChange = this.parentNode;TogglerMouseOver(DivToChange,SectionorList)}
			x[i].onmouseout = function () {SectionorList = 'list';DivToChange = this.parentNode;TogglerMouseOut(DivToChange,SectionorList)}
		} else if(x[i].id == 'Initial_section_Content') {
			x[i].onclick = function () {inSection = 'no';SectionorList = 'section';DivToChange = this.parentNode;ToggleDetails(DivToChange,SectionorList,inSection)}
			x[i].onmouseover = function () {SectionorList = 'section';DivToChange = this.parentNode;TogglerMouseOver(DivToChange,SectionorList)}
			x[i].onmouseout = function () {SectionorList = 'section';DivToChange = this.parentNode;TogglerMouseOut(DivToChange,SectionorList)}
		}  else if(x[i].id == 'Initial_section_Content_open') {
			x[i].id = 'Initial_section_Content'
			inSection = 'no';SectionorList = 'section';DivToChange = x[i].parentNode;ToggleDetails(DivToChange,SectionorList,inSection)
		} 
	}
	document.onkeydown = KeyDown;   
	document.onkeyup = KeyUp;       

	var y = document.getElementsByTagName('SPAN');

	for (var i=0;i<y.length;i++) {
		if(y[i].id == 'TextSelector'){
			y[i].onmouseover = function () {DontClick = 'yes'}
			y[i].onmouseout = function () {DontClick = 'no'}
		}  else if(y[i].id == 'ChangeSectionContents'){
			y[i].onclick = function () {
				inSection = 'no';
				SectionorList = 'section';
				DivToChange = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
				SectionDivToChange = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
				for (i=0; i<SectionDivToChange.childNodes.length; i++) {
					if (SectionDivToChange.childNodes[i].id=="ExpandedURL") {
						SectionDivToChange.childNodes[i].className = ChangeSectionURL;
					} else if (SectionDivToChange.childNodes[i].id=="CurrentState") {
						SectionDivToChange.childNodes[i].className = 'open';
					} else if (SectionDivToChange.childNodes[i].id=="ExpandedHeight") {
						SectionDivToChange.childNodes[i].className = 'notset';
					}
				}
				ToggleDetails(SectionDivToChange,SectionorList,inSection)
			}
		}
	}
	var z = document.getElementsByTagName('A');

	for (var i=0;i<z.length;i++) {
		z[i].onclick = function () {
			if (this.parentNode.id=="Initial_list_Content") {
				return false;
			} else  if (this.parentNode.id=="Initial_section_Content") {
				return false;
			}
		}
	}
}


var RefreshCache
RefreshCache=0;
function KeyDown(e)
{
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
   switch(KeyID)
   {
      case 82:
	RefreshCache = 1
      break; 
   }
}
function KeyUp(e)
{
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
   switch(KeyID)
   {
      case 82:
	RefreshCache = 0
      break; 
   }
}
function ToggleDetails(DivToChange,SectionorList,inSection) {
	if (DontClick == 'yes') {
		return true;
	} else {
		var UrlToLoad;
		var CurrentState;
		var CurrentStateNode;
		var InitialHeight;
		var InitialHeightNode;
		var ExpandedHeight;
		var ExpandedHeightNode;
		var idNumber;
		var Database;
		var InitialContentNode;
		var ExpandedContentNode;
		var URLToLoad;
		for (i=0; i<DivToChange.childNodes.length; i++) {
			if (DivToChange.childNodes[i].id=="CurrentState") {
				CurrentState = DivToChange.childNodes[i].className;
				CurrentStateNode = DivToChange.childNodes[i];
			} else if (DivToChange.childNodes[i].id=="InitialHeight") {
				InitialHeight = DivToChange.childNodes[i].className;
				InitialHeightNode = DivToChange.childNodes[i];
			} else if (DivToChange.childNodes[i].id=="ExpandedHeight") {
				ExpandedHeight = DivToChange.childNodes[i].className;
				ExpandedHeightNode = DivToChange.childNodes[i];
			} else if (DivToChange.childNodes[i].id=="ID") {
				idNumber = DivToChange.childNodes[i].className;
			} else if (DivToChange.childNodes[i].id=="Database") {
				Database = DivToChange.childNodes[i].className;
			} else if (DivToChange.childNodes[i].id=="Initial_" + SectionorList + "_Content") {
				InitialContentNode = DivToChange.childNodes[i];
			} else if (DivToChange.childNodes[i].id=="Expanded_" + SectionorList + "_Content") {
				ExpandedContentNode = DivToChange.childNodes[i];
			} else if (DivToChange.childNodes[i].id=="ExpandedURL") {
				UrlToLoad = DivToChange.childNodes[i].className;
			}
		}
		if (RefreshCache == 1) {
			 UrlToLoad = UrlToLoad + '&nocache=true';
			UrlToLoad = UrlToLoad.replace("&&","&");
		}
		if (InitialHeight == 'notset') {
			InitialHeight = DivToChange.scrollHeight;
		}
		
		if (CurrentState == 'initial') {
			window.status = 'Loading... please wait';
			ChangeProcess(DivToChange,UrlToLoad,CurrentState,CurrentStateNode,InitialHeight,InitialHeightNode,ExpandedHeight,ExpandedHeightNode,idNumber,Database,InitialContentNode,ExpandedContentNode,SectionorList,inSection);
		} else if (CurrentState == 'expanded')  {
			DivToChange.style.height = ExpandedHeight;
			ExpandedContentNode.style.height = ExpandedHeight - InitialContentNode.scrollHeight;
			ChangeProcess(DivToChange,UrlToLoad,CurrentState,CurrentStateNode,InitialHeight,InitialHeightNode,ExpandedHeight,ExpandedHeightNode,idNumber,Database,InitialContentNode,ExpandedContentNode,SectionorList,inSection);
		} else if (CurrentState == 'open')  {
			window.status = 'Loading... please wait';
			ChangeProcess(DivToChange,UrlToLoad,CurrentState,CurrentStateNode,InitialHeight,InitialHeightNode,ExpandedHeight,ExpandedHeightNode,idNumber,Database,InitialContentNode,ExpandedContentNode,SectionorList,inSection);
	
		}
		return false;
	}
}

function LoadDetails(DivToChange,UrlToLoad,CurrentState,CurrentStateNode,InitialHeight,InitialHeightNode,ExpandedHeight,ExpandedHeightNode,idNumber,Database,InitialContentNode,ExpandedContentNode,SectionorList,inSection) {
	
	function CallFinishFunction() {
		FinishFunction(DivToChange,UrlToLoad,CurrentState,CurrentStateNode,InitialHeight,InitialHeightNode,ExpandedHeight,ExpandedHeightNode,idNumber,Database,InitialContentNode,ExpandedContentNode,SectionorList,inSection)
	}
	
	if (UrlToLoad != '') {
		(function(){
			var ajax = new Ajax(UrlToLoad, {
			method:'get',
			evalScripts: true,
			onComplete:CallFinishFunction,
			wait:true,
			update:ExpandedContentNode
			}).request();
			}).delay(500);
	} else {
			CallFinishFunction();
	}
}

function ChangeProcess(DivToChange,UrlToLoad,CurrentState,CurrentStateNode,InitialHeight,InitialHeightNode,ExpandedHeight,ExpandedHeightNode,idNumber,Database,InitialContentNode,ExpandedContentNode,SectionorList,inSection){
	
	DivToChange.id = SectionorList + '-Loading';
	
	
	var summaryfadeout;
	var detailsfadeout;
	
	if (CurrentState == 'initial') {
		if (ExpandedHeight == 'notset') {
			var setInitialHeight = new Fx.Style(DivToChange, 'height', {wait: false, duration: 500, transition: Fx.Transitions.Quad.easeInOut});
			setInitialHeight.set(InitialHeight);
		}
		summaryfadeout = new Fx.Styles(InitialContentNode, {onComplete: CallLoadFunction, wait: false, duration: 200, transition: Fx.Transitions.Quad.easeInOut});
	} else if (CurrentState == 'expanded')   {
		summaryfadeout = new Fx.Styles(InitialContentNode, {wait: true, duration: 200, transition: Fx.Transitions.Quad.easeInOut});
		detailsfadeout = new Fx.Styles(ExpandedContentNode, {onComplete: CallFinishFunction, wait: false, duration: 200, transition: Fx.Transitions.Quad.easeInOut});
		detailsfadeout.start({ 'opacity': [1, 0]});
	} else if (CurrentState == 'open')   {
		summaryfadeout = new Fx.Styles(InitialContentNode, {wait: true, duration: 200, transition: Fx.Transitions.Quad.easeInOut});
		detailsfadeout = new Fx.Styles(ExpandedContentNode, {onComplete: CallLoadFunction, wait: false, duration: 200, transition: Fx.Transitions.Quad.easeInOut});
		detailsfadeout.start({ 'opacity': [1, 0]});
	}
	summaryfadeout.start({ 'opacity': [1, 0.5]});
	
	function CallLoadFunction() {
		LoadDetails(DivToChange,UrlToLoad,CurrentState,CurrentStateNode,InitialHeight,InitialHeightNode,ExpandedHeight,ExpandedHeightNode,idNumber,Database,InitialContentNode,ExpandedContentNode,SectionorList,inSection)
	}
	
	function CallFinishFunction() {
		FinishFunction(DivToChange,UrlToLoad,CurrentState,CurrentStateNode,InitialHeight,InitialHeightNode,ExpandedHeight,ExpandedHeightNode,idNumber,Database,InitialContentNode,ExpandedContentNode,SectionorList,inSection)
	}
}

function FinishFunction(DivToChange,UrlToLoad,CurrentState,CurrentStateNode,InitialHeight,InitialHeightNode,ExpandedHeight,ExpandedHeightNode,idNumber,Database,InitialContentNode,ExpandedContentNode,SectionorList,inSection){
	var SectionExpandedHeightNode;
	var SectionExpandedHeight;
	if (CurrentState == 'expanded') {
		ExpandedContentNode.style.display = 'none';
		FinishToggle();
		var heightChange = new Fx.Style(DivToChange, 'height', {
			wait: false, onComplete:FinishToggle,  duration: 500, transition: Fx.Transitions.Quad.easeInOut
		});
		heightChange.start(ExpandedHeight,InitialHeight);
	} else if (CurrentState == 'initial'){
		ExpandedContentNode.style.display = 'block';
		ExpandedContentNode.style.height = '1px';
		ExpandedContentNode.style.overflow = 'hidden';
		if (ExpandedHeight == 'notset') {
			ExpandedHeight = ExpandedContentNode.scrollHeight + InitialContentNode.scrollHeight;
		}
		var heightChange = new Fx.Style(DivToChange, 'height', {wait: true, onComplete:FinishToggle,  duration: 500, transition: Fx.Transitions.Quad.easeInOut});
		heightChange.start(InitialHeight,ExpandedHeight);
	} else if (CurrentState == 'open'){
		ExpandedHeight = ExpandedContentNode.scrollHeight + InitialContentNode.scrollHeight;
		var heightChange = new Fx.Style(DivToChange, 'height', {wait: true, onComplete:FinishToggle,  duration: 500, transition: Fx.Transitions.Quad.easeInOut});
		heightChange.start(ExpandedHeight);
	}

function FinishToggle() {
	if (CurrentState == 'expanded') {
		CurrentStateNode.className = 'initial';
		DivToChange.id = SectionorList;
		InitialContentNode.style.backgroundImage = 'url(/images/template/icons/more.gif)';
	} else {
			window.status = 'Loaded';
		if (SectionorList == 'list') {
			var Analytics =new Asset.javascript('/utilities/javascript/2008-analytics.js', {id: 'Button'});
			$(Analytics).injectInside(ExpandedContentNode);
			addLinkerEvents();
			ExpandedContentNode.style.height = ExpandedHeight - InitialContentNode.scrollHeight;
		} else if (SectionorList == 'section') {
			var ListJs =new Asset.javascript('/utilities/javascript/2008-databases.js', {id: 'ListJs'});
			$(ListJs).injectInside(ExpandedContentNode);
			var ListLoad =new Asset.javascript('/utilities/javascript/2008-databases-load.js', {id: 'ListLoad'});
			$(ListLoad).injectInside(ExpandedContentNode);
			ExpandedContentNode.style.height = 'auto';
			DivToChange.style.height = 'auto';
		}
		DivToChange.id = SectionorList + '-Selected';
		CurrentStateNode.className = 'expanded';
		var finalfadefxout2 = new Fx.Styles(ExpandedContentNode, {
		wait: false, duration: 200, transition: Fx.Transitions.Quad.easeInOut
	});
		InitialContentNode.style.backgroundImage = 'url(/images/template/icons/less.gif)';
		finalfadefxout2.start({ 'opacity': [0, 1]});
	}	
		ExpandedContentNode.style.overflow = 'visible';
		var fadefx = new Fx.Styles(InitialContentNode, {wait: true, duration: 300, transition: Fx.Transitions.Quad.easeInOut});
		fadefx.start({ 'opacity': [0.5, 1]});
	InitialHeightNode.className = InitialHeight;
	ExpandedHeightNode.className = ExpandedHeight;
	} 
}
	
	


// CODE FOR MOUSE OVER HIGHLIGHTING
function TogglerMouseOver(DivToChange,SectionorList) {
	
	var CurrentState;
	for (i=0; i<DivToChange.childNodes.length; i++){
		if (DivToChange.childNodes[i].id=="CurrentState") {
			CurrentState = DivToChange.childNodes[i].className;
		} else if (DivToChange.childNodes[i].id=="Initial_" + SectionorList + "_Content") {
			InitialContentNode = DivToChange.childNodes[i];
		}
	}
	if (DivToChange.className != SectionorList + '-Loading') {
		DivToChange.id = SectionorList + '-Highlighted';
		if (CurrentState == 'initial') {
			InitialContentNode.style.backgroundImage = 'url(/images/template/icons/more-highlighted.gif)';
		} else if (CurrentState == 'expanded') {
			InitialContentNode.style.backgroundImage = 'url(/images/template/icons/less-highlighted.gif)';
		}
	} 
}
function TogglerMouseOut(DivToChange,SectionorList) {
	
	var CurrentState;
	for (i=0; i<DivToChange.childNodes.length; i++){
		if (DivToChange.childNodes[i].id=="CurrentState") {
			CurrentState = DivToChange.childNodes[i].className;
		} else if (DivToChange.childNodes[i].id=="Initial_" + SectionorList + "_Content") {
			InitialContentNode = DivToChange.childNodes[i];
		}
	}
	if (DivToChange.id != SectionorList + '-Loading') {
		
		if (CurrentState == 'initial') {
			DivToChange.id = SectionorList;
			InitialContentNode.style.backgroundImage = 'url(/images/template/icons/more.gif)';
		} else if (CurrentState == 'expanded') {
			DivToChange.id = SectionorList + '-Selected';
			InitialContentNode.style.backgroundImage = 'url(/images/template/icons/less.gif)';
		}
	}
}


