/* Todo

To begin... load resort data with a random resort picked.
	 a. Send tracking code to Omniture
   b. Call AJAX cfm which has a xml file with the following format.
	
	<response>
		<slideshow>
			<imagesource>http://www.endlessfun.com/media/homeslideshow/imagename.jpg</imagesource>
			...
			...
			<imagesource>http://www.endlessfun.com/media/homeslideshow/imagename.jpg</imagesource>
		</slideshow>
		<description>innerHTML of lower area</description>
	</repsonse>
	
	c. Disable all click actions on the resort icons.
	d. Display 'Loading...' on the top portion and delete innerHTML in lower section.
	e. once XML has been loaded and parsed, do the following...
		1. Replace lower content with updated contents.
		2. Switch the resort icon active status to the one currently selected.
		3. Begin loading the slideshow for this resort.
		4. Reactive all click actions on the resort icons.
*/

var ResortFrontpage = {
	init: function(options) {
		this.options = Object.extend({
			resortids: [$('caravelle'), $('beachCove'), $('seaWatch'), $('oceanCreek')],
			resortinfo: $('resort-information'),
			defaultresort: Math.floor(Math.random()*4),
			ajaxurl: '/components/ajaxproperties.cfm',
			slideshowparent: $('mastheadhome'),
			slideshowimage: $('slideshow-mainimage')
		}, options || {});
		
		this.ajax = '';
		this.slideshow = '';
		/*Test URL String to see if a default hotel is asked for*/
		var searchString = document.location.search;
	  	searchString = searchString.substring(1);
	  	var nvPairs = searchString.split("&");
	  	for (i = 0; i < nvPairs.length; i++) {
			var nvPair = nvPairs[i].split("=");
			var name = nvPair[0];
			var value = nvPair[1];
			switch(name) {
				case 'resort':
					if (value >= 0 && value <= 3) this.options.defaultresort = value;
					else if (value == "Caravelle-Resort-Myrtle-Beach") this.options.defaultresort = 0;
					else if (value == "Beach-Cove-Resort-Myrtle-Beach") this.options.defaultresort = 1;
					else if (value == "Sea-Watch-Resort-Myrtle-Beach") this.options.defaultresort = 2;
					else if (value == "Ocean-Creek-Resort-Myrtle-Beach") this.options.defaultresort = 3;
					break;
	  		}
		}
		if (this.options.defaultresort > 3) this.options.defaultresort = 0; 
		/*Setup onclick calls*/
		for (var x = 0; x < this.options.resortids.length; x++) { this.options.resortids[x].onclick = this.requestresortinfo.pass(x, this); }
		this.requestresortinfo(this.options.defaultresort);
	},
	requestresortinfo: function(newresort) {
		// omniture calls
		/*s=s_gi(s_account);
		s.linkTrackVars="prop17";
		switch(newresort) {
			case 0:
				s.prop17 = 'caravelle - home';
				break;
			case 1:
				s.prop17 = 'beach cove - home';
				break;
			case 2:
				s.prop17 = 'sea watch - home';
				break;
			case 3:
				s.prop17 = 'ocean creek - home';
				break;
		}
		s.tl(this, 'o', s.prop17);  
		//end omniture calls*/
		var ajaxurl = this.options.ajaxurl + '?id=' + newresort;
		this.ajax = new Ajax(ajaxurl, {method: 'get', onComplete: this.setactiveresort.pass(newresort, this) }).request();
	},
	setactiveresort: function(newresort, ajaxcall) {
		var xmlnewimages = this.ajax.response.xml.getElementsByTagName('imagesource');
		var xmlnewhtml = this.ajax.response.xml.getElementsByTagName('description')[0].childNodes[0].nodeValue;
		var slideshowarray = new Array();
		for (var x = 0; x < xmlnewimages.length; x++) { slideshowarray.push(xmlnewimages[x].childNodes[0].nodeValue); }
		//Set new active button and blur the focused element.
		this.options.resortids.each(function(el){ el.removeClass('on'); });
		this.options.resortids[newresort].addClass('on');
		this.options.resortids[newresort].blur();
		//Ok, at this point the innerHTML of the lower area is loaded in xmlnewhtml and the array of images is in slideshowarray.
		if (this.slideshow != '') this.slideshow.remove();
		else delete this.slideshow;
		this.options.slideshowimage.src = slideshowarray[0];
		this.slideshow = new Slideshow(this.options.slideshowimage, this.options.slideshowparent, slideshowarray, {duration: 500});
		this.options.resortinfo.innerHTML = xmlnewhtml;
		//Try to clean up mamory usage.
		delete slideshowarray; 
	}
};

window.addEvent('domready', ResortFrontpage.init.bind(ResortFrontpage));