var comparisonListArray = new Array();
var readyToDisplay = false;
var tempListArray = new Array();

// icons
var addIcon = '<img src="//images.wotif.com/image/shortlist-add.gif" alt="+" title="Click to add this property to your shortlist" />';
var selectedIcon = '<img src="//images.wotif.com/image/shortlist-icon.gif" alt="-" title="Click to remove this property from your shortlist" />';

function addToComparisonList(elementId, iframe)
{
	if (iframe == null) {
		iframe = false;
	}
	if (readyToDisplay) {
		if (!arrayHasElement(comparisonListArray, elementId)) {
			var supplierToggle = document.getElementById(elementId);
			comparisonListArray.push(elementId);
			var newMapNode = null;
			if (supplierToggle && supplierToggle.nextSibling) {
				if (supplierToggle.nextSibling.nodeType != 1) {
					newMapNode = supplierToggle.nextSibling.nextSibling;
				} else if (supplierToggle.nextSibling) {
					newMapNode = supplierToggle.nextSibling;
				}
			}
			if (supplierToggle) {
				var supplierRow = supplierToggle.parentNode.parentNode.parentNode;
				if (iframe) {
					supplierToggle.href = 'javascript:removeFromComparisonList("' + elementId + '",true); IFrameChild.publishHeightAndNotify();';
                    dojo.addClass(supplierRow, "added");
				} else {
					supplierToggle.href = 'javascript:removeFromComparisonList("' + elementId + '");';
                    dojo.addClass(supplierRow, "added");
				}

				supplierToggle.innerHTML = selectedIcon;
				unhighlightSupplierRow(supplierRow);

				// make a copy of the entire table row, making sure that
				// 'add to comparison list' link has a different id.
				supplierToggle.id = 'comparisonList' + elementId;
				if (newMapNode && newMapNode.id) {
					var oldmapid = newMapNode.id;
					newMapNode.id = 'shortList' + newMapNode.id;
				}
				var comparisonRow = supplierRow.cloneNode(true);

				// set supplierToggle back to the correct id
				supplierToggle.id = elementId;
				if (newMapNode && newMapNode.id) {
					newMapNode.id = oldmapid
				}
				// change row id (so mapping will highlight in comparison list)
				if (supplierRow.id) {
					supplierRow.id = 'listed' + supplierRow.id;
				}

				var comparisonList = document.getElementById('comparisonList');
				comparisonList.lastChild.appendChild(comparisonRow);
				toggleComparisonList(true);
			}

		}
	} else {
		if (!arrayHasElement(tempListArray, elementId)) {
			tempListArray.push(elementId);
		}
	}
}

function toggleComparisonList(showList)
{
    var comparisonList = document.getElementById('comparisonList');
    comparisonList.style.display = (showList) ? '' : 'none';
}

function addToComparisonListArray(elementId)
{
    if (!arrayHasElement(comparisonListArray, elementId))
    {
        comparisonListArray.push(elementId);
    }
    updateHotelIcon('shortlist-top', elementId);
    updateHotelIcon('shortlist-bottom', elementId);
}

function removeFromComparisonListArray(elementId)
{
    removeElementFromArray(comparisonListArray, elementId);
    updateHotelIcon('shortlist-top', elementId);
    updateHotelIcon('shortlist-bottom', elementId);
}

function updateHotelIcon(linkId, elementId)
{
    var hotelComparison = document.getElementById(linkId);
    if (!hotelComparison)
    {
        return;
    }

    if (arrayHasElement(comparisonListArray, elementId))
    {
        hotelComparison.innerHTML = "<a href=\"javascript:removeFromComparisonListArray('" + elementId + "');\"'>" +
                   selectedIcon + " This property is in your shortlist (click here to remove it)</a>";
    }
    else
    {
        hotelComparison.innerHTML = "<a href=\"javascript:addToComparisonListArray('" + elementId + "');\"" +
                   "'>" + addIcon + " Add this property to your shortlist</a>";
    }
}

function removeAll(iframe)
{
    if(iframe == null) {
        iframe = false;
    }
    removeAllForRegion(iframe);
    toggleComparisonList(false);
    comparisonListArray = new Array();
    unloadComparisonList();
}
function removeAllForRegion(iframe)
{
    // clone comparisonListArray so we can iterate through and remove each element
    var list = comparisonListArray.slice();

    for (var i = 0; i < list.length; i++)
    {
        // remove visible properties
        if (document.getElementById(list[i]))
        {
            removeFromComparisonList(list[i], iframe);
        }
    }

    unloadComparisonList();
}

