// JavaScript Document

function hideshow(what){
	if (document.getElementById(what).style.display == 'none'){
		//Effect.BlindDown(document.getElementById(what));
		document.getElementById(what).style.display = 'block';
		additem_cookie_list(document.getElementById(what).id,'collapse');
		return 1
	}else{
		//Effect.BlindUp(document.getElementById(what));
		document.getElementById(what).style.display = 'none';
		removeitem_cookie_list(document.getElementById(what).id,'collapse');
		return 2
	}
}

function provide_collapse_system(){
	//Set up Remember Functions
	var storage = readCookie('collapse');
	if (!storage){
		createCookie('collapse','open=',0);
	}
	
	var collapseElements = getElementsByClassName('expand');
	for(var i=0;i<collapseElements.length;i++){
		var ce = collapseElements[i];
		//Handle Bad Hack For Page Validation
		ce.setAttribute('toggle',ce.getAttribute('title'));
		ce.setAttribute('title','');
		if (!check_in_cookie_list(ce.getAttribute('toggle'),'collapse')){
			document.getElementById(ce.getAttribute('toggle')).style.display = 'none';
		}
		ce.style.cursor = 'pointer';
		ce.onclick = function(){
			hideshow(this.getAttribute('toggle'));
		};
	}	
}

function provide_collapse_system2(){
	var collapseElements = getElementsByClassName('expand2');
	for(var i=0;i<collapseElements.length;i++){
		var ce = collapseElements[i];
		//Handle Bad Hack For Page Validation
		ce.setAttribute('toggle',ce.getAttribute('title').replace(/\s+/g,'_'));
		ce.setAttribute('id',ce.getAttribute('title').replace(/\s+/g,'_'));
		//ce.setAttribute('title','');
		ce.className = ce.className + ' toggle';
		if (!check_in_cookie_list(ce.getAttribute('toggle'),'collapse')){
			document.getElementById(ce.getAttribute('toggle')+'_togglebody').style.display = 'none';
		}
		ce.style.cursor = 'pointer';
		ce.onclick = function(){
			this.setAttribute('state',hideshow(this.getAttribute('toggle')+'_togglebody'));
			if (this.getAttribute('state') == 1){
				this.style.backgroundImage = 'url(/images/minus.gif)';
			}else{
				this.style.backgroundImage = 'url(/images/plus.gif)';
			}
		};
	}	
}

function control_collapse_system2(what){
	var obj = document.getElementById(what);
	obj.setAttribute('state',hideshow(obj.getAttribute('toggle')+'_togglebody'));
	if (obj.getAttribute('state') == 1){
		obj.style.backgroundImage = 'url(/images/minus.gif)';
	}else{
		obj.style.backgroundImage = 'url(/images/plus.gif)';
	}
}

function text_flow(){
	var TextFlowElements = getElementsByClassName('text_flow_hidden');
	for(var i=0;i<TextFlowElements.length;i++){
		var tf = TextFlowElements[i];
		tf.style.overflow='hidden';
		tf.style.height='60px';
		tf.style.position = 'relative';
		tf.className = tf.className + ' text_flow';
		var whiteout = document.createElement('div');
		whiteout.style.backgroundImage = 'url("/images/whiteout.png")';
		whiteout.style.backgroundRepeat = 'repeat-x';
		whiteout.style.position = 'absolute';
		whiteout.style.bottom = '0px';
		whiteout.style.height='60px';
		whiteout.style.width='100%';
		tf.whiteout = whiteout;
		var control = document.createElement('div');
		control.style.position = 'absolute';
		control.style.right = '0px';
		control.style.bottom = '3px';
		control.style.height='13px';
		control.style.width='36px';
		control.style.color='#888';
		control.style.fontSize='8pt';
		control.style.cursor='pointer';
		control.tf = tf;
		var control_text = document.createElement('span');
		control_text.appendChild(document.createTextNode('more...'));
		control.appendChild(control_text);
		
		control.onclick=function(){
			if (this.firstChild.firstChild.data == 'more...'){
				this.tf.style.height='auto';
				this.tf.whiteout.style.display='none';
				this.firstChild.firstChild.data = 'less...';
			}else{
				this.tf.style.height='60px';
				this.tf.whiteout.style.display='block';
				this.firstChild.firstChild.data = 'more...';
			}
		};
		
		tf.appendChild(whiteout);
		tf.appendChild(control);
	}
}

function check_in_cookie_list(itmName,cookieName){
	var tokens = tokenise_cookie_items('open',cookieName);
	for (var i=0; i < tokens.length; i++){
		if (tokens[i] == itmName){
			return true;	
		}
	}
	return false;
}

function removeitem_cookie_list(itmName,cookieName){
	var tokens = tokenise_cookie_items('open',cookieName);
	var newvalue = 'open=';
	var joiner = '';
	for (var i=0; i < tokens.length; i++){
		if (tokens[i] != itmName){
			newvalue = newvalue+joiner+tokens[i];
			joiner = ':';
		}
	}
	return createCookie(cookieName,newvalue,0);
}

function additem_cookie_list(itmName,cookieName){
	var tokens = tokenise_cookie_items('open',cookieName);
	for (var i=0; i < tokens.length; i++){
		if (tokens[i] == itmName){
			return false;	
		}
	}
	var currentValue = element_value_from_cookie('open',cookieName);
	return createCookie(cookieName,'open='+(currentValue ? currentValue+':' : '')+itmName,0);
}

function element_value_from_cookie(element,cookieName){
	var cookie = readCookie('collapse');
	if(cookie){
		var elements = cookie.split('&');
		for (var i=0; i < elements.length; i++){
			var pair = elements[i].split('=');
			if (pair[0] == element){
				return pair[1];
			}
		}
	}
	return false;
}

function tokenise_cookie_items(element,cookieName){
	var cookie = readCookie('collapse');
	if(cookie){
		var elements = cookie.split('&');
		for (var i=0; i < elements.length; i++){
			var pair = elements[i].split('=');
			if (pair[0] == element){
				return pair[1].split(':');
			}
		}
	}
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


