// JavaScript Document

/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
FONCTIONS
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/************ RESIZE ***********/			
function resize() {
	var currentSize = getViewportSize();
	if (currentSize[0] != g_prevSize[0] || currentSize[1] != g_prevSize[1]) {
		g_prevSize = currentSize;	
		resize_holder();
	}
}
/************ INIT RESIZE ***********/	
function init_resize() {		
	window.g_prevSize = getViewportSize();
	setInterval(resize, 100);
}
/************ RESIZE HOLDER ***********/	
function resize_holder() {
	var win_size = window.getSize();
	var win_x = win_size.x;
	var win_y = win_size.y;
	
	//NAVIGATION
	setNavigation();
}
/************ INIT HOLDER ***********/
function init_holder(nbSliders) {
	var win_size = window.getSize();
	var win_x = win_size.x;
	var win_y = win_size.y;

	for(var i=0; i<nbSliders; i++){
		var margin_coeff = parseInt($('img_left_'+i).getStyle('margin-left'));
		$('img_left_'+i).setStyle('left',(-1*win_x)+'px');
		$('img_center_'+i).setStyle('left','0px');
		$('img_right_'+i).setStyle('left',win_x+'px');
	}

	//NAVIGATION
	setNavigation();
}
/************ SHOW NEXT IMAGE ***********/
function showNextImage(array_images, num) {
	preloadSWF('on',$$('.catalog-03 .loading')[num]);
	
	imgActual += 1;
	var arrayPL = [];
	if(imgActual == array_images.length) imgActual=0;
	var imageSource = getImageSourceActual(array_images);
	arrayPL[0] = imageSource;
	var myImages = new Asset.images(arrayPL, {
		onProgress:function(counter) {
		setNavigation('hide');
		},
		onComplete: function(){
			//preloadSWF('off');
			setNavigation('show');
			animateImgNext(array_images, num);
		}
	});
}

/************ ANIMATE NEXT IMAGE ***********/
function animateImgNext(array_images, num) {
	var imageSource = getImageSourceActual(array_images);	
	num = num.toString();
	var img_size = $('img_center_'+num).getSize();
	var img_x = img_size.x;
	var img_y = img_size.y;
	
	var centerToLeft = new Fx.Morph($('img_center_'+num), {duration:1000,transition:Fx.Transitions.Pow.easeOut});
	var rightToCenter = new Fx.Morph($('img_right_'+num), {duration:1000,transition:Fx.Transitions.Pow.easeOut,
		onComplete:function(){
			$('img_center_'+num).set('src',imageSource);
			preloadSWF('off');
		}
	});
	$('img_right_'+num).set('src',imageSource);
	
	centerToLeft.start({'left':[0,-2*img_x]});
	rightToCenter.start({'left':[2*img_x,0]});
	replaceDebuggNext.delay(1100,null,[2*img_x,num]);
}

/************ REPLACE NEXT IMAGE ***********/
function replaceDebuggNext(img_x, num) {
	/* une fois les images plac�es, on repositionne les div center (left=0) et right (left=largeur window) pour initialiser la galerie*/
	$('img_center_'+num).setStyle('left',0);
	$('img_right_'+num).setStyle('left',img_x+'px');
}

/************ SHOW PREV IMAGE ***********/
function showPrevImage(array_images, num) {
	
	preloadSWF('on',$$('.catalog-03 .loading')[num]);
	imgActual -= 1;
	var arrayPL = [];
	if(imgActual < 0) imgActual=array_images.length-1;
	var imageSource = getImageSourceActual(array_images);
	arrayPL[0] = imageSource;
	var myImages = new Asset.images(arrayPL, {
		onProgress:function(counter) {
		setNavigation('hide');
		},
		onComplete: function(){
			//preloadSWF('off');
			setNavigation('show');
			animateImgPrev(array_images, num);
		}
	});
}
/************ ANIMATE PREV IMAGE ***********/
function animateImgPrev(array_images, num) {
	var imageSource = getImageSourceActual(array_images);
	num = num.toString();
	var img_size = $('img_center_'+num).getSize();
	var img_x = img_size.x;
	var img_y = img_size.y;
	
	var centerToRight = new Fx.Morph($('img_center_'+num), {duration:1000,transition:Fx.Transitions.Pow.easeOut});
	var leftToCenter = new Fx.Morph($('img_left_'+num), {duration:1000,transition:Fx.Transitions.Pow.easeOut,
		onComplete:function(){
			$('img_center_'+num).set('src',imageSource);
			preloadSWF('off');
		}
	});
	$('img_left_'+num).set('src',imageSource);

	centerToRight.start({'left':[0,2*img_x]});
	leftToCenter.start({'left':[-2*img_x,0]});
	replaceDebuggPrev.delay(1100,null,[2*img_x,num]);
}
/************ REPLACE PREV IMAGE ***********/
function replaceDebuggPrev(img_x, num) {
	/* une fois les images plac�es, on repositionne les div center (left=0) et right (left=largeur window) pour initialiser la galerie*/
	$('img_center_'+num).setStyle('left',0);
	$('img_left_'+num).setStyle('left',img_x+'px');
}
/************ GET IMAGE ACTUEL ***********/
function getImageSourceActual(array_images) {
	var imageId = array_images[imgActual];
	var imageSource = imageId;
	return imageSource;
}
/************ INIT NAVIGATION ***********/
function init_navigation() {
	$(document.body).getElements('a.btn_nav').set('opacity',0.3);
	$(document.body).getElements('a.btn_nav').each(function(el,i){
		el.addEvents({
		'mouseover':function() {
			el.set('opacity',1);
		},
		'mouseleave':function() {
			el.set('opacity',0.3);
		}});	
	});
};
/************ SET NAVIGATION ***********/
function setNavigation(param) {
	if(param == 'hide') { $(document.body).getElements('a.btn_nav').set('opacity',0); } 
	else if(param == 'show') { $(document.body).getElements('a.btn_nav').set('opacity',0.3); } 
	else {
		var win_size = window.getSize();
		var win_x = win_size.x;
		var win_y = win_size.y;
		//var navtop = (win_y-$('btn_up').getSize().y)/2;
		//$(document.body).getElements('a.btn_nav').setStyle('top',navtop+'px');
	}
}

/************ FIRST LOAD IMAGE ***********/
function firstLoadImage() {
	slider = showNextImage.periodical(intervalle)	
}

/************ PRELOADING SWF ***********/	
function preloadSWF(P_mode,conteneur_loading) {
	var content_so = '';
	var flashvars = {};
	var params = {wmode: "transparent"};
	var attributes = {};
	/* activation/desactivation */
	if(P_mode == 'on') {
		var newLoader = new Element('div', {'id':'pr-content','html':content_so});
		newLoader.injectInside(conteneur_loading);
		swfobject.embedSWF(_ROOT_URL+"gene/swf/loading.swf", "pr-content", "40", "40", "9.0.0", _ROOT_URL+"gene/swf/expressInstall.swf",flashvars,params);
	} else if(P_mode == 'off') {
		swfobject.removeSWF("pr-content");
	}
}

