//******************************************************************************
// Window
//******************************************************************************
function Window(title, windowid, containerid, width, height, x, y, closable, miniziable, draggable) {
    this.title = title;
    this.windowid = "_window_" + windowid;
	this.cookie = "_window_" + windowid;
    this.footerwindowid = "footer" + windowid;
    this.containerid = containerid;
    this.width = width;
    this.height = height;
	this.x = x;
	this.y = y;
	this.closable = closable;
	this.miniziable = miniziable;
	this.draggable = draggable;
	this.minimalized = false;
	this.hidden = false;
	
	this.getCookie();
}

Window.prototype = new GControl();

Window.prototype.initialize = function(parent) {
    return this.register2Parent(parent.getContainer());
}

Window.prototype.register2Parent = function(parentContainer) {

	var that = this;

    var agent = navigator.userAgent.toLowerCase();
    if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){this.ie = true} else {this.ie = false}

    var window = document.createElement("div");
    window.id = this.windowid;
    window.style.width = this.width + "px";
    window.style.border = "1px solid #000";
    window.style.zIndex = "10";
    window.style.backgroundColor = "#fff";
    if (this.hidden) {
		window.style.display = "none";
	}

    var windowhandle = document.createElement("div");
    windowhandle.style.textAlign = "left";
    windowhandle.style.fontWeight = "bold";
    windowhandle.style.backgroundColor = "#282828";
    windowhandle.style.padding = "2px";
    windowhandle.style.color = "#ffffff";
    windowhandle.style.verticalAlign = "middle";
    windowhandle.style.height = "16px";
    if (this.draggable) {
		windowhandle.style.cursor = "move";
	}

	if (this.ie) {
      //var loader = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='css/images/bg.png', sizingMethod='scale');";
      //windowhandle.style.filter=loader;
    } else {
      windowhandle.style.background = "url(css/images/bg.png)";
    }

    if (this.closable) {
        var close = document.createElement("img");
        close.style.width = "16px";
        close.style.height = "16px";
		close.style.cursor = "pointer";
        close.src = "images/icons/cross.png";
        if (this.ie) {
            close.style.styleFloat = "right";
        } else {
            close.style.cssFloat = "right";
        }
		
		GEvent.addDomListener(close, "click", function() {that.hide()});

        windowhandle.appendChild(close);
    }
    if (this.miniziable) {
        this.minimize = document.createElement("img");
        this.minimize.style.width = "16px";
        this.minimize.style.height = "16px";
		this.minimize.style.cursor = "pointer";
        this.minimize.src = (this.minimalized)? "images/icons/arrow_out.png" : "images/icons/arrow_in.png";
        if (this.ie) {
            this.minimize.style.styleFloat = "right";
        } else {
            this.minimize.style.cssFloat = "right";
        }
		
		GEvent.addDomListener(this.minimize, "click", function() {that.toggle()});

        windowhandle.appendChild(this.minimize);
    }

    this.titleObj = document.createElement("span");
    this.titleObj.style.textAlign = "center";
    this.titleObj.innerHTML = this.title;
    windowhandle.appendChild(this.titleObj);

    this.windowhandle = windowhandle;

    this.windowinner = document.createElement("div");
    this.windowinner.id = "inner" + this.windowid;

    var windowcontent = document.createElement("div");
	windowcontent.id = this.containerid;
	windowcontent.style.padding = "2px";
	if (this.height > 0) {
	    windowcontent.style.height = (this.height - 16) + "px";// handle height
	}

	if (this.minimalized) {
		this.windowinner.style.display = "none";
	}

	this.windowinner.appendChild(windowcontent);

	var windowbottomcontent = document.createElement("div");
	windowbottomcontent.id = "bottom" + this.windowid;
    this.windowinner.appendChild(windowbottomcontent);

    window.appendChild(this.windowhandle);
    window.appendChild(this.windowinner);
	
    this.container = window;
    this.contentContainer = windowcontent;

    GEvent.addDomListener(this.container, "click", function() {that.arrangeZindex()});

    // attach the control to the parent
    if (parentContainer) {
        parentContainer.appendChild(this.container);
	    this.parent = parentContainer;
	}
	
	// make window draggable
	if (this.draggable) {
		$('#' + this.windowid).draggable(
			{
				zIndex: 	9999,
				ghosting:	false,
				opacity: 	0.7,
				handle:	'#' + that.windowid,
				containment:	parentContainer,
                stop: function(event, ui) { that.setCookie(); GEvent.trigger(that, "ondragend")}
			}
		);
	}

    this.setCookie();

    return this.container;
}

