//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 >= 6) {
	    document.getElementsByTagName('html')[0].className = 'flashInstalled';
}

document.write('<style>#timetable table{display: none;}</style>')


function openWindow(thisurl){
	
		var left = Math.floor((screen.availWidth - 800) / 2);
		var top = Math.floor((screen.availHeight - 600) / 2);
		window.open(thisurl, 'newwindow', 'scrollbars=no, status=yes, left='+left+', top='+top+', width=800, height=600'); return false;
}

function openWindow2(thisurl){
	
		var left = Math.floor((screen.availWidth - 800) / 2);
		var top = Math.floor((screen.availHeight - 450) / 2);
		window.open(thisurl, 'newwindow', 'scrollbars=no, status=yes, left='+left+', top='+top+', width=800, height=450'); return false;
}


var citrusCMS = {
		
	load : function() {
		citrusCMS.openWindow();
		citrusCMS.printWindow();
		citrusCMS.formStyle();
		citrusCMS.openTour();
		citrusCMS.contactForm();
		citrusCMS.openFlyover();
		citrusCMS.openWeddingVideo();
		if($("leftside")) {citrusCMS.flashRewrite('leftside>a','/_img/layup/navi.swf', '', '1')}
		if($("middle")) {citrusCMS.flashRewrite('middle>h1','/_img/layup/title.swf', '', '1')}
		if ($('clubvideo')) {$('clubvideo').onclick=function(){openWindow2(this.href); return false;}}
	},
	
	openWindow : function(){
		
		allNodes = $A(document.getElementsByClassName("openWindow")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {
			if (this.href=='http://www.wentworthclub.com/flyover/westcourse/'){
				var left = Math.floor((screen.availWidth - 800) / 2);
						var top = Math.floor((screen.availHeight - 600) / 2);
						
						var  is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
						if (is_chrome){
							window.open('/flyover/westcourse/', 'newwindow', 'scrollbars=no, status=yes, left='+left+', top='+top+', width=719, height=600'); return false;
						} else {
							window.open('/flyover/westcourse/', 'newwindow', 'scrollbars=no, status=yes, left='+left+', top='+top+', width=719, height=542'); return false;
						}
			} else {
			window.open(this.href); return false};
			}
		});
	},
	
	
	openTour : function(){
		
		allNodes = $A(document.getElementsByClassName("tour")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {
		
					var left = Math.floor((screen.availWidth - 800) / 2);
					var top = Math.floor((screen.availHeight - 600) / 2);
					
					is_chrome = false;
					if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1){is_chrome = true;}
					if (navigator.userAgent.toLowerCase().indexOf('chrome/7') > -1){is_chrome = false;}
				
					if (is_chrome){
						window.open(this.href, this.title.replace(/ /g,''), 'scrollbars=no, status=yes, left='+left+', top='+top+', width=960, height=538'); return false;
					} else {
						window.open(this.href, this.title.replace(/ /g,''), 'scrollbars=no, status=yes, left='+left+', top='+top+', width=960, height=480'); return false;
					}
			 }
		});
	},
	
	
	openWeddingVideo : function(){
		
		allNodes = $A(document.getElementsByClassName("weddingtvbutton")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {
		
					var left = Math.floor((screen.availWidth - 800) / 2);
					var top = Math.floor((screen.availHeight - 600) / 2);
					
					is_chrome = false;
					if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1){is_chrome = true;}
					if (navigator.userAgent.toLowerCase().indexOf('chrome/7') > -1){is_chrome = false;}
				
					if (is_chrome){
						window.open(this.href, this.title.replace(/ /g,''), 'scrollbars=no, status=yes, left='+left+', top='+top+', width=640, height=400'); return false;
					} else {
						window.open(this.href, this.title.replace(/ /g,''), 'scrollbars=no, status=yes, left='+left+', top='+top+', width=640, height=400'); return false;
					}
			 }
		});
	},
	
	printWindow : function(){
		
		allNodes = $A(document.getElementsByClassName("printWindow")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {window.print()};
		});

	},
	
	contactForm : function() {
		if ($('Enquiry')) {
			allcNodes = $A(document.getElementsByClassName("contactform")); 
			allrNodes = $A(document.getElementsByClassName("recruitmentform"));
			$('Enquiry').onchange = function(){
				if ($('Enquiry').value=='13') {
					Element.removeClassName('uploader', "noshow")
					allcNodes.each(function(node, i) {Element.addClassName(allcNodes[i], "noshow")})
					allrNodes.each(function(node, i) {Element.removeClassName(allrNodes[i], "noshow")})
			} else { 
				if (($('Enquiry').value =='10') || (($('Enquiry').value =='18')) || ($('Enquiry').value =='5') || ($('Enquiry').value =='7') || ($('Enquiry').value =='9')) {
					Element.removeClassName('heardaboutus', "noshow2"); 
				} else {
					Element.addClassName('heardaboutus', "noshow2"); 
				}
					Element.addClassName('uploader', "noshow"); 
					allcNodes.each(function(node, i) {Element.removeClassName(allcNodes[i], "noshow")})
					allrNodes.each(function(node, i) {Element.addClassName(allrNodes[i], "noshow")})
			}
		  }
		}
	},
	
	formStyle : function(){
		
		allNodes = $A(document.getElementsByClassName("textform")); 
		allNodes.each(function(node, i) {
			
			Event.observe(allNodes[i], 'focus', 
				function(e){ 
					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";
					}
				);
		});
	},
	
	flashRewrite : function (x, movie, query, type) {
	
		if (flashversion >= 6){
				
			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++){
								
					width = Element.getDimensions($(x[i])).width;
					height = Element.getDimensions($(x[i])).height;
					title = x[i].innerHTML;
					
					
					
					if (title) {title = title.replace("&", "%26")}
					if (!query){q= "title="+ title;} else {q= query +"&title="+ title;}	
										
						// check for flash in 'a' tag
						if(x[i].nodeName == 'A') {
							
							q+= "&link="+ x[i].href.replace("&", "%26");
							if (x[i].className == "flash" || x[i].className == "flash on"){
								
								if (x[i].className == "flash on"){ q+= "&s=1" }
								citrusCMS.getFlash(x[i], movie, q, width, height)
							
							}
							
						// not a	
						}else{
							citrusCMS.getFlash(x[i], movie, q, width, height)
						}
						q = '';
					
				}
				
			} else { //id
				width= Element.getDimensions($(x)).width;
				if (x=='player') {width='500'; query='mc='+movie; movie='video.swf'}
				citrusCMS.getFlash($(x), movie, query, width, Element.getDimensions($(x)).height)

			}
			
		}
		
	},

	getFlash : function(what, movie, query, width, height) {

		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';
	
	
	},
	
	
	showFlyover : function(){
		citrusCMS.flashRewrite('player','Hole01.flv', '', '0');
		allNodes = $A(document.getElementsByClassName("flyover")); 
		allNodesClass = $A(document.getElementsByClassName("flyover")); 
		allNodes.each(function(node, i) {
				 
		allNodes[i].onclick = function() {
			allNodesClass.each(function(node, j){allNodesClass[j].className='flyover';});
			this.className +=' on'; 
			citrusCMS.flashRewrite('player',this.href,'' , '0'); 
			return false;
			};
		});
		
		if ($('playall')) {
			$('playall').onclick = function(){allNodesClass.each(function(node, j){allNodesClass[j].className='flyover';}); citrusCMS.flashRewrite('player','Wentworth_West_Course.flv','' , '0'); return false;}
		}
													  
	},
	
	openFlyover : function(){
		if ($('openflyover')) {
		var left = Math.floor((screen.availWidth - 800) / 2);
		var top = Math.floor((screen.availHeight - 600) / 2);
		
		var  is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
		if (is_chrome){
			$('openflyover').onclick=function(){window.open('/flyover/westcourse/', 'newwindow', 'scrollbars=no, status=yes, left='+left+', top='+top+', width=719, height=600'); return false;}
		} else {
			$('openflyover').onclick=function(){window.open('/flyover/westcourse/', 'newwindow', 'scrollbars=no, status=yes, left='+left+', top='+top+', width=719, height=542'); return false;}
		}
		}
		
		allNodesHole = $A(document.getElementsByClassName('viewFlyover'));
		allNodesHole.each(function(node, i) {
		allNodesHole[i].onclick = function() {		
			var left = Math.floor((screen.availWidth - 800) / 2);
			var top = Math.floor((screen.availHeight - 600) / 2);
			
				var  is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
				if (is_chrome){
					window.open(this.href, 'newwindow', 'scrollbars=no, status=yes, left='+left+', top='+top+', width=719, height=600'); return false;
				} else {
					window.open(this.href, 'newwindow', 'scrollbars=no, status=yes, left='+left+', top='+top+', width=719, height=542'); return false;
				}
		}
		});
	}
	
}


