// 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.clearOrder();
		citrusCMS.openClose();
		citrusCMS.formStyle();
		citrusCMS.boxLinks();
		citrusCMS.boxAnim();
		citrusCMS.loadScroller(); 
		citrusCMS.openPopup();
		citrusCMS.openPopup2();
		//if ($('redbar')){$(new Effect.Move('redbarbutton', { x: 0, y: 0, mode: 'absolute' }))};
		if ($('redbar')){
			new Effect.Move($('redbarbutton'), {x: 135, y: 0, duration: 0, mode: 'absolute' , afterFinish: function(){ 
				new Effect.Move('redbarbutton', {x: 0, y: 0, mode: 'absolute' })
				}
			});  
			}
		if ($('middle')){citrusCMS.flashRewrite('middle>h1','/_img/layup/title.swf', '', '1')}
		if ($('middle')){citrusCMS.flashRewrite('middle>h5','/_img/layup/title.swf', '', '1')}
		if ($('GiftRestaurant')){new Autocompleter.SelectBox('GiftRestaurant');}
	},
	
		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};
		});
	},
	
	
	openPopup : function(){
		
		allNodes = $A(document.getElementsByClassName("openPopup")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {
			var left = Math.floor((screen.availWidth - 960) / 2);
			var top = Math.floor((screen.availHeight - 440) / 2);
				window.open(this.href, 'newwindow', 'scrollbars=no, status=yes, left='+left+', top='+top+', width=960, height=438'); return false;
		}
	});
	},
	
	openPopup2 : function(){
		
		allNodes = $A(document.getElementsByClassName("openPopup2")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {
			var left = Math.floor((screen.availWidth - 880) / 2);
			var top = Math.floor((screen.availHeight - 660) / 2);
				window.open(this.href, 'newwindow', 'scrollbars=no, status=yes, left='+left+', top='+top+', width=880, height=660'); return false;
		}
	});
	},
	
	printWindow : function(){
		
		allNodes = $A(document.getElementsByClassName("printWindow")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {window.print()};
		});

	},
	
	boxLinks : function(){
		
		allNodes = $A(document.getElementsByClassName("adbox")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {
			window.location = this.getElementsByTagName("a")[0].href };
		});
		
	},
	
	clearOrder : function() {
	
		allNodes = $A(document.getElementsByClassName("basketbutton")); 
		allNodes.each(function(node, i) {		
		allNodes[i].onclick = function() {return confirm('Are you sure you want to remove the voucher from the basket?')}; 
		
		});
		
	},	
	
	openClose : function() {
		
		if ($("openclose")) {
			$("openclose").childNodes[0].onclick = function() {
			
				if(this.className=='on'){
					
					new Effect.Morph('middle', {  style: 'width: 405px;', afterFinish: function(){ Effect.Appear('holder'); } });
					if ($('redbar')){new Effect.Move('redbarbutton', { x: 0, y: 0, mode: 'absolute' });}
					
					this.className = ''
					this.innerHTML = 'Close'
				}else{
					
					new Effect.Fade('holder', {  afterFinish: function(){ 
						$('middle').morph('width: 76px;'); 
						if ($('redbar')){new Effect.Move('redbarbutton', { x: 135, y: 0, mode: 'absolute' });}
					} 
					});
					
					
					this.className = 'on'
					this.innerHTML = 'Open'
					
				}
			
			}
		}
		
	},
	
	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";
					}
				);
		});
	},
	
	