Window.prototype.getParent = function() {
    return this.parent;
}

Window.prototype.getContentContainer = function() {
    return this.contentContainer;
}

Window.prototype.getContainer = function() {
    return this.container;
}

Window.prototype.toggle = function() {
    this.minimalized = !this.minimalized;
    this.minimize.src = (this.minimalized)? "images/icons/arrow_out.png" : "images/icons/arrow_in.png";
	$('#' + this.footerwindowid).toggle(400);
	$('#' + this.windowinner.id).slideToggle(400);
	this.setCookie();
}

Window.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(this.x, this.y));
}

Window.prototype.addControl = function(_control) {
    _control.initialize(this);
    _control.initializePosition();
}

Window.prototype.show = function() {
    this.arrangeZindex();
    $('#' + this.windowid).show();
	this.hidden = false;
	this.setCookie();
}

Window.prototype.hide = function() {
    $('#' + this.windowid).hide();
	this.hidden = true;
	this.setCookie();
}

Window.prototype.addContent = function(_content) {
    getContainer().appendChild(_content);
}

Window.prototype.arrangeZindex = function() {
	$("div[id^='_window_']").css('z-index', 10);
    $("#" + this.windowid).css('z-index', 20);
}

Window.prototype.setTitle = function(title) {
    this.title = title;
    this.titleObj.innerHTML = this.title;
}

Window.prototype.setFooterText = function(text) {

	if (text && text.length > 0) {
		if (!this.windowfooter) {
			
			this.windowfooter = document.createElement("div");
			this.windowfooter.style.color = "#bbb";
			this.windowfooter.style.fontSize = "0.8em";
			this.windowfooter.style.height = "15px";
			this.windowfooter.style.overflow = "hidden";
			this.windowfooter.style.textAlign = "right";
			if (this.minimalized) {
				this.windowfooter.style.display = "none";
			}

			this.windowfooter.innerHTML = text;
			this.windowinner.appendChild(this.windowfooter);
			
		} else {
			this.windowfooter.innerHTML = text;
		}
	}
}

Window.prototype.initializePosition = function() {
    this.container.style.position = "absolute";
    this.container.style.top = this.y + "px";
    this.container.style.left = this.x + "px";
	this.setCookie();
}

Window.prototype.setCookie = function() {
	var _value = this.minimalized;
	if (this.draggable) {
	    _value += ',' + this.container.style.top.split('px')[0] + ',' + this.container.style.left.split('px')[0];
	}
	if (this.closable) {
	    _value += ',' + this.hidden;
	}
    $.cookie(this.cookie, _value, {expires: 365});
}

Window.prototype.getCookie = function() {
    var _value = $.cookie(this.cookie);
    if (_value) {
        var _values = _value.split(',');
		this.minimalized = ("true" == _values[0]);
		if (this.draggable && _values[1] && _values[2]) {
            this.y = _values[1];
            this.x = _values[2];
		}
		if (this.closable && _values[3]) {
		    this.hidden = ("true" == _values[3]);
		} else if (this.closable && !this.draggable && _values[1]) {
		    this.hidden = ("true" == _values[1]);
		}

        return _value;
    }
}

Window.prototype.addIcon = function(_id, _src, _title, _onclick) {
    var icon = document.createElement("img");
    icon.id = _id;
    icon.style.width = "16px";
    icon.style.height = "16px";
    icon.style.cursor = "pointer";
    icon.style.paddingRight = "2px";
    icon.src = _src;
    icon.title = _title;
    if (this.ie) {
        icon.style.styleFloat = "right";
    } else {
        icon.style.cssFloat = "right";
    }

    GEvent.addDomListener(icon, "click", _onclick);

    this.windowhandle.appendChild(icon);
}

//******************************************************************************
// Terrain Window
//******************************************************************************

function TerrainWindow(title, windowid, containerid, width, height, x, y, closable, miniziable, draggable) {
    this.title = title;
    this.windowid = "_window_" + windowid;
	this.cookie = "_window_" + windowid;
    this.footerwindowid = "footer" + windowid;
    this.containerid = containerid;
    this.width = width;
    this.height = height;
	this.x = x;
	this.y = y;
	this.closable = closable;
	this.miniziable = miniziable;
	this.draggable = draggable;
	this.minimalized = false;
	this.hidden = false;

	// specific for terrain
	this.earthCurved = true;
	this.refraction = false;
	this.cookieTerrain = "terrainProperties";

	this.getCookie();
	this.getCookieTerrain();
}

