var G_CLASSNAME_MORE_LINK = 'MoreLink';
var G_CLASSNAME_LESS_LINK = 'LessLink';
var G_CLASSNAME_DESCRIPTION_SHORT = 'ContentDisplayDescriptionShort';
var G_CLASSNAME_DESCRIPTION_LONG = 'ContentDisplayDescriptionLong';
var G_CLASSNAME_TAG_SHORT = 'TagContainerShort';
var G_CLASSNAME_TAG_LONG = 'TagContainerLong';
var G_CLASSNAME_MINI_CONTAINER = 'ContentDisplayMiniContainer';

//Contain the last clicked more/less link node, to be able to 
//close the last one before opening a new one.
var g_last_more_less_link = false;


//Detect current browser

//if (typeof DetectEnvironment != 'undefined' && !DetectEnvironment.detected) {

//            DetectEnvironment.detect();

//}

//**************************************************************************************************************************
//Triggered on page load
function init() {
	//hookMoreLessLink();
}

//window.onload = init;
//**************************************************************************************************************************
function hookMoreLessLink () {
	var link_list = document.getElementsByTagName("A");
	
	for (var i=0; i < link_list.length; i++) {		
		if (link_list[i].className == G_CLASSNAME_MORE_LINK) {
			link_list[i].onclick = new Function('handleMoreLessLink(this, expandContentDescription); return false;');
		} else if (link_list[i].className == G_CLASSNAME_LESS_LINK) {
			link_list[i].onclick = new Function('handleMoreLessLink(this, collapseContentDescription); return false;');
		}
	}	
}

//**************************************************************************************************************************
function handleMoreLessLink (node, callback) {
	if (g_last_more_less_link && (g_last_more_less_link != node)) {
		var last_more_less_link = g_last_more_less_link;
		g_last_more_less_link = false;
		handleMoreLessLink(last_more_less_link, collapseContentDescription);		
	}
		
	//To get to the parent TD container from the (more) link, we must go up two level in the DOM
	if (typeof node.parentNode == 'undefined' || typeof node.parentNode.parentNode == 'undefined' || typeof node.parentNode.parentNode.parentNode == 'undefined'){
		return false;
	}
	
	//This is the container that has the two description DIV
	var parent_container = node.parentNode.parentNode.parentNode;
	
	//Get DIV list from the parent container
	var div_list = parent_container.getElementsByTagName('DIV');
	
	var short_description_div = false;
	var long_description_div = false;
	
	//Filter them to extract the short & long description DIV
	for (var i=0; i < div_list.length; i++) {
		if (div_list[i].className == G_CLASSNAME_DESCRIPTION_SHORT || div_list[i].className == G_CLASSNAME_TAG_SHORT ) {
			short_description_div = div_list[i];
		} else if (div_list[i].className == G_CLASSNAME_DESCRIPTION_LONG || div_list[i].className == G_CLASSNAME_TAG_LONG ) {
			long_description_div = div_list[i];
		}
	}
	
	
	//If we found both div, pass them to the callback which will handle expand/collapse
	if (short_description_div && long_description_div) {
				
		//hack until i find something more clever
		var scrolling = false;
		
		var parent = parent_container;
		while (parent) {
			if (parent.className == G_CLASSNAME_MINI_CONTAINER) {
				scrolling = {
					x : parent.scrollLeft,
					y : parent.scrollTop 				
				};				
			}		
			parent = parent.parentNode;
		}		
		
		g_last_more_less_link = node;
		return callback(short_description_div, long_description_div, scrolling)	
	}

	//Something went wrong...do nothing
	return false;
}

//**************************************************************************************************************************
function expandContentDescription (short_desc_node, long_desc_node, scrolling) {
	
	//Find posx && posy of the short_desc_node to position the long_desc_node over
	var posx = findPosX(short_desc_node);
	var posy = findPosY(short_desc_node);

	if (scrolling) {
		posx -= scrolling.x;
		posy -= scrolling.y;	
	}	
	
	long_desc_node.style.position = 'absolute';
	long_desc_node.style.left = posx + 'px';
	long_desc_node.style.top = posy + 'px';
	
	short_desc_node.style.visibility = 'hidden';
	long_desc_node.style.display = '';	
}

