// true if IE
var IE = /*@cc_on!@*/false;


//detect flash
var flashversion = 0;
if (navigator.plugins && navigator.mimeTypes.length) {
	var x = navigator.plugins["Shockwave Flash"];
	if(x && x.description) {
		var y = x.description;
		flashversion = parseInt(y.substr(y.indexOf(".") - 2, 2), 10);
	}
} else {
	result = false;
	for(var i = 30; i >= 3 && result != true; i--){
		execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
		flashversion = i;
	}
}

if (flashversion >= 8) {
	    document.getElementsByTagName('html')[0].className = 'flashInstalled';
}
		document.getElementsByTagName('html')[0].className += ' json';



var citrusCMS = {
		
	load : function() {
		citrusCMS.openWindow();
		citrusCMS.printWindow();
		citrusCMS.formStyle();
		citrusCMS.changeImg();
		citrusCMS.boxLinks();
		citrusCMS.socialBookmarks();
		citrusCMS.searchAreas();
		citrusCMS.flashRewrite('middle>h1','/_img/layup/title2.swf', '', '1');
		citrusCMS.flashRewrite('searchform>h2','/_img/layup/title.swf', '', '1');
		if($('leftside')){citrusCMS.flashRewrite('leftside>h2','/_img/layup/title.swf', '', '1');}
		if($('middle')){citrusCMS.flashRewrite('middle>h2','/_img/layup/title.swf', '', '1');}
		if($('rightside')){citrusCMS.flashRewrite('rightside>h2','/_img/layup/title.swf', '', '1');}
		//new Autocompleter.SelectBox('sregion');
		//new Autocompleter.SelectBox('sarea');
		//new Autocompleter.SelectBox('stype');
		if ($('smonth')){new Autocompleter.SelectBox('smonth');}
	},
	
	openWindow : function(){
		
		allNodes = $A(document.getElementsByClassName("openWindow")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {pageTracker._trackPageview('/outbound/'+this.href.replace('http://',''));  window.open(this.href); return false};
		});
	},
	
	printWindow : function(){
		
		allNodes = $A(document.getElementsByClassName("printWindow")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {window.print()};
		});

	},
	
	backbutton : function() {
		allNodes = $A(document.getElementsByClassName("backtoresults")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {history.go(-1); return false;};
		});
	},
	
	socialBookmarks : function() {
		if ($('follow')) {
		allNodes = $A($('follow').childElements());
		allNodes.each(function(node, i) {
		allNodes[i].onmouseover = function() {
			if (this.nodeName=='A'){$('socialbookmarks').innerHTML = this.innerHTML;}
			}
		});
		}
	},
	
	searchAreas : function() {
		Event.observe($('sregion'), 'change', 
			function(e){
				citrusAjax.makeRequest('/content/searchareas.asp?ajax=1&sr=1&sregion='+$('sregion').value,'areaholder'); 
			});
		Event.observe($('sarea'), 'change', 
			function(e){
				citrusAjax.makeRequest('/content/searchareas.asp?ajax=1&st=1&sarea='+$('sarea').value+'&sregion='+$('sregion').value,'typeholder'); 
			});
	},
	
	changeImg : function() {
	
		allNodes = $A(document.getElementsByClassName("swapImg"));
		
		
		allNodes.each(function(node, i) {

			allNodes[i].onclick = function() {
				
				limg = this.href.split("/")[5];
				
				$("mainimg").childNodes[0].childNodes[0].src = this.href;
				$("mainimg").childNodes[0].href = '/_img/pics/l_' + limg ;
				$("mainimg").childNodes[0].title = this.getAttribute('title');
				$("mainimg").childNodes[0].childNodes[0].alt = this.getAttribute('title');
				return false;
			};
		});
	},
	
	formStyle : function(){
		
		allNodes = $A(document.getElementsByClassName("textform")); 
		allNodes.each(function(node, i) {
			
			Event.observe(allNodes[i], 'focus', 
				function(e){ 
					if(Event.element(e).nodeName != 'SELECT'){
						Event.element(e).className="textformfocus";
						if (Event.element(e).value.charAt(0) == '-') { Event.element(e).value='' }
					}
					}
				);

			Event.observe(allNodes[i], 'blur', 
				function(e){ 
					Event.element(e).className="textform";
					}
				);
		});
	},
	
	boxLinks : function(){
		
		allNodes = $A(document.getElementsByClassName("link")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {
			if (this.id !='nolink') {	window.location = this.getElementsByTagName("a")[0].href };
			}
		});
		
	},
	
	flashRewrite : function (x, movie, query, type, width, height) {
	
		if (flashversion >= 8){
			
			if (type == '1'){ //tag name
				xArea = x.split('>'); //if TagName in a id
	
				if (xArea[1]){var x = $(xArea[0]).getElementsByTagName(xArea[1]);} else {var x = document.getElementsByTagName(x);}
					
				for (var i=0;i<x.length;i++){
								
					if (!width){width = Element.getDimensions($(x[i])).width;}
					if (!height){height = Element.getDimensions($(x[i])).height;}
					color = $(x[i]).getStyle('color')
					title = x[i].innerHTML;
					
					if (title) {title = title.replace("&", "%26")}
					if (!query){q= "title="+ title + "&color="+ color;} else {q= query +"&title="+ title + "&color="+ color;}	
					
					citrusCMS.getFlash(x[i], movie, q, width, height)
					q = '';
				}
				
			} else { //id
					
				if (!width){width = Element.getDimensions($(x)).width;}
				if (!height){height = Element.getDimensions($(x)).height;}
				
				if (x=='month'){
						title = $(x).innerHTML;
						color = $(x).getStyle('color')
						query ="title="+ title + "&color="+ color;
				}
				
				citrusCMS.getFlash($(x), movie, query, width, height)

			}
			
		}	
	},

	getFlash : function(what, movie, query, width, height) {
		if (what.id == "month"){query += '&textcenterClass=1';}
		new_txt = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="'+width+'" height="'+height+'">\n';
		new_txt += '<param name="movie" value="'+movie+'">\n';
		new_txt += '<param name="quality" value="high" />\n';
		new_txt += '<param name="menu" value="false">\n';
		new_txt += '<param name="wmode" value="transparent">\n';
		new_txt += '<param name="flashvars" value="'+query+'">\n';
		new_txt += '<param name="scale" value="noscale" />\n';
		new_txt += '<param name="salign" value="lt" />\n';
		new_txt += '<embed src="'+movie+'" flashvars="'+query+'" width="'+width+'" height="'+height+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" menu="false" scale="noscale" salign="lt" wmode="transparent"></embed>\n';
		new_txt += '</object>';
		what.innerHTML = new_txt;
		what.style.visibility= 'visible';
	
	}
	
}


