/**
 * ZOOM OVERVIEW

 */

/*  Déclarations de la class  */

	var Overview = new Class({
		
		initialize : function () {
			var _this = this;
			var view = $('view');
			//console.log('test');
			
			if (!view)
				return;
			
			this.monBool = view.hasClass('withTickers');
			this.ctnItems =  $("viewMedia").getChildren();
			
			/* Recuperation des listes  */
			if($("thumbList")){
				//mode 1
				this.listItems = $("thumbList").getElements("ul li a");
				/* Ajouts evenements */
				/* click thumb */
				this.listItems.each(function(item, index) {
					item.addEvent('click', function(e){
						new Event(e).stop();
						_this.clicHandler(this.parentNode, index);
						
					});
					item.addEvent('mouseenter', function(e){
						new Event(e).stop();
						this.addClass("current")
						
					});
					item.addEvent('mouseleave', function(e){
						new Event(e).stop();
						this.removeClass("current")
						
					});
				});
				
			}else{
				//mode 2
				var index = 0;
				var nextBtn = view.getElement('.nextArrow');
				var prevBtn = view.getElement('.prevArrow');
				
					nextBtn.addEvent('click', function(e){
						new Event(e).stop();
						index = index == _this.ctnItems.length - 1 ? 0 : index + 1;
						_this.layerViewSwitcher(index);
						
						
						
						
						
					});
					
					prevBtn.addEvent('click', function(e){
						new Event(e).stop();
						index = index == 0 ? _this.ctnItems.length - 1 : index - 1;
						//_this.clicHandler(this.parentNode, index);
						_this.layerViewSwitcher(index);
						
					});
				//recuperer lien avant et apres
				//ajout evenement sur callhaque lien
				//chaque evernemtn va faire un layerViewSwitcher avec bon index

			}
			
			
			
			
			
			//this.tickersItem = this.ctnItems.getElements("a");
			
			//this.view_Overview  =  $$(".view_Overview")[0];
		//	this.tickerClose = this.view_Overview.getElements("a");
			/* Push dans les tabs  */
			//var listItems = small_thumbs.push();
			
			
			
			if (this.monBool){
			var tickers = view.getElements('a.ticker');
			var midSize = parseInt(((view.offsetWidth) / 2)) ;
			var leftView = view.getLeft();
			tickers.each(function(elm, index) {
				elm.posClass = parseInt(elm.style.left) < midSize ? 'rightPosition' : 'leftPosition';
			});
			
			}
			
			
			
			
			this.viewInit(this.ctnItems[0]);
		},
		
		avance  : function ( index) {
			this.ctnItems.each(function(index) {	
				index.addClass("hidden");
			});
			//console.log(this.ctnItems)
		},
		
		
		clicHandler :function(elm, index){
			this.listThumbsSwitcher(elm);
			this.layerViewSwitcher(index);
		},
		
		rollClass: function(elm, index){
		
			//console.log(index)
		},
		
		listThumbsSwitcher : function(elm){
			this.listItems.each(function(elm){
			
				elm.getParent().removeClass('current');
			});
			
			elm.addClass('current');
		},
		
		layerViewSwitcher : function (index) {
			this.ctnItems.each(function(elem) {
				$(elem).addClass("hidden");
			});
			//console.log($(this.ctnItems[index]))
			$(this.ctnItems[index]).removeClass("hidden");
			this.viewInit(this.ctnItems[index]);
		},
		
		
		
		
		viewInit : function(view, pos){
			var _this = this;
			
			$(view).removeClass("hidden");
			
			//recuperer la popin
			var popin = view.getElement('.view_Overview div');
			var parentPopin = view.getElement('.view_Overview');
			var closeBtn = view.getElement('.close');
			
			if (this.monBool){
				//recuperer les tickers
				var tickers = view.getElements('a.ticker');
			
			/* calcul de la width */
			tickers.addEvents({
				'click' : function(e){
					new Event(e).stop(e);
					//reset
					parentPopin.removeClass('leftPosition');
					parentPopin.removeClass('rigthPosition');
					parentPopin.addClass(this.posClass);
					_this.openHandler(this.getAttribute('rel'), popin);
					
				},
				'mouseenter' : function(){
					this.addClass('tickerCurrent')
				},
				'mouseleave' : function(){
					this.removeClass('tickerCurrent')
				}
			});
			closeBtn.addEvent('click', function(){
				_this.closePopin(popin);
			});
			view.addEvent('click', function(){
				_this.closePopin(popin);
			});
			}
			
			
			
		},

		
		openHandler : function(sId, popin) {
			//console.log(sId, popin);
			popin.innerHTML = $(sId).innerHTML;
			//popin.getParent().removeClass("hidden");
			popin.getParent().effect('opacity').start(1);
		},
		closePopin : function(popin){
			popin.getParent().effect('opacity').start(0);
		//	popin.getParent().addClass("hidden");
			popin.innerHTML = '';
		}

});

var OverviewEquipment = new Class({
		
	initialize : function () {
		var _this = this;
		var view = $('viewEquipement');
		
		if (!view)
			return;

		
		/* Recuperation des listes  */
		this.listItems = $('viewEquipement').getElements(".thumbList ul.list");
		
		this.listItems.each(function(elm, index) {
			var myElement = elm;
			var myElementIndex = index;
			var myElementTextContainer = myElement.getNext('.ctnEquipmentList').getElements(".body");
			var myListLinks = elm.getElements("li a");
			myListLinks.each(function(elm, index) {
				//console.log(elm,index,myElement,myElementIndex);
				//console.log(elm);
				
				elm.addEvent("mouseenter",function(e){
					this.getParent().addClass("hover");
				});
				elm.addEvent("mouseleave",function(e){
					this.getParent().removeClass("hover");
				});
				elm.addEvent("click",function(e){
					new Event(e).stop(e);
					// on change le current de la vignette
					myElement.getElement(".current").removeClass("current");
					elm.getParent().addClass("current");
					
					myElementTextContainer.addClass("hidden");
					myElementTextContainer[index].removeClass("hidden");
				});
			});
		});
	}
});

	
window.addEvent('domready', function(){

	overview1 =  new Overview ();
	overview2 =  new OverviewEquipment ();
	
});





