



var rightBottom = 1;
var rightTop = 2;
var leftBottom = 3;
var leftTop = 4;
var middleBottom = 5;
var middleTop = 6;

var rightBottomToolTipClass = "toolTip toolTip-tl";
var rightTopToolTipClass = "toolTip toolTip-bl";
var leftBottomToolTipClass = "toolTip toolTip-tr";
var leftTopToolTipClass = "toolTip toolTip-br";
var middleBottomToolTipClass = "toolTip toolTip-tm";
var middleTopToolTipClass = "toolTip toolTip-bm";

var backgroundDivClass = "background";
var backgroundDivClassPOI = "background";

function getRigthClassFromPosition(position) {
	if (position == rightBottom) { return rightBottomToolTipClass; }
	if (position == rightTop) { return rightTopToolTipClass; }
	if (position == leftBottom) { return leftBottomToolTipClass; }
	if (position == leftTop) { return leftTopToolTipClass; }
	if (position == middleBottom) { return middleBottomToolTipClass; }
	if (position == middleTop) { return middleTopToolTipClass; }
}

// Auxiliar DOM functions

function createDiv(divClass, id, top, left,  height, width) {
	// new DIV creation
	var newDiv = createSimpleDiv(divClass, id_toolTip_prefix + id);
	newDiv.style.display = "none";

	// position
	if (top != null) newDiv.style.left = left + "px";
	if (left != null) newDiv.style.top = top + "px";

	// dimensions
	if (height != null) newDiv.style.height = height + "px";
	if (width != null) newDiv.style.width = width + "px";

	return newDiv;
}

function createSimpleDiv(divClass, id) {
	// new DIV creation
	var newDiv = document.createElement("div");
	if (id != null) newDiv.id = id;
	if (divClass != null) newDiv.className = divClass;
	newDiv.style.position = "absolute";
	return newDiv;
}

function createA (aText, id, newClass, newHRef, newOnClickEventFunction) {
	var newA = document.createElement("a");
	if (id != "") newA.id = id;
	newA.appendChild(document.createTextNode(aText));
	if (newClass != "") newA.className = (newClass == null)?"":newClass;
	newA.href = (newHRef == null)?"javascript:;":newHRef;
	if (newOnClickEventFunction != null) { newA.onclick = function() { newOnClickEventFunction }; }
	return newA;
}

function createStrongEMText(aText) {
	var strong = document.createElement("strong");
	strong.appendChild(createEMText(aText));
	return strong;
}

function createEMText(aText) {
	var em = document.createElement("em");
	em.appendChild(document.createTextNode(aText));
	return em;
}

// Auxiliar Functions

function extractDimFromStyle(dimStyle){
	return parseInt(dimStyle.substr(0, dimStyle.indexOf("px")));
}

function getHotelData(id) {
	for (var i = 0; i < hotelToolTipData.length; i++) {
		if (hotelToolTipData[i][0] == id) {
			return hotelToolTipData[i];
		}
	}
}

function getHotelPrices(id) {
	for (var i = 0; i < hotelPrices.length; i++) {
		if (hotelPrices[i][0] == id) {
			return hotelPrices[i];
		}
	}
}

function getPOIData(id) {
	for (var i = 0; i < poiToolTipData.length; i++) {
		if (poiToolTipData[i][0] == id) {
			return poiToolTipData[i];
		}
	}
}

