// JavaScript Document

var _keylistener;
var _menus = Array();
var _menu0;
var _menu1;
var _menu2;
var _slider;
var _fullscreen;
var _currentPage;
var _current;
var currentMenu = 0;
var currentItem = 0;
var _tip0;
var _tip1;
var _tip2;
var index = 0;

var reg= new  RegExp("(http://www.jeanbedez.com)", "g");

window.addEvent('domready', function() {
	
	_log('passse');
	
	$('viewer').setStyle('display', 'none');
	
	_slider = new Slider($('view'), $('arrow_left'), $('arrow_right'), $('nav_info'), $('tempContent'));	
	_fullscreen = new FullScreenView($('fullscreenviewer'), $('fullscreen_arrow_left'), $('fullscreen_arrow_right'), $('fullscreen_nav_info'), $('tempContent'));	

	_menu0 = new Menu($('menu'), $('submenu'), 0);
	_menu1 = new Menu($('submenu'), $('subsubmenu'), 1);
	_menu2 = new Menu($('subsubmenu'), null, 2);
	_menus.push(_menu0);
	_menus.push(_menu1);
	_menus.push(_menu2);
	_keylistener = new Keylistener(_menus);

	_menu0.feed($('menuFeed').getElement('ul').getChildren('li'));
	
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);	
	
	$('fullscreen').addEvents( {		
		'click' : function(e) {
			e.stop();
			loadFullScreenPage();			
		}
	});
	
	$('fullscreen').store('tip:title', "FULLSCREEN");
	$('fullscreen').store('tip:text', "zoom");
	_tip0 = new Tips($('fullscreen'), {
		showDelay: 0,
		hideDelay: 0,
		offsets: {x: 10, y: 10},
		fixed: false
	});
	
	$('keyboard').store('tip:title', "KEYBOARD NAVIGATION");
	$('keyboard').store('tip:text', "navigation clavier");
	_tip1 = new Tips($('keyboard'), {
		showDelay: 0,
		hideDelay: 0,
		offsets: {x: 10, y: 10},
		fixed: false
	});
	
	$("navigationInfo").set("tween",{duration : 600, onComplete:hideNfo}).tween('opacity', 1);
	
});

function hideNfo(){
	
	(function() {
		$("navigationInfo").set("tween",{duration : 600}).tween('opacity', 0);
	}).delay(4000);
}

var handleChange = function(event) {
	
	_log('handleChange');
	_log(SWFAddress.getPathNames()[1]);
	
//	$('viewer').setStyle('display', 'block');
		
	if((_currentPage!=SWFAddress.getValue())&&SWFAddress.getPathNames()){

		var req = new Request.HTML( {
			url : "http://www.jeanbedez.com/_inc/ajax_section.php",
			method : 'post',
			encoding : 'uft-8',
			onRequest : function() {
			},
			onSuccess : function(data) {
				
				$('tempContent').empty().adopt(data);
				_keylistener.scopeOnBody();
				_slider.next();
				if($('ajax_title')){
					trackPage($('ajax_title').firstChild.data);		
					document.title = $('ajax_title').firstChild.data;
				}	
				if($('ajax_visu')){
					$('view').empty();
					$('ajax_visu').getElements('li').each(function(el) {

						$('view').adopt(el);
					});				
				}
				$('tempContent').empty();
			},
			onFailure : function() {

				_log("ajax error");
			}
		}).post( {
			'section' : SWFAddress.getPathNames()[1],
			'ratio': _fullscreen.options.hided
		});
		req.send();	
		
		if(SWFAddress.getPathNames()[1]){
			var arr = SWFAddress.getPathNames()[1].split('|');
			if(arr.length>0){
	
				_menu0.addEvent('expandComplete', onActivate0Complete);
				_menu0.forceActivate(arr[0]);			
			}else{
				_menu0.addEvent('expandComplete', onActivate0Complete);
				_menu0.forceActivate(SWFAddress.getPathNames()[1]);
			}
		}
	}
};

var initTouchX;
var initTouchY;
var _slideLock = false;

