/**
 *	Waltham.ch javascript document
 *	makes extensive use of mootools v.1.11 ( http://www.mootools.net/ )
 *	@author		sam rossetti | contreforme sàrl | http://www.contreforme.ch
 *	@version	1.1.1
 */

function walthamInit(){
	// --- if enter exists we are on the welcome page ---
	if($('enter')){
		// --- alert IE 6 users that their browser may not be suited for this website ---
		if(Browser.Engine.trident && Browser.Engine.version == 6){
			new Element('h5',{
				'styles':{
					'text-align':'center',
					'position':'absolute',
					'width':'100%'
				}
			}).set('html','This site is optimized for Internet Explorer above version 6.<br />Please <a href="http://www.microsoft.com/windows/products/winfamily/ie/default.mspx" target="_blank" style="text-decoration:underline">update your browser</a>.').injectTop($('bottom'));
		}
		// --- init slideshow for background pictures ---
		var myShow = new Slideshow($('backdrop'), ['bg1.jpg','bg2.jpg','bg3.jpg','bg4.jpg','bg0.jpg'], {hu: '__img/_statics/',width:950,height:340,loader:false,duration:1200});
		// ---  init top and bottom lines effects ---
		var toplinefx = new Fx.Tween($('topline'),{duration:1200,transition:Fx.Transitions.Quint.easeIn});
		var botlinefx = new Fx.Tween($('bottomline'),{duration:1200,transition:Fx.Transitions.Quint.easeIn});
		// ---  set top and bottom lines to 0
		toplinefx.set('left',0);
		botlinefx.set('left',0);
		// --- init enter button fade in/out ---
		var fx = new Fx.Tween($('enter'),'opacity');
		// --- set it at .5
		fx.set('opacity',0.5);
		// --- mouse over/out events for enter button
		$('enter').addEvent('mouseenter',function(ev){
			fx.start('opacity',1);
		});
		$('enter').addEvent('mouseleave',function(ev){
			fx.start('opacity',0.5);
		});
		// --- click event for enter button
		$('enter').addEvent('click',function(ev){
			// --- stop action ---
			ev = new Event(ev).stop();
			// --- start animation ---
			fx.start('opacity',0).chain(function(){
				var page = $('enter').get('rel');
				$('enter').destroy();
				botlinefx.start('left',-350);
				toplinefx.start('left',-350).chain(function(){
					// --- enter website ---
					window.location = page+"?l=en";							  
				});
			});
		});
	// --- if menu exists we are on the collections selection page ---
	}else if($('menu')){
		// --- menu effect init ---
		var fx = new Fx.Morph($('menu'),{duration:1500,transition:Fx.Transitions.Quint.easeIn});
		// --- close menu ---
		fx.set({
			'height':0
		});
		// --- init background images slideshow ---
		var myShow = new Slideshow($('backdrop'), ['bg1.jpg','bg2.jpg','bg3.jpg','bg4.jpg','bg0.jpg'], {hu: '__img/_statics/',width:950,height:340,loader:false,duration:1200});
		
		$each($$('#menu a'),function(el){
			// --- menu items effect init ---
			var ffx = new Fx.Tween(el,{duration:300, wait:false,transition:Fx.Transitions.Sine.easeOut});
			el.addEvent('mouseenter',function(){
				ffx.start('margin-left',10);
			});
			el.addEvent('mouseleave',function(){
				ffx.start('margin-left',0);
			});
		});
		// --- open menu ---
		fx.start({
			'height':250 
		});
	// --- else we are inside the pages ---
	}else{
		// --- warning for website maintenance, add double slash after 'test warning' line to enable ---
		/*/ test warning
		new Element('h5',{
			'styles':{
				'text-align':'center',
				'position':'absolute',
				'margin-top':'40px',
				'width':'100%'
			}
		}).set('html','<span style="color:#f00;">Warning. Tests are being conducted, the site may not respond normally.</span>').injectTop($('bottom'));
		//*/
		// --- elements' effects ---
		var accordionactive = false;
		var topfx = new Fx.Tween($('topmenu'),{duration:750,transition:Fx.Transitions.Cubic.easeInOut});
		var footerfx	= new Fx.Tween($('footer'),{duration:500,transition:Fx.Transitions.Cubic.easeIn});
		if($('content')) var contentfx = new Fx.Tween($('content'),{duration:250,link:'chain'});
		$('footer').setStyle('cursor','pointer');
		topfx.set('bottom',-15);
		footerfx.start('margin-top',135);
		// --- init scroller (will check if needed) ---
		initScroller();
		// --- if bottom menu exists add behaviour to it ---
		if($('bottommenu')){
			// --- url for ajax queries ---
			var url	= '_xhr.php?';
			// --- fx for bottom menu ---
			var botfx = new Fx.Tween($('bottommenu'),{duration:750,transition:Fx.Transitions.Cubic.easeInOut});
			botfx.set('top',-55);
			// --- if we are in history pages play with images ---
			if($('bottommenu').hasClass('history')){
				var imgfx = [];
				$each($$('#content img'),function(el,index){
					imgfx[index] = new Fx.Tween(el);
					if(el.hasClass('active')){
						_imgnum = index;
					}else{
						imgfx[index].set('opacity',0);
					}
				});
				$each($$('#bottommenu li a'),function(el,index){
					el.addEvent('click',function(ev){
						var ev = new Event(ev).stop();
						var r = el.get('rel');
						if(_imgnum != r){
							imgfx[r].start('opacity',1);
							imgfx[_imgnum].start('opacity',0);
							_imgnum = r;
							$each($$('#bottommenu li a'),function(e,i){
								e.removeClass('active');
							});
							el.addClass('active');
						}
					});
					
				});
			// --- otherwise ---
			}else{
				// --- for each bottom menu's element ---
				$each($$('#bottommenu li.toggler'),function(el,index){
					var a = el.getFirst();
					// --- prevent action ---
					a.addEvent('click',function(ev){
						ev = new Event(ev).stop();
					});
				});
				// --- make new accordion with injected ul's ---
				var accordion = new Accordion($$('li.toggler a.tog'),$$('ul.models'),{
					opacity:true,
					display:false,
					alwaysHide:true,
					onActive: function(toggler,element){
						if(toggler.hasClass('active')) return;
						toggler.addClass('active');
						accordionactive = element;
						footerfx.start('margin-top',135);
					},
					onBackground: function(toggler,element){
						toggler.removeClass('active');
					}
				});
			}
			// --- launch chained top and bottom effects for models and request content through ajax query ---
			topfx.start('bottom',5).chain(function(){
				botfx.start('top',5).chain(function(){
					// --- on complete get each model ---
					$each($$('a.model'),function(el,index){
						el.addEvent('click',function(ev){
							ev = new Event(ev).stop();
							// --- if active stop action ---
							if(this.hasClass('active')) return;
							// ---  else remove active class from all model ---
							$each($$('a.model'),function(e,index){
								e.removeClass('active');
							});
							this.addClass('active');
							// --- get url from rel attribute ---
							var target = el.get('rel');
							// --- ajax query ---
							new Request({
								url:url,
								method:'get',
								onComplete:function(responseText){
									contentfx.start('opacity',0).chain(function(){
										$('content').set('html',responseText);
										// --- launch content fade effect ---
										contentfx.start('opacity',1);
										initScroller();
									});
								}
							}).send(target);
						});
					});
				});
			});
			// --- footer behaviour for mouse over/out ---
			$('footer').addEvent('mouseenter',function(ev){
				footerfx.start('margin-top',80);
				accordion.display(null);
			});
			$('footer').addEvent('mouseleave',function(ev){
				footerfx.start('margin-top',135);
				if(accordionactive) accordion.display(accordionactive);
			});
		// --- else launch top effect ---
		}else{
			topfx.start('bottom',5);
			// --- footer behaviour for mouse over/out ---
			$('footer').addEvent('mouseenter',function(ev){
				footerfx.start('margin-top',80);
			});
			$('footer').addEvent('mouseleave',function(ev){
				footerfx.start('margin-top',135);
			});
		}
		// --- fade content action ---
		$each($$('a.toggler'),function(el,index){
			el.fx = [];
			el.fx.push(new Fx.Morph($(el.getAttribute('rel')+'_img')));
			el.fx.push(new Fx.Morph($(el.getAttribute('rel')+'_specs')));
			el.addEvent('click',function(ev){
				ev = new Event(ev).stop();
				for(var i=0;i<this.fx.length;i++){
					this.fx[i].start({
						'opacity':0
					});
				}
			});
		});
	}
}
// --- scroller function - create scrollbar if text area is bigger than visible area ---
function initScroller(){
	// --- scroller for collections'texts ---
	if(!$('txt')) return;
	// --- block visible area ---
	$('content').setStyle('overflow','hidden');
	// --- size of text container minus 20 pixels from padding-bottom (340 - 20 = 320) ---
	var h		= 366;
	var cont	= $('txt').getSize();
	// --- if text size is bigger than the visible area ---
	if(cont.y > h){
		// --- inject required elements for scroll bar ---
		var scrollzone = new Element('div',{'id':'scrollzone'}).inject($('content'));
		var knob = new Element('div',{'id':'knob'}).inject($('scrollzone'));
		// --- init text and knob scroll effects ---
		var txtscroll = new Fx.Morph($('txt'),{duration:750,wait:false,transition:Fx.Transitions.Cubic.easeInOut});
		var knobscroll = new Fx.Tween($('knob'),{duration:750,wait:false,transition:Fx.Transitions.Cubic.easeInOut});
		// --- reinit bg position (IE bug) ---
		$('txt').setStyle('background-position','45px 0px');
		// --- init slider ---
		var slider = new Slider($('scrollzone'),$('knob'),{
			steps: cont.y-340,
			mode: 'vertical',
			wheel:true,
			onChange: function(step){
				txtscroll.start({
					'margin-top':step*-1,
					'background-position':'45px '+step+'px'
				});
			},
			onTick:function(step){
				knobscroll.start('top',step);
			}
		}).set(0);
		// --- mousewheel event for slider ---
		$('txt').addEvent('mousewheel',function(ev){	
			ev = new Event(ev).stop();
			var step = slider.step-ev.wheel*30;	
			slider.set(step);					
		});
		var elS = $('txt').getStyles();
		var elT;
		for(var i=0;i<elS.length;i++){
			elT += i+" - "+elS[i]+"\n";
		}
	}
}
// --- launch on DOM ready state ---
window.addEvent('domready', function(){
	walthamInit();
});
var accordionactive,_imgnum;