var pold = 0, pnew = 0, penter = 0, interv = {}, boolp = true;
var current = {}, load = {};

window.onload = function() {
	document.body.style.overflow = 'hidden';
	scroll.init(document.getElementById('miniatures'), document.getElementById('slider'), 'img');
	scroll.clickmin(document.getElementById('slider'), 'img');
	menu.init();

};
var scroll = {
	init : function(cache, slider, typemin){
		var t = {
			allchilds : listChildByType(slider, typemin),
			distbord : 50 
			
		};
		t['unitwidth'] = t.allchilds[0].offsetWidth;
		t['unitspace'] = returnstyle(t.allchilds[0], 'marginLeft');// internet explorer bug .... 
		t['widthall'] = t.allchilds.length * (t.unitwidth + t.unitspace) + (t.unitspace*2);
		cache.style.overflow = 'hidden';
		if(document.body.offsetWidth > t.widthall){
			slider.style.width = document.body.offsetWidth-1+'px';
			var marginUnit = (document.body.offsetWidth-(t.allchilds.length*t.unitwidth)-15)/(t.allchilds.length-1);
			for(i=1, j=t.allchilds.length; i<j; i++){
				t.allchilds[i].style.marginLeft = marginUnit+'px';					
			}
		}else{
			slider.style.width = t.widthall+'px';
		} 
		addEvent(cache, 'mouseover', function(e){  penter = e.clientX; boolp = true;  });
		addEvent(cache, 'mouseout', function(e){  if( !e.relatedTarget || ((e.relatedTarget.id != 'slider') && (e.relatedTarget.id != 'cache') && (e.relatedTarget.nodeName.toLowerCase() != 'img'))){ clearInterval(interv); boolp = false;   } });
		addEvent(cache, 'mousemove', function(e){
			if(boolp && document.body.offsetWidth < slider.offsetWidth){
			pnew = e.clientX - penter;
			var ratiocurscreen = (pnew)/document.body.offsetWidth;
			var left = slider.offsetLeft - (ratiocurscreen * t.widthall)/10; // pour ralentir / par un nombre
			if(left > 0){ left = 0; }
			if(left < document.body.offsetWidth - t.widthall){ left = document.body.offsetWidth - t.widthall ;  }
			//alert(ratiocurscreen+' '+t.widthall+' '+slider.offsetLeft+' '+pnew+' '+penter);
			slider.style.left = (left < 0) ? '-'+Math.abs(left)+'px' : left+'px';
			
			if(e.clientX < t.distbord && left < 0){
				clearInterval(interv);
				interv = setInterval((function(cursor){ return function(){ moveBords(slider, true, 0,  cursor); }; })(e.clientY), 20);
			}
			else if( e.clientX > document.body.offsetWidth-t.distbord && left > document.body.offsetWidth - t.widthall ){
				clearInterval(interv);
				interv = setInterval((function(cursor){ return function(){ moveBords(slider, false, document.body.offsetWidth - t.widthall, cursor); }; })(e.clientY), 20);
			}
			else { 
				clearInterval(interv);

			  }
			}
		});	
		
		function moveBords (contain, dir, seuil, cursorTop){
			var left = contain.offsetLeft;
			if((dir && left < seuil) || (!dir && left > seuil)){
			var i = (dir) ? 10 : -10;
			contain.style.left = contain.offsetLeft+i+'px';
			}
		}
		current = t.allchilds[0];
		if(!base){
			Image.createImg(t.allchilds[0].className, document.getElementById('gd'));
		}
		Diapo.init(slider);
	},
	clickmin : function(slider, typemin){
		var childs = listChildByType(slider, typemin);
		for(var i = 0, j = childs.length; i<j; i++){
			addEvent(childs[i], 'click', function(e){
					
				current = e.srcElement || e.target;
				Image.createImg(current.className, document.getElementById('gd'));

			});

		}

	}
};



function addEvent (el, event, func) {
    if (el.attachEvent) {
      el.attachEvent('on'+event, func);
    } else {
      el.addEventListener(event, func, false);
    }
 };
function convert (str){ return parseInt(str.substr(0, (str).length-2)); };
function listChildByType (div, type){
	var a = div.childNodes;
	var b = new Array();
	for (var i=0, j=a.length ; i<j; i++ )
	{
		if(a[i].nodeName.toLowerCase() == type){
			b.push(a[i]);
		}		
	}
	return b;
	
}
function returnstyle(obj, attr){
		if (window.getComputedStyle) {  return convert(getComputedStyle(obj, null)[attr]); }
		else { return convert(obj.currentStyle[attr]); }
}

