// JavaScript Document
// main.js now MO0tools powered :)
function toggle(id) {
	if ($(id).style.display == 'block') 
	{
		$(id).style.display = 'none';
	}
	else {
		//$(id).style.opacity = .95;
		//$(id).style.filter = 'alpha(opacity=95)';
		$(id).style.display = 'block';
	}
}
function send_message() {
	$('s_message').style.display = 'block';
	
	var name = $('name').value;
	var email = $('email').value;
	var message = $('_message').value;
	var secCode = $('secCode').value;
	//var urli = "message.php?name=" + name + "&email=" + email + "&message=" + message + "&secCode=" + secCode + "&ajax=1";
	//rapid_ajax(url);
	//Now mootools technique :)
	var myRequest = new Request({
		url: 'message.php',
		method: 'get',
		data: {
			name: name,
			email: email,
			message: message,
			secCode: secCode,
			ajax: '1'
		},
		onSuccess: function(responseText, responseXML) {
			try {
				//$('s_message').style.display = 'none';
				if(responseText == "wrong") {
					$('secCode').value = "";
					$('secimg').src = "CaptchaSecurityImages.php?booooooooo" + Math.random();
					alert("Wrong security code. Please try again." + rapid.responseText);
				}
				else if(responseText == "success")
				{
					toggle('message');
					alert("Message Send. We shall get back to you as soon as possible. Thank You.");
				}
				else {
					toggle('message');
					alert(responseText);
					alert("Failed to send message.Please go to Contact us Page.");
				}
			}
			catch(e) {
			}		
		}
	});
	
	myRequest.send();
}

/*Ticker Technology Class */
var Ticker = new Class({
	setOptions: function(options) {
		this.options = Object.extend({
			speed: 3000,
			delay: 2000,
			direction: 'horizontal',
			onComplete: Class.empty,
			onStart: Class.empty
		}, options || {});
	},
	initialize: function(el,options){
		this.setOptions(options);
		this.el = $(el);
		ticker = this
		this.items = this.el.getElements('li');	//List of items to scroll
		
		this.items.each(function(li,index) {
				li.addEvents({
					mouseenter:function() {
						this.morph({backgroundColor:'#eeeeee'});
						ticker.fx.pause()
					},
					mouseleave: function() { 
						this.morph({backgroundColor:'#ffffff'});
						ticker.fx.resume()
					}
				})
			});
		
		var w = 0;
		var h = 0;
		
		if(this.options.direction.toLowerCase()=='horizontal') {
			h = this.el.getSize().y;
			this.items.each(function(li,index) {
				w += li.getSize().x + 10;
			});
		} 
		else {
			w = this.el.getSize().x;
			this.items.each(function(li,index) {
				h += li.getSize().y;
			});
		}
		
		this.el.setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			width: w,
			height: h
		});
		
		this.fx = new Fx.Morph(this.el , {duration:this.options.speed , transition: 'linear' , onComplete:function() {
			var i = (this.current==0) ? this.items.length : this.current;
			//var i = (this.current >= this.items.length)? 0 : this.current;
			this.items[i-1].injectInside(this.el);
			this.el.setStyles({
				left:0,
				top:0
			});
			this.next.bind(this).delay(this.options.delay);
		}.bind(this)});
		this.current = 0;
		this.next();
	},
	//This function scrolls to the next element
	next: function() {
		old = this.current
		this.current++
		if (this.current >= this.items.length) this.current = 0;
		var pos = this.items[old];
		
		this.fx.start({
			/*top: -pos.offsetTop,*/
			/*left: -pos.offsetLeft*/
			left: -pos.getSize().x
		});
		//this.next.bind(this).delay(this.options.delay + this.options.speed);
	}
});


function init() {
	window.defaultStatus='Omikrosys';
	//Time for mootools effects	
	//Sections on homepage
	if($('sections'))
		$$('#sections li').each(function(section) {
			section.addEvents({
				mouseenter: function() { 
					//this.get('tween').start('background-color', '#f6f6f6', '#ffeeee'); 
					this.morph({backgroundColor: '#ffeeee', borderColor:'#d52d00'});
				},
				mouseleave: function() { 
					this.morph({backgroundColor: '#f6f6f6', borderColor:'#ffffff'});
				}
			})
		});
	
	//Set up handler for the message button
	//Message button on homepage
	if($('a_message'))
		$('a_message').addEvent('click' , function() {
			toggle('message');
			$('secimg').src = "spinner.gif";
			//$('secimg').src = "securityCode.php?booooooooo" + Math.random();
			$('secimg').src = "CaptchaSecurityImages.php?booooooooo" + Math.random();
	});
	
	//Object oriented menu widget :)
	//Set handler for 1st level lis
	
		$('menu').getElement('ul.navigation-1').getChildren('li').each(function(li) {
			//Handler to dispalay uls
			li.addEvents({
				 mouseenter: function() {
					this.setStyle('background' , '#700');
					var ul = this.getElement('ul')
					if(ul)
						ul.setStyle('display' , 'block');					
				},
				mouseleave: function() {
					this.setStyle('background' , '#D52D00'); 
					var ul = this.getElement('ul')
					if(ul)
						ul.setStyle('display' , 'none');
				}
			});
			
			//Set handlers for 2nd level lis
			ul = li.getElement('ul')
			if(ul)
				ul.getChildren('li').each(function(li) { 
					li.addEvents({
						mouseenter: function() {
							this.setStyle('background' , '#a00');
							ul = this.getElement('ul')
							if(ul)
								ul.setStyle('display' , 'block');
						},
						mouseleave: function() {
							this.setStyle('background' , '#D52D00'); 
							var ul = this.getElement('ul')
							if(ul)
								ul.setStyle('display' , 'none');
						}
					});
				});
		});
	
	$$('input[type=text]').each(function(input) {
		input.addEvents({
			focus:function() {
				//this.style.background='#FFFBCF'
			},
			blur:function() {
				//this.style.background='#FFFFFF'
			}
		})
	})
	
	$$('textarea').each(function(input) {
		input.addEvents({
			focus:function() {
				this.style.background='#FFFBCF'
			},
			blur:function() {
				this.style.background='#FFFFFF'
			}
		})
	})
	
	$('request_quote').addEvents({
		'mouseenter' : function() {
			$('quote_info').get('tween').start('opacity', '0', '1'); 
		},
		'mouseleave' : function() {
			$('quote_info').get('tween').start('opacity', '1', '0'); 
		}
	})
	
	//Apple like sidebar widget :)
	//new Accordion($$('div#sidebar h1'), $$('div#sidebar ul') , {});
	/*
	if($('portfolio'))
	{
		new Ticker('portfolioTicker' ,{speed:1000,delay:1000,direction:'horizontal'});
		$('portfolio').addEvents({
			mouseenter: function() { 
				//this.get('tween').start('background-color', '#f6f6f6', '#ffeeee'); 
				this.morph({borderColor:'#d52d00'});
			},
			mouseleave: function() { 
				this.morph({borderColor:'#cccccc'});
			}
		})
	}
	*/	
	
	
	
}
//Call the init method on body load
window.addEvent('domready', init);
