/* CLIMATE ACTION - HOMEPAGE COMPONENTS */
/* date: 15 may 2008 */


/**********************/
/* YOUR ACTION COUNTS */
/**********************/

var vertical_tabList_mutex = false;
var image_effect_mutex = false;

function openContact(url){
	if(url != ''){
		window.open(url);
	}
}
function Random(limit) {
	today = new Date();
	num = today.getTime();
	num = Math.round(Math.abs(Math.sin(num) * 1000000)) % limit;
	return num;
}

function vertical_tabList_hide_content(DivContent) {
 DivContent.style.position ="absolute";
 DivContent.style.top = "-5000px";
 DivContent.style.left = "-5000px";
}

function vertical_tabList_show_content(DivContent) {
 DivContent.style.position ="";
 DivContent.style.top = "";
 DivContent.style.left = "";
}

function vertical_tabList_closeAll(ContainerID)
{
	if(!vertical_tabList_mutex)
	{
		vertical_tabList_mutex = true;
		var ContainerObject = document.getElementById(ContainerID);
		if(ContainerObject) {		
			/* clear all previous opened items */
			var lListChild = ContainerObject.childNodes;
			
			for (var i = 0; i < lListChild.length; i++){
				var lItemContent = lListChild[i].getElementsByTagName("div");
				
				if(lItemContent.length > 0){
					/* remove the content and change the class name */
					vertical_tabList_hide_content(lItemContent[0]);
					lListChild[i].className = "";
				}
			}
		}
		
		vertical_tabList_mutex = false;
	}
}

function vertical_tabList_RandomOpensubItem(ContainerID)
{
	/* close all items */
	vertical_tabList_closeAll(ContainerID);
	
	var ContainerObject = document.getElementById(ContainerID);
	
	if(ContainerObject) {		
		/* clear all previous opened items */
		var lListChild = ContainerObject.childNodes;
		
		var RandomIndex = Random(lListChild.length);
		//document.write(ContainerID);
		//document.write(lListChild[RandomIndex].id);
		vertical_tabList_OpensubItem(ContainerID, lListChild[0].id, '', '', '', 50);
	}
}
function vertical_tabList_OpensubItem(ContainerID, ItemID, Url, substrAfter, substrBefore, height)
{
	
	if(!vertical_tabList_mutex)
	{
		vertical_tabList_mutex = true;
		var ContainerObject = document.getElementById(ContainerID);
		
		if(ContainerObject) {		
			/* clear all previous opened items */
			var lListChild = ContainerObject.childNodes;
			
			for (var i = 0; i < lListChild.length; i++){
				if(lListChild[i].id == ItemID) {
					var lItemContent = lListChild[i].getElementsByTagName("div");
					if(lItemContent.length > 0){
						/* remove the content and change the class name */
						if(lListChild[i].className != "selected") {
							lListChild[i].className = "selected";
							/*get clientheight */
							var Original_Height;
							if(navigator.userAgent.indexOf("MSIE")!=-1) {
								Original_Height = lItemContent[0].clientHeight + 3;
							}
							else{
								Original_Height = lItemContent[0].clientHeight - 20; /*remove padding 10px*/
							}
							
							lItemContent[0].style.height = 0;
							lItemContent[0].style.opacity = 0;
							lItemContent[0].style.overflowY ='hidden';
							
							vertical_tabList_show_content(lItemContent[0]);

							lItemContent[0].style.filter = 'alpha(opacity=0)';
	
							if(Url!=''){
								/* call ajax function to open the external page */
								load_dynamic_panel(Url, Url, lItemContent[0].id, substrAfter, substrBefore);
							}
							trigger_vertical_sliding_effect(lItemContent[0], Original_Height, 100);
						}
						else{
							vertical_tabList_mutex = false;
						}
					}
				}
				else {
					
					if(lListChild[i].className == 'selected') {
						var lItemContent = lListChild[i].getElementsByTagName("div");
						
						if(lItemContent.length > 0){
							/* remove the content and change the class name */
							vertical_tabList_hide_content(lItemContent[0]);
							lListChild[i].className = "";
						}
					}
				}
			}
			
		}
		
		/* set scroll bar to the bottom of the bullet-list */
		/*window.scrollTo(0, findPosY(ContainerObject) + ContainerObject.clientHeight);*/
		return false;
	}
}
function change_image_fade_effect(ImageID, newImageUrl, newImageAlt, time){
	if(!image_effect_mutex)
	{
		image_effect_mutex = true;
		var ImageObject = document.getElementById(ItemID);
		if(ImageObject) {		
			/*ImageObject.style.height = 0;
			ImageObject.style.opacity = 0;
			ImageObject.style.filter = 'alpha(opacity=0)';*/
			ImageObject.src = newImageUrl;
			ImageObject.alt = newImageAlt;
			/*doFrame(ImageID, null, null, null);*/
		}
	}
}

function trigger_vertical_sliding_effect(el, newHeight, time)
{
  if(el == null)
    return;
 
  var cHeight;
  
  if(el.style.height == ""){
  	cHeight = 0;
  }else{
	cHeight = parseInt(el.style.height);
  }
 
  var totalFrames = 1;
  if(time> 0)
    totalFrames = time/20;
 
  var fHeight = newHeight - cHeight;
  if(fHeight != 0)
    fHeight /= totalFrames;
   
  doFrame(el.id, cHeight, newHeight, fHeight);
}

function doFrame(eID, cHeight, nHeight, fHeight)
{
   /* if(cHeight == 0){
   	setTimeout( 'doFrame("'+eID+'",'+1+','+nHeight+','+fHeight+')', 100);
   }
   else
   { */
		var el = document.getElementById(eID);
		if(el == null)
		 return;
		
		if(cHeight != null){
			cHeight = moveSingleVal(cHeight, nHeight, fHeight);
		
			el.style.height = Math.round(cHeight)+'px';
		}
		el.style.opacity = cHeight/nHeight;
		el.style.filter = 'alpha(opacity=' + Math.round((cHeight/nHeight)*100)+ ')';
			
		if(cHeight == nHeight)
		{
		el.style.height = "";
		vertical_tabList_mutex = false;
		return;
		}
		
		setTimeout( 'doFrame("'+eID+'",'+cHeight+','+nHeight+','+fHeight+')', 10);
/*	} */
}

function moveSingleVal(currentVal, finalVal, frameAmt)
{
  if(frameAmt == 0 || currentVal == finalVal)
    return finalVal;
 
  currentVal += frameAmt;
  if((frameAmt> 0 && currentVal>= finalVal)
    || (frameAmt <0 && currentVal <= finalVal))
  {
    return finalVal;
  }
  return currentVal;
}
