var _slideScroller;
var _swipeController;
var _clienti;
var _currentClient;
var _imageRotation;

function scrollSlide(el,page){
	$$('.page').each(function(el){el.setStyle('color','#000');});
	el.setStyle('color','#5291E4');
	_slideScroller.start(0,(page-1)*330);
};

window.addEvent('domready', function(){
	new Swiff('video/Tunnel_studios_creativity.swf', {width: '690',height:'64', params: {scale:'exactfit', wmode: 'transparent',wMode:'transparent',quality:'high',expressinstall:'Scripts/expressInstall.swf'},container: $('banner')});	
	
	if($('noi')!=null){
		$('noi').addEvents({
			mouseover : function(){
				 $('noi').morph({'width':200});
				if($('noi_b'))  $('noi_b').set('src','img/bottoni-menu/noi-over.jpg');
			},
			mouseout : function(){
				$('noi').morph({'width':45});
				if($('noi_b')) $('noi_b').set('src','img/bottoni-menu/noi.jpg');
			}
		});
	}
	
	if($('slide_scroller')!=null){
		_slideScroller = new Fx.Scroll('slide_scroller', {
			offset: {x: 0,y: 0},duration:500
		});
		
		var slides = $$('.singleSlide');
		var pageNumber = Math.ceil(slides.length/12);
		var page_select = "";
		for(k=1;k<=pageNumber;k++){
			if(k==1) page_select = page_select+'<span style="background-color:#FFF"><a class="page" style="color:#5291E4" onclick="scrollSlide(this,'+k+');" href="javascript:void(0)">'+k+'</a> |';
			else if(k<pageNumber) page_select = page_select+'<a class="page" style="color:#000" onclick="scrollSlide(this,'+k+');" href="javascript:void(0)">'+k+'</a> |';
			else page_select = page_select+'<a class="page" style="color:#000" onclick="scrollSlide(this,'+k+')" href="javascript:void(0)">'+k+'</a>';
		}
		page_select = page_select+'</span>';
		$('page_select').set('html',page_select);
		_slideScroller.start(0,0);
      
    }
	
	if($('occhiali')){
		$('occhiali').addEvents({
			'mouseover' : function(){
				$$('.video').each(function(obj){obj.setStyle('visibility','hidden');});
				$('logoBig').setStyle('display','block');
			},
			'mouseout' : function(){
				$$('.video').each(function(obj){obj.setStyle('visibility','visible');});
				$('logoBig').setStyle('display','none');
			}
		});	
	}

    if($('clienti')!=null){
        var obj1; 
        obj1 = new Swiff('video/tunnel_studios_clienti.swf', {width: '100%',height:'540', params: {scale:'exactfit', wmode: 'transparent',wMode:'transparent',quality:'high',expressinstall:'Scripts/expressInstall.swf'},container: $('clienti')});
    }
   
	if($('acquaLoop')!=null){
        var obj1; 
        obj1 = new Swiff('video/acqua.swf', {width: '100%',height:'400', params: {scale:'exactfit', wmode: 'transparent',wMode:'transparent',quality:'high',expressinstall:'Scripts/expressInstall.swf'},container: $('acquaLoop')});
		
		
		
		/*window.onresize  = function (){
			$('acquaLoop').setStyle('width',document.body.clientWidth);
			$('acquaLoop').setStyle('height',200);
		}*/
    }
	
	if($('light')!=null){
		$('light').set("morph",{duration:1000});
	}
	
	if($('clienti_cont')!=null){
		_clienti = new clienti();
		_imageRotation = new ImageRotation();
	}
	
	if($('external_cont')!=null){
		//TEAM CLASS
		_swipeController = new SwipeController(12);	
	}
	
	$$('.menu_button').each(function(obj){						
		obj.addEvents({
			mouseover: function(e){
			 var src = e.target.get('src');
			 var new_src = src.split(".")[0]+"-over.jpg";
			 e.target.set('src',new_src);
			},
			mouseout: function(e){
				var src = e.target.get('src');
			 	var new_src = src.split("-over")[0]+".jpg";
			    e.target.set('src',new_src);	
			}
		});
	});			
});


function setLayer(){
	$('webMedia').setStyle('z-index','-1');
}

//////////////////////TEAM CLASS

var SwipeController = new Class({
	initialize: function(numOfElem) {
		this.numOfElem = numOfElem;
		this.swipeScroller = new Array();
		this.swipeScroller[0] = new SwipeScroller('teste',this);
		this.swipeScroller[1] = new SwipeScroller('occhi',this);
		this.swipeScroller[2] = new SwipeScroller('nasi',this);
		this.swipeScroller[3] = new SwipeScroller('bocche',this);
	},
	_isFinished : function(){
		for(k=1;k<4;k++){
			if(this.swipeScroller[k-1]._getCurImg() != this.swipeScroller[k]._getCurImg()) return false;
		}
		$('team_'+this.swipeScroller[k-1]._getCurImg()).setStyle('display','block');
	},
	_scroll: function(index,direction){
		this.swipeScroller[index]._scroll(direction);	
	}
});

