

window.addEvent('domready', function() {	
	
	// font replacement //////////////////////////////////
	Cufon.replace('h4', {
	//color: '-linear-gradient(white, black)',
	fontFamily: 'United Italic Rg Bk'
	});
	Cufon.replace('.mainmenu a', {
		hover: true
	});
	Cufon.replace('h1.pagetitle');

	// gallery ///////////////////////////////////////////
	document.nS4 = new noobSlide({
			box: $('imgwrapper'),
			items: $$('#gallery .gallery_item'),
			size: 792,
			addButtons: {
				previous: $('prev'),
				next: $('next') //,	play: $('play'), stop: $('stop'),
			},
			numbercontainer: $('numbers'),
			autoPlay: true
	});
	
	// drawers and co //////////////////////////////////
	new BrandsDrawer();
	new ContactDrawer();
	new MainMenu();
	
	$$('.pagetitle').each(function(h1){
		
		if (h1.get('text')=='') h1.destroy();
	});
	
});

window.addEvent('load', function() {	
	document.nS4.centerImages();	
});

/////////////////////////////////////////////////////////////////////////////////////////////////////

var MainMenu = new Class({
	
	initialize: function(){
        
		this.uls = $$('.mainmenu .children');
		this.uls.each(function(ul) {

			ul.fx = new Fx.Morph(ul, {duration: 500, transition: Fx.Transitions.Sine.easeOut});	
			ul.fx.set({'opacity':0});
			
			var btn = ul.getParent();
			btn.getElement('a').set('href', 'javascript:void(0)');
			
			btn.addEvent('mouseenter', function(e){
				ul.fx.cancel();
				ul.fx.start({'height':200, 'opacity':1});
			});
			btn.addEvent('mouseleave', function(e){
				ul.fx.cancel();
				ul.fx.start({'height':0, 'opacity':0});
			});			
		});
	 }
});// end class

/////////////////////////////////////////////////////////////////////////////////////////////////////

var BrandsDrawer = new Class({
	
	initialize: function(){
        
		if (!$$('.brands .division')) return;
       
		this.boxes = $$('.brands .division');	
		this.boxes.each(function(box) {
			
			// align //////////////////////////////////
			
			box.as = box.getElements('a.division-brand');
			var modulo = box.as.length % 6;
			var boxwidth = 157;
			// if (box.as.length>6){
				if (modulo==0) boxwidth = 131; 
			//}
			box.as.each(function(a){
					a.setStyle('width', boxwidth);
			});
			//console.log("modulo :"+ box.as.length % 6);
			
			
			
			// behaviours ////////////////////////////////
			
			box.btn = box.getElement('h3 a');
			box.btn.drawer = box.getElement('.division-brands');
			
			if (box.btn.drawer.hasClass('open')) {
				box.btn.addClass('open');
				box.btn.drawer.setStyle('display', '');
			}else{
				box.btn.drawer.setStyle('display', 'none');
			}
			
			box.btn.drawer.fx = new Fx.Reveal(box.btn.drawer, {duration: 500, mode: 'vertical'});
			box.btn.set('href', 'javascript:void(0)');
			
			box.btn.addEvent('click', function(){
				
				box.btn.drawer.fx.toggle();
				
				if (box.btn.hasClass('open')) {
					box.btn.removeClass('open');
				}else{
					box.btn.addClass('open');
				}
				
			}.bind(this));	
		});
		
		
		/** rollovers for brands **/
		
		this.brands = $$('a.division-brand');
		this.brands.each(function(brand) {
			
			brand.fx = new Fx.Morph(brand, {duration: 500, transition: Fx.Transitions.Sine.easeOut});
			
			brand.addEvent('mouseenter', function(e){
				brand.fx.cancel();
				brand.fx.start('.brand-mouseenter');
				
			}.bind(this));
			
			
			brand.addEvent('mouseleave', function(e){
				brand.fx.cancel();
				brand.fx.start('.brand-mouseleave');
			}.bind(this));
			
		});   	
	 }
});// end class



/////////////////////////////////////////////////////////////////////////////////////////////////////

var ContactDrawer = new Class({
	
	initialize: function(){
        
		if (!$$('.contact-person-drawer')) return;
       
		this.boxes = $$('.contact-person-drawer');	
		this.boxes.each(function(box) {

			box.btn = box.getElement('a.drawer_link');
			box.btn.drawer = box.getElement('.drawer');
			
			if (box.btn.drawer.hasClass('open')) {
				box.btn.addClass('open');
				box.btn.drawer.setStyle('display', '');
			}else{
				box.btn.drawer.setStyle('display', 'none');
			}
			
			box.btn.drawer.fx = new Fx.Reveal(box.btn.drawer, {duration: 500, mode: 'vertical'});
			box.btn.set('href', 'javascript:void(0)');
			
			box.btn.addEvent('click', function(){
				
				box.btn.drawer.fx.toggle();
				
				if (box.btn.hasClass('open')) {
					box.btn.removeClass('open');
				}else{
					box.btn.addClass('open');
				}
				
			}.bind(this));	
		});
		  	
	 }
});// end class