function touchStart( e ) {
	e.preventDefault();	
	
	var targetEvent =  e.touches.item(0);
	initTouchX = targetEvent.clientX;
	initTouchY = targetEvent.clientY;	
	
	return false;
}
  
function touchMove( e ) {
	e.preventDefault();
	
	var targetEvent =  e.touches.item(0);
	var diffX = initTouchX-targetEvent.clientX;
	var diffY = initTouchX-targetEvent.clientY;
	
	if(_slideLock == false){
		_slideLock = true;
		unLockSlide.delay(0.5);
		if(diffX>10){		
			_slider.next();
		}
		if(diffX<-10){	
			_slider.previous();
		}
	}	
	return false;
}

function touchStartFullScreen( e ) {
	e.preventDefault();	
	
	var targetEvent =  e.touches.item(0);
	initTouchX = targetEvent.clientX;
	initTouchY = targetEvent.clientY;	
	
	return false;
}
  
function touchMoveFullScreen( e ) {
	e.preventDefault();
	
	var targetEvent =  e.touches.item(0);
	var diffX = initTouchX-targetEvent.clientX;
	var diffY = initTouchX-targetEvent.clientY;
	
	if(_slideLock == false){
		_slideLock = true;
		unLockSlide.delay(0.5);
		if(diffX>10){		
			_fullscreen.next();
		}
		if(diffX<-10){	
			_fullscreen.previous();
		}
	}	
	return false;
}


function unLockSlide(){
	_slideLock = false;
}

function onActivate0Complete(){

	_menu0.removeEvent('expandComplete', onActivate0Complete);
	var arr = SWFAddress.getPathNames()[1].split('|');
	if(arr.length>0){
		
		_menu1.addEvent('expandComplete', onActivate1Complete);
		_menu1.forceActivate(arr[0]+"|"+arr[1]);
	}
}

function onActivate1Complete(){

	_menu1.removeEvent('expandComplete', onActivate1Complete);
	var arr = SWFAddress.getPathNames()[1].split('|');
	if(arr.length>1){
		
		_menu2.forceActivate(arr[0]+"|"+arr[1]+"|"+arr[2]);
	}
}

function hideIntro(){
	
	$("intro").set("tween",{duration : 600, onComplete : onHideComplete.bind(this)}).tween('opacity', 0);	
}

function onHideComplete() {

	$("intro").destroy();
	$("layer").set("tween",{duration : 400}).tween('opacity', 0);
}


function sendContact(){

_log($('email_form').value);
_log($('message_form').value);

_log(window.location.href);
  
  var req = new Request.HTML({
    url: "http://www.jeanbedez.com/_inc/ajax_contact.php",
    method: 'post',
        encoding: 'uft-8',                  
    onSuccess: function(data) {                             
      
      onPartenaireAjx(data);      
    }, 
    onFailure: function() {
      
      _log('ajax error'); 
    }       
  }).post({'email': $('email_form').value, 'message': $('message_form').value});    
  req.send();
}

function onPartenaireAjx(response){
  
  $('view').empty();
  $('view').adopt(response);
}

