/*			Tristan VAN EGROO - tve@kitry.lu - 30/07/2009
			Non intrusive javascript which add functionality
			to the Generic Navigation Menu Portlet
			
			This script requires JQuery.
*/

/*TVE*/

/*NO CONFLICT MODE

	Some pages in the Site 4 portal use prototype which also redefine the function $().
	Switch jQuery to noConflict mode and replace the function $() by jQuery()
	
*/

jQuery.noConflict();


// Custom functions
var DEPTH = 6; 													//Up to how many level collapse/expand will be available : default : 1
/*

COOKIES FUNCTIONS

*/




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=/";		//Check if can be replaced by expires = session ???
}

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);
}

/*

END OF COOKIES

*/

/*

Collapse the bdepth1 level div with the id "node"


*/

function collapsenocookies(node){
	var PageGroupId = jQuery("input#pagegroupid").attr("value");
	jQuery("[id="+node+"]>div").slideUp("fast");
	jQuery("[id="+node+"]>p>div").attr("class", "imgcoll");
	


}

function expandnocookies(node)
{

	jQuery("[id="+node+"]>div").slideDown("fast");
	jQuery("[id="+node+"]>p>div").attr("class", "imgexp");
	
}
function collapse(node)
{
	var PageGroupId = jQuery("input#pagegroupid").attr("value");
	jQuery("[id="+node+"]>div").slideUp("fast");
	jQuery("[id="+node+"]>p>div").attr("class", "imgcoll");
	

	eraseCookie(node + "-"+ PageGroupId);
	createCookie(node+"-"+PageGroupId,"collapsed",365);
	

													
};
/*	Expand the bdepth1 level div with the id "node"
*/

function expand(node)
{
	jQuery("[id="+node+"]>div").slideDown("fast");
	jQuery("[id="+node+"]>p>div").attr("class", "imgexp");
	
	var PageGroupId = jQuery("input#pagegroupid").attr("value");
	
	eraseCookie(node + "-" + PageGroupId);
	createCookie(node+"-"+PageGroupId,"expanded",365);
};

/*	This function is called by th onclick action on the img and dispatch between expand and collapse functions
*/

function collapseexpand(node)
{

	if(jQuery("[id="+node+"]>p>div").attr("class") == "imgcoll")
	{
		expand(node);
	}
	else
	{
		collapse(node);
	}

}

function iscollapsedbydefault(node)
{
	var st = jQuery("div#leftmenu>div#"+node).attr("class");
	if(st.indexOf("collapsed")==-1)
		return false;
	return true;
}

/*	Add slashes ("\\") before a ".", this way, the "." will not be confused with
	the "." character that define a special CSS class.
*/
function doubleslashes(a)
{

	/* 	Regex that replaces . by \\.
		The 'g' modifier specify all occurences will be replaced.
	*/
	a = a.replace(/\./g,"\\\\.");

	return a;
}
function undoubleslashes(a)
{

	/* 	Regex that replaces \\. by .
		The 'g' modifier specify all occurences will be replaced.
	*/
	a = a.replace(/\\./g,".");

	return a;

}



// When page is loaded and/or can be parsed 




jQuery(document).ready(function(){


	/*	Collapse the nodes with the "expand" attribute set to NO
	*//*
	jQuery("div#leftmenu div.expand").each(function(){
	
		var iden = jQuery(this).attr("id");
		expandnocookies(iden);
	
	});*/
	

	
	/*	Javascriptize the leftmenu :-)
		
	*/
	var xp = "";
	for(i=0;i<DEPTH;i++)
	{
		jQuery('div#leftmenu>div>'+xp+'p').each(function(){
			var action = true; /*true : expand, false : collapse */
			var idd = jQuery(this).attr("id");
			var PageGroupId = jQuery("input#pagegroupid").attr("value");
			var iddparent = jQuery("[id="+idd+"]").parent().attr("id");
					
			/*	This condition checks if the node has "div" children. 
				If yes, it means that we can display the +/- icon
				otherwise, do nothing.
			*/

			if(jQuery("[id="+iddparent+"]>div").length){

				var st = readCookie(iddparent+"-"+PageGroupId);	
				//alert("status : " + st + " for " + iddparent);
			
				jQuery(this).append("<div class='imgexp' id='tg"+idd+"' onclick=\"collapseexpand('"+iddparent+"')\"></div>");
				
				if(st=="collapsed")
					action = false;
				else if(st=="expanded")
					action = true;
					
				

				/* Check if the item is collapsed by default in the page option.
				*/
				

				if(st!="collapsed" && st!="expanded" && i==0)	
					if(iscollapsedbydefault(iddparent))
						action = false;
					else
						action = true;
				
				if(action)
					expand(iddparent);
				else
					collapse(iddparent);
					
	
					
			}
		
		});
		xp = xp + "div>";
	}
	
	/* When clicking on a node, expands it if has children */
	jQuery("p[class~=haschildren]>a").click(function(){
	
		var idd = jQuery(this).parent().attr("id");
		var iddparent = jQuery("[id="+idd+"]").parent().attr("id");
		expand(iddparent);
	
	});
	

});  


