/*
* Portal JS object
* need oStorage
*
* ver 0.92
*
*	History:
*		0.92	30.07.09/goshi	add debug.start and debug.end methods
*		0.91	16.07.09/goshi	fix bug with IE, event deattach and callback function
*		0.9	04.05.09/goshi	add debug object into core
*		0.8	03.05.09/goshi	improve event listeners 
*		0.71	02.05.09/goshi	fixes some init bug 
*		0.7	30.04.09/goshi	added events.attach and events.remove methods 
*		0.6	20.04.09/goshi	added vars.langs property 
*		0.5	17.03.09/goshi	added .ready method
*		0.4	13.02.09/goshi	remove bug with page reload and reffer changer
*		0.3	11.02.09/goshi	added portal.storage instance
*/


/* prepare portal object */
function oPortal(){

	this._init();
	
};

/* declare portal prorotype */
oPortal.prototype = {
	
	mess: {},
	vars: {},
	storage: {},
	event : {},	// events object
	lang_id: null,
	lang_nick: null,
		
	_init: function(){
	
		// loading language variable
		// if it is not set - try get them throw Ajax
		if (window.mess) this.mess = mess;
		if (window.pphplangs) this.vars.langs = pphplangs;
		// determine friendly URL
		if (window.JsFriendly){ 
			this.vars.friendlyURL = window.JsFriendly;
		} else {
			if (/\w+\.php/i.test(document.location.href))
				this.vars.friendlyURL = false;
			else
				this.vars.friendlyURL = true;
		}
		// gettin lang nick
		if (window.lang_nick){ 
			this.lang_nick = lang_nick;
		} else {
			if (this.vars.friendlyURL){
				this.lang_nick = document.location.href.match(/\/lang\/(\w+)/i);
				
			} else {
				this.lang_nick = document.location.href.match(/\lang=(\w+)/i);
			}
			if (this.lang_nick){
				this.lang_nick = this.lang_nick[1];
			}
		}
		
		// determine current laqnguage
		if (window.lang_id){ 
			this.lang_id = window.lang_id;
		} else if (this.vars.langs){
			// trying to determine current language
			
			// setting active language
			if (this.lang_nick){
				var f_lang = 0;
				for (var i in this.vars.langs){
					if (!f_lang) f_lang = i;
					if (this.vars.langs[i]['title'] == this.lang_nick){
						this.lang_id = i; break;
					}
				}
			}
			
			if (!this.lang_id)
				this.lang_id = f_lang;
		}
		
		this.storage = new oStorage();
		
		this.regulars = regulars;
		
		// check - if URL is the same (anf checking inititalization for storage - IE5-IE7 bug)
		if (this.storage.initialized){
			if (this.storage.get('refferer') && this.storage.get('refferer').toLowerCase() != document.URL.toLowerCase()){
				this.storage.set('old_refferer', this.storage.get('refferer'));
			}
			this.storage.set('refferer', document.URL);
					
		} else {
			var aportal = this;
			syncEvent(function() {
							/*var list = [];
							var attrs = aportal.storage.XMLDocument.documentElement.attributes;
 							
							for(var i=0; i < attrs.length; i++) {
							    alert(attrs[i].name+':'+attrs[i].value);
							}*/
							if (aportal.storage.get('refferer') && aportal.storage.get('refferer') != document.URL){
								aportal.storage.set('old_refferer', aportal.storage.get('refferer'));
							}
							aportal.storage.set('refferer', document.URL);
					},
				'portal.storage.initialized'
			);
		
		}

			
	},
	
	/* execute on DOM ready (after load all images, etc.) */
	ready : function(func){
		domReady(func);
	},
	
	/* events model */
	events : {
	
		attach : function(object, event, handler, useCapture) {
			//alert('attach');
			object['_ev_'+event] = handler;
			if (object.addEventListener) {
				object.addEventListener(event, object['_ev_'+event], useCapture ? useCapture : false);
			} else if (object.attachEvent) {
				object.attachEvent('on' + event, object['_ev_'+event]);
			} else {
				object['on'+event] = object['_ev_'+event];
			}
		},
		
		remove : function (object, event, handler){
		
			if(object.removeEventListener){
				//alert('detah : '+object['_ev_'+event]);
				object.removeEventListener(event, handler ? handler : object['_ev_'+event], false);
			} else if(object.detachEvent) {
				if (typeof handler != "undefined")
					object.detachEvent('on'+event, handler);
				else
					object.detachEvent('on'+event, object['_ev_'+event]);
			} else object['on'+event] = false;
	
		}
	
	
	},
	
	debug : {
	
		_starttime : null,
			
		_var_dump : function(data, level){
		
			var out = level+"";
		
			if (typeof data == "sting"){
				out += data+"\r\n";
			} else if (typeof data == "object"){
				out += "{\r\n";
				for (var i in data){
					if (typeof data[i] == "object" || typeof data[i] == "array")
						out += i+" : "+portal.debug._var_dump(data[i], level+"\t");
					else
						out += i+"="+data[i]+"\r\n";
				}
				out += " }\r\n";
			} else if (typeof data == "array"){
				out += "[\r\n";
				for (var i=0,cnt=data.length ; i< cnt; i++){
					if (typeof data[i] != "string")
						out += i+" : "+portal.debug._var_dump(data[i], level+"\t");
					else
						out += data[i]+"\r\n";
				}
				out += " ]\r\n";
			}
		
			return out;
		},
	
		dump : function (data){
			var out = portal.debug._var_dump(data, "");
			alert(out);
		},
		
		start : function (){
			this.debug._starttime = new Date().valueOf();
					
		},
		
		end : function (){
			var end_time = new Date().valueOf();
			return (end_time - start_time);
		}
	}
		
	
};

var portal = new oPortal();