window.addEvent('domready', function() {
	var coursetabactions = $$('#courseTabActions li a');
	var coursetabs = $$('#courseTabs div.coursetab');
	var fx = null;
	
	coursetabactions.each(function(el, index) { 
		el.addEvent('click', function(e) {
			new Event(e).stop();
			setActiveTab(index, coursetabs);
			setTabColor(coursetabactions);
			el.setStyle('color','white');
		});
	});
	
	
	setActiveTab(0, coursetabs);
	if ($('holeDetailsPopup')) {
		$('holeDetailsPopup').setOpacity(0.8);
		//Now the Ajax Request for the hole details section.... $('contentArea').getElement('h2').getProperty('id') identifies the course we are getting the data for using the model glue format for it.
		var holelayouts = new HoleLayouts($$('#holeNums li a'), $('bottomHalf'), { xmlSource: '/courseXML/' + $('contentArea').getElement('h2').getProperty('id') + '/' });
	}

	// initiate the google map
	try { loadmaps(); }
	catch(err) { /*No Google map today for them!*/ }
});

setTabColor = function(cta){
		cta.each(function(el) { 
			el.setStyle('color','#000000');
									   
		});
}
		
setActiveTab = function(index, coursetabs) {
	//fx = new Fx.Styles(coursetabs[index], {duration:600, wait:false}); forget the fx
	coursetabs.each(function(obj) { 
		obj.setStyle('display', 'none'); 
		if (obj.getFirst().getTag() == 'div') { obj.getFirst().setStyle('margin-left', '-440px'); }
	});
	coursetabs[index].setStyle('display', 'block');
	if (coursetabs[index].getFirst().getTag() == 'div') {
		coursetabs[index].getFirst().setStyle('margin-left', '13px'); 
	
		}
	//fx.start({ 'height': 281 });
}

var HoleLayouts = new Class({
	setOptions: function(options) {
		this.options = {
			xmlSource: '/xml/source.xml'
		}
		Object.extend(this.options, options || {});
	},    

	initialize: function(holeLinks, holeWrapper, options) {
		this.setOptions(options);
		this.holeLinks = holeLinks;
		this.holeDom = holeWrapper; 
		
		this.holenames = new Array();
		this.holenumber = new Array();
		this.imagesources = new Array();
		this.descriptions = new Array();
		this.pars = new Array();
		this.yards = new Array();
		this.handicap = new Array();
		
		this.ajax = new Ajax(this.options.xmlSource, {method: 'get', onComplete: this.ajaxLoaded.bind(this) }).request();
	},
	
	ajaxLoaded: function() {
		var holesets = $A(this.ajax.response.xml.getElementsByTagName('holeset'));
		holesets.each(function(el, index) {
			this.holenames.extend($A(el.getElementsByTagName('name')).map(function(item, index) { return (index % 10) ? item.childNodes[0].nodeValue : null; }).remove(null));
			this.imagesources.extend($A(el.getElementsByTagName('imagesource')).map(function(item) { return item.childNodes[0].nodeValue; }));
			this.descriptions.extend($A(el.getElementsByTagName('description')).map(function(item) { return item.childNodes[0].nodeValue; }));
			this.pars.extend($A(el.getElementsByTagName('par')).map(function(item) { return item.childNodes[0].nodeValue; }));
			$A(holesets[index].getElementsByTagName('hole')).each(function(el2, count) {
				var yardtypesArray = $A(el2.getElementsByTagName('yards')[0].getElementsByTagName('type')).map(function(item) { return item.childNodes[0].nodeValue; });
				var yardsArray = $A(el2.getElementsByTagName('yards')[0].getElementsByTagName('length')).map(function(item) { return item.childNodes[0].nodeValue; });
				var handicaptypesArray = $A(el2.getElementsByTagName('handicap')[0].getElementsByTagName('type')).map(function(item) { return item.childNodes[0].nodeValue; });
				var handicapArray = $A(el2.getElementsByTagName('handicap')[0].getElementsByTagName('count')).map(function(item) { return item.childNodes[0].nodeValue; });
				this.holenumber.push(el2.getElementsByTagName('number')[0].childNodes[0].nodeValue);
				this.yards.push({ yardtypes: yardtypesArray, distance: yardsArray, ratio: yardtypesArray.length });
				this.handicap.push({ handicaptypes: handicaptypesArray, number: handicapArray, ratio: handicaptypesArray.length });
				delete yardtypesArray, yardsArray, handicaptypesArray, handicapArray;
			}.bind(this));
		}.bind(this));
		//console.log(this.yards);
		//console.log(this.handicap);
		this.addDataClicks();
	},
	
	addDataClicks: function() {
		this.holeLinks.each(function(el, index) {
			el.addEvent('click', function(e) {
				new Event(e).stop();
				this.holeLinks.each(function(obj) { obj.removeClass('selected'); });
				el.addClass('selected');
				el.blur();
				this.setActive(index);
			}.bind(this));
		}.bind(this));
	},
	
	setActive: function(index) {
		var data = this.returnData(index);
		this.holeDom.getElement('#holeDetailsText').setText(data.description);
		this.holeDom.getElement('#holeDetails img').setProperty('src', data.imagesource);
		this.holeDom.getElement('#courseID').setHTML('<span>' + data.number + '</span>Par: ' + data.par);
		var yardage = this.holeDom.getElement('dl.yardage').empty();
		data.yards.yardtypes.each(function(el, index) {
			new Element('dt', { 'class': el.toLowerCase() }).inject(yardage).setText(el);
			new Element('dd').inject(yardage).setText(data.yards.distance[index]);
		});
		var handicap = this.holeDom.getElement('dl.handicap').empty();
		data.handicap.handicaptypes.each(function(el, index) {
			new Element('dt', { 'class': el.toLowerCase() }).inject(handicap).setText(el);
			new Element('dd').inject(handicap).setText(data.handicap.number[index]);	
		});
		delete data;
	},
	
	returnData: function(index) {
		return { number: this.holenumber[index], name: this.holenames[index], imagesource: this.imagesources[index], description: this.descriptions[index], par: this.pars[index], yards: this.yards[index], handicap: this.handicap[index] }
	}
	
});

