var slideshow,articles=[];
window.addEvent('domready',initialize);

/*	Initialization function  */

	function initialize(){
		
		/*	External links */
		
			var C=$$('a[rel*=external]');C.each(function(A,B){A.addEvent('click',function(){window.open(this.href);return false;});});
			
		/*	Do browser specific adjustments */

			doBrowserChecks();
			
		/*	Do JavaScript powered element dimension adjustements */
		
			doSizeAdjustments.periodical(200);
			
		/*	If news accordion */
		
			if($('newsAccordion')) initializeNewsAccordion();
			
		/*	If careers accordion */
		
			if($('careersAccordion')) initializeCareersAccordion();
			
		/*	Initialize page */
		
			initializeDropDowns();
			doOpacityChanges();	
			
		/*	If slideshow */
			
			if($('bgSlideshow')){
				
				slideshow=new Slideshow('bgSlideshow',slideshowData,{'hu':slideshowRoot,'width':960,'height':673,'delay':6000});
				$$('#slideshowControls a').each(function(M,N){M.addEvent('click',function(){slideshow.go(this.get('text')-1);});});
			
			}
			
		/*	Initialize SlideshowPro */
		
			if($('slideshowPro')) initializeSlideshowPro();
			
		/*	Enable form tests */
		
			if($('submitBtn')) initializeForm();
			
		/*	Run through scrollable regions */
		
			/*initializeScrollableRegions();*/
			
		
	}
	
/*	Browser checks */

	function doBrowserChecks(){
		
		if(Browser.Engine.trident){
			
			$('watamisg').setStyle('margin-left',-100);	
			$('abtwatami').setStyle('margin-left',-80);
			
		}
		else{
			
			$('watamisg').setStyle('margin-left',-10);	
			$('abtwatami').setStyle('margin-left',-10);
			
		}
		
	}

/*	Size adjustments */

	function doSizeAdjustments(){
		
		if($('bodyBg')&&$('body')){
			
			var B=$('body').getCoordinates();
			if($('bodyBg')) $('bodyBg').setStyles({'height':B.height+100,'opacity':0.7});
			if($('con')) $('con').setStyles({'height':B.height+470});
			if($('homeCopyright')) $('homeCopyright').setStyles({'top':B.height+460});
			if($('innerFooterLine')) $('innerFooterLine').setStyles({'top':B.height+370});
			if($('innerFooter')) $('innerFooter').setStyles({'top':B.height+410});
			
		}
		else{
			
			if($('homeCopyright')) $('homeCopyright').setStyles({'top':660,'width':950});
			if($('innerFooterLine')) $('innerFooterLine').destroy();
			if($('innerFooter')) $('innerFooter').destroy();
			
		}
		
	}
	
/*	Initialize drop down menus */

	function initializeDropDowns(){
		
		$('mm').getElements('ul').each(function(M,N){
		
			M.setStyles({'opacity':0});
			M.getParent().addEvent('mouseover',function(){
				M.setStyle('opacity',1);
			});
			M.getParent().addEvent('mouseout',function(){
				M.setStyle('opacity',0);
			});
		
		});
		
		$$('#sm .smDropDown').each(function(M,N){
		
			M.setStyle('opacity',0);
			M.getParent().addEvent('mouseover',function(){
				M.setStyle('opacity',1);
			});
			M.getParent().addEvent('mouseout',function(){
				M.setStyle('opacity',0);
			});
		
		});
		
	}
	
/*	Change opacity of elements */

	function doOpacityChanges(){
		
		/*	Homepage */
		
			if($('homePanel1')) $('homePanel1').setStyle('opacity',0.5);
			if($('homePanel2')) $('homePanel2').setStyle('opacity',0.5);
			if($('afterHomePanelContents1')) $('afterHomePanelContents1').setStyle('opacity',0.5);
			if($('afterHomePanelContents2')) $('afterHomePanelContents2').setStyle('opacity',0.5);
		
	}
	
/*	News accordion */

	var startingNewsArticle=0;
	function initializeNewsAccordion(){
		
		/*	Decide which article to show */
		
			if(location.hash){
				
				if(location.hash.substr(0,8)=='#article'){
					
					startingNewsArticle=articles[location.hash.substr(8)];
					
				}
				
			}
		
		/*	Hide images */
		
			$$('.newsImages').each(function(M,N){M.setStyle('opacity',0);});
		
		/*	Initialize accordion */
		
			var newsAccordion=new Accordion($('newsAccordion'),'h2.toggler','div.element',{
					
				'show':startingNewsArticle,
				'opacity':false,
				'onActive':function(toggler,element){
					
					$('newsImages'+toggler.id.substr(7)).setStyle('opacity',1);
					
				},
				'onBackground':function(toggler,element){
					
					$('newsImages'+toggler.id.substr(7)).setStyle('opacity',0);
					
				}
				
			});
		
	}
	
/*	Careers accordion */

	function initializeCareersAccordion(){
		
		/*	Initialize accordion */
		
			var careersAccordion=new Accordion($('careersAccordion'),'h2.toggler','div.element',{
											  
				'opacity':false
				
			});
		
	}
	
/*	MooScroll */

	function initializeScrollableRegions(){
		
		var myScroll=new MooScroll({
		
			'increment':5,
			'smoothMooScroll':{
			
				'toAnchor':true
				
			}
		
		});	
		
	}

/*	Initialize slideshow pro */

	function initializeSlideshowPro(){
	
		new Swiff('/wp-content/themes/watami/assets/slideshowpro/slideshow-'+$('slideshowPro').get('text')+'.swf',{
		
			'container':$('slideshowPro'),
			'width':636,
			'height':363
		
		});
	
	}
	
/*	Form */

	function initializeForm(){
		
		$('submitBtn').addEvent('click',validateForm);
		if($('hobbiesOthersZone')) $('hobbiesOthersZone').setStyle('display','none');
		if($('dishOthersZone')) $('dishOthersZone').setStyle('display','none');
		if($('hobbiesOthers')) $('hobbiesOthers').addEvent('click',function(){$('hobbiesOthersZone').setStyle('display',this.checked?'':'none');});
		if($('dishOthers')) $('dishOthers').addEvent('click',function(){$('dishOthersZone').setStyle('display',this.checked?'':'none');});
		
	}
	
	function validateForm(){
		
		if($('firstname').value.trim()==''||$('lastname').value.trim()==''||$('email').value.trim()==''){
			
			alert('Please fill out all the required fields!');
			return;
			
		}
		
		/*	Compile dishes and hobbies */
		
		if($('dishes')) $('dishes').value='';
		$$('#dishesZone input').each(function(M,N){if(M.type=='checkbox'){if(M.checked&&M.value!='') $('dishes').value+=M.value+', ';}});
		
		if($('hobbies')) $('hobbies').value='';
		$$('#hobbiesZone input').each(function(M,N){if(M.type=='checkbox'){if(M.checked&&M.value!='') $('hobbies').value+=M.value+', ';}});
		
		$('form').submit();	
		
	}