TerrainWindow.prototype = new Window();

TerrainWindow.prototype.addControls = function(iconEarthcurvedTitle, iconRefractionTitle) {
    var that = this;
    this.addIcon('_refraction', 'script/images/knob' + (this.refraction? '_green' : '') + '.png', iconRefractionTitle, function() { that.refraction = !that.refraction; that.updateTerrain('_refraction'); });
    this.addIcon('_earthCurved', 'script/images/knob' + (this.earthCurved? '_green' : '') + '.png', iconEarthcurvedTitle, function() { that.earthCurved = !that.earthCurved; that.updateTerrain('_earthCurved'); });
}

TerrainWindow.prototype.setCookieTerrain = function() {
	var _value = this.earthCurved + ',' + this.refraction;
    $.cookie(this.cookieTerrain, _value, {expires: 365});
}

TerrainWindow.prototype.getCookieTerrain = function() {
    var _value = $.cookie(this.cookieTerrain);
    if (_value) {
        var _values = _value.split(',');

        this.earthCurved = ("true" == _values[0]);
        this.refraction = ("true" == _values[1]);

        return _value;
    }
}

TerrainWindow.prototype.updateTerrain = function (type) {
    this.setCookieTerrain();
    var _icon = $("#" + type);
    _icon.attr('src', 'script/images/knob' + ((_icon.attr('src').indexOf('green') < 0)? '_green' : '') + '.png');
    if (this.lat2 || this.lng2 || this.aboveGround2) {
        this.showTerrain2Points(this.lat, this.lng, this.aboveGround, this.lat2, this.lng2, this.aboveGround2);
    } else {
        this.showTerrain(this.id, this.lat, this.lng, this.aboveGround);
    }

}

TerrainWindow.prototype.showTerrain2Points = function (lat, lng, aboveGround, lat2, lng2, aboveGround2) {
    this.lat = lat;
    this.lng = lng;
    this.aboveGround = aboveGround;
    this.lat2 = lat2;
    this.lng2 = lng2;
    this.aboveGround2 = aboveGround2;

    this.show();
    var that = this;

    $("#" + this.containerid).html('<img src="images/ajax-loader.gif" alt="Profil" title="Profil" />');
    var profileURL = "/terrainprofile.html?params=" + this.lat + "," + this.lng + "," + this.aboveGround + "," + this.lat2 + "," + this.lng2 + "," + this.aboveGround2;
    $.ajax({
      url: profileURL,
      cache: false,
      success: function(res){
        htmlImg = '<img src="' + this.url + '" alt="Profil" title="Profil" />';
        $("#" + that.containerid).html(htmlImg);
      }
    });
}

TerrainWindow.prototype.showTerrain = function (id, lat, lng, aboveGround) {

    this.id = id;
    this.lat = lat;
    this.lng = lng;
    this.aboveGround = aboveGround;

    this.show();
    var that = this;
    $("#" + this.containerid).html('<img src="images/ajax-loader.gif" alt="Profil" title="Profil" />');
    var profileURL = "/terrainprofile.html?params=" + id + "," + lat + "," + lng + "," + aboveGround;
    $.ajax({
      url: profileURL,
      cache: false,
      success: function(res){
        htmlImg = '<img src="' + this.url + '" alt="Profil" title="Profil" />';
        $("#" + that.containerid).html(htmlImg);
      }
    });
}

//******************************************************************************
// Slider
//******************************************************************************
function SliderControlMax(id, min, max, value, title, tooltip, x, y) {
    this.id = id;
    this.min = min;
    this.max = max;
    this.initValue = ((value > max)? max : ((value < min)? min : value));//initial slider position
    this.title = title;//title
    this.tooltip = tooltip;//title
	this.x = x;
	this.y = y;
	this.enabled = true;
	this.inactiveValue = -1;
}

SliderControlMax.prototype = new GControl();

// This function positions the slider to match the specified value
SliderControlMax.prototype.setSlider = function(value, forceValue) {
    if (this.enabled || forceValue) {
        var _value = value;

        if (_value > this.max) {
            _value = this.max;
        }
        if (_value < this.min) {
            _value = this.min;
        }

        _value = this.max - _value;

        this.slide.top = _value;
        this.knob.style.top = _value + "px";
    }

    this.setValue();
}