boxAnim : function(){
		
		if ($('selector')) {
		
		height = Element.getDimensions($('leftside')).height;
		childNodes = Element.descendants($('selector'));
		childNodes.each(function(node,i){
		
			childNodes[i].onclick = function() {
				
				switch (childNodes[i].getAttribute('rel')){
					case 'restaurants':
						$('tab2').className = '';
						$('clubs').style.display = 'none';
						$(childNodes[i].id).className = 'on';
						$(childNodes[i].getAttribute('rel')).style.display = 'block';
					break;
					
					case 'clubs':
						$('tab1').className = '';
						$('restaurants').style.display = 'none';
						$(childNodes[i].id).className = 'on';
						$(childNodes[i].getAttribute('rel')).style.display = 'block';
					break;
					
					default:
					
					if (this.id=='showinfo'){
						currentstate = $('selector').className;
							if (currentstate==''){ 
								$('selector').className ='open'; 
								$('box').morph('margin-top: -'+(height-120)+'px; height: '+(height-77)+'px;');
								$('restaurants').className = 'on';
								$('box').className = 'closebox';
							} else {
								$('box').morph('margin-top: -'+(height-height)+'px; height: 43px;');
								$('selector').className ='';
								$('box').className = '';	
					}
					
				}
				}
				
			}
		});
	}
		
	},
	
	loadScroller : function() {
	
			function handle(delta) {
				slider.setValueBy(-delta);
			}

			function wheel(event){
				var delta = 0;
				if (!event) /* For IE. */
					event = window.event;
				if (event.wheelDelta) { /* IE/Opera. */
					delta = event.wheelDelta/7000;
					if (window.opera)
						delta = -delta;
				} else if (event.detail) { /** Mozilla case. */
					delta = -event.detail/150;
				}
	
				if (delta)
					handle(delta);
		

				if (event.preventDefault)
					event.preventDefault();
				
				event.returnValue = false;
			}
			
			function scrollVertical(value, element, slider) {
				element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
			}
			
			if ($('maintext')) {
				if ($('maintext').scrollHeight <= $('maintext').offsetHeight) {
				}else {
					$('track').style.display='block';
				}
				
				var slider = new Control.Slider('handle', 'track', {
					axis: 'vertical',
					onSlide: function(v) { scrollVertical(v, $('maintext'), slider);  },
					onChange: function(v) { scrollVertical(v, $('maintext'), slider); }
				});
				
				
				Event.observe('maintext', 'DOMMouseScroll', wheel); // mozilla
				Event.observe('maintext', 'mousewheel', wheel); // IE
			}
		},
	
	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++){
								
					width = Element.getDimensions($(x[i])).width;
					height = Element.getDimensions($(x[i])).height;
					color = $(x[i]).getStyle('color')
					title = x[i].innerHTML;
	
					
					if (title) {title = title.replace("%", "%25"); title = title.replace("&amp;", "%26"); 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;}
				
				citrusCMS.getFlash($(x), movie, query, width, 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';
	
	}
	
	
	
}


var citrusWeb = {
	
	setTab: function(theButton, theTab){
		
		$("restaurants").style.display = 'none';
		$("clubs").style.display = 'none';
		$("tab1").className = '';
		$("tab2").className = '';
	
		$(theTab).style.display = 'block';
		$(theButton).className = 'on';
	
	},
	
	showInfo : function() {
		
		allNodes = $A(document.getElementsByClassName("info"));
		allNodes.each(function(node, i) {
			
			
			allNodes[i].onmouseover = function() {
				iID = this.href.split('#_');$(iID[1]).style.display = 'block';
			};
				
		    allNodes[i].onmouseout = function() {
				iID = this.href.split('#_');$(iID[1]).style.display = 'none';
				};

		});
	},
	

	shippingAddress : function() {
			if ($('same_address')) {				
			
				$('same_address').onclick = function() {
					
					if ($('same_address').checked == true) {		
						$('ship_name').value = $('bill_name').value;
						$('ship_addr_1').value = $('bill_addr_1').value;
						$('ship_addr_2').value = $('bill_addr_2').value;
						$('ship_city').value = $('bill_city').value;					
						$('ship_state').value = $('bill_state').value;						
						$('ship_post_code').value = $('bill_post_code').value;
						$('ship_country').value = $('bill_country').value;					
					}
					if ($('same_address').checked == false) {	
						$('ship_name').value = '-Enter Name-';
						$('ship_addr_1').value = '-Enter Address 1-';
						$('ship_addr_2').value = '-Enter Address 2-';
						$('ship_city').value = '-Enter Town/City-';					
						$('ship_state').value = '-Enter County-';						
						$('ship_post_code').value = '-Enter Postcode-';
						$('ship_country').value = 'UK';					
					}									
				};				
			}
	},
	
	
	textAreaMax : function () {
		
		x = 400;
		$('Dedication').onkeypress = function() {
		$('textcount').innerHTML = x - $('Dedication').value.length;
		if ($('Dedication').value.length > x) {
			$('Dedication').value = $('Dedication').value.substring(0, x);
			alert('Sorry but you have entered more than the maximum amount of characters for this field')}
		};
		
		$('Dedication').onkeyup = function() {
		$('textcount').innerHTML = x - $('Dedication').value.length;
		if ($('Dedication').value.length > x) {
			$('Dedication').value = $('Dedication').value.substring(0, x);
			alert('Sorry but you have entered more than the maximum amount of characters for this field')}
		};
	}
	
	
	
}


var citrusAjax = {
	
	makeRequest : function(url,getID) { 
	
		var myAjax = new Ajax.Request(
		url,
		{
			method: 'get', 
			onComplete: function(originalRequest){$(getID).innerHTML = originalRequest.responseText;}
		});
	
	},
	
	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);
