MTileLayerControl = function(MOptions) {
	MOptions = MOptions ? MOptions : {};
	this.self = this;

	this.parent = MOptions.parent ? MOptions.parent : null;
	this.map = MOptions.map ? MOptions.map : null;
	this.caption = MOptions.caption ? MOptions.caption : 'Overlay';
	this.background = MOptions.background ? MOptions.background : '';
	this.sliderWidth = MOptions.sliderWidth ? MOptions.sliderWidth : 200;
	this.knobWidth = MOptions.knobWidth ? MOptions.knobWidth : 20;
	this.value = MOptions.value ? MOptions.value : 50;
	this.ondrag = MOptions.ondrag ? MOptions.ondrag : null;
	this.ondragend = MOptions.ondragend ? MOptions.ondragend : null;
	this.checked = MOptions.checked ? 'checked' : '';
	this.maxZoom = MOptions.maxZoom ? MOptions.maxZoom : null;
	this.minZoom = MOptions.minZoom ? MOptions.minZoom : null;

	this.overlay;
	this.overlayTheme = MOptions.overlayTheme ? MOptions.overlayTheme : null;


	if (this.overlayTheme) {
		this.create();
		this.overlay = this.createOverlay(this.overlayTheme);
		this.overlay.getTileLayer().op = 0.5;
		this.overlay.getTileLayer().getOpacity = function () {return this.op};
		this.setValue(this.value);

	}


};


MTileLayerControl.prototype.initialize = function() {

	if (this.checked) {
		this.cBox.checked = true;
		this.cbClick();
	}
	this.setValue(this.value);
}

MTileLayerControl.prototype.create = function() {
	var self = this.self;

	this.container = document.createElement('DIV');
	this.container.style.position = 'relative';
	this.container.style.overflow = 'hidden';
	this.container.style.textAlign = 'left';
	this.container.style.background = this.background;
	this.container.style.border = '1px solid #dddddd';

	if (this.parent) {
		this.parent.appendChild(this.container);
	}



	var oTable = document.createElement('TABLE');
	this.container.appendChild(oTable);
	oTable.setAttribute('width','100%');
	oTable.setAttribute('cellPadding','0');
	oTable.setAttribute('cellSpacing','0');
	oTable.style.background = this.background;

	var oTBody = document.createElement('TBODY');
	oTable.appendChild(oTBody);

// ----------------
	var oTRow = document.createElement('TR');
	oTBody.appendChild(oTRow);

	var oTCell = document.createElement('TD');
	oTCell.style.textAlign = 'center';
	oTCell.style.width = '10px';
	oTCell.style.verticalAlign = 'middle';
	oTRow.appendChild(oTCell);
	oTCell.style.font = 'normal 10px verdana';


	if (document.all && !window.opera && document.createElement) {
		this.cBox = document.createElement('<INPUT type="checkbox" '+this.checked+'>');
	}
	else {
		this.cBox = document.createElement('INPUT');
		this.cBox.setAttribute('type','checkbox');
		this.cBox.setAttribute('checked',this.checked);
	}
	this.cBox.onclick = function(){self.cbClick()};

	oTCell.appendChild(this.cBox);

//---
	var oTCell = document.createElement('TD');
	oTCell.style.textAlign = 'left';
	oTCell.style.verticalAlign = 'middle';
	oTRow.appendChild(oTCell);
	oTCell.style.font = 'normal 10px verdana';
	oTCell.innerHTML = this.caption;
	oTCell.style.width = '80px';



// ==========================================================


	var oTCell = document.createElement('TD');
	oTCell.style.textAlign = 'left';
	oTCell.style.verticalAlign = 'middle';
	oTRow.appendChild(oTCell);
	oTCell.style.font = 'normal 10px verdana';


	var oTable = document.createElement('TABLE');
	oTCell.appendChild(oTable);
	oTable.setAttribute('width','100%');
	oTable.setAttribute('cellPadding','0');
	oTable.setAttribute('cellSpacing','0');
//	oTable.setAttribute('border','1');
	oTable.style.background = this.background;
	this.sliderTable = oTable;

	var oTBody = document.createElement('TBODY');
	oTable.appendChild(oTBody);

// ----------------
	var oTRow = document.createElement('TR');
	oTBody.appendChild(oTRow);

//---
	var oTCell = document.createElement('TD');
	oTCell.style.textAlign = 'right';
	oTCell.style.verticalAlign = 'middle';
	oTRow.appendChild(oTCell);
	oTCell.style.font = 'normal 10px verdana';
	oTCell.innerHTML = 'Opacity:&nbsp;&nbsp;';

//---
	var oTCell = document.createElement('TD');
	oTRow.appendChild(oTCell);
	oTCell.style.textAlign = 'center';
	oTCell.style.verticalAlign = 'middle';
	oTCell.style.font = 'normal 10px verdana';
	oTCell.style.width = this.sliderWidth + 'px';

	this.slider = this.createSlider();
	oTCell.appendChild(this.slider);

//---
	var oTCell = document.createElement('TD');
	oTCell.style.textAlign = 'left';
	oTCell.style.verticalAlign = 'middle';
	oTRow.appendChild(oTCell);
	oTCell.style.font = 'normal 10px verdana';

	this.label = this.createLabel();
	oTCell.appendChild(this.label);
// ==========================================================

	this.totalPixels = this.slider.clientWidth - this.knobWidth + 2;
	this.setSliders();
	this.attachEventHandlers();
//	this.sliderTable.style.display = 'none';

};