// Return actual value of slider
SliderControlMax.prototype.getValue = function() {
    var _val = this.max - this.slide.top;
    return (!_val)? this.min : _val;
}

// This function reads the slider value and sets numeric value to slider
SliderControlMax.prototype.setValue = function() {

    var o = this.getValue();

    if (o >= this.min && o <= this.max) {
        //o = o + 1;
        var num1 = document.getElementById(this.id + '_num1');
        var num2 = document.getElementById(this.id + '_num2');
        var num3 = document.getElementById(this.id + '_num3');
        if (this.inactiveValue != o) {

            var sliderImg = (this.max < 100? "slider2" : "slider3");

            if (this.ie) {
              var loader = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='script/images/" + sliderImg + ".png', sizingMethod='scale');";
              this.knob.style.filter=loader;
            } else {
              this.knob.style.background = "url(script/images/" + sliderImg + ".png)";
            }

            if (this.max < 100) {
                if (o < 10) {
                    num1.src = 'images/numbers/noblank.png';
                    num1.style.width = '0px';
                    num2.src = 'images/numbers/no' + o + '.png';
                    num2.style.paddingLeft = "4px";
                } else {
                    var n = Math.floor(o / 10);
                    num1.src = 'images/numbers/no' + n + '.png';
                    num1.style.width = '8px';
                    num2.src = 'images/numbers/no' + (o - (n * 10)) + '.png';
                    num2.style.paddingLeft = "0px";
                }
            } else {
                if (o < 10) {
                    num1.src = "images/numbers/noblank.png";
                    num1.style.width = "0px";
                    num1.style.paddingLeft = "0px";
                    num2.src = "images/numbers/noblank.png";
                    num2.style.width = "0px";
                    num2.style.paddingLeft = "0px";
                    num3.src = 'images/numbers/no' + o + '.png';
                    num3.style.paddingLeft = "13px";
                } else if (o < 100) {
                    var n = Math.floor(o / 10);
                    num1.src = "images/numbers/noblank.png";
                    num1.style.width = "0px";
                    num1.style.paddingLeft = "0px";
                    num2.src = 'images/numbers/no' + n + '.png';
                    num2.style.width = '8px';
                    num2.style.paddingLeft = "8px";
                    num3.src = 'images/numbers/no' + (o - (n * 10)) + '.png';
                    num3.style.paddingLeft = "1px";
                } else {
                    var n = Math.floor(o / 100);
                    var n2 = Math.floor((o - (n * 100)) / 10);
                    num1.src = 'images/numbers/no' + n + '.png';
                    num1.style.width = '8px';
                    num1.style.paddingLeft = "4px";
                    num2.src = 'images/numbers/no' + n2 + '.png';
                    num2.style.width = '8px';
                    num2.style.paddingLeft = "1px";
                    num3.src = 'images/numbers/no' + (o - (n * 100) - (n2 * 10)) + '.png';
                    num3.style.width = '8px';
                    num3.style.paddingLeft = "1px";
                }
            }

            GEvent.trigger(this, "onchange");

        } else {

            var sliderImg = (this.max < 100? "slider2g" : "slider3g");

            if (this.ie) {
              var loader = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='script/images/" + sliderImg + ".png', sizingMethod='scale');";
              this.knob.style.filter=loader;
            } else {
              this.knob.style.background = "url(script/images/" + sliderImg + ".png)";
            }

            num1.src = "images/numbers/noblank.png";
            num1.style.width = "0px";
            num2.src = "images/numbers/dash.png";
            num2.style.paddingLeft = "4px";
            if (num3) {
                num2.src = "images/numbers/noblank.png";
                num2.style.width = "0px";
                num2.style.paddingLeft = "0px";
                num3.src = "images/numbers/dash.png";
                num3.style.paddingLeft = "13px";
            }
        }
    }
}


