/*
Javascript: display_functions.js
Contains functions used for display purposes (show/hide, animate, etc.)

*/

//fix IE pngs
/*
if (document.all && document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule)
 {document.styleSheets[0].addRule('*', 'behavior: url(iepngfix.htc)');}
*/


function IsDisplayed($WhichDiv) {
	if (document.getElementById($WhichDiv).style.display == "none") {
        return false;
	} else {
		return true;
	}
}

// Function used to show/hide the target div
function ToggleDiv($WhichDiv) {
	if (document.getElementById($WhichDiv).style.display == "none") {
		setOpac($WhichDiv,100); // set it to opaque (just in case)
		document.getElementById($WhichDiv).style.display = "block";
	} else {
		document.getElementById($WhichDiv).style.display = "none";
	}
}

// Function used to show/hide the target div utilizing fade effects below
function FadeToggleDiv($WhichDiv) {
	if (document.getElementById($WhichDiv).style.display == "none") {
		FadeIn($WhichDiv);
	} else {
		FadeOut($WhichDiv);
	}
}

// Function used to fade "in" a div
function FadeIn($WhichDiv) {
	setOpac($WhichDiv,0); // set it to transparent
	document.getElementById($WhichDiv).style.display = "block"; // display as block just in case
	for( var i = 0 ; i <= 100 ; i++ ) {
		   setTimeout( 'setOpac(\''+$WhichDiv+'\',' + i + ')' , 8 * i );
	}
}

// Function used to fade "out" a div
function FadeOut($WhichDiv) {
	for( var i = 0 ; i <= 100 ; i++ ) {
		   setTimeout( 'setOpac(\''+$WhichDiv+'\',' + (100 - i) + ')' , 8 * i );
	}
	setTimeout('ToggleDiv(\''+$WhichDiv+'\')', 8 * i);
	setOpac($WhichDiv,100); // set it to opaque (just in case)
}

// Function used to hide a div
function TurnOff($WhichDiv) {
	document.getElementById($WhichDiv).style.display = "none";
}

// Function used to show
function TurnOn($WhichDiv) {
	document.getElementById($WhichDiv).style.display = "block";
}

// Function used to change background color
function ChangeColor($WhichDiv,$color) {
	document.getElementById($WhichDiv).style.backgroundColor = $color;
}

// Function used to set the opacity of a div (from 0 to 10)
function setOpac( $WhichDiv, value ) {
	document.getElementById($WhichDiv).style.opacity = value / 100;
	document.getElementById($WhichDiv).style.filter = 'alpha(opacity=' + value + ')';
}


// Function used to shrink/grow a div vertically
function ToggleVert($WhichDiv, $MinHeight, $MaxHeight) {
	var $CurrHeight = parseInt(document.getElementById($WhichDiv).style.height);
	if ($CurrHeight < ($MaxHeight - 10)) {
		VertScale($WhichDiv,$MaxHeight);
	} else {
		VertScale($WhichDiv,$MinHeight);
	}
}

// Function used to animate the scaling of a div vertically
function VertScale($WhichDiv, $EndHeight) {
	var $StartHeight = parseInt(document.getElementById($WhichDiv).style.height);
	document.getElementById($WhichDiv).style.display = "block"; // display as block just in case
	document.getElementById($WhichDiv).style.overflow = 'hidden'; // set the overflow to hidden
	for( var i = 0 ; i <= 50 ; i++ ) {
		   $CurrHeight = ($StartHeight - ((($StartHeight - $EndHeight) / 50) * (i-1)));
		   setTimeout( 'setHeight(\''+$WhichDiv+'\',' + $CurrHeight + ')' , 8 * i );
	}
	if ($EndHeight == 0) {
		setTimeout('ToggleDiv(\''+$WhichDiv+'\')', 8 * i);
	}
}

// Function used to set the height of a div
function setHeight( $WhichDiv, value ) {
	document.getElementById($WhichDiv).style.height = parseInt(value) + "px";
}


function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
    	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

/*
function getElementsByStyleClass(className) {
    
  var all = document.all ? document.all :
    document.getElementsByTagName('*');
  var elements = new Array();
  
  for (var e = 0; e < all.length; e++)
    alert(all[e].id + " > " + all[e].className);
    if (all[e].className == className) {
        
        elements[elements.length] = all[e];
      }
  return elements;
}*/

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	//alert(elsLen);
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
	    //alert("<" + els[i].tagName + ">  id: " + els[i].id + ", class: " + els[i].className);
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

//get specific element of class group; 
//give class "active" and remove class"active" from others in class
function specificElementActive(id,classname) {
    
    var els = getElementsByClass(classname);
    for (i=0;i<els.length;i++)
    {
        //alert("<" + els[i].tagName + ">  id: " + els[i].id + ", class: " + els[i].className);
        removeClass(document.getElementById(els[i].id),'active');
    }
    
    addClass(document.getElementById(id),'active');
} 

function preLoadImages(arr,path) {
    // Pre-Loaded Images
    var ct = arr.length;
   
    for (i=0;i<ct-1;i++)
    {
        eval("var newImage_" + i + " = new Image(100,100);");  
        eval("newImage_" + i + ".src = path + arr[i];")
    }
}

function getURLParam(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

