var myTools = {
	
	timer: 0,
	
	makeShadow: function(){
		myTools.shadow = new Element('div').setStyles({
			'margin-left': '827px',
			'width': '5px',
			'background': '#000000',
			'position': 'fixed',
			'height': Window.getHeight()+'px',
			'top' : '5px',
			'opacity': '0.1',
			'z-index': '5'
		}).injectInside($('sitecontainer'));
	},
	
	start: function(){
		if(!window.ActiveXObject) {
			window.onresize = function(){
				myTools.shadow.setStyle('height', Window.getHeight()+'px');
			}
			this.makeShadow();
		} else $('sitecontainer').setStyle('height', $('sitecontainer').offsetHeight);
		this.slideMenu();
		
		$('kulkalnewsletter').onclick = function(){
			myTools.repoTitle.setHTML(lng_emailsignup);
			myTools.showBox();
			myTools.setSubmitClick('kulkalnewsletter');
			return false;
		};
		
		$('menunewsletter').onclick = function(){
			myTools.repoTitle.setHTML(lng_emailsignup);
			myTools.showBox();
			myTools.setSubmitClick('menunewsletter');
			return false;
		};
		
		myTools.box = new Element('div').addClassName('box').injectInside(document.body).addEvent('click', myTools.hideBox);
		myTools.boxEffect = myTools.box.effect('opacity', {duration: 500, transition: Fx.quadOut, wait: false}).set(0);
		myTools.repo = new Element('div').addClassName('repo').setStyle('left', Window.getWidth()/2-157+'px').injectInside($(document.body));
		
		myTools.repo.adopt('div').addClassName('repoclose').onclick = function(){
			myTools.hideBox();
		};
		
		myTools.repoTitle = myTools.repo.adopt('h3').addClassName('repotitle');
		myTools.repoArea = myTools.repo.adopt('input').addClassName('textrepo');
		
		myTools.repoSubmit = myTools.repo.adopt('a').addClassName('reposubmit');
		
		myTools.repoEffect = myTools.repo.effect('opacity', {duration: 400, transition: Fx.quadIn, wait: false}).set(0);
	},
	
	setSubmitClick: function(type){
		if (!type) {
			myTools.repoSubmit.onclick = null;
		} else {
			myTools.repoSubmit.onclick = function(){
				if (myTools.repoArea.value && myTools.repoArea.value != lng_emailprompt){
					var myRequest = new Ajax('/ajax-save-address.php', {
						postBody: {'lang': language, 'text': myTools.repoArea.value, 'type': type}
					}).request();
					
					myTools.repoArea.value = lng_thankyou;
					myTools.hideBox.delay(400);

					myTools.setSubmitClick();
				} else {
					myTools.repoArea.value = lng_emailprompt;
				}
				return false;
			};
		}
	},
	
	slideMenu: function(){
		$S('#menu li').each(function(el){
			el.setStyle('padding-left', '0');
			this.timer += 150;
			var slideIn = el.effect('padding-left', {transition: fx.backOut});
			slideIn.myCustom = function(){
				slideIn.custom('0', '150');
			};
			slideIn.myCustom.delay(this.timer);
		}, this);
	},
	
	showBox: function(title){
		myTools.repoArea.value = '';
		myTools.followBox();
		window.addEvent('scroll', myTools.followBox);
		
		myTools.repo.setStyle('top', Window.getScrollTop()+100+'px');
		
		myTools.box.setStyles({
			'height': Window.getHeight()+'px',
			'width': '100%',
			'visibility': 'visible'
		});
		myTools.boxEffect.custom(myTools.boxEffect.now,0.8).chain(function(){
			myTools.repoEffect.custom(myTools.repoEffect.now, 1);
		});
	},
	
	hideBox: function(){
		window.removeEvent('scroll', myTools.followBox);
		
		myTools.repoEffect.custom(myTools.repoEffect.now, 0).chain(function(){
			myTools.boxEffect.custom(myTools.boxEffect.now,0).chain(function(){
				myTools.box.setStyles({
					'height': '0',
					'width': '0',
					'visibility': 'hidden'
				});
			});
		});
	},
	
	followBox: function(){
		myTools.repo.setStyle('top', Window.getScrollTop()+100+'px');
		myTools.box.setStyle('top', Window.getScrollTop()+'px');
	}
};



var contactForm = {
	
	startform: function(){
		$('contactformsubmit').onclick = function(){
			contactForm.contactFormSubmit();
			return false;
		};
	},
	
	contactFormSubmit: function() {
		
		if ($('contactname').value != '' && $('contactemail').value != '' && $('contactmessage').value != '' && $('contactphone').value != '') {
			var mySendRequest = new Ajax('/ajax-send-contactform.php', {
				postBody: 	{	'name': $('contactname').value,
							'company': $('contactcompany').value,
							'email': $('contactemail').value,
							'phone': $('contactphone').value, 
							'message': $('contactmessage').value
						}
			}).request();
			$('contactform').effect('opacity', {duration: 500, transition: Fx.quadOut, wait: true}).custom(1, 0).chain(function(){
				$('contactform').setHTML('<table width="100%" height="100%" border="0" id="contactformtable"><tr><td align=center valign=center>' + lng_thankyou + '</td></tr></table>');
				$('contactform').effect('opacity', {duration: 500, transition: Fx.quadOut, wait: true}).custom(0, 1);
			});
		} else {
			
			contactForm.showerrorBox();
		
		}
		
		return false;
	},
	
	showerrorBox: function() {
		
		$('errorbox').effect('opacity', {duration: 700, transition: Fx.quadIn, wait: false}).custom(0,0.8);
		
	},
	hideerrorBox: function() {
		$('errorbox').effect('opacity', {duration: 700, transition: Fx.quadIn, wait: false}).custom(0.8,0);
	}
};


window.onload = function(){
	try {document.execCommand("BackgroundImageCache", false, true);} catch (e){};
	contactForm.startform();
	myTools.start();
};
