/************************************************************
                    Slider control creator
              By Mark Wilton-Jones 12-13/10/2002
Version 1.1 updated 22/10/2003 to provide hand cursor option
*************************************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

___________________________________________________________________________________________*/


/* Modifications and new functions by Armin Burger for use with MapServer */

// ***************** START OF CODE BY Armin Burger ************************

/*
 * Functions for setting scale to form input and load map
 **************************************************************************/
var trackHeight = 4 ;      	//height of track
var trackWidth = 30 ;       //width of track
var trackColor = '#967F54' ; //colour of track
var trackBorder = 1 ;        //thickness of track border
var trackColorBorder = '' ; //colour of track border
var runnerBorder = 3 ;         //thickness of runner (in the middle of the track)
var runnerColor = '' ; //colour of runner
var buttonHeight = 10 ;       //height of button
var buttonWidth = 6 ;        //width of button
var buttonColor = '' ; //colour of button
var buttonBorder = 0 ;        //thickness of button border (shaded to give 3D effect)
var buttonText = '<font size="2"></font>' ; //text of button (if any)
var travelDirection = true ;  //direction of travel (true = horizontal, false = vertical)

/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
//var myslider;
//var slScale; slideOpacityIsMove
function createOSlider(sliderElemId) {
		lft = (arguments[1])? arguments[1] : 0;
    myslider = new sliderOpacity(
    sliderElemId, trackHeight, trackWidth, trackColor, trackBorder, trackColorBorder,
    runnerBorder, runnerColor, buttonHeight, buttonWidth,
    buttonColor, buttonBorder, buttonText, travelDirection, true,lft);
}

var mouseOpacityIsPressed = false;
var MWJ_sliderOpacity_controls = 0;

function sliderOpacityMousePos(e) {
	if( !e ) { e = window.event; } if( !e || ( typeof( e.pageX ) != 'number' && typeof( e.clientX ) != 'number' ) ) { return [0,0]; }
	if( typeof( e.pageX ) == 'number' ) { var xcoord = e.pageX; var ycoord = e.pageY; } else {
		var xcoord = e.clientX; var ycoord = e.clientY;
		if( !( ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) || ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) || window.navigator.vendor == 'KDE' ) ) {
			if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) ) {
				xcoord += document.documentElement.scrollLeft; ycoord += document.documentElement.scrollTop;
			} else if( document.body && ( document.body.scrollTop || document.body.scrollLeft ) ) {
				xcoord += document.body.scrollLeft; ycoord += document.body.scrollTop; } } }
	return [xcoord,ycoord];
}

var LAYER_TO_MODIF = null;
function slideOpacityIsDown(e) {
	LAYER_TO_MODIF = this.getAttribute('att',false);
	window.msStartCoord = sliderOpacityMousePos(e);
	window.lyStartCoord = this.style?[parseInt(this.style.left),parseInt(this.style.top)]:[parseInt(this.left),parseInt(this.top)];
	if( document.captureEvents && Event.MOUSEMOVE ) {
		document.captureEvents(Event.MOUSEMOVE); document.captureEvents(Event.MOUSEUP); }
	window.storeMOUSEMOVE = document.onmousemove; window.storeMOUSEUP = document.onmouseup; window.storeLayer = this;
	mouseOpacityIsPressed = true;
    document.onmousemove = slideOpacityIsMove;
    document.onmouseup = slideOpacityIsMove; return false;
}

