//declarations
var formresultFX;

var myTimer;
	
var defaultoptions = {duration: 400, transition: 'expo:out', wait: false};

var windowScroll;

//functions	
function displayNotice(message)
{	

	if($defined($('notice')))
	{
		$('notice').set('html','<div class="inside">' + message + '</div>');
		
		var scrollFX = new Fx.Scroll(window,{offset: {'x':0,'y':-10}}).toElement('notice').chain(function()
		{
		
			formresultFX.slideIn().chain(function()
			{
				if($defined(myTimer))
				{	
					myTimer = $clear(myTimer);
				}
				
				myTimer = this.slideOut.delay(30000,this);
				
			});
		});
	}
	
}

//init
window.addEvent('domready',function()
{
	windowScroll = new Fx.Scroll(window,{duration: 'long', transition: 'expo:in:out', wait: false, offset: {'x':0,'y':-20}}); 
	
	$$('.returnformselect').addEvents(
	{
		'click' : function(e)
		{
			e = new Event(e).stop();
			
			$$('.formselect').each(function(f,i)
			{
				f.slide('in');
			});
			
			$$('.formpanel').each(function(f,i)
			{
				f.slide('out');
			});
		}
	});
	
	$$('.formselect').each(function(element,index)
	{
		element.set('slide',{duration: 'long', transition: 'expo:in:out'});	
		element.set('morph',defaultoptions);	
		
		$$('.formpanel')[index].set('slide',{duration: 'long', transition: 'expo:in:out'});	
		$$('.formpanel')[index].slide('hide');
		
		element.addEvents(
		{
			'mouseenter' : function()
			{
				this.morph(
				{
					'background-color' : '#06c'
				});
				
				this.setStyle('cursor','pointer');
				
			},
			
			'mouseleave' : function()
			{
				this.morph(
				{
					'background-color' : '#900'
				});
			},
			
			'click' : function()
			{
				$$('.formselect').each(function(f,i)
				{
					if(index != i)
					{
						f.slide('out');
					}
				});
				
				$$('.formpanel')[index].slide('in');
				
				windowScroll.toElement('registration');
			}
		});
	});
	
	$('menu').getElements('a').each(function(element,index)
	{
		var fx = new Fx.Morph(element, {duration: 100, wait: false});
		
		element.getParent().set('morph',defaultoptions);
		$$('.link')[index].set('morph',{duration:500});
		element.addEvents(
		{
			'mouseenter': function()
			{
				fx.start({'background-color': '#000','color': '#09f', 'padding-bottom': 20, 'margin-top': -10});
				
				element.getParent().morph({'border-color' : '#000'});
			},
		
			'mouseleave': function()
			{
				fx.start({'background-color': '#900','color': '#ffffff', 'padding-bottom': 10, 'margin-top': 0});
				
				element.getParent().morph({'border-color' : '#900'});
			},
			
			'click': function(e)
			{
				e = new Event(e).stop();
				windowScroll.toElement($$('.link')[index]).chain(function()
				{
					$$('.link')[index].morph({'background-color' : ['#0099ff','#ff9900']});
				});
			}
			
		});
	
	});
	
	if($defined($('notice')))
	{
		formresultFX = new Fx.Slide('notice',defaultoptions).hide();
		
		$('notice').addEvents(
		{
													
			'click' : function()
			{
				formresultFX.slideOut();
			},
		
			'mouseenter' : function()
			{
				this.setStyle('cursor','pointer');
			}
			
		});
	}
	
	$$('a.external').each(function(element,index)
	{
		element.setProperty('target', '_blank');
	});

	$$('.checkform').each(function(checkform,index)
	{
		checkform.addEvent('submit',function(e)
		{
			var errorresult = new Array();
			
			//collect errors from required fields
			var inputStuff = this.getElements('input[type=text]').extend(this.getElements('input[type=password]')).extend(this.getElements('textarea')).extend(this.getElements('select')).extend(this.getElements('input[type=file]'));
			
			inputStuff.each(function(element,index)
			{
				if($defined(element.getProperty('title')) && !element.disabled)
				{
					if(element.value.clean() == '')
					{
						errorresult.include(element.getProperty('title'));	
						element.morph({'background-color' : '#f66', 'color' : '#111' });
					}
					else
					{
						element.morph({'background-color' : '#fff', 'color' : '#000' });
					}
					
				}	
				
				if(element.hasClass('numericData'))
				{
					element.value = str_replace(',','',element.value);
					if(element.value.clean() == '' || isNaN(element.value))
					{
						if(element.getProperty('title') != undefined)
						{
							errorresult.include(element.getProperty('title'));	
						}
						else
						{
							errorresult.include(element.getProperty('alt'));	
						}
						
						element.morph({'background-color' : '#f66', 'color' : '#111'});
						
					}
					else
					{
						element.morph({'background-color' : '#fff', 'color' : '#000' });
					}
				}
				
			});
			
			//if error is found
			if(errorresult.length > 0)
			{
				var re = new RegExp(',','g');
				
				displayNotice('Some of the values you have placed are either missing or invalid. These fields are marked red:<br /><strong>' + errorresult.toString().replace(re,', ') + '</strong>');
				
				e = new Event(e).stop();
			}			
		
			
		});
					
	});
	
	$$('input[type="text"]','input[type="password"]','input[type="file"]','textarea').each(function(element,index)
	{
		if(!(element.readOnly) && !(element.disabled))
		{
			var fx = new Fx.Morph(element,{duration: 1000, transition: Fx.Transitions.Expo.easeOut, wait:false});
			
			element.addEvents({
			
				'blur': function()
				{
					fx.start({'background-color': '#fff','color': '#000'});
				},
			
				'focus': function()
				{
					fx.start({'background-color': '#0099ff','color': '#ffffff'});
				}
				
			});
		}
		
	});
	
	$$('.gototop').each(function(element,index)
	{
		element.addEvents({
		
			'click': function(event)
			{
				event = new Event(event).stop();
				windowScroll.toTop();
			}
		
		});
		
	});
	
	$$('input[type=submit],input[type=reset]').each(function(element,index)
	{
		var fx = new Fx.Morph(element, {duration: 200, transition: Fx.Transitions.Cubic.easeOut, wait:false});
		
		element.addEvents({
		
			'mouseleave': function()
			{
				fx.start({'background-color': '#006699', 'color' : '#f0f0f0'});
			},
		
			'mouseenter': function()
			{
				fx.start({'background-color': '#fff', 'color' : '#333'});
			},
			
			'focus': function()
			{
				fx.start({'background-color': '#fff', 'color' : '#333'});
			},
			
			'blur': function()
			{
				fx.start({'background-color': '#006699', 'color' : '#f0f0f0'});
			}
			
		});
		
	});
	
	
	$$('.tip').each(function(el)
	{
		var full_title;
		
		if($defined(el.getProperty('title')))
		{
			full_title = el.getProperty('title');
		}
		else
		{
			full_title = '';
		}
		
		var dual = full_title.split('::');
		if(dual.length > 1)
		{
			var tip_title = dual[0].trim();
			var tip_text	= dual[1].trim();
			el.store('tip:title', tip_title);
			el.store('tip:text', tip_text);
		}
		else
		{
			el.store('tip:title', '');
			el.store('tip:text',dual[0].trim());
		}
	});
	
	var myTips = new Tips('.tip',
		{
			'className' : 'tool',
			onShow: function(tip)
			{
				tip.set('tween', {duration: 200}).fade('in')
			},
			onHide: function(tip)
			{
				tip.set('tween', {duration: 200}).fade('out')
			}
		}
	);
	
	
});

