/*
 * The pop-up code-name window 
 * @param windowDiv The existing div to use for this window. 
 */	
Extreme.nameWindow = function(windowDiv) {
	
	var triggerEl = Ext.get('nametool-gobutton');
	var theWindow = Ext.get(windowDiv);
	var nameField = Ext.get('nametool_namebox');
	
	var thisClass = this;
	
	//load the google maps API and once complete create the name tool window which uses the maps	
	try {
		Extreme.themap = new GMap2(document.getElementById("extreme-dest-map"),{ size: new GSize(420,336)} );
		Extreme.themap.addControl(new GLargeMapControl());
	}
	catch(err) {
		//if this doesn't work, then the page is being called from the wrong location (case is important). So redirect.
		window.location = "http://www.penguin.com.au/puffin/mini-sites/extreme/default.cfm"
	}
	
	
	this.doOpen = function() {
		var enteredName = nameField.getValue();
		if(enteredName != "")
			thisClass.showBox(enteredName);
		else
			nameField.highlight("ff8d6f", { duration: 2 });
	}
	
	nameField.on('keyup', function(e){
							if(e.getKey() == Ext.EventObject.RETURN)
								this.doOpen();
						},this);	
	
	triggerEl.addListener('mouseup', this.doOpen, this, {preventDefault:true});
		
	Ext.get('nametool-closelink').addListener('mouseup', function(){this.hideBox();}, this, {stopEvent:true});
	
	/* Required to fix bug in safari for mac where window displays behind content instead of on top*/
	theWindow.setVisibilityMode(Ext.Element.DISPLAY);
	theWindow.hide();
	/*end fix */
	
	this.showBox = function(enteredName) {			
		
		Ext.get('main-container').mask();		
		this.loadData(enteredName);	
		theWindow.show();		
		theWindow.center();
		
		var windowDrag = new Ext.dd.DD(windowDiv,'drag-window');
		windowDrag.setHandleElId('nametool-window-header');
		
		
	}
	
	this.hideBox = function() {
		//theWindow.hide();
		theWindow.ghost();
		Ext.get('main-container').unmask();
	}
	
	
	this.loadData = function(enteredName){
		
		Ext.Ajax.request({
			url : 'services/getNameDest.cfm', 
			params : { name: enteredName},
			method: 'GET',
			
			success: function(result, request) { 	
				
				var jsonData = Ext.util.JSON.decode(result.responseText);
				
				Ext.get('nametool-window-codename').update(jsonData.name);
				Ext.get('nametool-window-destination').update(jsonData.destination);
				
				var longit = jsonData.longit;
				var latit = jsonData.latit;
				var zoomfact = jsonData.zoomfact;
				
				Extreme.themap.setCenter(new GLatLng(latit, longit), zoomfact, G_SATELLITE_MAP);
				//Extreme.themap.openInfoWindow(Extreme.themap.getCenter(), document.createTextNode(jsonData.destination));
				

			},
			failure: function( result, request) { 
				alert("For some unknown reason the computer couldn't get the information you requested."); 
			}
			 
		});
	
	}
	

}

