var ns = NUSA.namespace("Maps");

	function keepOpen() {
		var ts = document.getElementById("topicSelect");
    	if(ts && ts.options.length > 0) { 
    		ts.options[1].selected = true;
        	ts.focus();
    		ts.size = ts.options.length;
    	}
 	};
 	

ns.App = function() {
	
	this.settings = {restBase:("http://"+document.location.host+'/rest')};
	
	this.maps = [];
	this.topicCategories = [];
	this.defaultValues =[];
	this.defaultMaps=[];
	this.browser = {isIE:false};
	
	this.currentHash = "#";
	this.hashHistory = [];
	this.checkURLInterval;
	
	this.mainView = new NUSA.Maps.App.MainView(this);
	this.compareView = new NUSA.Maps.App.CompareView(this);
	this.summaryView = new NUSA.Maps.App.SummaryView(this);
	this.currentView;
	
	this.checkURLDelegate;	
	
	this.historyHack;
		
};

ns.App.prototype.init = function(viewName){
	
	this.setView(viewName);
	this.loadTopics();
	
	if (document.all) {
		this.browser.isIE = true;
	}
		
};

ns.App.prototype.isInitCompleted = function() {
	
	var initCompleted = true;
	
	if (!this.topicCategories.length) {
		initCompeleted = false;
	}
	
	if (initCompleted) {
		
		var changeViewDelegate = new NUSA.Utils.Delegate(this,this.changeView);
		var trapPoundDelegate = new NUSA.Utils.Delegate(this,this.trapPound);
				
		this.checkURLDelegate = new NUSA.Utils.Delegate(this,this.backButtonPressed);
		this.lastAnchor = document.location.hash;
		//this.checkURLInterval = setInterval(this.checkURLDelegate, 200);
		
		$("#compareTab").hide();
		$("#summaryTab").hide();
		
		this.historyHack = $("#historyHack").get()[0];		
		$("#mapTab").bind("click","main",changeViewDelegate);
		$("#compareTab").bind("click","compare",changeViewDelegate);
		$("#summaryTab").bind("click","summary",changeViewDelegate);
		$("#content").bind("click",trapPoundDelegate);
		
		this.getURLInfo();
		this.currentView.init();
	}
	 
}

//-------------------------------------------
// This method sets the inital view for the application
//-------------------------------------------

ns.App.prototype.setView = function(viewName){
	
	switch(viewName) {
		
		case "main":
			this.currentView = this.mainView;
			break;
		
		case "compare":
			this.currentView = this.compareView;
			break;	
	}

};

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

ns.App.prototype.backButtonPressed= function(response){
	
	var hash = null;

	
	/*if ($.browser.msie) {
		
		var hackIFrame = this.historyHack.contentDocument || this.historyHack.contentWindow.document;
		hash = hackIFrame.location.href.replace(/^[^\?]*\?/, '');
	}
	
	else {
		hash = document.location.hash.replace("#","");
	}*/
	
	hash = document.location.hash.replace("#","");
	if (this.currentHash != hash) {
		//if (hash != "" && hash != "#" && this.currentHash != "#"){
			this.currentView.handleBackButton(hash);
		//}
		
	}

};

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

ns.App.prototype.changeBackAnchor= function(hash){
	$("#backAnchor").html("<div id='"+hash+"'></div>");
};

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


ns.App.prototype.changeAnchor= function(hash){
	
	clearInterval(this.checkURLInterval);
	//this.hashHistory.push(anchor);
	
	/*if ($.browser.msie) {

		var hackIFrame = this.historyHack.contentWindow.document;
		//var newHREF = "historyHack.html?"+hash;
		var newHREF = "http://"+document.location.host+"/historyHack.html?"+hash;
		if (newHREF != hackIFrame.location.href) {
			hackIFrame.location.href = newHREF;
		}
	} */
	
	if (this.currentHash != "#"){
		$("#mapAnchor").html("<div id='"+hash+"'></div>");
		document.location.href="http://"+document.location.host+document.location.pathname+"#"+hash;
	}
	
	this.currentHash = hash;
	this.checkURLInterval = setInterval(this.checkURLDelegate, 200);
	
};

ns.App.prototype.changeView = function(viewName,reserved) {

	if (typeof viewName == "object") {
		viewName = viewName.data;
	}
	
	this.currentView.hide();
	var currentMap = this.currentView.getCurrentMap();
	
	switch(viewName) {
		
		case "main":			
			this.currentView = this.mainView;
			break;
		
		case "compare":
			this.currentView = this.compareView;
			break;	
			
		case "summary":
			this.currentView = this.summaryView;
	}
	
	if (this.currentView.isInitialized()) {
		this.currentView.show(currentMap);
	}
	
	else {
		this.currentView.init(currentMap);	
	}
	
	
	return(false);
	
};

/////////////////////////////////////////////
// COMMON METHODS
// This Section contains methods that are 
// used by each view.
/////////////////////////////////////////////

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

ns.App.prototype.getURLInfo = function(response){
	
	var URL = document.location.href;
	var varPos = URL.search(/\#/);
	var vars = new Object();
	
	if (varPos){
		var varString = URL.substr((varPos+1),URL.length);
		var rawStrings = varString.replace(/^[^(#|\?)]*(#|\?)/,"").split("&");
		
		
		for (var x = 0; x < rawStrings.length; x++){
			var t = rawStrings[x].split("=");
			if (t.length == 2){
				this.defaultValues[t[0]] = t[1];
			}
		}
		
	}
	
};

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

ns.App.prototype.loadTopics = function(){

	var topicsDelegate = new NUSA.Utils.Delegate(this,this.topicsLoaded);
	
	var restPath = "topics.json";
    var pars = '';
	
    $.getJSON(restPath,0,topicsDelegate);
    
};

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

ns.App.prototype.setCookie = function(cookieName,cookieValue,nDays){
	
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString();
};

ns.App.prototype.readCookie = function(name) {
	
	var keyword=[];
	
	keyword[name] = 1;

	var cookieValues = document.cookie.split(';');
	for(var i=0;i < cookieValues.length;i++) {
		
		var cookie = cookieValues[i];
		cookie = cookie.replace(/\s/i,'');
		var crumbs = cookie.split('=');
		if (keyword[crumbs[0]]){
			return(crumbs[1]);	
		}
	}
	
	return false;
};


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

ns.App.prototype.topicsLoaded = function(response){
	
	this.topicCategories = response.categories;
	this.defaultMaps = response.defaultMaps;
	this.isInitCompleted();	
	
};

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

ns.App.prototype.trapPound = function(event){
	
	if (event.target.href) {
		
		var url = event.target.href;
		
		if (url.substr((url.length-1),1) == "#"){
			return(false);
		}
		
	}

};