function loadPage(pStr) {
	
	_log('loadPage');
	
	$('viewer').setStyle('display', 'block');
	
	_current = pStr;
	
	$('loading').set("tween", { duration: 0}).tween("opacity", 1);
	$('view').set("tween", { duration: 100}).tween("opacity", 0);
	_log(pStr.type);
	if (pStr.type) {

		var req = new Request.HTML( {
			url : "http://www.jeanbedez.com/_inc/ajax_section.php",
			method : 'post',
			encoding : 'uft-8',
			onSuccess : function(data) {
				
				$('view').set("tween", { duration: 0}).tween("opacity", 0);
			
				$('tempContent').empty().adopt(data);
				_keylistener.scopeOnBody();
				_currentPage = pStr.href.replace(reg,"");
				try {
					SWFAddress.setValue(_currentPage);
				} catch (e) {}				
				trackPage($('ajax_title').firstChild.data);								
				document.title = $('ajax_title').firstChild.data;
				
				if($('ajax_description')!=null){
					
					$('nfo_detail').setProperty('html',$('ajax_description').get('html'));
				}
				if($('ajax_nfo_fr')!=null){
					
					$('nfo_fr').setProperty('html',$('ajax_nfo_fr').get('html'));
				}
				if($('ajax_nfo_en')!=null){
					
					$('nfo_en').setProperty('html',$('ajax_nfo_en').get('html'));
				}
				if($('ajax_media')!=null){
					
					var el = $('ajax_media').get('text');
					if(el.lastIndexOf(".pdf")>0){
						
						launchPdf(el);										
					}
				}
				if($('ajax_visu')){
					$('view').empty();
					var cl = $('ajax_visu').clone();
					cl.setProperties( {'id' : 'basicajax_visu'});
					$('view').adopt(cl);
					_slider = new Slider($('view'), $('arrow_left'), $('arrow_right'), $('nav_info'), $('tempContent'));					
				}
				$('loading').set("tween", { duration: 400}).tween("opacity", 0);
				$('tempContent').empty();
				$('view').set("tween", { duration: 800}).tween("opacity", 1);
			},
			onFailure : function() {

				_log("ajax error");
			}
		}).post( {
			'section' : pStr.type,
			'ratio': '0'
		});
		req.send();		
	}
}

function launchPdf(pEl){
	
	pop = window.open(pEl,'_blank','toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,copyhistory=0,menuBar=0,width=640,height=480');
	if(pop==null){
		
		alert('Pour consulter le fichier pdf, veuillez autoriser l\'ouverture de la fenetre');
	}
}

function loadFullScreenPage() {
	
	_log('loadFullScreenPage');
	
	pStr = _current;
	$('view').set("tween", { duration: 100}).tween("opacity", 0);
	$('fullscreenviewer').set("tween", { duration: 0}).tween("opacity", 1);
	//if (pStr.type) {

		var req = new Request.HTML( {
			url : "http://www.jeanbedez.com/_inc/ajax_section.php",
			method : 'post',
			encoding : 'uft-8',
			onSuccess : function(data) {
				$('tempContent').empty().adopt(data);
				_keylistener.scopeOnFullScreen();
				_currentPage = pStr.href.replace(reg,"");
				try {
					SWFAddress.setValue(_currentPage);
				} catch (e) {}				
				trackPage($('ajax_title').firstChild.data);								
				document.title = $('ajax_title').firstChild.data;		
				if($('ajax_visu')){
					$('fullscreenviewer').set("tween", { duration: 0}).tween("opacity", 0);				
					$('viewfullscreen').empty();					
					var cl = $('ajax_visu').clone();
					cl.setProperties( {'id' : 'fullscreenajax_visu'});
					$('viewfullscreen').adopt(cl);
					_fullscreen = new FullScreenView($('fullscreenviewer'), $('fullscreen_arrow_left'), $('fullscreen_arrow_right'), $('fullscreen_nav_info'), $('tempContent'));								
					_fullscreen.reveal();
				}
				$('fullscreenviewer').set("tween", { duration: 600}).tween("opacity", 1);	
				$('loading').set("tween", { duration: 400}).tween("opacity", 0);
				$('tempContent').empty();
			},
			onFailure : function() {

				_log("ajax error");
			}
		}).post( {
			'section' : pStr.type,
			'ratio': '1'
		});
		req.send();		
	//}
}

function trackPage(str){

	_gaq.push(['_setAccount', 'UA-21378358-1']);
	_gaq.push(['_trackPageview'], str);	
}

function _log(el) {
	try {
		console.log(el);
	} catch (e) {}
}

/*
 * 
 * 
 * Menu
 * 
 * 
 */

var _onColor = '#000000';
var _offColor = '#CCCCCC';
var _menuWidth = [ 120, 120, 120 ];
var _currentMenu = 0;

