/*
 * pour afficher les images en grand dans un popup
 * En fin de script, il y a de window.addevent qui regarde s'il doit initialiser ou pas la modal
 */
 
modal_bi =  new Object();

modal_bi.w = 0;
modal_bi.h = 0;
modal_bi.propy = 0;
modal_bi.propx = 0;
modal_bi.image = null;
modal_bi.image_cadre = new Object();
modal_bi.image_cadre.x = 0;
modal_bi.image_cadre.y = 0;
modal_bi.images_loaded = new Array();

modal_bi.less = null;

modal_bi.current_image = "";

/*
 * init
 * on ajoute notre div overlay
 * param : w et h de la div principale
 */
modal_bi.init = function(w,h) {


	
	modal_bi.w = w;
	modal_bi.h = h;
	


	// overlay
	var myover = new Element ( 'div' , { 'id' : 'modal_bi_overlay'} ) ;

	myover.inject('modal_bi_main', 'after');

	//myover.injectInside("body");
	
	$('modal_bi_overlay').setStyle("height",'100%');
	$('modal_bi_overlay').setStyle("width",'100%');
	$('modal_bi_overlay').setStyle("position",'fixed');
	$('modal_bi_overlay').setStyle("left",'0');
	$('modal_bi_overlay').setStyle("top",'0');
	$('modal_bi_overlay').setStyle("background-color",'#000000');
	$('modal_bi_overlay').setStyle("z-index",'1000');
	$('modal_bi_overlay').set("opacity", ".50");
	$('modal_bi_overlay').setStyle("display","none");

	// centrage de la div qui contient l'image
	//modal_bi.hCenterAbsolute('modal_bi_main');
	//modal_bi.vCenterAbsolute('modal_bi_main');

	$('modal_bi_main').setStyle('top',370);
	$('modal_bi_main').setStyle('left',328);
		
	// ajoute de l'event
	modal_bi.event_mouse();
	
}

/*
 * centrage horizontal d'un obj
 */
modal_bi.hCenterAbsolute = function(obj, reValue){
	var obj = $(obj);
	if ($defined(obj)){
		var coord = obj.getCoordinates();
		
		var l = (window.getWidth() - coord['width'])/2;
		if (reValue) return l;
		else obj.setStyle('left', l);
	}
};

/*
 * centrage vertical d'un obj
 */
modal_bi.vCenterAbsolute = function(obj, reValue){
	var obj = $(obj);
	if ($defined(obj)){
		var coord = obj.getCoordinates();
		
		var t = window.getHeight()/2 - coord['height']/2;
		
		if (reValue) return t;
		else obj.setStyle('top', t);
	}
};

/*
 * ajout l'event sur la zone de l'image pour la déplacer
 */
modal_bi.event_mouse = function() {
		
	$('modal_bi_content').addEvent('mousemove', function(e) {
		mousex = e.page.x;
		mousey = e.page.y;
		
		posy = mousey - modal_bi.less.y;
		posx = mousex - modal_bi.less.x;
		tok  = posx;
		
		rapportx = posx/modal_bi.w;
		newposx  = (modal_bi.image_cadre.x * rapportx * -1);

		rapporty = posy/modal_bi.h;
		newposy  = (modal_bi.image_cadre.y * rapporty * -1);

		
		$('modal_bi_img').setStyle("margin-left", newposx);
		$('modal_bi_img').setStyle("margin-top", newposy);
	
	});
}

/*
 * changement de l'image
 */
modal_bi.change_src = function(source) {
	$('modal_bi_img').src = source;
}

/*
 * show/hide de la div overlay
 * param : true si on veut la voir
 */
modal_bi.overlay_sh = function(show) {
	if(show == true) {
		$('modal_bi_overlay').setStyle("display","");
	} else {
		$('modal_bi_overlay').setStyle("display","none");
	}
}

modal_bi.maindiv_sh = function(show) {
	if(show == true) {
		$('modal_bi_main').setStyle("display","");
	} else {
		$('modal_bi_main').setStyle("display","none");
	}
}

/*
 * click sur la miniature :
 * chg de source, overlay affiché, modal affiché
 */
modal_bi.thumb_click = function(source) {

	mypos = souris_position_y;
	if(mypos > 280) { mypos += 30; } else { mypos = 280; }
	
	$('modal_bi_main').setStyle('top',mypos);
	
	my_source = $('thumb_' + source).src;
	my_source = my_source.replace(/Referentiel_thumbnails120/,'Referentiel_Photos');
	
	modal_bi.change_src(my_source);
	
	// on charge l'image : quand c fait on passe a la suite, on a la bonne taille.
	var x = new Element('img',{
			src: my_source
	});
	
	// bug de ie : si deja loaded event load n'est pas activé...donc on regarde
	// si déjà loadé, si oui, ben zou direct sans l'event
	if(modal_bi.images_loaded[my_source] == true) {
		$$('body').adopt(x);
		modal_bi.image = x.getSize();
		x.setStyle("display","none");
		modal_bi.thumb_click_suite();		
	} else {
		modal_bi.images_loaded[my_source] = true;
		
		x.addEvent('load',function(){
				$$('body').adopt(x);
				modal_bi.image = x.getSize();
				x.setStyle("display","none");
				modal_bi.thumb_click_suite();
		});
	}

}

modal_bi.thumb_click_suite = function() {
	modal_bi.image_cadre.x = modal_bi.image.x - modal_bi.w;
	modal_bi.image_cadre.y = modal_bi.image.y - modal_bi.h;
	//console.log("modal_bi.image_cadre.y = " + modal_bi.image_cadre.y);
	
		
	$('modal_bi_img').setStyle("margin-top",0);
	$('modal_bi_img').setStyle("margin-left",0);
	
	modal_bi.overlay_sh(true);
	modal_bi.maindiv_sh(true);

	modal_bi.less = $('modal_bi_content').getCoordinates();
	
	modal_bi.less.y = modal_bi.less.top;
	modal_bi.less.x = modal_bi.less.left;
}


/*
 * click sur fermeture modal:
 * overlay masqué, modal masqué
 */
modal_bi.maindiv_close = function() {
	modal_bi.overlay_sh(false);
	modal_bi.maindiv_sh(false);
}

// ------------------------------------------------------
// DEMARRAGE
// ------------------------------------------------------

var souris_position_y = null;
window.addEvent('domready',function() { 
  
	
	if($('modal_bi_main') == null) {
		return;
	}
	
	
	modal_bi.init(400,400);
	
	$("projet_list").addEvent(
	    'mousemove', function(event){
	        souris_position_y = event.page.y - event.client.y;
	});
});