function slideOpacityIsMove(e) {
	if(LAYER_TO_MODIF == null) {
		return;
	}
  if (mouseOpacityIsPressed) {
    var msMvCo = sliderOpacityMousePos(e); if( !e ) { e = window.event ? window.event : ( new Object() ); }
    var theLayer = window.storeLayer.style ? window.storeLayer.style : window.storeLayer;
    var oPix = document.childNodes ? 'px' : 0;
    if( window.storeLayer.hor ) {
      var theNewPos = window.lyStartCoord[0] + ( msMvCo[0] - window.msStartCoord[0] );
      if( theNewPos < 0 ) { theNewPos = 0; } if( theNewPos > window.storeLayer.maxLength ) { theNewPos = window.storeLayer.maxLength; }
      theLayer.left = theNewPos + oPix;
      
      /********************************************/
      /************ ICI ON MOUSE MOVE **************/
			var infoCentage = Math.round(parseInt(theLayer.left)*100/(trackWidth-(buttonWidth+(2*buttonBorder))));
			//document.getElementById('infoCentage').innerHTML = "";
    	/********************************************/
      /********************************************/
    } else {
        var theNewPos = window.lyStartCoord[1] + ( msMvCo[1] - window.msStartCoord[1] );
        if( theNewPos < 0 ) { theNewPos = 0; } if( theNewPos > window.storeLayer.maxLength ) { theNewPos = window.storeLayer.maxLength; }
        theLayer.top = theNewPos + oPix;
    }
    if( e.type && e.type.toLowerCase() == 'mousemove' ) {
        if( window.storeLayer.moveFunc ) { window.storeLayer.moveFunc(theNewPos/window.storeLayer.maxLength); }
    } else {
      	/********************************************/
	      /************ ICI MOUSE UP  ***********************/
     		var maxLenght = trackWidth-(buttonWidth+(2*buttonBorder));
      	xSlideOpacity = parseInt(theLayer.left)*100/maxLenght;
      	nomcouche = LAYER_TO_MODIF.replace(/^opacityDiv_/,'');
				makeTransparency(101-xSlideOpacity,nomcouche);
				LAYER_TO_MODIF = null;
	     	/********************************************/
    		/********************************************/
        document.onmousemove = storeMOUSEMOVE;
        document.onmouseup = window.storeMOUSEUP;
        if(window.storeLayer.stopFunc) { 
        	window.storeLayer.stopFunc(theNewPos/window.storeLayer.maxLength);
        }
    }
  }
}