function fillHotelData(className, tableHeigth, hotelData, hotelPrice, instanceName) {

	var table = initTable(className, tableHeigth);

	// Firts ROW
	table.appendChild(createFirstRow());

	// Second ROW [Main]
	var tr2 = document.createElement("tr");
	var td2 = document.createElement("td");
	td2.className = "content";
	td2.appendChild(createCloseSpan(instanceName));

	// Title
	var title = document.createElement("h3");
	title.appendChild(createA(hotelData[1], "", "", hotelData[2]));
	td2.appendChild(title);

	// Address
	var address = document.createElement("p");
	var em = document.createElement("em");
	em.appendChild(document.createTextNode(hotelData[3]))
	address.appendChild(em);
	td2.appendChild(address);

	// Countr restrictions
	var countRestrictions = 0;
	if (hotelPrice[2] > 0) countRestrictions++;
	if (hotelPrice[3] > 0) countRestrictions++;
	if (hotelPrice[5] > 2) countRestrictions++;
	if (hotelPrice[6] != undefined && hotelPrice[6] != '') countRestrictions++;

	// Description
	if (countRestrictions <= 1) {
		var p = document.createElement("p");
		p.appendChild(document.createTextNode(hotelData[5]))
		td2.appendChild(p);
	}

	// Average Price
	if (hotelPrice[1] == '') {
		var dispDiv = document.createElement("div");
		dispDiv.className = "without_availability";
		var strong = document.createElement("strong");
		strong.appendChild(document.createTextNode(hotelPrice[2] + " "));
		dispDiv.appendChild(strong);
		dispDiv.appendChild(document.createTextNode(hotelPrice[3]));
		td2.appendChild(dispDiv);
	} else {
		// Booking Button
		var button = document.createElement("input");
		button.type = "Submit";
		button.className = "button-1";
		button.value = "Reserve";
		button.onclick = "javascript:document.location='" + hotelData[6] + "'";
		button.setAttribute("onclick", "javascript:document.location='" + hotelData[6] + "'");
		td2.appendChild(button);

		td2.appendChild(document.createTextNode(hotelPricesPrefix + " "));
		var strong = document.createElement("strong");
		strong.appendChild(document.createTextNode(hotelPrice[1] + " " + hotelData[7]));
		td2.appendChild(strong);
		if (vatText != null) {
			var vatTextElement = document.createTextNode(vatText);
			td2.appendChild(document.createElement("br"));
			td2.appendChild(vatTextElement);
		}

		// Show restrictions
		if (countRestrictions > 0) {
			td2.appendChild(document.createElement("br"));
			td2.appendChild(document.createElement("br"));
			td2.appendChild(createStrongEMText("Special conditions:"));
		}
		if (hotelPrice[2] > 0) {
			td2.appendChild(document.createElement("br"));
			td2.appendChild(createEMText("Minimum days of prebooking: " + hotelPrice[2]));
		}
		if (hotelPrice[3] > 0) {
			td2.appendChild(document.createElement("br"));
			td2.appendChild(createEMText("Minimum number of nights: " + hotelPrice[3]));
		}
		if (hotelPrice[6] != undefined && hotelPrice[6] != '') {
			td2.appendChild(document.createElement("br"));
			td2.appendChild(createEMText("Checkin dates: " + hotelPrice[6]));
		}
		if (hotelPrice[5] == 3) {
			td2.appendChild(document.createElement("br"));
			td2.appendChild(createEMText("Close on arrival"));
		} else if (hotelPrice[5] == 4) {
			td2.appendChild(document.createElement("br"));
			td2.appendChild(createEMText("Close on departure"));
		}
	}
	tr2.appendChild(td2);
	tr2.appendChild(createRightTD());
	table.appendChild(tr2);
	// Third ROW
	table.appendChild(createThirdRow());
    // Wrapper Element
	var div = document.createElement("div");
	div.appendChild(table);
	return div;
}

function fillPOIData(className, tableHeigth, poiData, instanceName) {

	var table = initTable(className, tableHeigth);

	// Firts ROW
	table.appendChild(createFirstRow());

	// Second ROW [Main]
	var tr2 = document.createElement("tr");
	var td2 = document.createElement("td");
	td2.className = "content";
	td2.appendChild(createCloseSpan(instanceName));

	// Title
	var title = document.createElement("h3");
	title.appendChild(createA(poiData[1]));
	td2.appendChild(title);

	// Type
	var type = document.createElement("p");
	var em = document.createElement("em");
	em.appendChild(document.createTextNode(poiData[4]));
	type.appendChild(em);
	td2.appendChild(type);

	// Description
	td2.appendChild(document.createElement("br"));

	var desc = poiData[3];
	var p = document.createElement("p");
	p.appendChild(document.createTextNode(poiData[2]));
	td2.appendChild(p);

	tr2.appendChild(td2);
	tr2.appendChild(createRightTD());
	table.appendChild(tr2);
	// Third ROW
	table.appendChild(createThirdRow());

	// Wrapper Element
	var div = document.createElement("div");
	div.appendChild(table);
	return div;
}

function initTable(className, tableHeigth) {
	var table = document.createElement("table");
	table.className = className;
	//table.height = tableHeigth + "px";

	table.width = "300px";
	table.border = "0";
	table.cellPadding = "0";
	table.cellSpacing = "0";
	return table;
}

function createFirstRow() {
    var tr1 = document.createElement("tr");
	var td1 = document.createElement("td");
	td1.colSpan = "2";
	td1.className = "bg-top";
	td1.innerHTML = "&nbsp;";
	tr1.appendChild(td1);
	return tr1;
}

function createCloseSpan(instanceName) {
	var span = document.createElement("span");
	span.className = "background";
	// A cerrar
	var a = document.createElement("a");
	a.className = "close_tooltip";
	a.appendChild(document.createTextNode("X"));
	if (instanceName) {
		a.href = "javascript:" + instanceName + ".showHideToolTip();";
	} else {
		a.href = "javascript:deletePreviousRateToolTip();";
	}

	span.appendChild(a);
	return span;
}

function createRightTD() {
    var td2_5 = document.createElement("td");
	td2_5.className = "bg-right";
	td2_5.innerHTML = "&nbsp;";
	return td2_5;
}

function createThirdRow() {
	var tr3 = document.createElement("tr");
	var td3 = document.createElement("td");
	td3.height = "25";
	td3.colSpan = "2";
	td3.className = "bg-bottom";
	td3.innerHTML = "&nbsp;";
	tr3.appendChild(td3);
	return tr3;
}