var Image = {
createImg : function (src , contain){

	var bordure = {
		larg : 45,
		color : '#fff'
	};
	slider = document.getElementById('slider');
	contain.style.backgroundColor = bordure.color;
	load = document.createElement('img');
	load.id= 'loadgd';
	load.src = 'vue/loader.gif';
	load.style.display = 'block';
	load.style.position = 'absolute';
	load.style.left = document.body.offsetWidth/2-16 -contain.offsetLeft+'px';
	var placeY = document.body.offsetHeight-(140+121); // 121 = #miniatures.width
	var marginleft = (80+2+127+10+127+2+30);
	var placeX = document.body.offsetWidth-marginleft;
	var maxHeight = 0;
	var maxWidth = 0;
	var ratio = 0;
	var a = document.createElement('img');
	a.id = 'gdimg';
	a.src = src;
	a.style.visibility = 'hidden';
	a.style.display = 'block';
	a.style.border = '1px solid #222';
	contain.innerHTML = '';
	contain.appendChild(load);
	contain.appendChild(a);
	//partie suppression de la desciption de base 
	if(base){
		var corps = document.getElementById('corps');
		corps.style.display = 'none';

	}
	//partie titre+tags 
	var mini2 = new Array();
	mini2 = listChildByType(slider, 'img');
	var miniclic = searchSrcInArray(mini2, src);
	if(miniclic[0].title != ''){
	var h6 = document.createElement('h6');
	h6.id ='imgtitle';
	h6.innerHTML = miniclic[0].title;
	contain.appendChild(h6);
	}
	var p = miniclic[0].alt;
	if(typeof tags[p] == 'object' ){
		var ptags = document.createElement('p');
		ptags.id='tags';
		contain.appendChild(ptags);
		for(i=0, j=tags[p].length; i<j; i++)
		{
			var tlien = document.createElement('a');
			tlien.href = "index.php?t="+tags[p][i];
			tlien.innerHTML = tags[p][i];
			ptags.appendChild(tlien);
			if(i != j-1){
			ptags.innerHTML += ', ';
			}
		}
	}
	addEvent(a, 'load', function(){
		maxHeight = a.offsetHeight;
		maxWidth = a.offsetWidth;
		placeY = document.body.offsetHeight-(140+121); // 121 = #miniatures.width		
		placeX = document.body.offsetWidth-marginleft- bordure.larg-10;
		ratio = maxWidth/maxHeight;
		
		a.style.height = (a.offsetHeight+2*bordure.larg > placeY) ? placeY-2*bordure.larg+'px' : a.offsetHeight+'px';
		if(a.offsetHeight*ratio< placeX){
		a.style.width = a.offsetHeight*ratio+'px'; 
		}
		else{
		a.style.width = (a.offsetWidth < maxWidth) ? placeX-2*bordure.larg+'px' : maxWidth+'px';
		a.style.height = a.offsetWidth/ratio+'px'; 
		}	
		contain.style.width = a.offsetWidth+bordure.larg+'px';
		contain.style.height = a.offsetHeight+bordure.larg+'px';
		contain.style.padding = bordure.larg+'px 0 0 '+bordure.larg+'px';
		var parleft1 = document.body.offsetWidth/2 -a.offsetWidth/2;
		var xleft = (parleft1 < marginleft) ? marginleft+'px' : parleft1+'px';
		contain.style.left = xleft;
		contain.removeChild(load);
		a.style.visibility = 'visible';
	});
	a.oncontextmenu = function() { return false;  }///////////////////////////////////////////////////anti clic droit ////////////////////////////////////////////////////////////////
	addEvent(window, 'resize', function(e){
		placeY = document.body.offsetHeight-(140+121); // 121 = #miniatures.width		
		placeX = document.body.offsetWidth-marginleft- bordure.larg-10;
		ratio = maxWidth/maxHeight;
		a.style.height = (a.offsetHeight <= maxHeight) ? placeY-2*bordure.larg+'px' : maxHeight+'px';
		if(a.offsetHeight*ratio< placeX){
		a.style.width = a.offsetHeight*ratio+'px'; 
		}
		else{
		a.style.width = (a.offsetWidth < maxWidth) ? placeX-2*bordure.larg+'px' : maxWidth+'px';
		a.style.height = a.offsetWidth/ratio+'px'; 
		}
		contain.style.width = a.offsetWidth+bordure.larg+'px';
		contain.style.height = a.offsetHeight+bordure.larg+'px';
		var parleft1 = document.body.offsetWidth/2 -a.offsetWidth/2;
		var xleft = (parleft1 < marginleft) ? marginleft+'px' : parleft1+'px';
		contain.style.left = xleft;
		
		//correction d'un bug sur le scrolleur
		var slid = document.getElementById('slider');
		if(document.body.offsetWidth < slid.offsetWidth){
			if ((document.body.offsetWidth-slid.offsetWidth) > slid.offsetLeft){
				slid.style.left = document.body.offsetWidth-slid.offsetWidth+'px';
			} 
		}
		 
		if(document.body.offsetWidth > slid.offsetWidth){
			slid.style.left = '0px';
		
		}
		//remise en place du bouton pour le diapo 
		var b = document.getElementById('diapbutton');
		button.style.top = document.body.offsetHeight - (40+121)+'px';
		button.style.left = document.body.offsetWidth - 40+'px';		
	});
}
}