var citrusWeb = {
	
	showTabs : function() {
		if ($('tabs')) { 
		allNodes = $A($("tabs").childNodes)
			
			allNodes.each(function(node, i) {
				if (node.nodeName=="LI") {
					node.onclick = function() {
						
						onNodes = $("widget").select('.tabon'); 
						onNodes.each(function(node, j) {
							node.className='taboff';
						});
						
						onNodes = $("tabs").select('.on'); 
						onNodes.each(function(node, j) {
							node.className='';
						});
					
					Element.addClassName(this.childNodes[0], "on"); 
					$(this.childNodes[0].id.replace('tab','pinfo')).className = 'tabon'; 
					if (node.childNodes[0].id!='tab1'){$('pinfo').className='info';} else {$('pinfo').className='info tablebg';}
					return false;
					};
				}
			
			});	
		}
	},
	
	calbuttons : function(){
		if ($('monthbuts')){	
		allNodes = $A($("monthbuts").childNodes)
				allNodes.each(function(node, i) {
				 if (node.nodeName=='A') {
					node.onclick = function() {
						var getthismonth = this.href.split('=');
						citrusAjax.makeRequest('/content/minical.asp?month='+getthismonth[1],'pinfo1');
						return false;
					}
				 }
			});
		}
	},
	
	changeList : function(){
		if ($('homeattractions')) {
		attractionsNodes = $A($('homeattractions').childNodes);
		attractionsNodesi = $A($('homeattractions').childNodes);
	
			attractionsNodes.each(function(node, i) {
				
				if (node.nodeName == 'LI'){
					node.onmouseover = function() {
						
						attractionsNodesi.each(function(node, j){if (attractionsNodesi[j].nodeName=='LI'){	Element.removeClassName(attractionsNodesi[j], 'on');}});
							Element.addClassName(this, 'on'); 
							$('attractionimage').src=this.childNodes[0].src;
					}
					
					
					
				}
		});
		
		}


	if ($('homeoffers')) {
		offersNodes = $A($('homeoffers').childNodes);
		offersNodesi = $A($('homeoffers').childNodes);
	
			offersNodes.each(function(node, i) {
				
				if (node.nodeName == 'LI'){
					
					node.onmouseover = function() {
						
						offersNodesi.each(function(node, j){if (offersNodesi[j].nodeName=='LI'){	Element.removeClassName(offersNodesi[j], 'on');}});
							Element.addClassName(this, 'on'); 
							$('offersimage').src=this.childNodes[0].src;
					}
					
					
					
				}
		});
			
	}
	
	},
	
	specifyOther : function() {
		if ($('s_referral')) {
			Event.observe($('s_referral'), 'change', 
			function(e){
				if ($('s_referral').value=='Other'){$('referralother').style.display='block';}
			});
		}
	},
	
	
	openOffers : function(){
		
		allNodes = $A(document.getElementsByClassName("openOffer")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {pageTracker._trackPageview('/claimoffer/'+this.title);   window.open(this.href); return false};
		});
	}
	
}