// == This gets called by the API when addControl(new XSlider()) is used ==
SliderControlMax.prototype.initialize = function(parent) {

    // obtain Function Closure on a reference to "this"
    var that=this;
    // store a reference to the map so that we can make calls on it
    this.parent = parent;

    // Is this MSIE, if so we need to use AlphaImageLoader
    var agent = navigator.userAgent.toLowerCase();
    if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){this.ie = true} else {this.ie = false}

    var componentcontainer = document.createElement("div");

    // crete slider container
    var slidercontainer = document.createElement("div")
    //slidercontainer.style.border = "1px solid red";
    slidercontainer.style.width = "20px";
    slidercontainer.style.height = (this.max + 25 - this.min) + "px";
    //slidercontainer.style.paddingLeft = "10px";
    //slidercontainer.style.paddingRight = "10px";

    var imagecontainer = document.createElement("div");
    //imagecontainer.style.border = "1px solid red";
    imagecontainer.style.position="absolute";
    imagecontainer.style.top = "23px";
    imagecontainer.style.height = (this.max - this.min) + "px";
    imagecontainer.style.width = (this.max < 100? 25 : 34) + "px";
    imagecontainer.style.background = "url(script/images/slidebgmax.png)";
    imagecontainer.style.backgroundRepeat = "no-repeat";
    imagecontainer.style.backgroundPosition = (this.max < 100? 4 : 8) + "px -" + (301 - this.max + this.min) + "px";
    this.imagecontainer = imagecontainer;

    slidercontainer.appendChild(imagecontainer);

    // create the knob as a GDraggableObject
    // Handle transparent PNG files in MSIE
    this.knob = document.createElement("div");
    this.knob.style.height="25px";
    this.knob.style.width= (this.max < 100? 25 : 34) + "px";

    if (this.tooltip) {
        this.knob.title = this.tooltip;
    }

    var sliderImg = (this.max < 100? "slider2" : "slider3");

    if (this.ie) {
      var loader = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='script/images/" + sliderImg + ".png', sizingMethod='scale');";
      this.knob.style.filter=loader;
    } else {
      this.knob.style.background = "url(script/images/" + sliderImg + ".png)";
    }

    var num1 = document.createElement("img");
    num1.id = this.id + "_num1";
    num1.src = ""
    num1.style.width="8px";
    num1.style.height="10px";
    num1.style.paddingTop = "4px";
    num1.style.paddingLeft = (this.max < 100? 5 : 1) + "px";

    var num2 = document.createElement("img");
    num2.id = this.id + "_num2";
    num2.src = "images/numbers/no0.png"
    num2.style.width="8px";
    num2.style.height="10px";
    num2.style.paddingTop = "4px";
    num2.style.paddingLeft = "4px";

    this.knob.appendChild(num1);
    this.knob.appendChild(num2);

    if (this.max >= 100) {
        var num3 = document.createElement("img");
        num3.id = this.id + "_num3";
        num3.src = "images/numbers/no0.png"
        num3.style.width="8px";
        num3.style.height="10px";
        num3.style.paddingTop = "4px";
        num3.style.paddingLeft = "4px";

        this.knob.appendChild(num3);
    }

    slidercontainer.appendChild(this.knob);
    this.slide=new GDraggableObject(this.knob, {container:slidercontainer});
    this.slide.setDraggableCursor('pointer');
    this.slide.setDraggingCursor('pointer');

    componentcontainer.appendChild(slidercontainer);

    this.container = componentcontainer;

    // attach the control to the parent
    parent.getContainer().appendChild(this.container);

    // init slider value
    this.setSlider(this.initValue, true);
	
    // Listen for the slider being draged
    GEvent.addListener(this.slide, "drag", function() {if (that.enabled) {that.setValue()}});
    GEvent.addListener(this.slide, "dragstart", function() {if (that.enabled) {that.oldValue = that.getValue()}});
    GEvent.addListener(this.slide, "dragend", function() {if (that.enabled) {that.onchange()}});
    // Listen for the slider being clicked, increase value by one
    GEvent.addListener(this.slide, "click", function() {if (that.enabled) {that.setSlider(that.getValue() + 1); that.onchange();}});

    return this.container;
}

SliderControlMax.prototype.initializePosition = function() {
    this.container.style.position = "absolute";
    this.container.style.top = this.y + "px";
    this.container.style.left = this.x + "px";
}

SliderControlMax.prototype.isEnabled = function() {
    return this.enabled;
}

SliderControlMax.prototype.enable = function() {
    this.enabled = true;
    if (this.slide) {
        this.slide.enable();
    }
}

SliderControlMax.prototype.disable = function() {
    this.enabled = false;
    if (this.slide) {
        this.slide.disable();
    }
}

SliderControlMax.prototype.setInactiveValue = function (value) {
    this.inactiveValue = value;
}

