/* Shows/Hides a box */

//addEvent(window,'load',minimizeOnLoad, true);
var http = getHTTPObject(); // We create the HTTP Object

var server = 'www.rentmore.us';

function minimizeOnLoad() 
{
	var userid = document.getElementById("tenantuserid");

	if (userid && userid.value)
	{
		url = "http://" + server + "/includes/AJAXTenantMinMaxAll.php?userid=" + userid.value;
	}
	else
	{
		userid = document.getElementById("manageruserid");
		
		if (userid && userid.value)
		{
			url = "http://" + server + "/includes/AJAXManagerMinMaxAll.php?userid=" + userid.value;
		}
	}
	
	if (userid.value && http && url)
	{
		//alert(url);
		http.open("GET", url, true);
		http.onreadystatechange = handleHttpResponseOnLoad;
	 	http.send(null);
	}
}

/* Used everytime the minimize/maximize is clicked */
function switchMenu(icon, obj, lines, userid, usertype) 
{
	var el = document.getElementById(obj);
	var icon = document.getElementById(icon);
	
	if (icon.title == 'Minimize') 
	{
		/* This is needed for IE. 
		   Unfortunately this disables the animation when minimizing -->*/
		//el.style.display = 'none'; Hmm, now it works!????
		/* <-- */
		
		icon.src = "../../pics/maximize.gif";
		icon.title = 'Maximize';		

		/*var anim = new YAHOO.util.Anim(obj,{height:{to:0}},1,YAHOO.util.Easing.easeIn);*/
		var anim = new YAHOO.util.Anim(obj,{height:{to:0}},1,YAHOO.util.Easing.easeIn);
		anim.animate();

		/* This is css, and will hide the text in the box when its minimized. I can't change this is the css sheet 
		   since it will affect hover and other things sitewide. By having it here it only works when its actually needed */
		el.style.overflow = 'hidden';

		// Only save settings for users that are logged on
		if (userid != 0 && usertype != "")
		{
			minimize(obj, userid, usertype);
		}
	}
	else 
	{
		el.style.display = 'none';
		el.style.display = '';
		
		icon.src = "../../pics/minimize.gif";
		icon.title = 'Minimize';

		/*var anim = new YAHOO.util.Anim(obj,{height:{to:152}},1,YAHOO.util.Easing.easeOut);*/
		/* 10 is how many lines. I should use this as a parameters which will be sent via php when I create the javascript code */
		var anim = new YAHOO.util.Anim(obj,{height:{to:lines, unit:'em'}},1,YAHOO.util.Easing.elasticOut);
		anim.animate();
		
		// Only save settings for users that are logged on
		if (userid != 0 && usertype != "")
		{
			maximize(obj, userid, usertype);
		}
		/* This is css, and will show the hidden text again. I can't change this is the css sheet 
		   since it will affect hover and other things sitewide. By having it here it only works when its actually needed */

		// This one lines makes the hover on shortcut description work again, but when it flows out it shows the text immediately
		// before the actual box has completely flowed out! Therefore only enable this for the shortcuts
		if (obj == "tenant_favorites")
		{
			el.style.overflow = ''; 
		}
	}
}

// Ajax methods to save settings
function minimize(obj, userid, usertype)
{
	switch (usertype)
	{
		case 'tenant'  : url = "http://" + server + "/includes/AJAXTenantMinMax.php?type=minimize&userid=" + userid + "&name=" + obj; break;
		case 'manager' : url = "http://" + server + "/includes/AJAXManagerMinMax.php?type=minimize&userid=" + userid + "&name=" + obj; break;		
	}

	if (http)
	{
		http.open("GET", url, true);
		http.onreadystatechange = handleHttpResponse;
	 	http.send(null);
	}
}

function maximize(obj, userid, usertype)
{
	switch (usertype)
	{
		case 'tenant'  : url = "http://" + server + "/includes/AJAXTenantMinMax.php?type=maximize&userid=" + userid + "&name=" + obj; break;
		case 'manager' : url = "http://" + server + "/includes/AJAXManagerMinMax.php?type=maximize&userid=" + userid + "&name=" + obj; break;		
	}
	http.open("GET", url, true);
	http.onreadystatechange = handleHttpResponse;
 	http.send(null);
}

function getHTTPObject() 
{
	var xmlhttp;

	  /*@cc_on
	
	  @if (@_jscript_version >= 5)
	
	    try {
	
	      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	
	    } catch (e) {
	
	      try {
	
	        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	
	      } catch (E) {
	
	        xmlhttp = false;
	
	      }
	
	    }
	
	  @else
	
	  xmlhttp = false;
	
	  @end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
	    try 
		{
	    	xmlhttp = new XMLHttpRequest();
    	} 
		catch (e) 
		{
	    	xmlhttp = false;
	    }

	}
	return xmlhttp;
}

function handleHttpResponse() 
{
 
}

function handleHttpResponseOnLoad() 
{
	if (http.readyState == 4)
	{
		//alert("Response from server:" + http.responseText);
		var results = http.responseText.split(",");
		var result;

		for (result in results)
		{
			var ele = document.getElementById(results[result]);
			var icon = document.getElementById("icon_" + results[result]);
			
			if (ele)
			{
				ele.style.overflow = 'hidden';
				ele.style.display = 'none'
			}
			if (icon)
			{
				icon.src = "../../pics/maximize.gif";
				icon.title = 'Maximize';		
			}
		}
	}
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function()
{
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

addEvent(window,'unload', EventCache.flush);
addEvent(window,'load',minimizeOnLoad);
