var lk=false;
var opened='';
/*
function slide(layer){
	if(opened != layer && opened != '' && lk!=true){
		Effect.SlideUp(opened,{duration:0.5,afterSetup:function(){document.getElementById("mov").style.visibility='hidden';}});
		Effect.SlideDown(layer,{duration:0.5,afterFinishInternal:function(){document.getElementById("mov").style.visibility='visible';}});
		opened=layer;
	}else if(opened ==''){
		Effect.SlideDown(layer,{duration:0.5,afterFinishInternal:function(){document.getElementById("mov").style.visibility='visible';}});
		opened=layer;
	}else if(opened == layer && lk!=true){
		Effect.SlideUp(layer,{duration:0.5,afterSetup:function(){document.getElementById("mov").style.visibility='hidden';}});
		opened='';
	}
}
function stay(layer){
	Effect.SlideDown(layer,{duration:0,afterFinishInternal:function(){document.getElementById("mov").style.visibility='visible';}});
	opened=layer;
	}
*/
function setlink(){
	lk=true;
}
function setunlink(){
	lk=false;
}
function setColor(layer,color){
	layer.style.backgroundColor=color;
}

/*
FONCTIONS POUR MEMORISER LE SCROLL
*/
function SaveScrollXY() {
   var mapositiony=window.pageYOffset;//document.body.scrollTop;
   EcrireCookie("positiony", mapositiony);	 
}
function ResetScrollPosition() {
    var hidx, hidy;
	hidx = 0;
    hidy = LireCookie("positiony");
	
    if (typeof hidx != 'undefined' && typeof hidy != 'undefined') {
      window.scrollTo(hidx, hidy);
    }
}
function EcrireCookie(nom, valeur)
{
   var argv=EcrireCookie.arguments;
   var argc=EcrireCookie.arguments.length;
   var expires=(argc > 2) ? argv[2] : null;
   var path=(argc > 3) ? argv[3] : null;
   var domain=(argc > 4) ? argv[4] : null;
   var secure=(argc > 5) ? argv[5] : false;
   document.cookie=nom+"="+escape(valeur)+
      ((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
      ((path==null) ? "" : ("; path="+path))+
      ((domain==null) ? "" : ("; domain="+domain))+
      ((secure==true) ? "; secure" : "");
}
function getCookieVal(offset)
{
   var endstr=document.cookie.indexOf (";", offset);
   if (endstr==-1) endstr=document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom)
{
   var arg=nom+"=";
   var alen=arg.length;
   var clen=document.cookie.length;
   var i=0;
   while (i<clen)
   {
      var j=i+alen;
      if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
      i=document.cookie.indexOf(" ",i)+1;
      if (i==0) break;
   }
   return null;
}

function vimeoIpadEmbed(myClip_id, myWidth, myHeight){
	
	var agent=navigator.userAgent.toLowerCase();
	var is_iphone = (agent.indexOf('iphone')!='-1');
	var is_ipad = (agent.indexOf('ipad')!='-1');
	
	if (is_iphone) {
	$('box').innerHTML = '<video src=\"http://www.vimeo.com/play_redirect?clip_id=' + myClip_id + '&quality=mobile\" controls=\'controls\' width=\''+ myWidth + ' \' height=\'' + myHeight + '\'></video>';
	} 
	else if (is_ipad) {
	$('box').innerHTML = '<video src=\"http://www.vimeo.com/play_redirect?clip_id=' + myClip_id + '\" controls=\'controls\' width=\''+ myWidth + ' \' height=\'' + myHeight + '\'></video>';
	} 
	else {
	//document.write('<object width=\'' + myWidth + '\' height=\'' + myHeight + '\'><param name=\'allowfullscreen\' value=\'true\' /><param name=\'allowscriptaccess\' value=\'always\' /><param name=\'movie\' value=\"http://vimeo.com/moogaloop.swf?clip_id=' + myClip_id + '&server=vimeo.com&show_title=0&show_byline=0&show_portrait=0&color=ffffff&fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=' + myClip_id + '&server=vimeo.com&show_title=0&show_byline=0&show_portrait=0&color=ffffff&fullscreen=1\" type=\'application/x-shockwave-flash\' allowfullscreen=\'true\' allowscriptaccess=\'always\' width=\'' + myWidth + '\' height=\'' + myHeight + '\'></embed></object>');
	$('box').innerHTML = '<object width=\'' + myWidth + '\' height=\'' + myHeight + '\'><param name=\'allowfullscreen\' value=\'true\' /><param name=\'allowscriptaccess\' value=\'always\' /><param name=\'movie\' value=\"http://vimeo.com/moogaloop.swf?clip_id=' + myClip_id + '&server=vimeo.com&show_title=0&show_byline=0&show_portrait=0&color=ffffff&fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=' + myClip_id + '&server=vimeo.com&show_title=0&show_byline=0&show_portrait=0&color=ffffff&fullscreen=1\" type=\'application/x-shockwave-flash\' allowfullscreen=\'true\' allowscriptaccess=\'always\' width=\'' + myWidth + '\' height=\'' + myHeight + '\'></embed></object>';
	}
	
	$('box').addEvent('mousedown',function(e){	
				//e.stop();
				lk = true;
	});
	
	$('box').addEvent('mouseup',function(e){	
				//e.stop();
				lk = false;
	});
	
	$('box').addEvent('mouseleave',function(e){	
				//e.stop();
				lk = false;
	});
	

}



/*******************************************************************************************************************************************************************************/

	function slide(el){
		if(lk !=true){
			/*********************************************************************************************************************************
			// ************* variante s'ouvre d'abord, et se ferme ensuite
			el.toggle().chain(function(){
				
				$$( '.bloc' ).each(function(item){
					if(el["subject"].id !=item.getElement( '.opener' ).id){
						  new Fx.Slide( item.getElement( '.opener' )).slideOut();
					}
				});
			});
			*/
			//-----------------------------------------------------------------------------------------------------------------------------------------------------------
			//*********************************************************************************************************************************
			// ************* variante s'ouvre se ferme simultanément
			el.toggle().chain(function(){
				//e.stop();
				//alert(el["subject"].id);	
				if(el["subject"].id =='d5' && el.open == true){
					new Fx.Morph('bande3',{duration: 'long', transition: Fx.Transitions.Sine.easeOut}).start({'background-color':'#333','color':'#fff'});
				}
				if(el["subject"].id =='d5' && el.open == false){
					new Fx.Morph('bande3',{duration: 'short', transition: Fx.Transitions.Sine.easeOut}).start({'background-color':color_game[3],'color':'#fff'});
				}
				
			});
			$$( '.bloc' ).each(function(item){
					if(el["subject"].id !=item.getElement( '.opener' ).id){
						  new Fx.Slide( item.getElement( '.opener' )).slideOut().chain(function(){
							if(item.getElement( '.opener' ).id =='d5'){
								new Fx.Morph('bande3',{duration: 'short', transition: Fx.Transitions.Sine.easeOut}).start({'background-color':color_game[3],'color':'#fff'});
							}
							});
					}
			});
		}
	}
	
	function loadComment(div,imageID){
		var options = {
						url: 'ether_comment.php?ether_id='+imageID, 	
						method : 'get', 
						update: $(div),
						evalScripts: true,					
						onRequest: function() {	
							$(div).innerHTML='<img src="gfx/loadinfo.gif" width="16px" height="16px" border="0" alt="loading" style="width:16px;height:16px;margin:5px;">';
						},									
						onSuccess: function() {	
						}
					};									
		var monObjetAjax= new Request.HTML(options);
		monObjetAjax.send();
	}
	
	var domReady={
	start:function(){
		domReady.interaction();
		domReady.launchThumb();
		domReady.launchViewer();
	},
	
	interaction:function(){
		var colorNumber = 0
		$$( '.bloc' ).each(function(item){
			  var thisSlider = new Fx.Slide( item.getElement( '.opener' ));
			  //pour laisser ouvert la bande
			  if(item.getElement( '.opener' ).id!=openBox){
			 	 thisSlider.hide();
				}
			  item.addEvent( 'mousedown', function(){
					slide(thisSlider);
				});
			  item.addEvent('mouseover',function(){
					if(item.id != 'bande3' || thisSlider.open != true){
						setColor(this,color_game[this.id.charAt(this.id.length-1)-1]);	
					}
				});
			  item.addEvent('mouseout',function(){
					if(item.id != 'bande3' || thisSlider.open != true){
					//	alert(this.id.charAt(this.id.length-1));
						setColor(this,color_game[this.id.charAt(this.id.length-1)]);	
					}
				});
			  colorNumber++;
		 } );
	},
	
	launchViewer:function(){
			var Vr = new viewer($$('#box img)'),{
						modes:['alpha'],
						fxOptions: {duration:300},
						onWalk:function(current_index){
							lk=false;
							//alert($$('#box img)')[current_index].id);
							loadComment($$('#box img)')[current_index].id);
							}
						});
			$('box').addEvent('mousedown',function(e){	
				e.stop();
				lk = true;
				Vr.next();
			});
	},
	
	launchThumb:function(){
			var ref = $$('#all img');
			ref.fade(0.3);
			ref[0].fade('in');
			ref.addEvent('mousedown',function(e){
				e.stop();	
				ref.fade(0.3);
				var thistitle = this.title;
				lk = true;
				var options = {
						url: 'ether.php?month='+this.alt, 	
						method : 'get', 
						update: $('pages_menu'),
						evalScripts: true,					
						onRequest: function() {	
							$('pages_menu').innerHTML='<img src="gfx/loadinfo.gif" width="16px" height="16px" border="0" alt="loading" style="width:16px;height:16px;margin:5px;">';
						},									
						onSuccess: function() {	
							lk=false;
							domReady.launchViewer();
							ref[parseFloat(thistitle)].fade('in');
						}
					};									
				var monObjetAjax= new Request.HTML(options);
				monObjetAjax.send();
			 })
		}
};
