// @Nom:          js-builder.js //
// @Annee:        2010          //
// @Auteur:       Benjamin JEHL //
// @Copyright:    db-clic.com   //

/* BOUTONS いいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいい */
function bouton(elment, position, dimension, sens){
    btn_id = elment;
    btn_poz = position;
    if (!dimension){dimension = 0;}
    if (!sens){
        sens= 0;
        document.getElementById(btn_id).style.marginTop= 0+"px";
        document.getElementById(btn_id).style.marginLeft= 0+"px";
    }
    btn_moove = dimension;
    btn_fx = sens;
    if (btn_poz == "over"){btn_poz = (-1)*btn_moove;}
    if (btn_poz == "out"){btn_poz = 0;}
    if (btn_fx == "top"){document.getElementById(btn_id).style.marginTop= btn_poz+"px";}
    if (btn_fx == "left"){document.getElementById(btn_id).style.marginLeft= btn_poz+"px";}
}
/* FAVORIS いいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいい */
function addFavoris(description,url) {
    site_description = description;
    site_url = url;
    if ( navigator.appName != 'Microsoft Internet Explorer' ){
	window.sidebar.addPanel(site_description,site_url,"");
    }else{
	window.external.AddFavorite(site_url,site_description);
    }
}
/* COOKIES -------------------------------------------------------------------*/
function makeCookie(nom,valeur,date) {
    dateExp = date.toGMTString();
    document.cookie = nom + '=' + escape(valeur) + '; expires=' + dateExp + ';';
}
function sessionCookie(nom,valeur,path) {
    if (navigator.appName != 'Microsoft Internet Explorer'){document.cookie = nom + '=' + escape(valeur) + '; expires=' + 0 + '; path=' + path+';';}else{document.cookie = nom + '=' + escape(valeur) + '; path=' +path+';';}
}
/* OPACITY いいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいい */
function opacity(cible,valeur){
    id_cible = document.getElementById(cible);
    id_opacity = valeur;
    id_cible.style['opacity'] = (id_opacity/100);
    id_cible.style['-moz-opacity'] = (id_opacity/100);
    id_cible.style['filter'] = "alpha(opacity="+id_opacity+")";
    id_cible.style.display = "block";
}
function opaciTweenStart(cible,valeurA,valeurB,timeUp){
    document.getElementById(cible).style.display = "block";
    opaciTween = new OpacityTween(document.getElementById(cible),Tween.regularEaseIn, valeurA, valeurB, timeUp);
    opaciTween.start();
}
function opaciTweenInit(cible){
    opaciTween.stop();
    opacity(cible,0);
}
/* [ MOTEUR ] -> Bar de Scrollbar ------------------------------------------- */
/* Scrollbar 1 */
function DragPage(page){
    cible = page;
    div_id = document.getElementById(cible);
    div_poz = div_id.offsetTop;
    div_height = div_id.offsetHeight;
    //On recupere l'ancienne position
    div_box = document.getElementById('agenda_box').offsetHeight;
    div_max = -1*(div_id.offsetHeight-div_box);
    scro_box = document.getElementById('scrollbox1').offsetHeight;
    scro_bar = -1*(document.getElementById('scrollbar1').offsetTop);
    // Calcul de la nouvelle position
    position = Math.round((scro_bar*div_height)/scro_box);
    if (position>0){position=0}
    if(position<div_max){position=div_max;}
    div_id.style.top = position +"px";
}
function DragDrop(){
    // Init Tween
    if (typeof mytweenPage != 'undefined'){mytweenPage.stop();}
    //Variables
    var srollBar_elmt = document.getElementById ("scrollbar1");
    var limitmax = (document.getElementById('scrollbox1').offsetHeight-document.getElementById('scrollbar1').offsetHeight);
    //Moteur Drag and drop
    Drag.init (srollBar_elmt, null, 0, 0, 0, limitmax);
    srollBar_elmt.onDrag = DragPage('agenda_bg');
}
/* Scrollbar 2 */
function DragPage2(page){
    cible = page;
    div_id = document.getElementById(cible);
    div_poz = div_id.offsetTop;
    div_height = div_id.offsetHeight;
    //On recupere l'ancienne position
    div_box = document.getElementById('laCarte_Content').offsetHeight;
    div_max = -1*(div_id.offsetHeight-div_box);
    scro_box = document.getElementById('scrollbox2').offsetHeight;
    scro_bar = -1*(document.getElementById('scrollbar2').offsetTop);
    // Calcul de la nouvelle position
    position = Math.round((scro_bar*div_height)/scro_box);
    if (position>0){position=0}
    if(position<div_max){position=div_max;}
    div_id.style.top = position +"px";
}
function DragDrop2(){
    // Init Tween
    if (typeof mytweenPage != 'undefined'){mytweenPage.stop();}
    //Variables
    var srollBar_elmt = document.getElementById("scrollbar2");
    var limitmax = (document.getElementById('scrollbox2').offsetHeight-document.getElementById('scrollbar2').offsetHeight);
    //Moteur Drag and drop
    initScrollBox2 = -parseInt(document.getElementById('scrollbar2').style.marginTop);
    Drag.init (srollBar_elmt, null, 0, 0, initScrollBox2, limitmax+initScrollBox2);
    srollBar_elmt.onDrag =DragPage2('laCarte_bg');
}
/* Scrollbar 3 */
function DragPage3(page){
    cible = page;
    div_id = document.getElementById(cible);
    div_poz = div_id.offsetTop;
    div_height = div_id.offsetHeight;
    //On recupere l'ancienne position
    div_box = document.getElementById('photos_box').offsetHeight;
    
    div_max = -1*(div_id.offsetHeight-div_box);
    scro_box = document.getElementById('scrollbox3').offsetHeight;
    scro_bar = -1*(document.getElementById('scrollbar3').offsetTop);
    // Calcul de la nouvelle position
    position = Math.round((scro_bar*div_height)/scro_box);
    if (position>0){position=0}
    if(position<div_max){position=div_max;}
    div_id.style.top = position +"px";
}
function DragDrop3(){
    // Init Tween
    if (typeof mytweenPage != 'undefined'){mytweenPage.stop();}
    //Variables
    var srollBar_elmt = document.getElementById("scrollbar3");
    var limitmax = (document.getElementById('scrollbox3').offsetHeight-document.getElementById('scrollbar3').offsetHeight);
    //Moteur Drag and drop
    Drag.init (srollBar_elmt, null, 0, 0, 0, limitmax);
    srollBar_elmt.onDrag =DragPage3('photos_bg');
}





var Drag = {
	obj : null,
	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
                o.onmousedown	= Drag.start;
		o.hmode		= bSwapHorzRef ? false : true ;
		o.vmode		= bSwapVertRef ? false : true ;
                o.root = oRoot && oRoot != null ? oRoot : o ;
		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";
		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;
		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;
		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},
	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);
		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;
		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}
		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}
		document.onmousemove	= Drag.drag;
		document.onmouseup	= Drag.end;
		return false;
	},
	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;
                if(chooseContentBox =="agenda"){DragDrop();}
		if(chooseContentBox =="defaut"){DragDrop2();}
		if(chooseContentBox =="photos"){DragDrop3();}
		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;
		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)
		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;
		return false;
	},
	end : function()
	{
                document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(        parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
						parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},
	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};
