
addImg = new Image();
remImg = new Image();
waiImg = new Image();
addImg.src = "/Media/Graphics/SPG/Buttons/pg_btn_hotel_icon_add.gif";
remImg.src = "/Media/Graphics/SPG/Buttons/pg_btn_hotel_icon_remove.gif";
waiImg.src = "/Media/Graphics/SPG/Buttons/pg_btn_hotel_icon_wait.gif";

function manageFavoriteImg(p_nodeId, p_nodeText) {
    if(window.opener != null) {
        doc = window.opener.document;
    } else {
        doc = window.document;
    }

    var nodeObj = new nodeObject(p_nodeId, p_nodeText,doc)
	nodeObj.switchHandler = genericSwitchHandler;
	manageFavorite(nodeObj,doc);

    if(window.opener != null) {
        // Update popup window image
        var nodes = document.getElementsByTagName('IMG');
        testString = "FAVid" + nodeObj.nodeId;
        for( var x = 0; x < nodes.length ; x++ ) {
            if (nodes[x].id == testString) {
                if (nodeObj.action=="add") {
                    nodes[x].src = remImg.src;
                    nodes[x].setAttribute('alt', 'Remove this hotel from My Favorites');
                    nodes[x].setAttribute('title', 'Remove this hotel from My Favorites');
                } else {
                    nodes[x].src = addImg.src;
                    nodes[x].setAttribute('title', 'Add this hotel to My Favorites');
                    nodes[x].setAttribute('alt', 'Add this hotel to My Favorites');
                }
            }
        }
    }
}

function manageFavoriteDiv(p_nodeId, p_nodeText,doc) {
    var nodeObj = new nodeObject(p_nodeId, p_nodeText)
	nodeObj.switchHandler = genericSwitchHandler;
	manageFavorite(nodeObj,doc);
}

function nodeObject(p_nodeId, p_nodeText,doc) {
	this.nodeId = p_nodeId
	this.nodeText = p_nodeText
	this.action = "unknown";
	this.addLocationNode = null;
	this.waitHandler = null;
	this.switchHandler = null;
    this.doc = doc;
    return this;
}

function manageFavorite(nodeObj,doc) {
	findNodeByAlph(nodeObj,doc);
	serverRequest(nodeObj,doc);
}

function findNodeByAlph(nodeObj,doc) {
	var r = doc.getElementById("id" + nodeObj.nodeId)
	if (r != null) {
 		nodeObj.action = "remove";
		return;
	}
	var nodes = doc.getElementById('properties').getElementsByTagName('DIV');
	for( var x = 0; x < nodes.length ; x++ ) {
		if (nodes[x].firstChild.firstChild.nodeValue > nodeObj.nodeText) {
			nodeObj.action = "add";
			nodeObj.addLocationNode = nodes[x];
			return;
		}
	}
}

function serverRequest(nodeObj,doc) {
	requestImg = new Image();
	requestImg.src = "/manageFavorite.qq?propertyID=" + nodeObj.nodeId + "&action=" + nodeObj.action + "&timestamp=" + new Date().getTime();
	requestImg.onLoad = requestOnLoad(nodeObj,doc);
}

function requestOnLoad(nodeObj,doc) {
	if (nodeObj.action=="remove") {
		removeDivNode(nodeObj,doc);
	} else {
		addNode(nodeObj,doc);
	}
	setDefaultCopy(doc);
	nodeObj.switchHandler(nodeObj);
	notifyFavorites(doc);
}

function setDefaultCopy(doc) {
	if (doc.getElementById('properties').getElementsByTagName('DIV').length == 1 ) {
		docu.getElementById('defaultCopy').style.display = "block";
	} else {
		doc.getElementById('defaultCopy').style.display = "none";
	}
}

function addNode(nodeObj,doc) {
	var linkNode = doc.createElement("a");
	linkNode.appendChild(doc.createTextNode(nodeObj.nodeText));
	linkNode.setAttribute('href',getHREF(nodeObj.nodeId));

	var newNode = doc.createElement("DIV");
	newNode.tagName;
	newNode.id = "id" + nodeObj.nodeId;
	newNode.appendChild(linkNode);

	var item = doc.getElementById('properties');
	item.insertBefore(newNode,nodeObj.addLocationNode);
}

function removeDivNode(nodeObj,doc) {
	var r = doc.getElementById("id" + nodeObj.nodeId)
	if (r != null) {
 		r.parentNode.removeChild(r);
	}

}

function getHREF(nodeId) {
	return 'javascript:property_onClick('+ nodeId + ');';
}

function setImages(nodeObj) {
	var nodes = nodeObj.doc.getElementsByTagName('IMG');
	testString = "FAVid" + nodeObj.nodeId;
	for( var x = 0; x < nodes.length ; x++ ) {
		if (nodes[x].id == testString) {
			if (nodeObj.action=="add") {
				nodes[x].src = remImg.src;
				nodes[x].setAttribute('alt', 'Remove this hotel from My Favorites');
				nodes[x].setAttribute('title', 'Remove this hotel from My Favorites');
			} else {
				nodes[x].src = addImg.src;
				nodes[x].setAttribute('title', 'Add this hotel to My Favorites');
				nodes[x].setAttribute('alt', 'Add this hotel to My Favorites');
			}
		}
	}
}

function setSpans(nodeObj) {
	var nodes = nodeObj.doc.getElementsByTagName('SPAN');
    testString = "Fav" + nodeObj.nodeId + "_";
    for( var x = 0; x < nodes.length ; x++ ) {
		if (nodes[x].id.indexOf(testString) > 0) {
			if (nodeObj.action=="add") {
				nodes[x].style.display = (nodes[x].id.indexOf("remove") != -1) ? "block" : "none";
				nodes[x].style.display = (nodes[x].id.indexOf("add") != -1) ? "none" : "block";
			} else {
				nodes[x].style.display = (nodes[x].id.indexOf("remove") != -1) ? "none" : "block";
				nodes[x].style.display = (nodes[x].id.indexOf("add") != -1) ? "block" : "none";
			}
		}
	}
}

function genericSwitchHandler(nodeObj) {
	try {setImages(nodeObj); } catch(e) {}
	try {setSpans(nodeObj); } catch(e) {}
}

function notifyFavorites(doc){
	var favCount = doc.getElementById('properties').getElementsByTagName('DIV').length;
	doc.getElementById('favCount').firstChild.nodeValue = favCount - 1;
	doc.getElementById('favoritesButton').className = "notify";
	//window.setTimeout('endNotify('+doc+')', 700);
}
function endNotify(doc) {
	doc.getElementById('favoritesButton').className = "";
}