var SwipeScroller = new Class({
	initialize: function(section,controller) {
		this.controller = controller;
		this.section = section;
		this.currentMouseGap = 0;
		this.mouseDwon = 0;
		this.mouseIsOver = 0;
    	this.mouseStart = 0;
		this.mouseStop = 0;
		this.direction = "right";
		this.size = 440;
		this.img = $(this.section+'_cont').getChildren('img');
		
		var src = this.img[0].get('src').replace(".jpg","");
		var ar = src.split("/");
		this.currentImg = ar[3];
		
		$(this.section+"_ext").addEvents({
			'mouseenter':function(e){
				$$('.swipeButton').each(function(el){el.setStyle('visibility','hidden');})
				var maxScroll = (this.controller.numOfElem-1)*this.size;
				this.mouseIsOver = 1;
				if($(this.section+'_scroller').getScroll().x>0)$(this.section+"Back").setStyle('visibility','visible');
				if($(this.section+'_scroller').getScroll().x<maxScroll)$(this.section+"Next").setStyle('visibility','visible');
			}.bind(this),
			'mouseleave':function(e){
				this.mouseIsOver = 0;
				$(this.section+"Back").setStyle('visibility','hidden');
				$(this.section+"Next").setStyle('visibility','hidden');
			}.bind(this)
		});
		
		this.scroller = new Fx.Scroll(this.section+'_scroller', {
			duration:1000,
			transition: Fx.Transitions.Bounce.easeOut,
			onComplete: function(){
				var elem_position = (($(this.section+'_scroller').getScroll().x)/this.size).toInt();
				var src = this.img[elem_position].get('src').replace(".jpg","");
				var ar = src.split("/");
				this.currentImg = ar[3];
				var maxScroll = (this.controller.numOfElem-1)*this.size;
				if($(this.section+'_scroller').getScroll().x==0)$(this.section+"Back").setStyle('visibility','hidden');
				else if($(this.section+'_scroller').getScroll().x==maxScroll)$(this.section+"Next").setStyle('visibility','hidden');
				else if(this.mouseIsOver==1) {$(this.section+"Next").setStyle('visibility','visible');$(this.section+"Back").setStyle('visibility','visible');}
				$$('.roles').each(function(item){item.setStyle('display','none')});
				this.controller._isFinished();
			}.bind(this)
		});	
		this.img.each(function(item){
			item.addEvents({
				'mousedown':function(e){
					e.preventDefault();
					this.mouseStart = e.page.x - e.target.offsetLeft;
					this.mouseDwon = 1;
				}.bind(this),
				'mousemove':function(e){
					e.preventDefault();
					if(this.mouseDwon == 1){
						var _mouseGap = -(e.page.x - e.target.offsetLeft - this.mouseStart);
						//scrolla
						this.scroller.set($(this.section+'_scroller').getScroll().x + (_mouseGap-this.currentMouseGap));
						if(this.currentMouseGap < _mouseGap) this.direction = "right";
						else this.direction = "left";
						this.currentMouseGap = _mouseGap;
						if(Math.abs(_mouseGap)>40){ 
							//morph scroller
							var scrollX = $(this.section+'_scroller').getScroll().x;
							if(this.direction == "right") targetX = (Math.floor((scrollX / this.size))*this.size + this.size);
							else targetX = (Math.ceil((scrollX / this.size))*this.size - this.size);
							this.scroller.start(targetX,0);
							this._reset();
						}
					}
				}.bind(this),
				'mouseup':function(e){
					if(this.mouseDwon == 1){
						this.mouseStop = e.page.x - e.target.offsetLeft;
						this.currentMouseGap = this.mouseStop - this.mouseStart;
						this._reset();
					}
					
				}.bind(this),
				'mouseout':function(e){
					if(this.mouseDwon == 1) this._reset();
				}.bind(this)
			});
		}.bind(this));
	},
	_reset : function(){
		this.currentMouseGap = 0;
		this.mouseDwon = 0;
    	this.mouseStart = 0;
		this.mouseStop = 0;
	},
	_getCurImg : function(){
		return this.currentImg;
	},
	_scroll: function(direction){
		var maxScroll = (this.controller.numOfElem-1)*this.size;
		if(direction=="next" && $(this.section+'_scroller').getScroll().x<maxScroll) this.scroller.start($(this.section+'_scroller').getScroll().x+this.size,0);	
		else if(direction=="back" && $(this.section+'_scroller').getScroll().x>0) this.scroller.start($(this.section+'_scroller').getScroll().x-this.size,0);	
	}
});


function changeView(sec){
	$('didascalia').set("morph",{duration:1000});
	$('img_lucafede').set("morph",{duration:1000});
	if(sec==1){
		$('didascalia').setStyle("z-index",'2');
		$('img_lucafede').setStyle("z-index",'1');
		$('img_lucafede').morph({'opacity':'0'});
		$('didascalia').morph({'opacity':'1'});
	}else{
		$('didascalia').setStyle("z-index",'1');
		$('img_lucafede').setStyle("z-index",'2');
		$('img_lucafede').morph({'opacity':'1'});
		$('didascalia').morph({'opacity':'0'});	
	}
}

//////////////////////TEAM CLASS