var Menu = new Class({

	Implements : [ Events, Options ],

	options : {
		id : null,
		el : null,
		nextEl : null,
		ref : null
	},

	initialize : function(pEl, pNextEl, pId) {

		this.options.el = pEl;
		this.options.el.menu = this;
		this.options.nextEl = pNextEl;
		this.options.id = pId;

		var ref = this;
		this.options.ref = ref;
	},

	clear : function() {

		this.options.el.getElement('ul').destroy();
		this.options.el.adopt(new Element('ul').setProperties( {'class' : 'menuContainer'}));
	},
	
	forceActivate: function(pId){

		var ref = this;
		this.options.el.getElements('a').each(function(el) {

			if (el.type == pId) {

				ref.onClick(el);				
			}
		});
	},

	feed : function(pNode) {

		var ref = this;

		this.options.el.getElement('ul').destroy();

		this.options.el.adopt(new Element('ul').setProperties( {'class' : 'menuContainer'}));

		pNode.getChildren('a').each(
				function(el) {

					var cl = el.clone();
					cl.setProperties( {'class' : 'link'});
					cl.setStyle('opacity', 0);
					ref.options.el.getElement('ul').adopt(new Element('li').adopt(cl));
				});
		this.update();
	},

	update : function() {

		var ref = this;
		var i = 0;
		this.options.el.getElements('a').each(function(el) {

			el.index = i;

			el.addEvents( {
				'mouseout' : function(e) {
					e.stop();
					ref.onMouseOut(el);
				}.bind(ref),
				'mouseover' : function(e) {
					e.stop();
					ref.onMouseOver(el);
				}.bind(ref),
				'click' : function(e) {
					e.stop();
					ref.onClick(el);
				}.bind(ref)
			});
			(function() {
				el.set('tween', {duration : 400}).tween('opacity', 1);
			}).delay(parseInt(100 * i));

			i++;
		});
	},

	onMouseOut : function(el) {

		this.options.el.getElements('a').each(function(e) {
			if (e.get('class') != "link_on") {

				e.set('tween', {duration : 400}).tween('color', _offColor);
				e.setStyle('opacity', 1);
			}
		});
	},

	onMouseOver : function(e) {

		if (e.get('class') != "link_on") {

			e.set('tween', {duration : 200}).tween('color', _onColor);
			e.setStyle('opacity', 1);
		}
	},

	onClick : function(el) {
		
		if(this.options.id == 0){
			
			_menu2.resetBeforeFeed();
		}

		this.options.el.getElements('li').each(function(e) {

			e.set('class','link');
		});
		this.options.el.getElements('a').each(function(e) {

			e.set('class','link');
		});
		el.set('class','link_on');
		el.getParent().set('class','link_on');
		_currentMenu = this.options.id;
		this.onMouseOut();
		this.collapse();
	},

	expand : function() {

		if (this.options.nextEl != null) {
			
			this.onExpandComplete();
			//this.options.nextEl.set("tween",{transition : Fx.Transitions.Circ.easeOut, duration : 600, onComplete : this.onExpandComplete.bind(this.options.ref)}).tween('margin-left', parseInt(_menuWidth[this.options.id + 1]*parseInt(this.options.id + 1)) + 'px');
		}
	},
	
	resetBeforeFeed : function(){
		
		var i = this.options.el.getElements('li').length;
		_log(i);
		this.options.el.getElements('li').each(function(e) {

			(function() {
				e.set('tween', {duration : 200, onComplete:e.destroy()}).tween('opacity', 0);
			}).delay(parseInt(50 * i));
			i--;			
		});
	},

	onExpandComplete : function() {

		this.fireEvent('expandComplete', null, 10);
	},

	collapse : function() {

		if (this.options.nextEl != null) {
			

			this.options.nextEl.menu.resetBeforeFeed();
	
			this.onCollapseComplete.bind(this.options.ref).delay(1000);

		}else{
			
			loadPage($('menuFeed').getElement('ul').getChildren('li')[_menu0.getIndex()].getElement('ul').getChildren('li')[_menu1.getIndex()].getElement('ul').getChildren('li')[this.getIndex()].getElement('a'));
		}
	},

	onCollapseComplete : function() {

		if (this.options.nextEl != null) {

			if (this.options.id == 0) {

				if ($('menuFeed').getElement('ul').getChildren('li')[this.getIndex()].getElement('ul')) {
					
					if ($('menuFeed').getElement('ul').getChildren('li')[this.getIndex()].getElement('ul').getChildren('li')) {

						this.options.nextEl.menu.feed($('menuFeed').getElement('ul').getChildren('li')[this.getIndex()].getElement('ul').getChildren('li'));
						this.expand();
					} else {

						this.options.nextEl.menu.clear();
						loadPage($('menuFeed').getElement('ul').getChildren('li')[this.getIndex()].getElement('a'));
					}
				} else {

					this.options.nextEl.menu.clear();
					loadPage($('menuFeed').getElement('ul').getChildren('li')[this.getIndex()].getElement('a'));
				}
			}
			if (this.options.id == 2) {

				loadPage($('menuFeed').getElement('ul').getChildren('li')[_menu0.getIndex()].getElement('ul').getChildren('li')[_menu1.getIndex()].getElement('ul').getChildren('li')[this.getIndex()].getElement('a'));
			}
			if (this.options.id == 1) {

				if ($('menuFeed').getElement('ul').getChildren('li')[_menu0.getIndex()].getElement('ul').getChildren('li')[this.getIndex()].getElement('ul')) {
					
					if ($('menuFeed').getElement('ul').getChildren('li')[_menu0.getIndex()].getElement('ul').getChildren('li')[this.getIndex()].getElement('ul').getChildren('li')) {

						this.options.nextEl.menu.feed($('menuFeed').getElement('ul').getChildren('li')[_menu0.getIndex()].getElement('ul').getChildren('li')[this.getIndex()].getElement('ul').getChildren('li'));
						this.expand();
					} else {

						this.options.nextEl.menu.clear();
						loadPage($('menuFeed').getElement('ul').getChildren('li')[_menu0.getIndex()].getElement('ul').getChildren('li')[this.getIndex()].getElement('a'));
					}
				} else {

					this.options.nextEl.menu.clear();
					loadPage($('menuFeed').getElement('ul').getChildren('li')[_menu0.getIndex()].getElement('ul').getChildren('li')[this.getIndex()].getElement('a'));
				}
			}
		}
	},

	getTotal : function() {

		return this.options.el.getElements('a').length;
	},

	getIndex : function() {

		var ref = this;
		var ret;
		var i = 0;
		this.options.el.getElements('a').each(function(el) {

			if (el.get('class') == 'link_on') {

				ret = i;
			}
			i++;
		});
		return ret;
	},

	previous : function() {

		var id;

		if (this.getIndex() > 0) {

			id = this.getIndex() - 1;
		} else {

			id = this.getTotal() - 1;
		}

		return this.options.el.getElements('a')[id];
	},

	next : function() {

		var id;

		if ((this.getIndex() + 1) < this.getTotal()) {

			id = this.getIndex() + 1;
		} else {

			id = 0;
		}

		return this.options.el.getElements('a')[id];
	},

	getLink : function(id) {

		return this.option.sc.getLink(id);
	}
});

