/*
Chandler CVB ~ Ajax Function for listings application
Author: Sean D'Addamio
Created: 03/19/2007
Last Modified: 
Notes: 

*/

/*
addLoadEvent function
allows the addition of multiple onload events to be added dynamically
args: func (string) ~ name of the function to add
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}else{
		window.onload = function () {
			oldonload();
			func();
		}
	}	
}

/*
getListingDetail function
ajax call to get the listing detail
args: 
	url (string) ~ url of the page (fuse) to request
	dataToSend (number, string, object) ~ url params to send to the requested page
	objectID (object) ~ id of the div to load the response into
	linkNode (object) ~ node of the link that was clicked
*/
function getListingDetail(url, dataToSend, objectID, linkNode) {
	//alert(linkNode.firstChild.nodeValue);
	var pageRequest = false; // init pageRequest var
	var obj = document.getElementById(objectID); // shortcut to div for ajax content
	var linkText = linkNode.firstChild;
	// if statement block checks content of link
	if (linkText.nodeValue == 'less info...') { // if detail is already showing, hide it
		linkText.nodeValue = 'more info...';
		//obj.style.display = "none";
		obj2 = document.getElementById(objectID);
		window.setTimeout('Effect.SlideUp(obj2, {duration:.3})',500);
		//new Spry.Effect.Slide(obj,{duration: 1000,from: '100%',to: '0%'});		
	}else if (obj.innerHTML != "") { // if detail is not showing but has already been requested, show it
		linkText.nodeValue = 'less info...';
		//obj.style.display = "block";
		obj3 = document.getElementById(objectID);
		window.setTimeout('Effect.SlideDown(obj3, {duration:.3})',500);
		//new Spry.Effect.Slide(obj,{duration: 1000,from: '0%',to: '100%'});		
	}else{	// request detail page
		linkText.nodeValue = 'less info...'; // change link text
		// check whether to use IE or Mozilla XML Object
		if (window.XMLHttpRequest) {
			pageRequest = new XMLHttpRequest();
		}else if (window.ActiveXObject) {
			pageRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			return false;
		}
		// callback function executes whenever the request state changes
		pageRequest.onreadystatechange = function () {
			// if the request state is 4 (ready) display the results
			if (pageRequest.readyState == 4) {
				obj = document.getElementById(objectID);
				obj.innerHTML = pageRequest.responseText;
				obj4 = document.getElementById(objectID);
				window.setTimeout('Effect.SlideDown(obj4, {duration:.3})',500);
				//obj.style.display = "block";
				//Effect.SlideDown(obj, {duration:.2});
				//new Spry.Effect.Slide(obj,{duration: 1000,from: '0%',to: '100%'});
			}
		}
		// if there is data being sent to page, use post
		if (dataToSend) {
			var sendData = 'linkNum=' + dataToSend;
			pageRequest.open('POST', url, true);
			pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			pageRequest.send(sendData);
		// if there is no data being sent use get
		}else{
			pageRequest.open('GET', url, true);
			pageRequest.send(null);
		}
	}
	return false;
}

/*
// setDetailLinks not used in current script
function setDetailLinks() {
	alert("set detail links");
	if(!document.getElementById || !document.getElementsByTagName) {
		alert("javascript disabled");
		if (!document.getElementById("listingsList")) {
			alert("no listings output");
			return true;
		}
	}else{
		var listingList = document.getElementById("listingsList");
		//alert(listingList.nodeName);
		var listingLinks = listingList.getElementsByTagName("a");
		
		for (var i=0; i<listingLinks.length; i++) {			
			if (listingLinks[i].className == "listingDetailLink") {
				//alert('detail_'+listingLinks[i].href);
				listingLinks[i].onclick = function() {
					//return getListingDetail('');

				}
			}
		}
	}
}
*/