var citrusWeb = {
	
		
	showTable : function(){
		
	timeNodes = $A($("timetable").childNodes)
		
		timeNodes.each(function(node, i) {
			if (node.nodeName=="LI") {
				node.onclick = function() {
					timeNodes.each(function(node, j) {timeNodes[j].className="";})
					Element.addClassName(this, "on");
					//new Effect.BlindDown(this.childNodes[1])
					 
				};
			}

		});
		
	},

	
  	 gmapload : function() {
		if (GBrowserIsCompatible()) {
		  
			var map = new GMap2($('googlemap'));
			map.addControl(new GSmallMapControl());
			map.setCenter(new GLatLng(51.401759,-0.589195), 13);
	
		
		function createMarker(point) {
			var marker = new GMarker(point);
		
		  GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml("<span style=\"color: #000;\"><strong>Wentworth Club</strong><br />Wentworth Drive<br />Virginia Water<br />Surrey<br />GU25 4LS<br />Tel: +44(0)1344 842201<br />&nbsp;</span>");
			
		  });
		
		  return marker;
		}
		
			var point = new GPoint(-0.589195,51.401759);
			var marker = createMarker(point);
			map.addOverlay(marker);
			  }
			}


}

var citrusAjax = {
	
	makeRequest : function(url,getID,what) { 
	
	
		allNodes = $A(document.getElementsByClassName("link")); 
		allNodes.each(function(node, i) {
				Element.removeClassName(node, "on")
		});
				

		what.className +=' on';
	
		$('waiting2').innerHTML = '<img src="/_img/layup/loading.gif" />'
		Element.show('waiting2'); //show waiting div
		$('waiting2').style.height = '465px';
	
	
		var myAjax = new Ajax.Request(
		window.location,
		{
			method: 'post', 
			parameters: 'ajax=1&' + url,
			onComplete: function(originalRequest){
				$(getID).innerHTML = originalRequest.responseText;
				new Effect.Fade('waiting2') ; 
				citrusCMS.openWindow();
				citrusCMS.openFlyover();
				}
		});
	
	},
	
	
	submitForm : function() {
		
		
		$('waiting').innerHTML = '<img src="/_img/layup/loading.gif" />'
		$('submitButton').type == 'button'; //if js then remove the sumit function of the button
		$('submitButton').disabled = 'disabled'; //when submited disable button
		
		waitPadd = (Element.getDimensions($('cmsForm')).height - 50) /2
		$('waiting').style.paddingTop =  waitPadd + "px"
		$('waiting').style.paddingBottom =  waitPadd + "px"
		Element.show('waiting'); //show waiting div
		
		
		var myAjax = new Ajax.Request(
		window.location,
		{
			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);