/*
 * 
 * 
 * keylistener
 * 
 * 
 */

var Keylistener = new Class( {

	Implements : [ Events, Options ],

	options : {

		doc : null,
		menus : null,
		scope: null
	},

	initialize : function(pMenus) {

		var ref = this;

	  	this.options.menus = pMenus;
		this.options.scope = "menu";
		
		$(window).addEvents({
			
            keydown : function(e){

				var keys = ['left','right','up','down','escape'];
				var key = e.key;
				if(keys.indexOf(key) > -1)e.stop();
					
				if(key == 'up'){
					
					ref.doUp();
					
				}else if(key == 'down'){
					
					ref.doDown();
				
				}else if(key == 'left'){
					
					ref.doLeft();
					
				}else if(key == 'right'){
					
					ref.doRight();
					
				}else if(key == 'escape'){
					
					ref.doEscape();
				}
			}
		});
	},
	
	doUp : function(){
		
		var el = this.options.menus[_currentMenu].previous();
		this.options.menus[_currentMenu].onMouseOver(el);
		this.options.menus[_currentMenu].onClick(el);
		this.scopeOnMenu();	
	},
	
	doDown : function(){
		
		var el = this.options.menus[_currentMenu].next();
		this.options.menus[_currentMenu].onMouseOver(el);
		this.options.menus[_currentMenu].onClick(el);
		this.scopeOnMenu();
	},
	
	doLeft : function(){
		
		_log('doleft');
		
		if(this.options.scope =="body"){
			if(_slider.options.index>0){
				_log('_slider.previous');
				_slider.previous();
			}else{
				_log('force left');
				this.scopeOnMenu();
				this.doLeft();
			}
		}else if(this.options.scope =="fullscreen"){
			_log('_fullscreen.previous');
			_fullscreen.previous();
		}else{
			_log('_currentMenu');
			_log(_currentMenu);
			if (parseInt(_currentMenu) > 0) {
		
				_currentMenu--;
				_log('_currentMenu--')
				_log(_currentMenu);
				var el = this.options.menus[_currentMenu].next();
				this.options.menus[_currentMenu].onMouseOver(el);
				this.options.menus[_currentMenu].onClick(el);
			}
		}
	},
	
	doRight : function(){

		if(this.options.scope =="body"){
			
			_slider.next();
		}else if(this.options.scope =="fullscreen"){
			
			_fullscreen.next();
		}else{
			if (parseInt(_currentMenu + 1) < this.options.menus.length) {
		
				if (this.options.menus[_currentMenu + 1].getTotal() > 0) {
					_currentMenu++;
					var el = this.options.menus[_currentMenu].next();
					this.options.menus[_currentMenu].onMouseOver(el);
					this.options.menus[_currentMenu].onClick(el);
				}
			}
		}	
	},
	
	doEscape : function(){
		
		_fullscreen.hide();
	},
	
	scopeOnBody: function(){
		
		this.options.scope = "body";
	},
	
	scopeOnMenu: function(){
		
		this.options.scope = "menu";
	},
	
	scopeOnFullScreen: function(){
		
		this.options.scope = "fullscreen";
	}
});

