
// we want to make sure that the hover state images don't need to load on the actual hover
var preload = [
    "/img/global/sitebar/link-hover-l.gif",
    "/img/global/sitebar/link-hover-r.gif",
    "/img/global/sitebar/dropdown-top.png",
    "/img/global/sitebar/dropdown-bottom.png",
    "/img/global/th3/subitem-bottom-border.gif"
];
setTimeout(function() {
    preload.each(function(img) {
        new Element('img', { 'src': (window.CRS) ? '/static'+img:img });
    });
}, 10);

document.observe('dom:loaded', function(e) {
    $$('ol#sitenav > li').each(function(el) {
        new Tablet.HoverElement(el, {hasActive:true, cssHover:true});
    });
    if ($('subnav')) {
        $$('ol#subnav li.hoverparent').each(function(el) {
            new Tablet.HoverElement(el);
        });
    }
    // this search thing is no longer here
    // var sitebar = $('sitebar');
    // if (sitebar) {
    //     var frm = sitebar.down('form.findlocation');
    //     if (frm) {
    //         var npt = frm.down('[name=ftl]');
    //         
    //         if (npt) {
    //             var sbmt = npt.next('input.hoverelement');
    //             if (sbmt) {
    //                 npt.observe('focus', function(e) {
    //                     sbmt.activate();
    //                 });
    //                 
    //                 npt.observe('blur', function(e) {
    //                     sbmt.deactivate();
    //                 });
    //             }
    //         }
    //     }
    // }
});

document.observe('dom:loaded', function(e) {
    new Tablet.Search.TextInput('finddestination', {
        url:'/javascript/LocationComplete',
        completer:'destinationcomplete',
        select:false
    });
});
Event.onReady(function() {
	selectLabelRegion = $('selectregion').down('p').innerHTML;
	selectLabelCity = $('selectcity').down('p').innerHTML;

	if (Collections) {
		loadSelections('grouplist', Collections, null, 1);
	}

	if (Features) {
		var featuresOrder = Features.locationsOrder || null;
		loadSelections('featuredlist', Features, featuresOrder, 1);
	}

	if (Countries) {
		var countriesOrder = Countries.countriesOrder || null;
		loadSelections('countrylist', Countries, countriesOrder, defaultColumns);
	}


	Event.addBehavior({
		"#selectcountry:click" : function(e) {
			togglePopup('countries');
			/* function called after toggle because widths are set based on screen real estate, which is nothing if the display is none */
			// setPopupWidths('countrylist');
			}
		,
		"#countries span.close:click" : function(e) {
			togglePopup('countries');
		}
		,
		"#regions span.close:click" : function(e) {
			togglePopup('regions');
		}
		,
		"#cities span.close:click" : function(e) {
			togglePopup('cities');
		}
		,
		"#countries ul a:click" : function(e) {
			setValue(this, 'selectcountry', 'cou');
			$('rgn').value = '';
			$('cty').value = '';
			loadRegions();
			togglePopup('countries');

			return false;
		},
		"#featuredcities ul a:click" : function(e) {
			setValue(this, 'selectregion', 'rgn');
			hidePopup('regions');
			$('cty').value = '';
			loadCities();
			return false;
		},
		"#regionlist a:click" : function(e) {
			setValue(this, 'selectregion', 'rgn');
			hidePopup('regions');
			$('cty').value = '';
			loadCities();

			return false;
		},
/*
		set custom action for featured cites here. should set reg and cty in the form. do appropriate things to the other boxes.
*/
		"#citylist a:click" : function(e) {
			setValue(this, 'selectcity', 'cty');
			hidePopup('cities');

			return false;
		}   
	});

	if ($('cou')) {
		if (Countries[$('cou').value]) {
			selectedCountry = Countries[$('cou').value];
			setLabelValue('selectcountry', selectedCountry.name);
			$('selectcountry').addClassName('set');

			loadRegions();

			if ($('rgn') && selectedCountry.regions) {
				if (selectedCountry.regions[$('rgn').value] && $('rgn').value != '') {
					selectedRegion = selectedCountry.regions[$('rgn').value];

					if (selectedRegion) {          
						setLabelValue('selectregion', selectedRegion.name);
						$('selectregion').addClassName('set');

						loadCities();
						if ($('cty') && selectedRegion.cities) {
							if (selectedRegion.cities[$('cty').value] && $('cty').value != '') {
								selectedCity = selectedRegion.cities[$('cty').value];

								setLabelValue('selectcity', selectedCity.name);
								$('selectcity').addClassName('set');            
							}
						}
					}
				}
			}  

		} else if ($('cou').value.slice(0,1) == 'z') {
		    if (Collections[$('cou').value.slice(1)]) {
    			selectedArea = Collections[$('cou').value.slice(1)];

    			setLabelValue('selectcountry', selectedArea.name);
    			$('selectcountry').addClassName('set');
    		}
		}  
	}
	
}); // Event.onReady(function()
var Tablet = Tablet || {};
Tablet.Results = Tablet.Results || {};

Tablet.Results.map = null;
Tablet.Results.initMap = function() {
    var map = new Tablet.Map('resultsmap', {
        preMarkerClick:function() {
            $('search').addClassName('fullmap');
            // Tablet.Results.map.centerAndZoom();
        }
    });
    var markers = Tablet.Results.markers;
    
    if (typeof markers !== 'undefined') {
        var count = markers.length, marker = null;
        for (var i=count-1;i>=0;i--) {
            marker = markers[i];
            
            map.addPoint(marker.latitude, marker.longitude, {
                index:i+1, 
                bubble:marker.html
            });
        }
        
        map.centerAndZoom();
        map.checkMapZoom();
    }
    
    Tablet.Results.map = map;
    // markers = drawMarkers(data.hotelMarkers, {'offsetPoint': new GPoint(-156, -180), 'markerClick':'enlargeMapNoZoom()'});
    // 
    // var zoomLevel = getZoomLevel(data.hotelMarkers);
    // presentMap(zoomLevel);
    // 
    // if (checkZoomLevel) {
    //   zoomInterval = setInterval('checkZoomLevel(4)', 50); 
    // }
};
Event.onGoogleLoad(Tablet.Results.initMap);
