/**
 * @author toby
 */
var CustomControl = function(thismap){
	this.setText("zoom in");
	this.setTextOff("zoom in");
	this.setWidth(100);
	this.setHeight(15);
	this.setClassname("map-button");
	this.setAnchor(G_ANCHOR_TOP_LEFT);
	this.setOffsetx(5);
	this.setOffsety(5);
	this.setType("zoomin");
	this.thismap = thismap;
}

CustomControl.prototype = new GControl(true);

CustomControl.prototype.initialize = function(map){

	this.container_ = document.createElement("div");
	this.container_.innerHTML = this.getText();
	this.container_.style.width = this.getWidth() + "px";
	this.container_.style.height = this.getHeight() + "px";
	this.container_.className = this.getClassname();
	this.container_.style.cursor =  "pointer";

	var customcontrol = this;
	var thismap = this.thismap;

	switch(this.getType()){
		
		/* Zoom In button */
		case "zoomin":
		GEvent.addDomListener(this.container_, "click", function(){
			var currentzoom = map.getZoom();
			if(currentzoom < 15){
				map.setZoom(currentzoom + 1);	
			}		
		});
		break;
		
		/* Zoom Out button */
		case "zoomout":
		GEvent.addDomListener(this.container_, "click", function(){
			var currentzoom = map.getZoom();
			if(currentzoom > 1){
				map.setZoom(currentzoom - 1);
			}		
		});
		break;
		
		/* Jump In button */
		case "jumpin":
		GEvent.addDomListener(this.container_, "click", function(){
			if(map.getZoom()<5){
				map.setZoom(6);
			} else if(map.getZoom()>=5 && map.getZoom()<10){
				map.setZoom(10);
			} else {
				map.setZoom(15);
			}
			
		});
		break;
		
		/* Add User Content button */
		case "addusercontent":
		
		GEvent.addDomListener(this.container_, "click", function(){
			var state = thismap.getState();
			if(state == "view"){
				map.closeInfoWindow();

				thismap.getWrapper().className = "editstate";
				
				thismap.setState("edit");
				customcontrol.container_.innerHTML = customcontrol.getTextOff();
				addusercontent = GEvent.addListener(map,'click',function(overlay,latlng){
					if(latlng){
						var lng = latlng.lng();
						var lat = latlng.lat();
						var uploadform = thismap.uploadForm(lng,lat);
						thismap.map.openInfoWindow(latlng,uploadform);
					}
				});
				
				customcontrol.inst = document.getElementById("instructions");
				customcontrol.instlinkadd = document.getElementById("instructions-link-add");
				customcontrol.instlinkbrowse = document.getElementById("instructions-link-browse");
				customcontrol.instlinkclose = document.getElementById("instructions-link-close");
				customcontrol.instlinkprint = document.getElementById("instructions-link-print");
				
				if(customcontrol.inst){
					customcontrol.inst.style.display = "block";
					customcontrol.inst.style.width = '700px';
					customcontrol.inst.style.height = '340px';
					customcontrol.inst.style.top = '5px';
					customcontrol.inst.style.padding = '20px';
					customcontrol.instlinkadd.style.display = 'block';
					customcontrol.instlinkbrowse.style.display = 'block';
					customcontrol.instlinkclose.style.display = 'block';
					customcontrol.instlinkprint.style.display = 'block';
					customcontrol.instlinkprint.style.position = 'absolute';
					customcontrol.instlinkprint.style.bottom = '15px';
					customcontrol.instlinkprint.style.right = '5px';
					customcontrol.instlinkprint.style.paddingTop = 0;
				}

				if(customcontrol.instlinkadd){
					customcontrol.instlinkadd.onclick = function(){
						customcontrol.inst.style.width = '260px';
						customcontrol.inst.style.height = '220px';
						customcontrol.inst.style.top = '130px';
						customcontrol.inst.style.padding = '10px';
						customcontrol.inst.style.paddingTop = '20px';
						customcontrol.inst.style.overflow = 'auto';
						customcontrol.instlinkadd.style.display = 'none';
						//customcontrol.instlinkclose.style.display = 'none';
						customcontrol.instlinkprint.style.position = 'relative';
						customcontrol.instlinkprint.style.bottom = 0;
						customcontrol.instlinkprint.style.right = 0;
						customcontrol.instlinkprint.style.marginTop = '20px';
						return false;
					}
				}
				
				if(customcontrol.instlinkbrowse){
					customcontrol.instlinkbrowse.onclick = function(){
						customcontrol.closeInstructions();
						return false;
					}
				}
				
				if(customcontrol.instlinkclose){
					customcontrol.instlinkclose.onclick = function(){
						customcontrol.closeInstructions();
						return false;
					}
				}
				
				customcontrol.closeInstructions = function(){
					customcontrol.inst.style.display = "none";
					thismap.getWrapper().className = "";
					if(customcontrol.inst) customcontrol.inst.style.display = "none";
					GEvent.removeListener(addusercontent);
					thismap.setState("view");
					customcontrol.container_.innerHTML = customcontrol.getText();
				}
				
				if(customcontrol.instlinkprint){
					customcontrol.instlinkprint.onclick = function(){
						popup('http://www.vam.ac.uk/apps/map_uploads/1632_beach/instructions.html','Instructions',400, 560);
						return false;
					}
				}
				
			}
			else{
				thismap.getWrapper().className = "";
				if(customcontrol.inst) customcontrol.inst.style.display = "none";
				map.closeInfoWindow();
				GEvent.removeListener(addusercontent);
				thismap.setState("view");
				customcontrol.container_.innerHTML = customcontrol.getText();
			}
		});
		break;
		
		case "showplacenames":
		
		GEvent.addDomListener(this.container_, "click", function(){
			var state = customcontrol.getState();
			if(state == "show"){
				customcontrol.setState("hide");
				customcontrol.container_.innerHTML = customcontrol.getText();
				map.setMapType(myMap.getMapType());
			}
			else if(state == "hide"){
				customcontrol.setState("show");
				customcontrol.container_.innerHTML = customcontrol.getTextOff();
				map.setMapType(G_SATELLITE_MAP);
			}
		});
		break;
		
		case "reset":

    this.container_.setAttribute("id","resetbutton");
    GEvent.addDomListener(this.container_, "click", function(){
                thismap.setLatitude(initLat);
                thismap.setLongitude(initLng);
                thismap.setLocation();
                thismap.setZoomLevel(initZoom);
                map.setCenter(thismap.getLocation(), thismap.getZoomLevel());
                map.closeInfoWindow();
    });

    break;

		
	}
	map.getContainer().appendChild(this.container_);
	return this.container_;
}

