//path to image folder
if(IMAGE_PATH=='undefined')
	IMAGE_PATH='themes/spartan/images/tree';


//preload essential image
function Preload() {
	var args = Preload.arguments;
	document.imageArray = new Array(args.length);
	for(var i=0; i<args.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = IMAGE_PATH + args[i];
	}
}

//show/hide div
function toggleTreeDiv(targetId,imgId,level){ 
  if (document.getElementById){ 
        target = document.getElementById(targetId); 
		img=document.getElementById(imgId);
		   if (target.style.display == "none"){ 
              target.style.display = ""; 
			  if(level=='root')
				  img.src=IMAGE_PATH+'downarrow.gif';
			  else
			  	  img.src=IMAGE_PATH+'minus.gif';
           } else { 
              target.style.display = "none"; 
  			  if(level=='root')
				  img.src=IMAGE_PATH+'rightarrow.gif';
			  else
			  	  img.src=IMAGE_PATH+'plus.gif';

           } 
     } 
} 
//hide div
function HideTreeDiv(targetId,imgId,level){ 
  if (document.getElementById){ 
        target = document.getElementById(targetId); 
		img=document.getElementById(imgId);
		   
        target.style.display = "none"; 
	    if(level=='root')
			img.src=IMAGE_PATH+'rightarrow.gif';
		else
		    img.src=IMAGE_PATH+'plus.gif';

     } 
} 
Preload('rightarrow.gif', 'downarrow.gif', 'blank.gif', 'divider.gif', 'plus.gif', 'minus.gif');


function setTreeCookie(c_name,value,expiredays)
{
	//alert(c_name);
	//value format : divid^imgid^level
	var value_arr=value.split('^');
	var c_value=getTreeCookie(c_name); //get previously opened tree ids

	if(value_arr[2] =='root') //root node is clicked
	{
		if (c_value != null && c_value != "") //if ids found
		{
			var c_value_arr = c_value.split(','); //make an array of ids
			for (counter in c_value_arr) //lood thru each element of array
			{
				temp = c_value_arr[counter];
				temp_arr = temp.split('^');
				HideTreeDiv(temp_arr[0],temp_arr[1],temp_arr[2]); //hide previously opened trees
			}
			//now that we have hide previously opened trees, remove all previously saved cookie value
			eraseCookie(c_name);
			
			//special case: if there is only 1 opened root node and that is clicked again, then that node
			//should be closed and no cookie values should be saved i.e. a situation where all root nodes are
			//manually closed by user
			if(c_value==value)
				return;
		
		}
		//alert('root clicked - removed prev cookie');
		c_value='';
	}
	//fetch the saved cookie values again :)
	c_value=getTreeCookie(c_name); //get previously opened tree ids
	if (c_value != null && c_value != "") //if ids found
	{
		var c_value_arr = c_value.split(','); //make an array of ids
		var alreadyThere=false; //used to check whether supplied tree id is already in cookie or not
		var index; //holds the index of array element that needs to be removed (in case supplied tree id found in cookie)
		var temp; //temp variables
		var temp_arr=Array(); //temp variables
		for (counter in c_value_arr) //lood thru each element of array
		{
			temp = c_value_arr[counter];
			temp_arr = temp.split('^')
			if(temp_arr[0] == value_arr[0]) //if supplied tree id found in cookie
			{
				alreadyThere=true; 
				index=counter;
				break;
			}
		}
		if(alreadyThere==true) //if supplied tree id found in cookie, remove it
		{
			c_value_arr.splice(index,1); //remove
		}
		else //else add it to array
		{
			c_value_arr.push(value); //add
		}
		c_value = c_value_arr.join(); //comma seperated tree ids
		//alert('prev cookie found - appending');
	}
	else //if ids not found
	{
		c_value = value;
		//alert('no cookie found - adding');
	}		

	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(c_value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}


function getTreeCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}

function setKeyCookie(c_name,value,expiredays)
{
	var c_value=getTreeCookie(c_name); //get previously opened tree ids
	if (c_value != null && c_value != "") //if some value already exists in cookie
	{
		var c_value_arr = c_value.split(',');
		var alreadyThere = false;
		for (counter in c_value_arr) //lood thru each element of array
		{
			if(c_value_arr[counter] == value)
			{
				alreadyThere = true;
				break;
			}
		}
		if(alreadyThere==false)
			c_value_arr.push(value); //add to array
		
		c_value = c_value_arr.join(); //comma seperated ids

	}
	else
	{
		c_value = value; //add	
	}

	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(c_value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

//this function will open previously opened tree when a new page loads, uses cookie. 
//do not call this function if u want to refresh tree after every page load
function loadTree(c_name) 
{
	var c_value=getTreeCookie(c_name); //get previously opened tree ids
	if (c_value != null && c_value != "") //if ids found
	{
		var c_value_arr = c_value.split(','); //make an array of ids
		var temp_arr=Array(); //temp variables
		var temp; //temp variables
		for (counter in c_value_arr) //lood thru each element of array
		{
			temp=c_value_arr[counter];
			temp_arr=temp.split('^');
			toggleTreeDivCustom(temp_arr[0],temp_arr[1],temp_arr[2]); //open tree (based on tree id)
		}
	}	
}
//custom function to open previously opend tree nodes
function toggleTreeDivCustom(targetId,imgId,level){
	//alert(targetId+','+imgId+','+level);
	//return; 
  if (document.getElementById){ 
        target = document.getElementById(targetId); 
		img=document.getElementById(imgId);
        target.style.display = ""; 
		if(level=='root')
		  img.src=IMAGE_PATH+'downarrow.gif';
		else
		  img.src=IMAGE_PATH+'minus.gif';
     } 
}

//make querystring accessible with javascript
var objURL;
function makeQueryString()
{
	// Build an empty URL structure in which we will store
	// the individual query values by key.
	objURL = new Object();
 
	// Use the String::replace method to iterate over each
	// name-value pair in the query string. Location.search
	// gives us the query string (if it exists).
	window.location.search.replace(new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
 
			// For each matched query string pair, add that
			// pair to the URL struct using the pre-equals
			// value as the key.
			function( $0, $1, $2, $3 )
			{
				objURL[ $1 ] = $3;
			}
		);
}
makeQueryString(); //function call

//delete tree cookie for agent center pages
function clearTreeCookie()
{
	if((objURL['sm'] == 'undefined' || objURL['sm'] == '' || objURL['sm'] == null) && (objURL['op'] != 'agentcenter'))
	{
		var key = getTreeCookie('openedTree_key');
		if(key != '' && key != null)
		{
			var key_arr=key.split(',');
			for (counter in key_arr) //lood thru each element of array
			{
				//alert(key_arr[counter]);
				setKeyCookie(key_arr[counter],'',0);
			}
		}
		setKeyCookie('openedTree_key','',0);
	}
}

//delete tree cookie for files module
function clearFilesTreeCookie()
{
	if((objURL['name'] == 'undefined' || objURL['name'] == '' || objURL['name'] == null || objURL['name'] != 'files'))
	{
		//alert('here');
		setKeyCookie('FilesTree','',0);
	}
}

//forcibly erase cookie
function eraseCookie(name) {
	//alert(name);
	var date = new Date();
	date.setTime(date.getDate()+0);
	var expires = "; expires="+date.toGMTString();
	var value='';
	document.cookie = name+"="+value+expires+";";
}