SliderControlMax.prototype.isInactiveValue = function () {
    return this.inactiveValue == this.getValue();
}

SliderControlMax.prototype.onchange = function() {
    if (this.oldValue != this.getValue()) {
        try {
            processEventSliderSetValue();
        } catch (e) {}
    }
}

SliderControlMax.prototype.setDefaultPosition = function(anchor, x, y) {
    this.position = new GControlPosition(anchor, new GSize(x, y));
}

// Set the default position for the control on map under zoom controls
SliderControlMax.prototype.getDefaultPosition = function() {
    return (this.position)? this.position : new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(this.x, this.y));
}

//******************************************************************************
// POSITION
//******************************************************************************
function PositionControl(title, lat, lng) {
    this.title = title;
    this.lat = lat;
    this.lng = lng;
    this.alt = 0;
}

PositionControl.prototype = new GControl(true, true);

// This function positions the slider to match the specified value
PositionControl.prototype.update = function() {

    if (!this.lat || !this.lng) {
        return;
    }

    $(this.containerLat).children().remove();
    $(this.containerLng).children().remove();
    $(this.containerAlt).children().remove();

    this.addPosition(this.containerLat, this.GPS2Degree(Math.abs(this.lat)), (this.lat > 0)? 'N' : 'S');
    this.addPosition(this.containerLng, this.GPS2Degree(Math.abs(this.lng)), (this.lng > 0)? 'E' : 'W');

    var _this = this;
    // altitude
    $.ajax({
        url: "altitude.html?lat=" + this.lat + "&lng=" + this.lng,
        cache: false,
        success: function(xml) {
            _this.addNumber(_this.containerAlt, Math.floor(xml));
            _this.addImg(_this.containerAlt, 'm', 1);
        }
    });
}

PositionControl.prototype.addPosition = function(parent, position, type) {
    var positions = position.split(" ");

    this.addNumber(parent, positions[0], true);
    this.addImg(parent, 'degree');
    this.addNumber(parent, positions[1], true);
    this.addImg(parent, 'mark');
    this.addNumber(parent, positions[2], true, true);
    this.addImg(parent, 'doublemark');

    this.addImg(parent, type, 1);
}

PositionControl.prototype.addImg = function(parent, name, left) {
    var img = document.createElement("img");
    img.src = 'images/numbers/' + name + '.png';
    if (left && 0 < left) {
        img.style.paddingLeft = left + "px";
    }
    parent.appendChild(img);
}
PositionControl.prototype.addNumber = function(parent, number, appendZero, appendFraction) {
    var _number = '' + number;
    if (number < 10) {
        _number = '0' + _number;
    }
    if (appendFraction && _number.indexOf('.') == -1) {
        _number = _number + '.0';
    }
    for (i = 0; i< _number.length; i++) {
        this.addImg(parent, ('.' == _number[i])? 'dot' : 'no' + _number[i]);
    }
}

PositionControl.prototype.setValue = function(lat, lng) {
    this.lat = lat;
    this.lng = lng;
    this.update();
}

PositionControl.prototype.GPS2Degree = function(value) {

    var loc = value;

    var hour = Math.floor(loc);
    var minutes = ((loc - hour) * 60);
    var min = Math.floor(minutes);
    var sec = (Math.round((minutes - min) * 600)) / 10;

    if (sec == 60) {
        sec = 0;
        min++;
    }

    return (hour + " " + min + " " + sec);
}

PositionControl.prototype.initialize = function(map) {

    // obtain Function Closure on a reference to "this"
    var that = this;
    // store a reference to the map so that we can make calls on it
    this.map = map;

    // Is this MSIE, if so we need to use AlphaImageLoader
    var agent = navigator.userAgent.toLowerCase();
    if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){this.ie = true} else {this.ie = false}

    // create the background graphic as a <div> containing an image
    var container = document.createElement("div");
    container.title = this.title;
    container.style.width="92px";
    container.style.height="42px";
    container.style.padding="6px";
    container.style.background = "url(script/images/positioner.png)";
    container.style.backgroundRepeat = "no-repeat";
    container.style.backgroundPosition = "0px 0px";

    this.containerLat = document.createElement("div");
    this.containerLat.style.paddingBottom = "5px";
    this.containerLng = document.createElement("div");
    this.containerLng.style.paddingBottom = "5px";
    this.containerAlt = document.createElement("div");

    container.appendChild(this.containerLat);
    container.appendChild(this.containerLng);
    container.appendChild(this.containerAlt);

    this.container = container;

    // attach the control to the map
    map.getContainer().appendChild(container);

    this.update();

    return container;
}