function removeFromComparisonList(elementId, iframe)
{
    if(iframe == null) {
        iframe = false;
    }
    if (readyToDisplay) {
		removeElementFromArray(comparisonListArray, elementId);
		var supplierRow = document.getElementById(elementId).parentNode.parentNode.parentNode;
		if (supplierRow) {
			// remove 'listed' from front of id
			supplierRow.id = supplierRow.id.substring(6, supplierRow.id.length);
			// remove hover highlight from supplier row
			unhighlightSupplierRow(supplierRow);
			// remove highlight from supplier row
			//if (oldHighlightedResultRow == supplierRow.id)
			//{
			//  supplierRow.className = '';
			//}
		}
		// remove row from comparisonList
		var comparisonToggle = document
				.getElementById('comparisonList' + elementId);
		var comparisonRow = comparisonToggle.parentNode.parentNode.parentNode;
		comparisonRow.parentNode.removeChild(comparisonRow);
		if (comparisonListArray.length == 0) {
			toggleComparisonList(false);
		}
		// alter toggle in search results
		var supplierToggle = document.getElementById(elementId);
		if (iframe) {
			supplierToggle.href = 'javascript:addToComparisonList("' + elementId + '", true); IFrameChild.publishHeightAndNotify();';
            dojo.removeClass(supplierRow, "added");
		} else {
			supplierToggle.href = 'javascript:addToComparisonList("' + elementId + '")';
            dojo.removeClass(supplierRow, "added");
		}
		supplierToggle.innerHTML = addIcon;
		// hide comparison list if there are no deal rows
		var comparisonList = document.getElementById('comparisonList');
		if (comparisonList.lastChild.lastChild
				&& (comparisonList.lastChild.lastChild.nodeType == 3 || comparisonList.lastChild.lastChild.className == 'info')) {
			toggleComparisonList(false);
		}
	} else {
		removeElementFromArray(tempListArray, elementId);
	}
}

function removeElementFromArray(array, element)
{
    for (var i = 0; i < array.length; i++)
    {
        if (array[i] == element)
        {
            array.splice(i, 1);
            return;
        }
    }
}

function arrayHasElement(array, element)
{
    for (var i = 0; i < array.length; i++)
    {
        if (array[i] == element)
        {
            return true;
        }
    }
    return false;
}

function loadComparisonList()
{
    comparisonListArray = new Array();
    readyToDisplay = true;

    if (hasCookie('WotifComparisonList'))
    {
        var comparisonCookie = getCookie('WotifComparisonList');
        var elements = comparisonCookie.split('-');

        for (var i = 0; i < elements.length; i++)
        {
            if (elements[i] != '')
            {
                // if element is one of the old elemenets add a W to the start.
                var PREFIX = "compare";
                var id = elements[i].substr(PREFIX.length);
                if (id > 0) {   // quick numeric check, returns false
                    elements[i] = PREFIX+"W"+id;
                }
                addToComparisonList(elements[i]);
            }
        }
    }
    for (var i = 0; i < tempListArray.length; i++) {
    	if (!arrayHasElement(comparisonListArray, tempListArray[i])) {
    		addToComparisonList(tempListArray[i]);
		}
    }
    tempListArray = new Array();
}

function reRenderComparisonList() {
    unloadComparisonList();
    var nodesToRemove = new Array();
    var tbody = document.getElementById("comparisonList").tBodies[0];
    var comparisonListRows = tbody.childNodes;
    for (var index = 0; index < comparisonListRows.length; index++) {
        var row = comparisonListRows.item(index);
        if (new String(row.className).indexOf("customerDeals") == 0) {
            nodesToRemove.push(row);
        }
    }
    for (var index = 0; index < nodesToRemove.length; index++) {
        tbody.removeChild(nodesToRemove[index]);
    }
    loadComparisonList();
}

function unloadComparisonList()
{
    var rows = '';
    for (var i = 0; i < comparisonListArray.length; i++)
    {
        rows = rows + comparisonListArray[i] + '-';
    }

    var expiryDate = new Date();
    expiryDate.setTime(expiryDate.getTime()+(365*24*60*60*1000));
    setCookie('WotifComparisonList', rows, expiryDate, 'wotif.com', '/');
}
