/*========================================================
 @site		Keepthinking
 @company	Keepthinking LTD
 @address	http://keepthinking.it
 @author	Oskar Krawczyk (o.krawczyk@keepthinking.it)
 @rev		01/05/2007
 ========================================================*/

var Keepthinking = {

	options: {
		body: 'wedgwood',
		zoomToggler: 'zoom-toggler',
		logo: function() {
			return $('logo')
		}
	},

	/* Initialize needed methods
	 ======================================================================= */
	init: function() {
		this.accordion();
		this.blankWindow();
		this.replacers();
		this.niceFields();
		this.formActions();
		this.slides();
	},

	/* 
	 ======================================================================= */
	accordion: function() {
		var accordion;
		var accordionTogglers;
		var accordionContents;

		var accordionTogglers = $$('.title');
		var accordionContents = $$('.accordion');

		accordionTogglers.each(function(toggler) {
			toggler.origColor = toggler.getStyle('background-color');
			toggler.fx = new Fx.Style(toggler, 'background-color');
		});

		accordion = new Fx.Accordion(accordionTogglers, accordionContents,{
			onActive: function(toggler){
				toggler.getFirst().setStyle('background', 'url('+absoluteUrl+'template/img/arr-gray-down.gif) 100% 2px no-repeat');
				toggler.fx.start('#fff');
			},
			onBackground: function(toggler){
				toggler.setStyle('background-color', toggler.origColor);
				toggler.getFirst().setStyle('background', 'url('+absoluteUrl+'template/img/arr-gray.gif) 100% 2px no-repeat');
			}
		});
	},

	/* Unobtrusive new window opener
	 ======================================================================= */
	blankWindow: function() {
		$$('a[rel=window]').each(function(link) {
			link.addEvent('click', function(e) {
				var newWindow = window.open(this.getProperty('href'), '_blank').focus();
				new Event(e).stop();
			});
		});
	},
	
	/* 
	 ======================================================================= */
	slides: function() {
		$$('.image').each(function(image) {
			var fx = new Fx.Style(image, 'left');
			var initial = image.getStyle('left');
			
			image.addEvents({
				mouseover: function(e) {
					fx.start(0);
				},
				mouseout: function(e) {				
					(function() {
						fx.start(initial);
					}).delay(1000)
				}	
			});
		});
	},
	
	/* 
	 ======================================================================= */
	replacers: function() {
		var replaceMail = $('c-mail');
		replaceMail.innerHTML = '<a title="Contact" href="mailto:info@keepthinking.it">info@keepthinking.it</a>';

		var replaceSubmit = $('thanks');
		replaceSubmit.innerHTML = '<input id="submit-inp" type="submit" value="Submit" onclick="sendForm(); return false" />';
		
		var addAjaxSubmit = $('theform');
		addAjaxSubmit.setProperty('action', 'form.php');
	},
		
	/* 
	 ======================================================================= */
	niceFields: function() {
		$('name-spam').addEvents({
			focus: function(e) {
				if(this.value == 'Your/company name') {
					this.value = '';
				}
			},
			blur: function(e) {
				if(this.value == '') {
					this.value = 'Your/company name';
				}
			}
		});
		
		$('mail-spam').addEvents({
			focus: function(e) {
				if(this.value == 'Your e-mail/telephone') {
					this.value = '';
				}
			},
			blur: function(e) {
				if(this.value == '') {
					this.value = 'Your e-mail/telephone';
				}
			}
		});
		
		$('web-spam').addEvents({
			focus: function(e) {
				if(this.value == 'Current website (optional)') {
					this.value = '';
				}
			},
			blur: function(e) {
				if(this.value == '') {
					this.value = 'Current website (optional)';
				}
			}
		});
		
		$('text-spam').addEvents({
			focus: function(e) {
				if(this.value == 'Your inquiry') {
					this.value = '';
				}
			},
			blur: function(e) {
				if(this.value == '') {
					this.value = 'Your inquiry';
				}
			}
		});
	},
	
	/* 
	 ======================================================================= */
	formActions: function() {
		var button1 = $('potential');
		var content1 = $('contact-form');

		var b1Toggle = new Fx.Style('contact-form', 'height', {
			duration: 500
		});

		button1.addEvent('click', function(e) {
			if(content1.getStyle('height').toInt() > 0) {
				b1Toggle.start(0);
			} else {
				b1Toggle.start(c1Height+70);
			}
			
			new Event(e).stop();
		});
		
		var button2 = $('get-in-touch');
		button2.addEvent('click', function(e) {
			if(content1.getStyle('height').toInt() > 0) {
				b1Toggle.start(0);
			} else {
				b1Toggle.start(c1Height+70);
			}
			
			new Event(e).stop();
		});
		

		content1.setStyles({
		   overflow: 'hidden',
		   height: '0'
		});

		var c1Height = content1.getSize().scrollSize.y;
	}
}

window.addEvent('domready', function() {
	Keepthinking.init();
});


sendForm = function() {   
	new Ajax('form.php', {
		postBody: $('theform'), 
		onActive: $('thanks').innerHTML = '<p class="loading"><img src="'+absoluteUrl+'template/img/ajax-loader.gif" alt="Loader." /> Sending message ...</p>',
		update: 'thanks',
		/*onComplete: (function(){
			var b1Toggle = new Fx.Style('contact-form', 'height',{duration: 500});
			b1Toggle.start(0);
		}).delay(10000),*/
		onFailure: (function(){
			$('thanks').innerHTML = '<p class="oops">Sorry, problem occured while sending your e-mail. Please try again.</p>'+'<input id="submit-inp" type="submit" value="Submit" />'
		}),
		evalScripts: true
	}).request();
}
