/* --------------------------------------------------------------------------------------------
	Javascript for Sitemap popup
	- VictoriaChan 03/10/06
-------------------------------------------------------------------------------------------- */

// uses yahoo dom, event and animation
var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;
var $U = YAHOO.util.Connect;
var $ = $D.get; // get element by ID

// Vars for this file
var id_siteMap = 'sitemap';//ID of the sitemap parent
var id_siteMapButton = 'top-sitemap-button';//ID df the sitemap button
var id_openMap = 'sitemap-box'; //ID of expanded sitemap
var div_max_height = '348';

//init function
function initSitemap(){	
	//set button href to nothing
	$(id_siteMapButton).setAttribute('href', '#');
	
	//add button actions	
	$E.addListener($(id_siteMapButton),'click', function(){toggleSiteMap(id_siteMap, id_siteMapButton, id_openMap); return false; });
}

/*------------------------------------------------------------------
	Main Functions
-----------------------------------------------------------------*/

function toggleSiteMap(div_id, button_id, open_id) {
	
	/*Show siteMap*/
	if ($(div_id).firstChild.id != open_id){
		//load CSS
		loadSiteMapCSS();
		
		//Add new div for the expanded bit
		var div_OpenMap = document.createElement('div'); 
		div_OpenMap.id = open_id;
		div_OpenMap.innerHTML = '<div id="sitemap-box-content"></div>';
		$(div_id).insertBefore(div_OpenMap, $(div_id).firstChild);
		
		//change button
		$(div_id).style.position= 'absolute';//fixing css problem
		changeButton(button_id, false);
		
		// Get Map from AJAX 
		AjaxObject.startRequest();
		
		/*animate */
		var myAnim = new YAHOO.util.Anim(open_id, { height: { to: 370 }  }, 1, YAHOO.util.Easing.easeOut); 
		myAnim.animate();
		
	}else{
	/*Hide siteMap*/
		
		/* animate	*/	
		var myAnimBack = new YAHOO.util.Anim(open_id, { height: { to: 3 }  }, 1, YAHOO.util.Easing.easeIn); 
		myAnimBack.animate(); 
		
		//unload CSS & remove div & change button
		myAnimBack.onComplete.subscribe(function(){
			$(div_id).style.position= 'relative';//fixing css problem
			$(div_id).style.float= 'right';//fixing css problem
			removeSiteMapCSS(); 
			$(div_id).removeChild($(open_id)); 
			changeButton(button_id, true);
		});
	}
	
}

function changeButton(button_id, open){
	if(open == true){
		$(button_id).innerHTML = '<img src="/display_images/research_centre/sitemap.gif" width="84" height="25" alt="sitemap" />';
	}else{
		$(button_id).innerHTML = '<img src="/display_images/research_centre/sitemap_close.gif" width="84" height="25" alt="close sitemap" />';
	}
}

/*--------------------------------------------------------------
		AJAX
--------------------------------------------------------------*/
var AjaxObject = { 
		content_div_id: 'sitemap-box-content',
		
	    handleSuccess:function(o){ 
	        // This member handles the success response 
	        // and passes the response object o to AjaxObject's 
	        // processResult member. 
	        this.processResult(o); 
	        
			/* Please see the Success Case section for more
			* details on the response object's properties.
			* o.tId
			* o.status
			* o.statusText
			* o.getResponseHeader[ ]
			* o.getAllResponseHeaders
			* o.responseText
			* o.responseXML
			* o.argument
			*/ 	        
	    }, 
	 
	    handleFailure:function(o){ 
	        // Failure handler
	        $(this.content_div_id).innerHTML = '<p class="loading">Sorry, there is an error retrieving the sitemap.</p>';
	    }, 
	 
	    processResult:function(o){ 
	        // This member is called by handleSuccess 
			$(this.content_div_id).innerHTML = o.responseText;
		}, 
	 
	    startRequest:function() { 
		   $(this.content_div_id).innerHTML = '<p class="loading">loading sitemap...</p>';
	       $U.asyncRequest('GET', '/applications/research_centre/sitemap.rm', callback, null); 
	    } 
	 
}; 
var callback = 
	{ 
	    success:AjaxObject.handleSuccess, 
	    failure:AjaxObject.handleFailure, 
	    scope: AjaxObject 
	}; 
	 
/*------------------------------------------------------------------
	Load CSS for the sitemap popup
-----------------------------------------------------------------*/
function setCSS(css) {
	try {
		// append stylesheet to alter
		document.getElementsByTagName("head")[0].appendChild(css);
	} catch (e) {
		setTimeout(function(){setCSS(css)}, 100);
	}
}
function removeCSS(css) {
	try {
		// append stylesheet to alter
		document.getElementsByTagName("head")[0].removeChild(css);
	} catch (e) {
		setTimeout(function(){removeCSS(css)}, 100);
	}
}
function loadSiteMapCSS(){
	// create CSS element to set up the page
	var js_css = document.createElement("link");
	js_css.setAttribute("href","/css/front/research_centre/popup_sitemap.css");
	js_css.setAttribute("rel","stylesheet");
	js_css.setAttribute("type","text/css");
	js_css.id = "js_sitemap_css";
	
	// attempt to add the css and then keep trying till we do
	setCSS(js_css);
	js_css = null;
}
function removeSiteMapCSS(){
	// attempt to add the css and then keep trying till we do
	removeCSS($('js_sitemap_css'));
}



/*--------------------------------------------------------------------------------------------
	Actions
----------------------------------------------------------------------------------------------*/

// on load
$E.addListener(window,'load',initSitemap);