//////////////////////////////////////////////////////////////////////////////////////////////////////

/*
Author:
	luistar15, <leo020588 [at] gmail.com>
License:
	MIT License
 
Class
	noobSlide (rev.19-06-08)

Arguments:
	Parameters - see Parameters below

Parameters:
	box: dom element | required
	items: dom collection | required
	size: int | item size (px) | default: 240
	mode: string | 'horizontal', 'vertical' | default: 'horizontal'
	addButtons:{
		previous: single dom element OR dom collection| default: null
		next:  single dom element OR dom collection | default: null
		play:  single dom element OR dom collection | default: null
		playback:  single dom element OR dom collection | default: null
		stop:  single dom element OR dom collection | default: null
	}
	button_event: string | event type | default: 'click'
	handles: dom collection | default: null
	handle_event: string | event type| default: 'click'
	fxOptions: object | Fx.Tween options | default: {duration:500,wait:false}
	interval: int | for periodical | default: 5000
	autoPlay: boolean | default: false
	onWalk: event | pass arguments: currentItem, currentHandle | default: null
	startItem: int | default: 0

Properties:
	box: dom element
	items: dom collection
	size: int
	mode: string
	buttons: object
	button_event: string
	handles: dom collection
	handle_event: string
	previousIndex: int
	nextIndex: int
	fx: Fx.Tween instance
	interval: int
	autoPlay: boolean
	onWalk: function
	
Methods:
	previous(manual): walk to previous item
		manual: bolean | default:false
	next(manual): walk to next item
		manual: bolean | default:false
	play (interval,direction,wait): auto walk items
		interval: int | required
		direction: string | "previous" or "next" | required
		wait: boolean | required
	stop(): stop auto walk
	walk(item,manual,noFx): walk to item
		item: int | required
		manual: bolean | default:false
		noFx: boolean | default:false
	addHandleButtons(handles):
		handles: dom collection | required
	addActionButtons(action,buttons):
		action: string | "previous", "next", "play", "playback", "stop" | required
		buttons: dom collection | required

Requires:
	mootools 1.2 core
*/
var noobSlide = new Class({

	initialize: function(params){
		this.items = params.items;
		
		if (this.items.length == 0) {
			if ($('gallery')) $('gallery').setStyle('display', 'none'); 
			return;
		}
		if (!params) return;
		if (this.items.length < 2) return;
		if (this.items.length == 1) {
			$('buttons').setStyle('display', 'none'); 
			return;
		}
		
		
		this.items.each(function(item) {
			
			
			var titletxt = item.getElement('.title').get('text');
			var captiontxt = item.getElement('.caption').get('text');
			if ((captiontxt =="")&&(titletxt=="")) return;
			item.legend = item.getElement('.legend');
			item.legend.bg = new Element('div', {
			    'class': 'bg',
			    'opacity':0.5
			});
			item.legend.bg.inject(item.legend,'top');
			
			item.legend.fx = new Fx.Morph(item.legend, {duration: 500, transition: Fx.Transitions.Sine.easeOut});	
			item.legend.fx.set({'opacity':0});
			
			item.addEvent('mouseenter', function(e){
				item.legend.fx.cancel();
				item.legend.fx.start({'opacity':1});
				//console.log("enter..");
				this.stop();
				
			}.bind(this));
			
			
			item.addEvent('mouseleave', function(e){
				item.legend.fx.cancel();
				item.legend.fx.start({'opacity':0});
				//console.log("leave...");
				this.play(this.interval,'next',false);
				
			}.bind(this));
			
			
		}.bind(this));
		
		this.size = params.size || 600;
		this.mode = params.mode || 'horizontal';
		this.modes = {horizontal:['left','width'], vertical:['top','height']};
		this.box = params.box.setStyle(this.modes[this.mode][1],(this.size*this.items.length)+'px');
		this.button_event = params.button_event || 'click';
		this.handle_event = params.handle_event || 'click';
		this.onWalk = params.onWalk || null;
		this.currentIndex = null;
		this.previousIndex = null;
		this.nextIndex = null;
		this.interval = params.interval || 7000;
		this.autoPlay = params.autoPlay || false;
		this._play = null;
		this.handles = params.handles || null;
		if(this.handles){
			this.addHandleButtons(this.handles);
		}
		this.numbercontainer = params.numbercontainer || null;
		if(this.numbercontainer){
			this.addNumberButtons(this.numbercontainer);
			this.actualnumber = undefined;
		}
		this.buttons = {
			previous: [],
			next: [],
			play: [],
			playback: [],
			stop: []
		};

		if(params.addButtons){
			for(var action in params.addButtons){
				this.addActionButtons(action, $type(params.addButtons[action])=='array' ? params.addButtons[action] : [params.addButtons[action]]);
			}
		}
		this.fx = new Fx.Tween(this.box,$extend((params.fxOptions||{duration:500,wait:false}),{property:this.modes[this.mode][0]}));
		this.walk((params.startItem||0),true,true);
	},
	
	
	centerImages: function(){
		this.items.each(function(item) {	
			item.img = item.getElement('img');
			var imgheight = item.img.getStyle('height').toInt();
			var imgmargin = Math.floor((420-imgheight)/2);
			if (imgmargin < 0) imgmargin = 0;
			item.img.fx = new Fx.Morph(item.img, {duration: 600, transition: Fx.Transitions.Sine.easeOut});
			item.img.fx.start({'margin-top':imgmargin});
		}.bind(this));
	},
	
	
	addNumberButtons: function(target){
		this.btns = [];
		this.items.each(function(item,i) {
			this.btns[i] = new Element('a', {
			    'class': 'number',
			    'href' : 'javascript:void(0)',
			    'html':i+1
			});
			this.btns[i].addEvent(this.handle_event,this.walk.bind(this,[i,true]));
			this.btns[i].inject(target);		
		}.bind(this));
		// align:
		var container_w = target.getStyle('width').toInt(); 
		var w = 21;
		
		var elswidth = w*this.items.length;
		var diff = Math.abs(container_w - elswidth);
		var newwidth = container_w -diff;
		var newpadding = diff/2;
		
		target.setStyles({
			'padding-left': newpadding,
			'padding-right': newpadding,
			'width': newwidth
		});
		
	},
	
	addHandleButtons: function(handles){
		for(var i=0;i<handles.length;i++){
			handles[i].addEvent(this.handle_event,this.walk.bind(this,[i,true]));
		}
	},

	addActionButtons: function(action,buttons){
		for(var i=0; i<buttons.length; i++){
			switch(action){
				case 'previous': buttons[i].addEvent(this.button_event,this.previous.bind(this,[true])); break;
				case 'next': buttons[i].addEvent(this.button_event,this.next.bind(this,[true])); break;
				case 'play': buttons[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'next',false])); break;
				case 'playback': buttons[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'previous',false])); break;
				case 'stop': buttons[i].addEvent(this.button_event,this.stop.bind(this)); break;
			}
			this.buttons[action].push(buttons[i]);
		}
	},

	previous: function(manual){
		this.walk((this.currentIndex>0 ? this.currentIndex-1 : this.items.length-1),manual);
	},

	next: function(manual){
		this.walk((this.currentIndex<this.items.length-1 ? this.currentIndex+1 : 0),manual);
	},

	play: function(interval,direction,wait){
		this.stop();
		if(!wait){
			this[direction](false);
		}
		this._play = this[direction].periodical(interval,this,[false]);
	},

	stop: function(){
		$clear(this._play);
	},

	walk: function(item,manual,noFx){
		
		if(item!=this.currentIndex){
			
			if (this.actualbtn) this.actualbtn.removeClass('active');
			
			this.currentIndex=item;
			this.previousIndex = this.currentIndex + (this.currentIndex>0 ? -1 : this.items.length-1);
			this.nextIndex = this.currentIndex + (this.currentIndex<this.items.length-1 ? 1 : 1-this.items.length);
			
			this.actualbtn = this.btns[this.currentIndex];
			if (this.actualbtn) this.actualbtn.addClass('active');
			 
			if(manual){
				this.stop();
			}
			if(noFx){
				this.fx.cancel().set((this.size*-this.currentIndex)+'px');
			}else{
				this.fx.start(this.size*-this.currentIndex);
			}
			
			
			/***********************/
			/*if($('video')){
				//this.showvideoplayer = new ShowVideoPlayer();
				if (this.currentIndex == this.items.length-1) {
					this.showvideoplayer.setMaskHeight(450);
				}else{
					this.showvideoplayer.setMaskHeight('h0');	
				}
			}*/
			/**********************/
			if(manual && this.autoPlay){
				this.play(this.interval,'next',true);
			}
			if(this.onWalk){
				this.onWalk((this.items[this.currentIndex] || null), (this.handles && this.handles[this.currentIndex] ? this.handles[this.currentIndex] : null));
			}
		}
	}
	
});