PositionControl.prototype.setDefaultPosition = function(anchor, x, y) {
    this.position = new GControlPosition(anchor, new GSize(x, y));
}

// Set the default position for the control on map under zoom controls
PositionControl.prototype.getDefaultPosition = function() {
    return (this.position)? this.position : new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(54, 45));
}

//******************************************************************************
// Opacity control by Klokan Petr Pridal (based on XSlider of Mike Williams)
//******************************************************************************

function OpacityControl(overlay, opacity) {
  this.overlay = overlay;
  this.opacity = opacity;
}
OpacityControl.prototype = new GControl();

// This function positions the slider to match the specified opacity
OpacityControl.prototype.setSlider = function(pos) {
  var left = Math.round((58 * pos));
  this.slide.left = left;
  this.knob.style.left = left + "px";
  this.knob.style.top = "0px"; // correction001
}

// This function reads the slider and sets the overlay opacity level
OpacityControl.prototype.getOpacity = function() {
    return this.opacity;
}

OpacityControl.prototype.setOpacity = function() {
    this.opacity = this.slide.left / 58;

    if(typeof(this.overlay.style.filter)=='string') {
        //this.overlay.style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity=50)';
        //alert(this.overlay.filters.item("DXImageTransform.Microsoft.Alpha").apply());
    }
    if(typeof(this.overlay.style.KHTMLOpacity)=='string'){
        this.overlay.style.KHTMLOpacity = this.opacity;
    }
    if(typeof(this.overlay.style.MozOpacity)=='string'){
        this.overlay.style.MozOpacity = this.opacity;
    }
    if(typeof(this.overlay.style.opacity)=='string'){
        this.overlay.style.opacity = this.opacity;
    }

    GEvent.trigger(this, "onchange");
}

// This gets called by the API when addControl(new OpacityControl())
OpacityControl.prototype.initialize = function(map) {
  var that = this;
  this.map = map;

  // Is this MSIE, if so we need to use AlphaImageLoader
  var agent = navigator.userAgent.toLowerCase();
  if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){this.ie = true} else {this.ie = false}

  // create the background graphic as a <div> containing an image
  var container = document.createElement("div");
  container.style.width = "70px";
  container.style.height = "21px";

  // Handle transparent PNG files in MSIE
  if (this.ie) {
    var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='script/images/opacity-slider.png', sizingMethod='crop');";
    container.innerHTML = '<div style="height:21px; width:70px; ' + loader + '" ></div>';
  } else {
    container.innerHTML = '<div style="height:21px; width:70px; background-image:url(script/images/opacity-slider.png)" ></div>';
  }

  // create the knob as a GDraggableObject
  // Handle transparent PNG files in MSIE
  if (this.ie) {
    var loader = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='script/images/opacity-slider.png', sizingMethod='crop');";
    this.knob = document.createElement("div");
    this.knob.style.height = "21px";
    this.knob.style.width = "13px";
    this.knob.style.overflow = "hidden";
    this.knob_img = document.createElement("div");
    this.knob_img.style.height = "21px";
    this.knob_img.style.width = "83px";
    this.knob_img.style.filter = loader;
    this.knob_img.style.position = "relative";
    this.knob_img.style.left = "-70px";
    this.knob.appendChild(this.knob_img);
  } else {
    this.knob = document.createElement("div");
    this.knob.style.height = "21px";
    this.knob.style.width = "13px";
    this.knob.style.backgroundImage = "url(script/images/opacity-slider.png)";
    this.knob.style.backgroundPosition = "-70px 0px";
  }
  container.appendChild(this.knob);
  this.slide=new GDraggableObject(this.knob, {container:container});
  this.slide.setDraggableCursor('pointer');
  this.slide.setDraggingCursor('pointer');
  this.container = container;

  // attach the control to the map
  map.getContainer().appendChild(container);

  // init slider
  this.setSlider(this.opacity);
  this.setOpacity(this.opacity);

  // Listen for the slider being moved and set the opacity
  GEvent.addListener(this.slide, "dragend", function() {that.setOpacity()});

  return container;
}

// Set the default position for the control
OpacityControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(250, 40));
}
