var carousel=false,slideshowData=false,slideshowCaptions=false,accordion=false,worksSearch=false,worksSearchRequest=false,slideShowHeight=350,footerEffect=false,footerMorph=false;













window.addEvent('domready',function(){

	/*	Check for band */
	
	if($('band')){
	
		var slideShowDimensions=$('workSlideShow').getCoordinates();
		$('band').setStyles({'top':slideShowDimensions.top+'px','left':(slideShowDimensions.left+427)+'px','display':'block'});
	
	}

	/*	Adjust body height to minimum of 200. This script is required because IE does not support min-height */
	
	adjustBodyHeight();
	
	/*	If sidebar is present, adjust 'Back to Top' link position */
	
	adjustBackToTop();
	
	/*	If homepage, launch carousel */
	
	if($('carousel')) carousel=new Carousel({'viewWidth':720});
	if($('startupNav')){$('startupNav').addEvent('mouseover',function(){$('startupNav').set('class','green');$('smeNav').set('class','');$('enterpriseNav').set('class','last');carousel.gotoPos(0);carousel.stop();});$('startupNav').addEvent('mouseout',function(){carousel.start();});}
	if($('smeNav')){$('smeNav').addEvent('mouseover',function(){$('startupNav').set('class','');$('smeNav').set('class','red');$('enterpriseNav').set('class','last');carousel.gotoPos(1);carousel.stop();});$('smeNav').addEvent('mouseout',function(){carousel.start();});}
	if($('enterpriseNav')){$('enterpriseNav').addEvent('mouseover',function(){$('startupNav').set('class','');$('smeNav').set('class','');$('enterpriseNav').set('class','blue last');carousel.gotoPos(2);carousel.stop();});$('enterpriseNav').addEvent('mouseout',function(){carousel.start();});}
	
	/*	If is works listings, adjust mouseover effects */
	
	adjustWorksEffect();
	
	/*	Footer Mouseover Effects */
	
	adjustFooterEffect();
	
	/*	If slideshow is available, launch it */
	
	launchSlideShow();
	
	/*	If accordion is available, launch it */
	
	launchAccordion();
	
	/*	If searching is available */
	
	if($('worksSearchBtn')) $('worksSearchBtn').addEvent('click',function(){search('works');});
	if($('newsSearchBtn')) $('newsSearchBtn').addEvent('click',function(){search('news');});
	if($('askAriginSendBtn')) $('askAriginSendBtn').addEvent('click',function(){submitEnquiryForm();});
	$$('.searchBtn').each(function(M,N){M.addEvent('mouseover',function(){this.set('src',this.get('src').replace(/off/,'over'));});M.addEvent('mouseout',function(){this.set('src',this.get('src').replace(/over/,'off'));});});
	
	/*	Initialize any forms */
	
	initializeForms($('mainContents'));

});












function submitEnquiryForm(){

	/*	Housekeeping */
	
	$('askAriginError').empty();
	
	/*	Validate */

	if(!$('name').value.length||$('name').value=='This field is required.'||!$('email').value.length||$('email').value=='This field is required.'||!$('contact_number').value.length||$('contact_number').value=='This field is required.'){
	
		$('askAriginError').set('html','<br /><br />Please fill out the required fields!');
		return false;
	
	}
	
	return true;

}

function search(){

	var url=$('searchType').value=='works'?'/our-work/search.php':'/news/search.php';

	$('searchError').empty();
		
	if($('keywordSearch').value.trim().length<3||$('keywordSearch').value=='search here'){
	
		$('searchError').set('html','<br />Please enter at least 3 characters');
		return;
	
	}

	$('searchResults').set('html','<hr /><h3>Searching...</h3><br /><img src="/biomechanix/themes/default/images/preloader.gif" width="16" height="16" alt="" />');

	if(worksSearchRequest) worksSearchRequest.cancel();
	worksSearchRequest=new Request({
	
		'method':'post',
		'url':url,
		'data':{'keywordSearch':$('keywordSearch').value.trim()},
		'onSuccess':function(response){
		
			$('searchResults').set('html',response);
		
		}
	
	}).send();

}