//**************************************************************************************************************************
function collapseContentDescription (short_desc_node, long_desc_node) {
	long_desc_node.style.display = 'none';
	short_desc_node.style.visibility = '';
}

//**************************************************************************************************************************
function SwapButton(oEl, e) {   
	if (oEl.src.indexOf('/') != -1) {
		var firstpos = oEl.src.lastIndexOf('/') + 1;
		var lastpos = oEl.src.length;
		
		var buttonPath =  oEl.src.substring(0,firstpos);
		var buttonName = oEl.src.substring(firstpos,lastpos);                
		
		var buttonExt = buttonName.replace(/\w+\.(\w+)/, "$1");  
		buttonName = buttonName.replace(/(\w+)\.\w+/, "$1"); 

		if(buttonName.indexOf("over") >= 0 && e.type =="mouseout") {
			buttonName = buttonName.replace(/(\w+)_\w+/, "$1"); 
		} else if(e.type =="mouseover")	{
			buttonName = buttonName + "_over";
		}		
		
		oEl.src = buttonPath + buttonName + "." +buttonExt;
	}		
}   

//**************************************************************************************************************************

function findPosX(obj) {

            var curleft = 0;
            var posAdjustment = 0;

            if (typeof DetectEnvironment != 'undefined' && DetectEnvironment.isIE() && DetectEnvironment.browser.version >= 6) {
                        var posAdjustment = 10;
            }

            if (obj.offsetParent) {
                while (obj.offsetParent) {
                    curleft += obj.offsetLeft;
                    obj = obj.offsetParent;
                }
            } else if (obj.x) {
                curleft += obj.x;
            } 
            
            return curleft + posAdjustment;

}

 

//************************************************************************************************************************** 

function findPosY(obj) {

            var curtop = 0;
            var posAdjustment = 0; 
            
            if (typeof DetectEnvironment != 'undefined' && DetectEnvironment.isIE() && DetectEnvironment.browser.version >= 6) {
                        var posAdjustment = 16;
            }           

            if (obj.offsetParent) {
                        while (obj.offsetParent) {
                                    curtop += obj.offsetTop
                                    obj = obj.offsetParent;
                        }
            } else if (obj.y) {
                        curtop += obj.y;
            }  
            return curtop + posAdjustment;

}

//init();


//////////////////
// GIMLET
//////////////////
function ToggleLayer(e, className) {
        
	var parentDiv = e.parentNode;
			
	while (parentDiv.className != 'content') {
		if (typeof parentDiv.parentNode != 'undefined') {
			parentDiv = parentDiv.parentNode;
		} else {
			return;
		}
	}
					
	var pList = parentDiv.getElementsByTagName('p');				
	var normal = false;
	var expanded = false;
	
	for (var i=0; i < pList.length; i++) {
		if (pList[i].className == className) {
			normal = pList[i];
		} else if (pList[i].className == 'overlap' + className) {
			expanded = pList[i];
		}
	}
	
	if (!normal || !expanded) {
		return;
	}
	
	if (normal.style.display == 'none') {
		normal.style.display = '';
		expanded.style.display = 'none';
	} else {
		normal.style.display = 'none';
		expanded.style.display = '';				
	}				
}		



function OpenMore(e) {
    
	var parentDiv = e.parentNode;					
	parentDiv.style.display = "none";
	
	var nextSib = "";
	
	if (navigator.appName != 'Microsoft Internet Explorer')	
		nextSib = parentDiv.nextSibling.nextSibling;	
	else
		nextSib = parentDiv.nextSibling;					
	
	nextSib.style.display = "block";	
}	

function CloseMore(e) {
    
	var parentDiv = e.parentNode;			
	parentDiv.style.display = "none";
	
	var previousSib = "";
	
	if (navigator.appName != 'Microsoft Internet Explorer')	
		previousSib = parentDiv.previousSibling.previousSibling;	
	else
		previousSib = parentDiv.previousSibling;	
		
	previousSib.style.display = "block";	
}	