//////////////////////CLIENTI
var clienti = new Class({
	initialize: function() {
		 this.size = 320;
		 this.scroller = new Fx.Scroll('clienti_cont', {
			duration:1000,
			transition: Fx.Transitions.Bounce.easeOut
		});
		
		$('clienti_next').addEvent('click',function(){
			if($('clienti_cont').getScrollSize().y-$('clienti_cont').getScroll().y-this.size >= this.size) this.scroller.start(0,$('clienti_cont').getScroll().y+this.size);
			else this.scroller.toBottom();
		}.bind(this));
		$('clienti_back').addEvent('click',function(){
			if($('clienti_cont').getScroll().y >= this.size) this.scroller.start(0,$('clienti_cont').getScroll().y-this.size);
			else this.scroller.toTop();
		}.bind(this));
		
		$$('.clien_img').each(function(obj){
			obj.set('morph',{duration:400,link:'cancel'});
			obj.addEvents({
				'mouseover':function(e){
					if(Browser.ie7 || Browser.ie8) e.target.setStyle('opacity','1');
					else e.target.morph({'opacity':'1'});
				},
				'mouseout':function(e){
					if(Browser.ie7 || Browser.ie8) e.target.setStyle('opacity','0.1');
					else e.target.morph({'opacity':'0.1'});	
				}/*,'click':function(e){
					var prod = e.target.get('alt').split(" ");
					alert(e.target.get('alt'));	
				}*/
			});
		});	
	}						
});
function openTeam(sec){
	$('team_members').setStyle('display','none');
	$('lucafede').setStyle('display','none');
	if(sec == "team_members") {
		$('role_container').setStyle('display','block');
		$('team_button').setStyle('opacity','1');
		$('lucafede_button').setStyle('opacity','0.5');
	}else{ 
		$('role_container').setStyle('display','none');
		$('team_button').setStyle('opacity','0.5');
		$('lucafede_button').setStyle('opacity','1');
	}
	$(sec).setStyle('display','block');
}



var ImageRotation = new Class({
	initialize: function(){
		this.src = null;
		this.imageNum = null;
		this.current = null;	
		this.idInterval = null;
		this.id = null;
		this.controllerEnabled = true;
	},
	enableController : function(){
		var counter = function(){
			this.controllerEnabled = true;
		}.bind(this);
		counter.delay(2500);
	},
	play : function(){
		this.idInterval = setInterval(function(){ this.rotation() }.bind(this), 6000);	
		$('pause').setStyle('display','block');
		$('play').setStyle('display','none');
	},
	pause : function(){
		clearInterval(this.idInterval);
		$('play').setStyle('display','block');
		$('pause').setStyle('display','none');
	},
	next : function(){
		if(this.controllerEnabled){
			this.controllerEnabled = false;
			clearInterval(this.idInterval);
			$('play').setStyle('display','block');
			$('pause').setStyle('display','none');
			this.rotation('next');
			this.enableController();
		}
	},
	prev : function(){
		if(this.controllerEnabled){
			this.controllerEnabled = false;
			clearInterval(this.idInterval);
			$('play').setStyle('display','block');
			$('pause').setStyle('display','none');
			this.rotation('back');
			this.enableController();
		}
	},
	startRotation : function(id,src,num){
		this.src = src;
		this.id = id;
		this.imageNum = num;
		this.current = 1;
		var img = new Element('img', {
			alt: 'clineti tunnel studios',
			styles : {'opacity':0,'height':'315px','width':'560px','opacity':0,'position':'absolute'},
			events: {
				load: function(e) {
					img.set('morph',{duration:1000});
					img.morph({opacity:1});
				}
			}
		});
		
		$(this.id).set('html','');
		img.set('src',this.src+"/1.jpg").inject(this.id);
		this.idInterval = setInterval(function(){ this.rotation() }.bind(this), 6000);	
		$('pause').setStyle('display','block');
		
	},
	rotation : function(direction){
		var dir = (direction) ? direction : 'next';
		if(dir == "next"){
			if(this.current==this.imageNum) this.current=1;
			else this.current += 1;
		}else{
			if(this.current==1) this.current=this.imageNum;
			else this.current -= 1;
		}
		img = new Element('img',{
			alt:"clienti tunnel studios",
			styles: {
				'height':'315px','width':'560px','opacity':0,'position':'absolute'
			},
			events: {
				load: function(e) {
					$(this.id).getChildren()[0].set("morph",{duration :1000, link:'cancel',onComplete:function(){
							$(this.id).getChildren()[0].destroy();
						}.bind(this)
					});
					$(this.id).getChildren()[0].morph({'opacity':'0'});
					img.set("morph",{duration :2000, link:'cancel'});
					img.morph({'opacity':'1'});
					e.stop();
				}.bind(this)
			}
		});
		img.set('src',this.src+"/"+this.current+'.jpg').inject($(this.id));
	},
	stopRotation : function(){
		if(this.idInterval != null) clearInterval(this.idInterval);
		if(this.id != null)$(this.id).set('html','');
		$('pause').setStyle('display','none');
		$('play').setStyle('display','none');
	}
});

//////////////////////CLIENTI