// google map code
loadmaps = function() {
	if (GBrowserIsCompatible()) {
		coursesmap = new GMap2(document.getElementById("googleMapCourses"));
		hotelsmap = new GMap2(document.getElementById("googleMapHotels"));
		
		icons = new GIcon();
		icons.image = "/media/map-icons/mm_20_blue.png"
		icons.iconSize = new GSize(25, 20);
		icons.shadowSize = new GSize(1, 1);
		icons.iconAnchor = new GPoint(15, 15);
		icons.infoWindowAnchor = new GPoint(5,1);
		
		var region = document.location.pathname.split('/')[1];
		addPoints(coursesmap, '/mapXML/' + region + '/courses/', icons);
		addPoints(hotelsmap, '/mapXML/' + region + '/accommodations/', icons);
	}
}

function clickedhtml(name, address, address2, city, state, zip) {
	var tmp = "<div class='infoWindow'><h3>" + name + "</h3><p>" + address;
	//if (address2.length) tmp = tmp + ' ' + address2;
	var tmp = tmp + '<br />' + city + ', ' + state + ' ' + zip + '</p>';
	return tmp;
}

function addPoints(map, xmlpath, icons) {
	var rentals = new Array();
	map.clearOverlays();
	GDownloadUrl(xmlpath, function(data, responseCode) {
		var xml = GXml.parse(data);
		
		var base = xml.documentElement.getElementsByTagName('base');
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(base[0].getAttribute('lat'), base[0].getAttribute('lng')), 10);  //this needs to be dynamic based on the region
		
	  	var markers = xml.documentElement.getElementsByTagName("marker");
	  	for (var i = 0; i < markers.length; i++) {
	    	rentals[i] = new Object();
	    	rentals[i].point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
			rentals[i].gdetail = clickedhtml(markers[i].getAttribute("name"), markers[i].getAttribute("address"), markers[i].getAttribute("address2"), markers[i].getAttribute("city"), markers[i].getAttribute("state"), markers[i].getAttribute("zip"));
			rentals[i].containerId = 'dropDownContainer' + markers[i].getAttribute("idkey");
			rentals[i].address = markers[i].getAttribute("address");
			rentals[i].city = markers[i].getAttribute("city");
			rentals[i].state = markers[i].getAttribute("state");
			rentals[i].marker = new GMarker(rentals[i].point, icons[markers[i].getAttribute("category")-1]);
	    	map.addOverlay(rentals[i].marker);
			GEvent.bind(rentals[i].marker, "click", rentals[i], function(){ this.marker.openInfoWindowHtml(this.gdetail); });
			//if (id > 0) { rentals[i].marker.openInfoWindowHtml(rentals[i].gdetail); }
	  	}
	});
}

window.onunload = function() {
	try { GUnload(); }
	catch(err) { }
}