locationsArray = new Array();

window.onload = function(){

	init();
	
}

function init(){
	
	resetCountriesDD();
	resetStatesDD();
	resetCitiesDD();

	//get locations xml for dds
	new Ajax.Request('ajax.php', {method:'post',postBody:'action=getlocations',onComplete:parseLocationsXML});

}

parseLocationsXML = function(t){
	
	//create locations array
	var tags_record = t.responseXML.getElementsByTagName('record');
	
	locationsArray = new Array();
	
	var temp_array = new Array();
	
	for (var i=0;i<tags_record.length;i++){
					
		var record = tags_record[i];
		
		temp_array = new Array();
					
		for(var k=0;k<record.childNodes.length;k++){
						
			if(record.childNodes[k].firstChild!=null){
				if(record.childNodes[k].nodeType != 1) continue;
				if(record.childNodes[k].nodeName=="country"){ temp_array["country"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="state"){ temp_array["state"] = record.childNodes[k].firstChild.nodeValue; }
				if(record.childNodes[k].nodeName=="city"){ temp_array["city"] = record.childNodes[k].firstChild.nodeValue; }
			}
					
		}
		locationsArray.push(temp_array);
		
	}
	
	fillCountriesDD();
	
}

function fillCountriesDD(){

	var el_option;
	
	resetCountriesDD();
	resetStatesDD();
	resetCitiesDD();
		
	for(var i=0;i<locationsArray.length;i++){
	
		if(!$('form_country_'+locationsArray[i]['country'])){
		
			//fill in country
			el_option = new Element('option');
			el_option.id = "form_country_" + locationsArray[i]['country'];
			el_option.value = locationsArray[i]['country'];
			el_option.innerHTML = locationsArray[i]['country'];
			$('form_country').appendChild(el_option);
			
		}
	
	}

}

function fillStatesDD(){

	var activeCountry = $F('form_country');
	var el_option;
	var tempArray = new Array();
	
	resetStatesDD();
	resetCitiesDD();
	
	if(activeCountry){
		
		//create temp array
		for(var i=0;i<locationsArray.length;i++){
		
			if(locationsArray[i]['country']==activeCountry){
				
				tempArray.push(locationsArray[i]['state']);
				
			}
		
		}
		
		//modify array
		tempArray = tempArray.unique();
		tempArray = tempArray.sort();
		
		//create options
		for(var i=0;i<tempArray.length;i++){
			
				el_option = new Element('option');
				el_option.id = "form_state_" + tempArray[i];
				el_option.value = tempArray[i];
				el_option.innerHTML = tempArray[i];
				$('form_state').appendChild(el_option);
		
		}
		
	}

}

function fillCitiesDD(){

	var activeCountry = $F('form_country');
	var activeState = $F('form_state');
	var el_option;
	var tempArray = new Array();
	
	resetCitiesDD();
	
	if(activeCountry && activeState){
	
		//create temp array
		for(var i=0;i<locationsArray.length;i++){
		
			if(locationsArray[i]['country']==activeCountry && locationsArray[i]['state']==activeState){
				
				tempArray.push(locationsArray[i]['city']);
				
			}
		
		}
		
		//modify array
		tempArray = tempArray.unique();
		tempArray = tempArray.sort();
		
		//create options
		for(var i=0;i<tempArray.length;i++){
			
				el_option = new Element('option');
				el_option.id = "form_city_" + tempArray[i];
				el_option.value = tempArray[i];
				el_option.innerHTML = tempArray[i];
				$('form_city').appendChild(el_option);
		
		}
		
	}

}

function launchCity(){

	var activeCity = $F('form_city');
	var activeState = $F('form_state');
	
	$('propbox').style.display = "";
	
	clearHotels();
	
	$('city_title').innerHTML = activeCity;
	
	new Ajax.Request('ajax.php', {method:'post',postBody:'action=getcity&city='+activeCity+"&state="+activeState,onComplete:parseCityXML});
	
}

parseCityXML = function(t){
	
	var tags_record = t.responseXML.getElementsByTagName('record');
	
	var temp_array = new Array();
	
	var hotelsArray = new Array();
	
	for (var i=0;i<tags_record.length;i++){
					
		var record = tags_record[i];
		
		temp_array = new Array();
					
		for(var k=0;k<record.childNodes.length;k++){
						
			if(record.childNodes[k].firstChild!=null){
				if(record.childNodes[k].nodeType != 1) continue;
				if(record.childNodes[k].nodeName=="prop_name"){ temp_array["prop_name"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="brand"){ temp_array["brand"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="address1"){ temp_array["address1"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="city_display"){ temp_array["city"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="state_name"){ temp_array["state"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="zip"){ temp_array["zip"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="country_name"){ temp_array["country"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="telno"){ temp_array["telno"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="prop_desc"){ temp_array["desc"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="overview_url"){ temp_array["overview_url"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="photo_url"){ temp_array["photo_url"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="sop_url"){ temp_array["sop_url"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="offer1copy"){ temp_array["offer1copy"] = record.childNodes[k].firstChild.nodeValue;  }
				if(record.childNodes[k].nodeName=="booknow_url"){ temp_array["booknow_url"] = record.childNodes[k].firstChild.nodeValue;  }
				
				if(record.childNodes[k].nodeName=="prop_id"){ 
					temp_array["prop_id"] = record.childNodes[k].firstChild.nodeValue;  
					temp_array["thumbnail"] = "http://www.starwoodpromos.com/lib/property_thumbnails/" + temp_array["prop_id"] + ".jpg";
				}
			}
					
		}
		
		hotelsArray.push(temp_array);
		
	}
	
	fillHotels(hotelsArray);
	
}

function fillHotels(hotelsArray){
	
	var open = true;
	
	hotelsArray = hotelsArray.shuffle();
	
	if(hotelsArray.length>2){ open = false; }
		
	for(var k=0;k<hotelsArray.length;k++){
		
		createHotel(hotelsArray[k],open);
	
	}


}

function createHotel(row_array,open){
	
	var tempOption;
	var ratesFrom = "";
	
	//main hotel box
	var el_hotel = new Element('div');
	el_hotel.className = "hotel";
	el_hotel.id = "hotel_"+row_array['prop_id'];
	
	//titlebar link
	var el_titlebar_a = new Element('a');
	el_titlebar_a.href = "#";
	el_titlebar_a.className = 'hotel_titlebar_a';
	el_titlebar_a.onclick = function(){ toggleBlock(row_array['prop_id']); return false; }
	el_hotel.appendChild(el_titlebar_a);
	
	//titlebar
	var el_titlebar = new Element('div');
	el_titlebar.id = "hotel_"+row_array['prop_id']+"_titlebar";
	el_titlebar.className = "titlebar";
	
	//rates from
	if(row_array['rates_from']!=null){
		ratesFrom = " - <span class='rates_from'>Rates from $" + row_array['rates_from']+"</span>";
	}
	
	if(open){ 
		el_titlebar.innerHTML = "[-] " + row_array["prop_name"] + ratesFrom;
	}else{
		el_titlebar.innerHTML = "[+] " + row_array["prop_name"] + ratesFrom;
	}
	el_titlebar_a.appendChild(el_titlebar);
	
	//block
	var el_block = new Element('div');
	el_block.id = "hotel_"+row_array['prop_id']+"_block";
	if(!open){ el_block.style.display = 'none'; }
	el_block.className = "block";
	
	el_hotel.appendChild(el_block);
	
	//fieldset
	var el_fieldset = new Element('fieldset');
	el_block.appendChild(el_fieldset);
	
	//left
	var el_left = new Element('div');
	el_left.className = 'hotel_left';
	el_fieldset.appendChild(el_left);
	
	//thumbnail
	var el_thumbnail = new Element('img');
	el_thumbnail.className = 'hotel_tn';
	el_thumbnail.src = row_array['thumbnail'];
	el_left.appendChild(el_thumbnail);
	
	//overview link
	var el_photos_link = new Element('a');
	el_photos_link.href = "#";
	el_photos_link.innerHTML = "Overview";
	el_photos_link.onclick = function(){ window.open(formatTag(row_array['overview_url'],row_array['prop_id'],row_array['brand'],'LEARN')); }
	el_left.appendChild(el_photos_link);
	
	//photos link
	var el_photos_link = new Element('a');
	el_photos_link.href = "#";
	el_photos_link.innerHTML = "Photos";
	el_photos_link.onclick = function(){ launchGallery(formatTag(row_array['photo_url'],row_array['prop_id'],row_array['brand'],'PHOTOS')); }
	el_left.appendChild(el_photos_link);
	
	//sop link
	var el_photos_link = new Element('a');
	el_photos_link.href = "#";
	el_photos_link.innerHTML = "View MORE Offers";
	el_photos_link.onclick = function(){ window.open(formatTag(row_array['overview_url'],row_array['prop_id'],row_array['brand'],'LEARN')); }
	el_left.appendChild(el_photos_link);
	
	//mid
	var el_mid = new Element('div');
	el_mid.className = 'hotel_mid';
	el_fieldset.appendChild(el_mid);
	
	//hotel address
	var el_hotel_address = new Element('div');
	el_hotel_address.className = "hotel_address";
	el_hotel_address.innerHTML = row_array['address1'] + " " + row_array['city'] + ", " + row_array['state'] + " " + row_array['zip'] + " Phone:" + row_array['telno'];
	el_mid.appendChild(el_hotel_address);
	
	//hotel desc
	var el_hotel_description = new Element('div');
	el_hotel_description.className = "hotel_description";
	el_hotel_description.innerHTML = row_array['desc'];
	el_mid.appendChild(el_hotel_description);
	
	//bn box
	var el_bnbox = new Element('div');
	el_bnbox.className = 'bnbox';
	el_bnbox.innerHTML = "<span>Earn 1,000 Alaska Airlines Mileage Plan™ Bonus Miles for stays seven days a week.</span>";
	el_bnbox.innerHTML += "<a href='"+formatTag(row_array['booknow_url'],row_array['prop_id'],row_array['brand'],'BOOKNOW')+"' target='_blank'><img src='images/btn_bn.jpg' class='btn_bn'></a>";
	
	el_mid.appendChild(el_bnbox);
	
	$('hotels').appendChild(el_hotel);

}

function launchGallery(url){
		window.open(url,'photosPopUp','width=650,height=440,resizable=no,scrollbars=no,status=no');
}

function toggleBlock(id){
	
	var el = $("hotel_"+id+"_block");
	var el_titlebar = $("hotel_"+id+"_titlebar");
	
	if(el.style.display=="none"){
		el.style.display = "";
		el_titlebar.innerHTML = el_titlebar.innerHTML.replace('[+]','[-]');
	}else{
		el.style.display = "none";
		el_titlebar.innerHTML = el_titlebar.innerHTML.replace('[-]','[+]');
	}
	
}

function clearHotels(){

	//clear hotels
	while ( $('hotels').childNodes.length >= 1 ){
		$('hotels').removeChild( $('hotels').firstChild );     
    }

}

function closePropBox(){
	
	$('propbox').style.display = "none";
	
	init();
	
}


function menuSelect(num){

	if(currentSlide!==num){

		$('menu'+currentSlide).className = "";
		$('menu'+num).className = "active";
		
		$('slide'+currentSlide).fade({ duration: 1.0, fps:60 });
		$('slide'+num).appear({ duration: 1.0, fps:60 });
		
		currentSlide = num;
	
		init();
		
	}

}

function launchTerms(){
		window.open('terms.html','terms','left=20,top=20,width=500,height=560,toolbar=0,resizable=1,menu=0,scrollbars=0,status=0');
}

function resetCountriesDD(){

	//clear states
	while ( $('form_country').childNodes.length >= 1 ){
		$('form_country').removeChild( $('form_country').firstChild );     
    }
	var el_option = new Element('option');
	el_option.innerHTML = "Select Country";
	$('form_country').appendChild(el_option);

}

function resetStatesDD(){

	//clear states
	while ( $('form_state').childNodes.length >= 1 ){
		$('form_state').removeChild( $('form_state').firstChild );     
    }
	var el_option = new Element('option');
	el_option.innerHTML = "Select State/Province";
	$('form_state').appendChild(el_option);

}

function resetCitiesDD(){

	//clear states
	while ( $('form_city').childNodes.length >= 1 ){
		$('form_city').removeChild( $('form_city').firstChild );     
    }
	var el_option = new Element('option');
	el_option.innerHTML = "Select City/Area";
	$('form_city').appendChild(el_option);

}