CustomControl.prototype.getDefaultPosition = function(){
	return new GControlPosition(this.anchor_, new GSize(this.offsetx_, this.offsety_));
}

CustomControl.prototype.setText = function(text){
	this.text_ = text;
}

CustomControl.prototype.getText = function(){
	return this.text_;
}

CustomControl.prototype.setTextOff = function(textoff){
	this.textoff_ = textoff;
}

CustomControl.prototype.getTextOff = function(){
	return this.textoff_;
}

CustomControl.prototype.setWidth = function(width){
	this.width_ = width;
}

CustomControl.prototype.getWidth = function(){
	return this.width_;
}

CustomControl.prototype.setHeight = function(height){
	this.height_ = height;
}

CustomControl.prototype.getHeight = function(){
	return this.height_;
}

CustomControl.prototype.setClassname = function(classname){
	this.classname_ = classname;
}

CustomControl.prototype.getClassname = function(){
	return this.classname_;
}

CustomControl.prototype.setAnchor = function(anchor){
	this.anchor_ = anchor;
}

CustomControl.prototype.getAnchor= function(){
	return this.anchor_;
}

CustomControl.prototype.setOffsetx = function(offsetx){
	this.offsetx_ = offsetx;
}

CustomControl.prototype.getOffsetx = function(){
	return this.offsetx_;
}

CustomControl.prototype.setOffsety = function(offsety){
	this.offsety_ = offsety;
}

CustomControl.prototype.getOffsety = function(){
	return this.offsety_;
}

CustomControl.prototype.setType = function(type){
	this.type_ = type;
}

CustomControl.prototype.getType = function(){
	return this.type_;
}

CustomControl.prototype.setState = function(state){
	this.state_ = state;
}

CustomControl.prototype.getState = function(){
	return this.state_;
}