function activateTagSearch(tag){

	if($('keywordSearch')) $('keywordSearch').value=tag;
	$('keywordSearch').focus();
	search();

}

function launchAccordion(){

	if($('accordion')){
	
		var firstShow=0;
		
		if(accordionStart){
		
			if(accordionStart=='sme') firstShow=1;
			if(accordionStart=='enterprise') firstShow=2;
		
		}
		
		accordion=new Accordion($$('.accordionHandler'),$$('.accordionContent'),{'show':firstShow,'duration':250});
	
	}

}

function launchSlideShow(){

	if($('workSlideShow')){
	
		slideShow=new Slideshow('workSlideShow',slideshowData,{
	
			'width':500,
			'height':slideShowHeight,
			'hu':'/biomechanix/images/',
			'delay':5000,
			'captionIndex':-1,
			'captions':true,
			'controller':true
		
		});
	
	}

}

function adjustBodyHeight(){

	var bodyDimensions=$('mainContents').getCoordinates();
	if(bodyDimensions.height<400) $('mainContents').setStyle('height','400px');

}

function adjustBackToTop(){

	if($('sidebarBackToTop')&&$('mainContents')){
	
		var bodyDimensions=$('mainContents').getCoordinates();
		var backToTopDimensions=$('sidebarBackToTop').getCoordinates();
		var offset=(bodyDimensions.height+bodyDimensions.top)-backToTopDimensions.top-50;
		$('sidebarBackToTop').setStyle('margin-top',offset+'px');
	
	}

}

function adjustFooterEffect(){

	footerEffect=new Fx.Tween($('featuredWorkImage'),{'duration':500,'link':'cancel'});
	footerMorph=new Fx.Morph($('featuredWork'),{'duration':500,'link':'cancel'});

	$('featuredWork').addEvent('mouseenter',function(){
	
		footerEffect.start('top',parseInt($('featuredWorkImage').getStyle('top')),120);
		footerMorph.start({'background-color':'#ffffff','border-color':'#ffffff','color':'#252525'});
	
	});
	
	$('featuredWork').addEvent('mouseleave',function(){
	
		footerEffect.start('top',parseInt($('featuredWorkImage').getStyle('top')),0);
		footerMorph.start({'background-color':'#959595','border-color':'#959595','color':'#959595'});
	
	});
	
	$('featuredWork').addEvent('click',function(){location.href='/our-work/';});
	
}

function adjustWorksEffect(){

	var worksLi=$$('.ourWorksList');
	if(worksLi[0]){
	
		worksLi=worksLi[0].getElements('li');
	
		worksLi.each(function(value,key){
		
			value.addEvent('mouseover',function(){
			
				this.getElements('h1')[0].setStyle('color','#252525');
			
			});
		
			value.addEvent('mouseout',function(){
			
				this.getElements('h1')[0].setStyle('color','#959595');
			
			});
		
		});

	}
	
}







/*	Carousel */

var Carousel=new Class({
					  
	'Implements':Options,
	
	'options':{
	
		'data':[],
		'viewWidth':0,
		'itemsPerView':1,
		'maxMargin':0,
		'animation':null,
		'items':null,
		'autoStart':true,
		'periodical':5000,
		'interval':null
				
	},
	
	'initialize':function(options){
		
		this.setOptions(options);
		this.setup();
		
	}

});

Carousel.implement({setup:function(){

	this.options.items=$$('#carousel li');
	this.options.maxMargin=this.options.items.length/this.options.itemsPerView*this.options.viewWidth-this.options.viewWidth;
	this.options.animation=new Fx.Tween($('carousel'),{'duration':500,'link':'cancel','onComplete':function(){this.changeSubMenu();}.bind(this)});
	
	$('homeButton').addEvent('click',function(){location.href='/services/startup/';});
	this.start();

}});

Carousel.implement({nextItem:function(){

	var pos=parseInt($('carousel').getStyle('left'));
	if(pos==-this.options.maxMargin*this.options.itemsPerView) this.options.animation.start('left',0);
	else this.options.animation.start('left',pos-this.options.viewWidth);

}});