function sliderOpacity(sliderElemId,oThght,oTwdth,oTcol,oTBthk,oTBcol,oTRthk,oTRcol,oBhght,oBwdth,oBcol,oBthk,oBtxt,oAlgn,oCrs,lft) {
	fLeft = (lft != '0')? Math.round(((100-lft) * (oTwdth-(oBwdth+(2*oBthk))))/100) : 0;
	
	/*
	var sliderStr = (
    '<span style="display:inline;text-align:left;font-size:10px;line-height:0px;font-weight:bold;width:100%;color:#ffffff;white-space:nowrap" id="infoCentage"><\/span><div style="position:relative;_position:static;float:left;left:0px;top:0px;height:'+(oThght+(2*oTBthk))+'px;width:'+(oTwdth+(2*oTBthk))+'px;background-color:'+oTBcol+';font-size:0px;">'+
    '<div style="position:relative;_position:static;left:'+oTBthk+'px;top:'+oTBthk+'px;height:'+oThght+'px;width:'+oTwdth+'px;background-color:'+oTcol+';font-size:0px;">'+
    '<div style="position:absolute;_position:static;left:'+(oAlgn?0:Math.floor((oTwdth-oTRthk)/2))+'px;top:'+(oAlgn?Math.floor((oThght-oTRthk)/2):0)+'px;height:'+(oAlgn?oTRthk:oThght)+'px;width:'+(oAlgn?oTwdth:oTRthk)+'px;background-color:'+oTRcol+';font-size:0px;"><\/div>'+
    '<div att="'+sliderElemId+'" style="border:2px solid green;position:relative;left:'+(oAlgn? fLeft : Math.floor((oTwdth-(oBwdth+(2*oBthk))))+lft)+';top:'+(oAlgn?Math.floor((oThght-(oBhght+(2*oBthk)))/2):0)+'px;height:'+(oBhght+(2*oBthk))+'px;width:'+(oBwdth+(2*oBthk))+'px;font-size:0px;" ondragstart="return false;" onselectstart="return false;" onmouseover="this.hor='+oAlgn+';this.maxLength='+((oAlgn?oTwdth:oThght)-((oAlgn?oBwdth:oBhght)+(2*oBthk)))+';this.onmousedown=slideOpacityIsDown;" id="MWJ_sliderOpacity_controls">'+
    '<div style="border-top:'+oBthk+'px solid #ffffff;border-left:'+oBthk+'px solid #ffffff;border-right:'+oBthk+'px solid #000000;border-bottom:'+oBthk+'px solid #000000;">'+
    '<div style="height:'+oBhght+'px;width:'+oBwdth+'px;font-size:0px;background-color:'+oBcol+';cursor:'+(oCrs?'pointer;cursor:e-resize':'default')+';">'+
    '<span style="width:3px;height:5px;text-align:center;background: red;">'+oBtxt+'<\/span><\/div><\/div><\/div><\/div><\/div>'
	);
	*/
	var sliderStr = (
    //'<span style="display:inline;text-align:left;font-size:10px;line-height:0px;font-weight:bold;width:100%;color:#ffffff;white-space:nowrap" id="infoCentage"><\/span>'+
    
    '<div style="float:left;left:0px;top:0px;height:'+(oThght+(2*oTBthk))+'px;width:'+(oTwdth+(2*oTBthk))+'px;background-color:'+oTBcol+';font-size:0px;background-color:red;border:1px solid blue;">'+
    	//'<div style="border:2px solid blue;position:relative;_position:static;left:'+oTBthk+'px;top:'+oTBthk+'px;height:'+oThght+'px;width:'+oTwdth+'px;background-color:'+oTcol+';font-size:0px;">'+
    		//'<div style="position:absolute;_position:static;left:'+(oAlgn?0:Math.floor((oTwdth-oTRthk)/2))+'px;top:'+(oAlgn?Math.floor((oThght-oTRthk)/2):0)+'px;height:'+(oAlgn?oTRthk:oThght)+'px;width:'+(oAlgn?oTwdth:oTRthk)+'px;background-color:'+oTRcol+';font-size:0px;"><\/div>'+
    		
    		'<div att="'+sliderElemId+'" style="cursor:'+(oCrs?'pointer;cursor:e-resize':'default')+';border:2px solid green;position:relative;Left:'+(oAlgn? fLeft : Math.floor((oTwdth-(oBwdth+(2*oBthk))))+lft)+';top:'+(oAlgn?Math.floor((oThght-(oBhght+(2*oBthk)))/2):0)+'px;height:'+(oBhght+(2*oBthk))+'px;width:'+(oBwdth+(2*oBthk))+'px;font-size:0px;" ondragstart="return false;" onselectstart="return false;" onmouseover="this.hor='+oAlgn+';this.maxLength='+((oAlgn?oTwdth:oThght)-((oAlgn?oBwdth:oBhght)+(2*oBthk)))+';this.onmousedown=slideOpacityIsDown;" id="MWJ_sliderOpacity_controls">'+
    			//'<div style="border-top:'+oBthk+'px solid #ffffff;border-left:'+oBthk+'px solid #ffffff;border-right:'+oBthk+'px solid #000000;border-bottom:'+oBthk+'px solid #000000;">'+
    			//	'<div style="height:'+oBhght+'px;width:'+oBwdth+'px;font-size:0px;background-color:'+oBcol+';cursor:'+(oCrs?'pointer;cursor:e-resize':'default')+';">'+
    			//		'<span style="width:3px;height:5px;text-align:center;background: red;">'+oBtxt+'<\/span>'+
    			//	'<\/div>'+
    			//'<\/div>'+
    		'<\/div>'+
    		
    		
    	//'<\/div>'+
    '<\/div>'
	);
	var newDivContainer = document.createElement('div');
	newDivContainer.innerHTML = sliderStr;
	document.getElementById(sliderElemId).appendChild(newDivContainer);
}

function makeTransparency(iTransparency, thm){
	thm = thm.replace("opac_","");
	parURL = 'transparence='+iTransparency+'&layername='+thm+'&act=setopacity';
	gMap.sendX_Load(parURL) ;	
}