
var Main = Class.create();
Main.prototype = {
	messages : null,
	feedPoller : null,
	hilitedUsers : null,
	topFlash : null,
	
	nick : null,
	avatarURL : null,
	status : null,
	
	lightBoxWin : null,
	login : null,
	
	initialize : function(main){
	},
	isChatOn : function(){
		return chatMode;
	},
	setLogin : function(l){
		this.login = l;
	},
	start : function(){
		this.messages = new Messages(this);
		this.hilitedUsers = new HilitedUsers(this, this.messages);
		this.topFlash = new TopFlash(this);
		
		this.messages.getMessages('all', false);
	
		this.updateHilitedUsers();
		setTimeout("main.getGlobalStats()", 2000);	
		// this.feedPoller = new FeedPoller(this.messages);
	},
	
	getInclude : function(id, replacements){
		var s = includes[id];
		
		for (var i=replacements.length-1; i>=0; i--){
		//for (var i=0; i<replacements.length; i++){
			s = s.gsub("%" + (i+1), replacements[i] );
		}
		
		return s;
	},
	
	getNick : function(){
		return $("nick").value;
	},
	getStatus : function(){
		return $("status_id").value;
	},
	logout : function(){
		$("nick").value = "";
		this.topFlash.reset();
	},
	setSession : function(name, val){
		var str = "name=" + name + "&val=" + val;
		//alert("session=" + str);
		
		new Ajax.Request('app/setSession.php?' + str, 
			{
				method:'post' 
			}
		);
	},
	
	setSessionWithWait : function(name, val){
		var str = "name=" + name + "&val=" + val;
		//alert("session=" + str);
		
		new Ajax.Request('app/setSession.php?' + str, 
			{
				method:'post',
				
				onSuccess: function(transport){
				  var response = transport.responseText || "no response text";
					if (response == "err"){
					} else {
						jumpToJaiku();
					}
			    }.bind(this)
			}
		);
	},
	
	// misc
	openJaikuLoginBox : function(){
		Element.show($("login-wrapper"));
	},
	openJaikuRegistration : function(){
		document.location = includes["jaiku_return_url"];
	},
	closePopup : function(){
		if (this.lightBoxWin != null){
			this.lightBoxWin.deactivate();
			this.lightBoxWin = null
		}
		this.topFlash.slowAll();
	},
	openGroupInfo : function(num){
		
		//this.saveUserStatus(num);
		//this.updateAfterStatusChange(num);
		
		var urls = ["rookinurkka_en_lopeta.php", "rookinurkka_harkitsen.php", "rookinurkka_aion_lopettaa.php", "rookinurkka_lopettamassa.php", "rookinurkka_lopettanut.php", "rookinurkka_en_polta.php"];
		
		var objLink = document.createElement('a');
		var url = urls[num-1] + "?anonymous=" + this.login.isAnonymous() + "&status_id=" + this.getStatus() + "&logged=";
		url += this.getNick().length > 0? "true" : "false";
		objLink.setAttribute('href',url);
		
		this.lightBoxWin = new lightbox(objLink);
		this.lightBoxWin.activate();
	},
	groupInfoClick : function(nick){
		this.messages.getMessages("user", false, nick);
		this.closePopup();
	},
	groupInfoStatusClick : function(status_id){
		this.messages.getMessages("status", false, status_id);
		this.closePopup();
	},
	updateStatus : function(num){
		if (this.lightBoxWin != null){
			this.lightBoxWin.deactivate();
			this.lightBoxWin = null;
			this.topFlash.slowAll();
		}
		
		
		this.saveUserStatus(num);
		this.updateAfterStatusChange(num);
	},
	
	updateAfterStatusChange : function(num){
		var firstLoad = (this.status == null);

		this.status = num;
		
		$("own_status").firstChild.nodeValue = status_names[num];
		$("own_status").className =  "s_" + num;
		$("status_id").value = num;
		
		this.setSession("status", num);
		this.setSession("status_txt", status_names[num]);

		this.topFlash.hiliteBubble(num);
		
		if (firstLoad == true){
			//this.updateHilitedUsers();
			//this.getGlobalStats();
		}
	},
	
	updateHilitedUsers : function(){
		// last switcher, to same status as me
		this.hilitedUsers.getMessage("newestSwitcher", this.status);
		// same user
		this.hilitedUsers.getMessage("static", "fressismatti");
		// newest quitter
		this.hilitedUsers.getMessage("status", 4);
	},
	
	saveUserStatus : function(num){
		var str = "?nick=" + this.getNick() + "&status=" + num;
	
		new Ajax.Request('app/setUserStatus.php' + str, 
			{ method:'post'}
		);
	},
	getGlobalStats : function(){
		var str = "?numOfStatuses=" + status_names.length;
	
		new Ajax.Request('app/getGlobalStats.php' + str, 
			{
				method:'post', 
				
				onSuccess: function(transport){
				  var response = transport.responseText || "no response text";
					if (response == "err"){
					} else {
						var j = response.evalJSON();
						this.topFlash.showStats(j);
					}
			    }.bind(this)
				
				
			}
		);
	},
	jumpToJoinSection : function(num){
		window.location = "tulemukaan.php?group=" + num;
	}
	
	
}