Carousel.implement({previousItem:function(){

	var pos=parseInt($('carousel').getStyle('left'));

	if(pos==0) this.options.animation.start('left',-this.options.maxMargin);
	else this.options.animation.start('left',pos+this.options.viewWidth);

}});

Carousel.implement({gotoPos:function(newPos){

	
	this.options.animation.start('left',-newPos*this.options.viewWidth);

}});

Carousel.implement({stop:function(){

	$clear(this.options.interval);

}});

Carousel.implement({start:function(){

	if(this.options.periodical>0) this.options.interval=this.nextItem.periodical(this.options.periodical,this);

}});

Carousel.implement({changeSubMenu:function(){

	if(parseInt($('carousel').getStyle('left'))==0){
	
		$('startupNav').set('class','green');$('smeNav').set('class','');$('enterpriseNav').set('class','last');
		$('homeButton').addEvent('click',function(){location.href='/services/startup/';});
	
	}
	else if(parseInt($('carousel').getStyle('left'))==-720){
	
		$('startupNav').set('class','');$('smeNav').set('class','red');$('enterpriseNav').set('class','last');
		$('homeButton').addEvent('click',function(){location.href='/services/sme/';});
	
	}
	else{
	
		$('startupNav').set('class','');$('smeNav').set('class','');$('enterpriseNav').set('class','blue last');
		$('homeButton').addEvent('click',function(){location.href='/services/enterprise/';});
	
	}

}});


/*	Form Initializer */

var formDefaultsValues={};
function initializeForms(obj){

	/*	Declare default blur styles */
	
	var formDefaultsConfig={'color':'#CCCCCC'};
	
	/*	Get all textfields + textareas */
	
	obj=$pick(obj,$('right'));
	var m=obj.getElements('input').extend(obj.getElements('textarea'));
	
	m.each(function(value,key){
	
		if((value.type=='text'||value.tagName.toLowerCase()=='textarea')&&value.value!=''&&value.name!=undefined){
		
			formDefaultsValues[value.name]=value.value;
			
			value.setStyles({'color':formDefaultsConfig.color,'font-style':formDefaultsConfig.fontStyle});
			
			value.addEvent('focus',function(e){
			
				if(this.value==formDefaultsValues[this.name]) this.value='';
				
				this.setStyles({'color':'','font-style':''});
			
			});
			
			value.addEvent('blur',function(e){
			
				if(this.value==''||this.value==formDefaultsValues[this.name]){
					
					this.value=formDefaultsValues[this.name];
					
					this.setStyles({'color':formDefaultsConfig.color,'font-style':formDefaultsConfig.fontStyle});
					
				}
			
			});
			
		}
		
		if(value.type=='password'&&value.name){
			
			/*	New Element */
			
			formDefaultsValues[value.name]=new Element('input',{
			
				'id':'Password'+value.name,
				'value':value.value,
				'class':'formItem',
				'styles':{
					
					'color':formDefaultsConfig.color,
					'font-style':formDefaultsConfig.fontStyle,
					'width':value.getSize().x-8
					
				},
				'events':{
					
					'focus':function(e){
			
						this.setStyle('display','none');
						$(this.id.substr(8)).setStyle('display','');
						$(this.id.substr(8)).focus();
					
					}
					
				}
			
			}).inject(value,'before');
			
			/*	Original Element */

			value.setStyle('display','none');
			value.addEvent('blur',function(e){
			
				if(this.value==''){
					
					this.setStyle('display','none');
					$('Password'+this.name).setStyle('display','');
					
				}
			
			});
			
		}
	
	});
	
}


	
/*	PNG fix for IE 6 */
/*
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

if ((version >= 5.5) && (document.body.filters)) 
{
   for(var i=0; i<document.images.length; i++)
   {
	  var img = document.images[i]
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	  {
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 img.outerHTML = strNewHTML
		 i = i-1
	  }
   }
}*/