var	_TUITRACK = {};
var dls_deals;
var dls_hotdeals = Class.create({
	initialize: function(instanceName) {
		this.instanceName = instanceName;
		this.container = false;
		this.update_url = '/hotdeals_update.cfm';
		this.data = "";
		this.modal = "";

		this.boards_filter = "";
		this.departs_filter = "";
		this.months_filter = "";
		this.nights_filter = "";
		this.initial_load = true;

		this.current_offer_id = 0;
	},
	getData: function() {
		var objectParent = this;
		new Ajax.Request(this.update_url, {
			method:'get',
			parameters: {
				hotdeals_departs:objectParent.departs_filter,
				hotdeals_boardbasis:objectParent.boards_filter,
				hotdeals_nights:objectParent.nights_filter,
				hotdeals_month:objectParent.months_filter
			},
			onSuccess: function(transport) {
				var json = transport.responseText.evalJSON();
				objectParent.data = json;
			}
		});
	},
	getDataAndUpdate: function() {
		var objectParent = this;
		try {$('dls_hotdeals_wait').style.display = 'block';} catch (e) {}
		new Ajax.Request(this.update_url, {
			method:'get',
			parameters: {
				hotdeals_departs:objectParent.departs_filter,
				hotdeals_boardbasis:objectParent.boards_filter,
				hotdeals_nights:objectParent.nights_filter,
				hotdeals_month:objectParent.months_filter
			},
			onSuccess: function(transport) {
				var json = transport.responseText.evalJSON();
				var temp = objectParent.data.optionsData;
				objectParent.data = json;
				objectParent.data.optionsData = temp;
				objectParent.update();
				try {$('dls_hotdeals_wait').style.display = 'none';} catch (e) {}
			}
		});
	},
	update: function() {   
		var Parent = document.getElementById(this.id_Target);
		var minstar = 2;
		var cellWidth = Math.floor(200/(6-minstar));
		while(Parent.hasChildNodes())
		{
			Parent.removeChild(Parent.firstChild);
		}
		
		var mainTable = document.createElement('table');
		mainTable.id = this.id_Target + "_table";
		mainTable.cellSpacing = '0px';
		mainTable.cellPadding = '0px';
		$(this.id_Target).appendChild(mainTable);

		var starRow = mainTable.insertRow(mainTable.rows.length);
		
		var starPrefix = starRow.insertCell(0);
		starPrefix.style.width = "140px";
		starPrefix.style.height = "20px";
		var nextCellInsert = 1;
		
		for (var i=minstar; i<6; i++) {
			starCell = starRow.insertCell(nextCellInsert);
			nextCellInsert = nextCellInsert + 1;
			starCell.style.width = cellWidth+'px';
			starCell.style.height = "20px";
			starCell.style.textAlign = "center";
			starCell.style.color = "#ff6600";
			starCell.style.fontWeight = "bold";
			starCell.innerHTML = i;
			starImg = document.createElement('img');
			starImg.src = "http://www.directline-skiing.co.uk/images/matrix-star.gif";
			starImg.style.width = "10px";
			starImg.style.height = "9px";
			starCell.appendChild(starImg);
		}
		var last_resort = "";
		var last_star = minstar;
		var cur_resort_num = 0;
		var curRow;

		if (this.data.data.DATA.length > 0) {
			for (var i=0; i<this.data.data.DATA.length; i++) {
				if (cur_resort_num < 9) {
					var curBg;
					var curItem = this.data.data.DATA[i];
					for (var j=0; j<this.data.data.DATA[i].length; j++) {
						window[this.data.data.COLUMNS[j]] = curItem[j];
					}
					if (TD_STAR_RATING > minstar - 1) { 
						if (RESORT_DISPLAY_NAME != last_resort) {
							cur_resort_num++;
	
							if (cur_resort_num < 9) {
	
								curRow = mainTable.insertRow(mainTable.rows.length);
								curRow.id = RESORT_DISPLAY_NAME + "_holder";
								
								last_star = minstar;
								last_resort = RESORT_DISPLAY_NAME;
								if (cur_resort_num % 2) {
									curBg = "#EFF4FF";
								} else {
									curBg = "#FFFFFF";
								}
								var resort_name = curRow.insertCell(0);
								resort_name.id = RESORT_DISPLAY_NAME + "_holder";
								resort_name.style.width = "140px";
								resort_name.style.height = "20px";
								resort_name.style.background = curBg;
								resort_name.style.paddingLeft = '3px';
								resort_name.innerHTML = RESORT_DISPLAY_NAME;
								var nextCellInsert = 1;
								for (var k=minstar; k<6; k++) {
									var price_cell = curRow.insertCell(nextCellInsert);
									nextCellInsert = nextCellInsert + 1;
									price_cell.style.background = curBg;
									price_cell.style.width = cellWidth+'px';
									price_cell.style.height = "20px";
									price_cell.style.textAlign = "center";
									price_cell_link = document.createElement('a');
										price_cell_link.id = RESORT_DISPLAY_NAME+'_'+k+'_price';
										price_cell_link.style.textAlign = "center";
										price_cell_link.style.textDecoration = 'underline';
										price_cell_link.style.color = "#003399";
									price_cell.appendChild(price_cell_link);
								}
							}
						}
						
						if (cur_resort_num < 9) {
							var curStar = $(RESORT_DISPLAY_NAME + "_" + TD_STAR_RATING + "_price");
							curStar.innerHTML = "&pound;" + TD_PRICE_SYSTEM;
							curStar.style.cursor = "pointer";
							curStar.alt = ID;
							curStar.observe('click',this.clickedOfferPrice.bind(this));
						}
					}
				}

			}
		}

		/* populate search fields */
		if (this.initial_load) {
			var divtitle = document.createElement('div');
			divtitle.className = 'hd_title';
			divtitle.appendChild(document.createTextNode('Filter Hot Deals'));
			$(this.id_Target + "_search").appendChild(divtitle);
			
			this.addSearchField('departs',"Departure Airport");
			this.addSearchField('boards',"Board Basis");
			this.addSearchField('nights', "Duration");
			this.addSearchField('months', "Month");
			
			var divtitle = document.createElement('div');
			divtitle.className = 'hd_blurb';
			divtitle.appendChild(document.createTextNode('The grid shows some of'));
			divtitle.appendChild(document.createElement('br'));
			divtitle.appendChild(document.createTextNode('our hottest deals. Just'));
			divtitle.appendChild(document.createElement('br'));
			divtitle.appendChild(document.createTextNode('click on a price or filter'));
			divtitle.appendChild(document.createElement('br'));
			divtitle.appendChild(document.createTextNode('the deals with the above'));
			$(this.id_Target + "_search").appendChild(divtitle);
			
			this.initial_load = false;
		}
	},
	addSearchField: function(searchFieldName,searchFieldTitle) {
		var new_select = document.createElement('select');
		new_select.id = searchFieldName + "_select";

		var newOption = document.createElement('option');
		newOption.value = "";
		newOption.innerHTML = searchFieldTitle;
		new_select.appendChild(newOption);

		for (var i=0; i<this.data.optionData[searchFieldName].DATA.length; i++) {
			var curItem = this.data.optionData[searchFieldName].DATA[i];

			newOption = document.createElement('option');
			newOption.value = curItem[0];
			newOption.innerHTML = curItem[1];
			if (curItem[0] == this[searchFieldName + "_filter"]) {
				newOption.selected = "selected";
			}
			new_select.appendChild(newOption);
		}
		$(this.id_Target + '_search').appendChild(new_select);
		$(new_select.id).observe('change',this.searchFieldChanges.bind(this));
		$(this.id_Target + '_search').appendChild(document.createElement('br'));
	},
	searchFieldChanges: function(event) {
		try {
			var currentSelectName = event.target.id.split("_")[0];
			var curValue = event.target.options[event.target.selectedIndex].value;
			try {
				if (pageTracker !== undefined)
				{
					pageTracker._trackEvent("TUI Offers", "Filter", currentSelectName + " " + event.target.options[event.target.selectedIndex].text);
				}
			} catch (e) {}
			this[currentSelectName + "_filter"] = curValue;
			this.getDataAndUpdate();
		} catch (e) {}
	},
	clickedOfferPrice: function(event) {
		this.current_offer_id = event.target.alt;
		var arr = event.target.id.split("_");
		_TUITRACK.country = arr[0];
		_TUITRACK.stars = arr[1];
		this.popupModal(event.clientX,event.clientY);
	},
	setupModal: function() {
		this.modal = document.createElement('div');
		this.modal.style.width = "170px";
		//this.modal.style.height = "200px";
		this.modal.id = this.id_Target + "_modal_popup";
		this.modal.style.position = "absolute";
		this.modal.style.background = "#fff";
		this.modal.style.border = "1px solid #000";
		this.modal.style.display = "none";
		this.modal.style.zIndex = "100";
		this.modal.style.padding = "10px";
		this.modal.style.textAlign = "left";
		
		this.modal_content = document.createElement('div');
		this.modal_content.id = this.id_Target + "_modal_content";
		this.modal_content.style.zIndex = "101";
		this.modal.appendChild(this.modal_content);

		var modal_close = document.createElement('div');
		modal_close.style.width = "15px";
		modal_close.style.height = "15px";
		modal_close.style.lineHeight = "15px";
		modal_close.style.fontWeight = "bold";
		modal_close.style.textAlign = "center";
		modal_close.style.border = "1px solid #000";
		modal_close.style.background = "#fff";
		modal_close.style.position = "absolute";
		modal_close.id = 'dh_modal_close';
		modal_close.style.top = "2px";
		modal_close.style.right = "2px";
		modal_close.style.cursor = "pointer";
		modal_close.style.zIndex = "102";
		modal_close.appendChild(document.createTextNode('X'));;
		this.modal.appendChild(modal_close);

		var room_select_title = document.createElement('div');
		room_select_title.style.fontWeight = "bold";
		room_select_title.style.paddingBottom = "5px";
		room_select_title.innerHTML = "Rooms required";
		this.modal.appendChild(room_select_title);
		this.room_select = document.createElement('select');
		this.room_select.id = 'dh_roomselect';
		for (var i=0; i<6; i++) {
			var room_option = document.createElement('option');
			room_option.value = i+1;
			room_option.innerHTML = (i+1) + ((i>0)?" rooms":" room");
			this.room_select.appendChild(room_option);
		}
		this.modal.appendChild(this.room_select);
		
		var room_occupants = document.createElement('div');
		room_occupants.id = this.id_Target + "_occupants_div";
		this.modal.appendChild(room_occupants);
		var room_children_ages = document.createElement('div');
		room_children_ages.id = this.id_Target + "_children_ages_div";
		this.modal.appendChild(room_children_ages);
		document.body.appendChild(this.modal);
		
		$(this.room_select.id).observe('change',this.changedNumberOfRooms.bind(this));
		$(modal_close.id).observe('click', this.closeModal.bind(this));

		this.renderOccupants(1,2,0);
		this.renderChildrenAgeFields();
	
		var submit_button = document.createElement('input');
		submit_button.type = "button";
		submit_button.value = "Search";
		//submit_button.style.marginTop = "3px";
		submit_button.style.position = 'absolute';
		submit_button.style.bottom = '11px';
		submit_button.style.right = '10px';
		submit_button.style.width = '62px';
		submit_button.id = 'hd_searchbutton';
		this.modal.appendChild(submit_button);
		$(submit_button.id).observe('click',this.submitSearch.bind(this));
	},
	submitSearch: function(event) {
		var final_params = new Object();

		var offer_id = this.current_offer_id;
		var number_of_rooms = this.room_select.options[this.room_select.selectedIndex].value;
		var numberOfAdultsTotal = 0;
		var numberOfChildrenTotal = 0;
		for (var i=0; i<number_of_rooms; i++) {
			var curAdultSelect = $("room_" + (i+1) + "_adults");
			var curChildSelect = $("room_" + (i+1) + "_children");
			
			numberOfAdultsTotal += parseInt(curAdultSelect.options[curAdultSelect.selectedIndex].value);
			numberOfChildrenTotal += parseInt(curChildSelect.options[curChildSelect.selectedIndex].value);
			
			final_params["frm_AdultsRoom" + (i+1)] = parseInt(curAdultSelect.options[curAdultSelect.selectedIndex].value);
			final_params["frm_ChildrenRoom" + (i+1)] = parseInt(curChildSelect.options[curChildSelect.selectedIndex].value);

		}

		for (var i=0; i<numberOfChildrenTotal; i++) {
			var curChildAgeSelect = $("child_age_" + (i+1));
			
			final_params["frm_Child" + (i+1)] = parseInt(curChildAgeSelect.options[curChildAgeSelect.selectedIndex].value);
		}
		
		final_params["offer_id"] = offer_id;
		final_params["frm_RoomsRequired"] = parseInt(number_of_rooms);
		
		try {final_params['board_basis'] = $('boards_select').value;} catch (e) {}
		try {
			if (pageTracker !== undefined && _TUITRACK !== undefined)
			{
				pageTracker._trackEvent("TUI Offers", "Search", _TUITRACK.country + " " + _TUITRACK.stars + " star");
			}
		} catch (e) {}
		var end_form = document.createElement('form');
		end_form.method = "post";
		end_form.action = "/searchRedirect.cfm";

		for(var index in final_params) {
			var hidden_field = document.createElement('input');
			hidden_field.type = "hidden";
			hidden_field.name = index;
			hidden_field.value = final_params[index];
			end_form.appendChild(hidden_field);
		}
		document.body.appendChild(end_form);
		end_form.submit();
	},
	changedNumberOfRooms: function(event) {
		var numberOfRooms = event.target.options[event.target.selectedIndex].value;
		this.renderOccupants(numberOfRooms,2,0);
		this.renderChildrenAgeFields();
	},
	changedNumberOfChildren: function(event) {
		var roomNumber = event.target.id.split("_")[1];
		var numberOfChildren = event.target.options[event.target.selectedIndex].value;

		this.renderChildrenAgeFields();
	},
	renderChildrenAgeFields: function() {
		var Parent = document.getElementById(this.id_Target + "_children_ages_div");
		while(Parent.hasChildNodes())
		{
			Parent.removeChild(Parent.firstChild);
		}
		var numberOfRooms = this.room_select.options[this.room_select.selectedIndex].value;
		var numberOfChildrenTotal = 0;
		for (var i=0; i<numberOfRooms; i++) {
			var curChildSelect = $("room_" + (i+1) + "_children");
			
			numberOfChildrenTotal += parseInt(curChildSelect.options[curChildSelect.selectedIndex].value);
		}

		if (numberOfChildrenTotal > 0) {
			var title_span = document.createElement('div');
			title_span.style.fontWeight = 'bold';
			title_span.style.paddingBottom = '4px';
			title_span.style.paddingTop = '3px';
			title_span.style.fontSize = '10px';
			title_span.innerHTML = "Please specify the ages of children at time of travel.";
			Parent.appendChild(title_span);

			for (var i=0; i<numberOfChildrenTotal; i++) {
				var this_row = document.createElement('div');
					var child_title = document.createElement('span');
					child_title.style.fontWeight = "bold";
					child_title.innerHTML = "Child " + (i+1) + "&nbsp;&nbsp;&nbsp;";
					var child_age_select = document.createElement('select');
					child_age_select.id = "child_age_" + (i+1);
	
					for (var j=0; j<17; j++) {
						var child_age_option = document.createElement('option');
						child_age_option.value = j;
						child_age_option.innerHTML = j;
						child_age_select.appendChild(child_age_option);
					}
					this_row.appendChild(child_title);
					this_row.appendChild(child_age_select)
				Parent.appendChild(this_row);
			}
		} else {
			var filler_div = document.createElement('div');
			filler_div.style.height = '28px';
			Parent.appendChild(filler_div);
		}
	},
	renderOccupants: function(occupants_count,adult_default,child_default) {
		var Parent = document.getElementById(this.id_Target + "_occupants_div");
		while(Parent.hasChildNodes())
		{
			Parent.removeChild(Parent.firstChild);
		}
		
		var div_title_adult = document.createElement('div');
		var div_title_child = document.createElement('div');
		var div_title_div = document.createElement('div');
		
		div_title_adult.appendChild(document.createTextNode('Adults'));
		div_title_child.appendChild(document.createTextNode('Under 18s'));
		
		
		div_title_div.className = 'hd_occ_titles';
		
		div_title_adult.style.width = '94px';
		div_title_child.style.width = '72px';
		
		div_title_div.appendChild(div_title_adult);
		div_title_div.appendChild(div_title_child);
		
		Parent.appendChild(div_title_div);
		
		for(var i=0; i<occupants_count; i++) {
			var this_row = document.createElement('div');
				
				var room_title = document.createElement('span');
				room_title.style.fontWeight = "bold";
				room_title.innerHTML = "Room " + (i+1) + "&nbsp;&nbsp;&nbsp;";
	
				var room_adult_select = document.createElement('select');
				room_adult_select.id = "room_" + (i+1) + "_adults";
				room_adult_select.style.marginLeft = '0px';
	
				var room_child_select = document.createElement('select');
				room_child_select.id = "room_" + (i+1) + "_children";
				room_child_select.style.marginLeft = '14px';
	
				for (var j=0; j<10; j++) {
					var select_option_adult = document.createElement('option');
					select_option_adult.value = (j);
					select_option_adult.innerHTML = (j);
					if (j == adult_default) { select_option_adult.selected = "selected"; }
					
					var select_option_child = document.createElement('option');
					select_option_child.value = (j);
					select_option_child.innerHTML = (j);
					if  (j == child_default) { select_option_child.selected = "selected"; }
	
					room_adult_select.appendChild(select_option_adult);
					room_child_select.appendChild(select_option_child);
				}
				this_row.appendChild(room_title);
				this_row.appendChild(room_adult_select);
				this_row.appendChild(room_child_select);
				
			Parent.appendChild(this_row);
			$(room_child_select.id).observe('change', this.changedNumberOfChildren.bind(this));
		}
	},
	popupModal: function(x,y) {
		this.closeModal();
		this.setupModal();
			
		this.modal.style.display = "block";
		this.modal.style.left = x + "px";
		this.modal.style.top = y + "px";
	},
	closeModal: function() {
		try {
			this.modal.style.display = "none";
			document.body.removeChild(this.modal);
			this.modal = "";
		} catch (e) {}
	}
});