var citrusAjax = {
	
	makeRequest : function(url,getID) { 
	
		var myAjax = new Ajax.Request(
		url,
		{
			method: 'post', 
			onComplete: function(originalRequest){
					$(getID).innerHTML = originalRequest.responseText;
					//if(getID=='areaholder'){new Autocompleter.SelectBox('sarea'); }
					//if(getID=='typeholder'){new Autocompleter.SelectBox('stype');}
					if (($('smonth') && getID=='smonth') || (getID=='pinfo1')){new Autocompleter.SelectBox('smonth'); citrusCMS.flashRewrite('month','/_img/layup/title.swf', '', '0');}
					citrusCMS.openWindow();
					citrusCMS.printWindow();
					citrusCMS.formStyle();
					citrusCMS.changeImg();
					citrusCMS.backbutton();
					citrusCMS.boxLinks();
					citrusWeb.showTabs();
					citrusWeb.calbuttons();
					citrusWeb.changeList();
			}
		});
	
	},
	
	submitForm : function(pageUrl) {
		
		
		$('waiting').innerHTML = '<img src="/_img/layup/loading.gif" />'
		$('submitButton').type == 'button'; //if js then remove the submit function of the button
		$('submitButton').disabled = 'disabled'; //when submited disable button
		
		waitPadd = (Element.getDimensions($('cmsForm')).height - 45) /2
		$('waiting').style.paddingTop =  waitPadd + "px"
		$('waiting').style.paddingBottom =  waitPadd + "px"
		Element.show('waiting'); //show waiting div
		
		//stops whole site appearing when ajax is called
		if(pageUrl) {pageLocation = pageUrl} else {pageLocation = window.location}
		
		var myAjax = new Ajax.Request(
		pageLocation,
		{
			method: 'post', 
			parameters: 'ajax=1&' + Form.serialize("cmsForm"),
			onComplete: citrusAjax.showResponse
		});
			
	},

	showResponse : function(originalRequest) {
			
			if(originalRequest.responseText.substring(0,4) == 'http') {
				window.location = originalRequest.responseText; //redirect
			  } else {
				new Effect.Fade('waiting') ; //hide waiting div
				$('cmsForm').innerHTML = originalRequest.responseText; //show response
				new Effect.Shake('submitButton')
				citrusCMS.formStyle(); //load js functions again for new content
  			}

	}
	
}


Event.observe(window, 'load', citrusCMS.load, false);