/*
 * 
 * 
 * slider
 * 
 * 
 */

var Slider = new Class( {

	Implements : [ Events, Options ],

	options : {

		el : null,
		left : null,
		right : null,
		nfo : null,
		feedEl : null,
		index: null,
		currentImg: null
	},

	initialize : function(pEl, pLeft, pRight, pNfo, pFeedEl) {

		this.options.view = pEl;		
		this.options.left = pLeft;
		this.options.right = pRight;
		this.options.feedEl = pFeedEl;
		this.options.nfo = pNfo;
		this.options.index = 0;	
		
		var ref = this;
		
		this.options.left.set('tween', {duration : 400}).tween('opacity', 0.4);
		this.options.right.set('tween', {duration : 400}).tween('opacity', 0.4);
		
		if(this.options.view!=null){
			this.options.view.addEvent('mousewheel', ref.onMouseWheel);
		}
		
		this.options.left.addEvents( {
			'mouseout' : function(e) {
				e.stop();
				ref.onLMouseOut(ref.options.left);
			}.bind(ref),
			'mouseover' : function(e) {
				e.stop();
				ref.onLMouseOver(ref.options.left);
			}.bind(ref),
			'click' : function(e) {
				e.stop();
				ref.previous();
			}.bind(ref)
		});
		
		this.options.right.addEvents( {
			'mouseout' : function(e) {
				e.stop();
				ref.onRMouseOut(ref.options.right);
			}.bind(ref),
			'mouseover' : function(e) {
				e.stop();
				ref.onRMouseOver(ref.options.right);
			}.bind(ref),
			'click' : function(e) {
				e.stop();
				ref.next();
			}.bind(ref)
		});
		
		this.manageElement();
	},
	
	onMouseWheel: function(e){
		
		e.stop();
		if (e.wheel < 0){

			_slider.previous();
		} else {		
			
			_slider.next();
		}
	},
	
	onLMouseOut : function(el) {
		
		if(this.options.index>0){
			el.set('tween', {duration : 400}).tween('opacity', 0.4);
		}
	},

	onLMouseOver : function(el) {
		
		if(this.options.index>0){
			
			el.set('tween', {duration : 400}).tween('opacity', 1);
		}
	},
	
	onRMouseOut : function(el) {
		
		if((this.options.index+1)<$("view").getElements('li').length){
			el.set('tween', {duration : 400}).tween('opacity', 0.4);
		}
	},

	onRMouseOver : function(el) {
		
		if((this.options.index+1)<$("view").getElements('li').length){
			el.set('tween', {duration : 400}).tween('opacity', 1);
		}
	},
	
	previous : function(){
		
		if(this.options.index>0){
			
			this.options.index--;
			this.manageElement();			
		}
	},
		
	next : function(){

		if((this.options.index+1)<$("view").getElements('li').length){
			
			this.options.index++;
			this.manageElement();			
		}
	},
		
	manageElement : function(){

		var ref = this;
		
		if(this.options.index<1){
			this.options.left.set('tween', {duration : 400}).tween('opacity', 0);
			$('nav_left').set('tween', {duration : 400}).tween('opacity', 0);
		}else{
			this.options.left.set('tween', {duration : 400}).tween('opacity', 0.4);	
			$('nav_left').set('tween', {duration : 400}).tween('opacity', 1);
		}
		if(this.options.index+1<$("view").getElements('li').length){
			this.options.right.set('tween', {duration : 400}).tween('opacity', 0.4);
			$('nav_right').set('tween', {duration : 400}).tween('opacity', 1);
		}else{
			this.options.right.set('tween', {duration : 400}).tween('opacity', 0);
			$('nav_right').set('tween', {duration : 400}).tween('opacity', 0);
		}
		
		var el = $('view').getElements('li')[this.options.index];
		
		$('view').getElements('li').each(function(item, index){
			
			if(item == el){

				$(item).set("tween", { duration: 400}).tween("opacity", 1);	
			}else{

				$(item).set("tween", { duration: 400}).tween("opacity", 0);
			}
		});
		
		if($('view').getElements('li').length>1){
		
			$(this.options.nfo).set('text', parseInt(this.options.index+1)+"/"+parseInt($("view").getElements('li').length));
		}else{
			$(this.options.nfo).set('text', '');
		}		
	}		
});