MTileLayerControl.prototype.createOverlay = function(theme) {
	var cRight = new GCopyrightCollection('MarceloŠ2009');
	var newLayer = new GTileLayer(cRight,11,13);
	newLayer.getOpacity = function () {return 0.2};

	if (this.minZoom) {
		newLayer.minResolution = function(){return this.minZoom};
	}
	if (this.maxZoom) {
		newLayer.maxResolution = function(){return this.maxZoom};
	}

	var newOverlay = new GTileLayerOverlay(newLayer);
	return newOverlay;
}

























MTileLayerControl.prototype.cbClick = function() {
	if (this.cBox.checked) {
		this.overlay.show();
		this.sliderTable.style.display = '';
	}
	else {
		this.overlay.hide();
		this.sliderTable.style.display = 'none';
	}
}

MTileLayerControl.prototype.createLabel = function() {
	var label = document.createElement('DIV');
	label.style.position = 'relative';
	label.style.overflow = 'hidden';
	label.style.textAlign = 'center';
	label.style.height = '16px';
	label.style.width = '50px';
	label.style.font = 'bold 12px verdana';
	return label;
};

MTileLayerControl.prototype.createSlider = function() {
	var slider = document.createElement('DIV');
	slider.style.position = 'relative';
	slider.style.overflow = 'hidden';
	slider.style.border = '1px solid gray';
	slider.style.textAlign = 'left';
	slider.style.height = '12px';
	slider.style.width = this.sliderWidth + 'px';
	slider.style.margin = 'auto';

//----------
	this.sliderDiv1 = document.createElement('DIV');
	this.sliderDiv1.style.width = this.knobWidth + 'px';
	this.sliderDiv1.style.height = this.knobWidth + 'px';
	this.sliderDiv1.style.background = '#888888';
	this.sliderDiv1.title = 'Drag and release to change opacity ...';
	slider.appendChild(this.sliderDiv1);

//----------

	this.slider1 = new GDraggableObject(this.sliderDiv1,{container:slider,left:0,top:0});
	return slider;

};





MTileLayerControl.prototype.attachEventHandlers = function() {
	var self = this.self;
	GEvent.addListener(this.slider1,'mousedown',function(){self.sliderMouseDown(this)});
	GEvent.addListener(this.slider1,'mouseup',function(){self.sliderMouseUp(this)});
	GEvent.addListener(this.slider1,'drag',function(){self.sliderDrag(this)});
	GEvent.addListener(this.slider1,'dragend',function(){self.sliderDragEnd(this)});
};

MTileLayerControl.prototype.sliderMouseDown = function(slider) {
	if (typeof this.onmousedown == 'function') {
		this.onmousedown(slider);
	}
};

MTileLayerControl.prototype.sliderMouseUp = function(slider) {
	if (typeof this.onmouseup == 'function') {
		this.onmouseup(slider);
	}
};


MTileLayerControl.prototype.sliderDrag = function(slider) {
	this.value = (this.slider1.left / this.totalPixels * 100);
	this.setLabel();
	if (typeof this.ondrag == 'function') {
		this.ondrag(this.value);
	}
};


MTileLayerControl.prototype.sliderDragEnd = function() {
	this.map.removeOverlay(this.overlay);
	this.overlay.getTileLayer().op = this.value/100;
	this.map.addOverlay(this.overlay);
	
	if (typeof this.ondragend == 'function') {
		this.ondragend(this.value);
	}
};

MTileLayerControl.prototype.setLabel = function() {
	this.label.innerHTML = this.value.toFixed(0) + ' %';
};

MTileLayerControl.prototype.setValue = function(pos1) {
	this.value = pos1;
	this.setSliders();
	this.setLabel();
	this.sliderDragEnd();
};


MTileLayerControl.prototype.setSliders = function() {
	this.pxPos1 = Math.round((this.value/100)*this.totalPixels);
	var p1 = new GPoint(this.pxPos1,0);
	this.slider1.moveTo(p1);

};



MTileLayerControl.prototype.show = function() {
	this.container.style.display = '';
	if (this.overlay) {
//		this.overlay.show();
	}
};
MTileLayerControl.prototype.hide = function() {
	this.container.style.display = 'none';
	if (this.overlay) {
//		this.overlay.hide();
	}
};



