window.addEvent('domready', function() {

	$$('.phpForm').each(function(form){
		var turing = new Request({
			url: '/includes/token.php',
			method: 'get',
			onSuccess: function(responseText) {
				var myFirstElement  = new Element('input', {id: 'ts',type: 'hidden', name: 'ts', value: responseText});
				myFirstElement.inject(form);
			}
		});
		turing.send();
	});

	if($('submit')) {
		$('submit').addEvent('click',function(e){
			e.stop();
			$('contactForm').set('send', {
				method: 'post',
				onRequest: function(){
					$('contactForm').getChildren()[0].fade('.5');
				},
				onSuccess: function(response){
					$('contactError').setStyles({
						'visibility':'visible',
						'opacity':0
					});
					$('contactError').set('html', (response));
					$('contactError').fade('1');
					$('contactForm').getFirst().fade('1');
				
					if($('contactError').getChildren('.error').length = 0){
						(function(){
						    $('contactError').fade('0');
						}).delay(3000).chain(function(){
							$('contactError').setStyle('visibility','hidden');
						});
					}
				},
				onFailure: function(response){
				}
			});
			$('contactForm').send();
		});
	};

	if ($('locationSelect')){
		$('locationSelect').addEvents({
			'mouseover': function(){
				this.setStyle('height','auto');
			},
			'mouseout': function(){
				this.setStyle('height','27px');
			}
		});
		$('locationSelect').getChildren('a').each(function(links){
			links.addEvent('click',function(e){
				e.stop();
				$('menu').getChildren('div').each(function(menus){
					menus.setStyle('display','none');
				});
				$(this.get('href')).setStyle('display','block');
				this.inject($('locationSelect'), 'top');
				$('locationSelect').setStyle('height','27px');
				if (this.get('href') == 'menuChicago')
					$('pdfMenuLink').set('href', '/uploads/Gullivers_Menu_Chicago.pdf');
				if (this.get('href') == 'menuGlenview')
					$('pdfMenuLink').set('href', '/uploads/Gullivers_Menu_Glenview.pdf');
				if (this.get('href') == 'menuOakbrook')
					$('pdfMenuLink').set('href', '/uploads/Gullivers_Menu_Oakbrook.pdf');
			});
		});
	}
	
	if ($$('ul.menuNav').length > 0) {
		var menuSectionLinks = $$('ul.menuNav a');
		var menuSection;
		menuSectionLinks.each(function(menuSectionLink){
			menuSectionLink.addEvent('click',function(e){
				e.stop();
				this.getParent().getParent().getParent().getElement('div.content').getChildren().each(function(a){
					a.setStyle('display','none');
				})
				$(this.name).setStyle('display','block');
			})
		})
	}

	if ($$('a.modal').length > 0) {
		var links = $$('a.modal');
		var i = 0;
		var shadow = new Element('div',{
			'id':'shadow',
			'styles': {
			}
		});
		var box = new Element('div',{'id':'modal'});
		var boxTop = new Element('img',{'src':'/images/modalTop.png','styles':{
			'position':'absolute',
			'top':'-10px',
			'left':'-5px'
		}});
		var boxBottom = new Element('img',{'src':'/images/modalBottom.png','styles':{
			'position':'absolute',
			'bottom':'-25px',
			'left':'-5px'
		}});
		var button = new Element('a',{'id':'btnClose'});
		
		shadow.inject($(document.body),'bottom');
		box.inject(shadow,'bottom');
		boxTop.inject(box);
		boxBottom.inject(box);
		button.inject(box,'top');
		
		var shadowFx = new Fx.Tween(shadow);
		
		shadowFx.set('opacity','0');

		var scrollX;
		var scrollY;
		var boxX;
		
		links.each(function(modal){
			modal.addEvent('click',function(e){
				e.stop()
				// alert(this.getNext('div.modalContent').innerHTML);
				shadow.setStyle('visibility','visible');
				scrollX = $('header').getScrollWidth();	
				scrollY = $$(document.body)[0].getScrollHeight();
				shadow.setStyles({
					'width': $$(document.body)[0].getScrollWidth(),
					'height': scrollY
				});

				var content = this.getNext('div.modalContent').clone();
				content.inject(box,'bottom');
				boxY = (window.getScrollTop() + (window.getHeight()/2) - (content.getHeight()/2) - 30);

				if ((boxY < 30) || (content.getHeight() > window.getHeight())){
					boxY = window.getScrollTop() + 30;
				}

				boxX = (window.getScrollLeft() + (window.getWidth()/2) - (content.getWidth()/2));

				if ((boxX < 30) || (content.getWidth() > window.getWidth())){
					boxX = window.getScrollLeft() + 30;
				}

				box.setStyles({
					'left':boxX,
					'top':boxY
				});
				shadow.fade('in');
				
			});
		})
		button.addEvent('click',function(e){
			e.stop();
			shadowFx.start('opacity',1,0).chain(function(){
				shadow.setStyle('visibility','hidden');
				box.getLast().destroy();
			});
		});
	};

	// if (($$('.services') != 0) && ($$('.homepage') < 1)) {
	// 
	// 	$$('.box .content')[1].setStyle('display','none');
	// 
	// 	$$('.tabs a').each(function(tab,i){
	// 		tab.addEvent('click', function(e){
	// 			e.stop();
	// 			if (tab.get('class') != 'active') {
	// 				$$('.box .content').each(function(box){box.setStyle('display','none');});
	// 				$$('.tabs a').each(function(tabsClear){tabsClear.erase('class');});
	// 				$$('.box .content')[i].setStyle('display','block');
	// 				tab.addClass('active');
	// 			};
	// 		})
	// 	});
	// };
	// 
	// $$('.fauxSelect a').each(function(selOption,i){
	// 	selOption.addEvent('click', function(e){
	// 		e.stop();
	// 		$('item_selection_1').set('value', (i + 1));
	// 		$$('.fauxSelect a').each(function(selClear){
	// 			selClear.setStyle('background-position','top');
	// 		});
	// 		selOption.setStyle('background-position','bottom');
	// 	});
	// });
	// 
	// if ($$('fieldset') != 0){
	// 	$$('fieldset input[type=text]').each(function(inputs,i){
	// 		inputs.addEvent('focus', function(e){
	// 			this.setStyle('background','#788e8c');
	// 			this.getPrevious().setStyle('visibility','hidden');
	// 		});
	// 		inputs.addEvent('blur', function(e){
	// 			if(this.value == ''){
	// 				this.getPrevious().setStyle('visibility','visible');
	// 			};
	// 		});
	// 	});
	// 
	// 	$$('fieldset textarea').each(function(inputs,i){
	// 		inputs.addEvent('focus', function(e){
	// 			this.getPrevious().setStyle('visibility','hidden');
	// 		});
	// 		inputs.addEvent('blur', function(e){
	// 			if(this.value == ''){
	// 				this.getPrevious().setStyle('visibility','visible');
	// 			};
	// 		});
	// 	});
	// 
	// 	$$('fieldset select').each(function(select,i){
	// 		select.getPrevious().setStyle('visibility','visible');
	// 		select.setStyles({
	// 			'visibility':'hidden',
	// 			'position':'absolute',
	// 			'height':'0', 'width':'0',
	// 			'z-index':'-999'
	// 		});
	// 		
	// 		var selectBox = new Element('div',{'id':'select'+i, 'class':'selectBox'});
	// 		var selectInput = new Element('input',{'value':select.getPrevious().innerHTML, 'class':'selectInput '+select.get('id'), 'styles':{'color':'#ccc'}});
	// 		selectBox.inject(select,'after');
	// 		selectInput.inject(selectBox,'before');
	// 		select.getPrevious().setStyle('visibility','hidden');
	// 		selectInput.set('value',select.getPrevious().innerHTML);
	// 
	// 		select.getElements('option').each(function(option,i){
	// 			if (i>0){
	// 				var optionInput = new Element('a',{
	// 					'href': option.get('value')
	// 				});
	// 
	// 				optionInput.inject(selectBox,'bottom');
	// 				optionInput.set('html',option.get('value'));
	// 
	// 				optionInput.addEvent('click',function(e){
	// 					e.stop();
	// 					option.selected = 'true';
	// 					selectInput.set('value', this.innerHTML);
	// 					selectBox.setStyle('height','0');
	// 				});
	// 			}
	// 		});
	// 		
	// 		selectInput.addEvent('focus',function(e){
	// 			this.set('value','');
	// 			this.setStyle('color','#fff');
	// 			selectBox.setStyle('height','auto')
	// 		})
	// 	});
	// 
	// };
	// 
	// 
	// 
	// $('contactSubmit').addEvent('click',function(e){
	// 	e.stop();
	// 	$('contactForm').set('send', {
	// 		method: 'post',
	// 		onRequest: function(){
	// 
	// 			$('contactForm').getChildren()[0].fade('.5');
	// 		},
	// 		onSuccess: function(response){
	// 			$('contactError').setStyles({
	// 				'visibility':'visible',
	// 				'opacity':0
	// 			})
	// 			$('contactError').getChildren()[0].set('html', (response));
	// 			$('contactError').fade('1');
	// 			$('contactForm').getFirst().fade('1');
	// 			
	// 			var messageBox = $('contactError').getChildren()[0].getChildren()[0];
	// 			if(messageBox.innerHTML == 'Errors:'){
	// 				
	// 				if ($('errorName')) {
	// 					$('contactName').setStyle('background','#956c77');
	// 				};
	// 
	// 				if ($('errorContact')) {
	// 					$('contactEmail').setStyle('background','#956c77');
	// 					$('contactPhone').setStyle('background','#956c77');
	// 				};
	// 
	// 				if ($('errorLocation')) {
	// 					$$('input.contactLocation')[0].setStyle('background','#956c77');
	// 				};
	// 
	// 				if ($('errorEmail')) {
	// 					$('contactEmail').setStyle('background','#956c77');
	// 				};
	// 
	// 				if ($('errorPhone')) {
	// 					$('contactPhone').setStyle('background','#956c77');
	// 				};
	// 
	// 			} else {
	// 				$('contactForm').getElements('label').each(function(label){
	// 					label.setStyles({
	// 						'visibility':'visible',
	// 						'opacity':'0'
	// 					});
	// 					label.fade('1');
	// 				});
	// 				
	// 				$('contactForm').getElements('input').each(function(input){
	// 					input.value = '';
	// 				});
	// 				
	// 				$('contactForm').getElements('textarea').each(function(input){
	// 					input.value = '';
	// 				});
	// 
	// 				(function(){
	// 				    $('contactError').fade('0');
	// 				}).delay(3000).chain(function(){
	// 					$('contactError').setStyle('visibility','hidden');
	// 				});
	// 			}
	// 		},
	// 		onFailure: function(response){
	// 		}
	// 	});
	// 	$('contactForm').send();
	// 
	// });


});