/*
 * 
 * 
 * FullScreenView
 * 
 * 
 */

var FullScreenView = new Class( {

	Implements : [ Events, Options ],

	options : {

		el : null,
		left : null,
		right : null,
		nfo : null,
		feedEl : null,
		index: null,
		currentImg: null
	},

	initialize : function(pEl, pLeft, pRight, pNfo, pFeedEl) {

		this.options.view = pEl;		
		this.options.left = pLeft;
		this.options.right = pRight;
		this.options.feedEl = pFeedEl;
		this.options.nfo = pNfo;
		this.options.index = 0;	
		
		var ref = this;
		
		this.options.left.set('tween', {duration : 400}).tween('opacity', 0.4);
		this.options.right.set('tween', {duration : 400}).tween('opacity', 0.4);
		
		_log('__fullscreen');
		_log(this.options.view);
		
		if(this.options.view!=null){
			this.options.view.addEvent('mousewheel', ref.onMouseWheel);
		}
				
		$('fullscreen_close').addEvents( {
			'click' : function(e) {
				e.stop();
				ref.hide();
			}.bind(ref)
		});
		
		this.options.left.addEvents( {
			'mouseout' : function(e) {
				e.stop();
				ref.onLMouseOut(ref.options.left);
			}.bind(ref),
			'mouseover' : function(e) {
				e.stop();
				ref.onLMouseOver(ref.options.left);
			}.bind(ref),
			'click' : function(e) {
				e.stop();
				ref.previous();
			}.bind(ref)
		});
		
		this.options.right.addEvents( {
			'mouseout' : function(e) {
				e.stop();
				ref.onRMouseOut(ref.options.right);
			}.bind(ref),
			'mouseover' : function(e) {
				e.stop();
				ref.onRMouseOver(ref.options.right);
			}.bind(ref),
			'click' : function(e) {
				e.stop();
				ref.next();
			}.bind(ref)
		});
		
		var fx0 = new Fx.Morph(ref.options.left, {duration : 0}).start( {'top' : [ (document.body.getSize().y/2) ]});
		var fx1 = new Fx.Morph(ref.options.right, {duration : 0}).start( {'top' : [ (document.body.getSize().y/2) ]});
		
		this.manageElement();
	},
	
	onMouseWheel: function(e){
		
		_log('fullscreen, onMouseWheel');
		
		e.stop();
		if (e.wheel < 0){

			_fullscreen.previous();
		} else {		
			
			_fullscreen.next();
		}
	},
	
	onLMouseOut : function(el) {
		
		if(this.options.index>0){
			el.set('tween', {duration : 400}).tween('opacity', 0.2);
		}
	},

	onLMouseOver : function(el) {
		
		if(this.options.index>0){
			
			el.set('tween', {duration : 400}).tween('opacity', 0.6);
		}
	},
	
	onRMouseOut : function(el) {
		
		if((this.options.index+1)<$("viewfullscreen").getElements('li').length){
			el.set('tween', {duration : 400}).tween('opacity', 0.2);
		}
	},

	onRMouseOver : function(el) {
		
		if((this.options.index+1)<$("viewfullscreen").getElements('li').length){
			el.set('tween', {duration : 400}).tween('opacity', 0.6);
		}
	},
	
	previous : function(){
		
		if(this.options.index>0){
			
			this.options.index--;
			this.manageElement();			
		}
	},
		
	next : function(){

		if((this.options.index+1)<$("viewfullscreen").getElements('li').length){
			
			this.options.index++;
			this.manageElement();			
		}
	},
		
	manageElement : function(){

		var ref = this;
		
		if(this.options.index<1){
			this.options.left.set('tween', {duration : 400}).tween('opacity', 0);
			$('nav_left').set('tween', {duration : 400}).tween('opacity', 0);
		}else{
			this.options.left.set('tween', {duration : 400}).tween('opacity', 0.4);	
			$('nav_left').set('tween', {duration : 400}).tween('opacity', 1);
		}
		if(this.options.index+1<$("viewfullscreen").getElements('li').length){
			this.options.right.set('tween', {duration : 400}).tween('opacity', 0.4);
			$('nav_right').set('tween', {duration : 400}).tween('opacity', 1);
		}else{
			this.options.right.set('tween', {duration : 400}).tween('opacity', 0);
			$('nav_right').set('tween', {duration : 400}).tween('opacity', 0);
		}
		
		var el = $('viewfullscreen').getElements('li')[this.options.index];
		
		$('viewfullscreen').getElements('li').each(function(item, index){
			
			if(item == el){

				$(item).set("tween", { duration: 400}).tween("opacity", 1);	
			}else{

				$(item).set("tween", { duration: 400}).tween("opacity", 0);
			}
		});	
		if($('viewfullscreen').getElements('li').length>1){
		
			$(this.options.nfo).set('text', parseInt(this.options.index+1)+"/"+parseInt($("viewfullscreen").getElements('li').length));	
		}else{
			$(this.options.nfo).set('text',''); 	
		}	
	},
	
	reveal: function(){
		
		_log("reveal ");
		this.options.hided = false;
		
		_keylistener.scopeOnFullScreen();
		
		$('fullscreenview').setStyle('opacity',0);
		$('fullscreenview').setStyle('display','block');
		$('fullscreenview').set("tween", { duration: 400}).tween("opacity", 1);	
		

		$('fullscreen_close').store('tip:title', "fermer / close");
		$('fullscreen_close').store('tip:text', "fermer / close");
		_tip2 = new Tips($('fullscreen_close'), {
			showDelay: 0,
			hideDelay: 0,
			offsets: {x: 10, y: 10},
			fixed: false
		});
		
	},
	
	hide: function(){
		
		_log("hide ");		
		this.options.hided = true;
		
		_keylistener.scopeOnBody();
		
		$('fullscreenview').set("tween", { duration: 400, onComplete:this.remove.bind(this)}).tween("opacity", 0);					
	},
	
	remove: function(){
					
		$('fullscreenview').setStyle('opacity',0);
		$('fullscreenview').setStyle('display','none');			
	}	
});


