/**
* web-T::CMS Portal javascript object
* need oStorage
*
* @version 1.52
* @author goshi
* @package javascript::share
*
*	ChangeLog:
*		1.52	27.01.11/goshi	remove informer from core
*		1.51	29.12.10/goshi	fix onload method
*		1.5		29.12.10/goshi	add css.load method, add events.attachLoad, fix script.load method
*		1.44	04.11.10/goshi	fix bug with IE and detachEvent
*		1.43	28.10.10/goshi	minor fixing for attach
*		1.42	27.10.10/goshi	add getKey to the events section
*		1.41	15.10.10/goshi	event listners can handle strings
*		1.4		06.10.10/goshi	add subdomain suppport on parsing
*		1.3		05.09.10/goshi	add blockSelect events
*		1.2		29.08.10/goshi	add attachUnload and removeUnload methods
*		1.1		12.06.10/goshi	move session object to portal
*		1.0		21.05.10/goshi	add hash method hash.set
*		0.99	14.05.10/goshi	add in ajax response checking for redirect property
*		0.98	05.05.10/goshi	add print method 
*		0.97	20.03.10/goshi	add clearing hash 
*		0.96	10.03.10/goshi	update portal.dom.getAbsPos method - now it return all dimensions of the object 
*		0.95	01.03.10/goshi	update portal.dom.swap method - now it can swap not only inner HTML 
*		0.94	21.02.10/goshi	add dom.getInPos
*		0.93	03.01.10/goshi	add css object, upgrade debug.dump function, add addBlock and has Block methods to the portal.events object
*		0.92	03.01.10/goshi	add dom object (with tools), add to events object new 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,
	
	_hash : {}, // storage for parsed hash
		
	_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 : {
		
		// events queue
		_events_queue : {},
	
		attach : function(object, event, handler, useCapture) {
			if(typeof object == "string"){object = $_(object)};
			if (!object) return false;
			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(typeof object == "string"){object=$_(object);};
			if (!object) return false;
			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 if (object['_ev_'+event] != null) {
					object.detachEvent('on'+event, object['_ev_'+event]);
				}
			} else object['on'+event] = false;
	
		},
		
		stop: function (event){
			var event = event ? event : window.event;
		
			if (event.preventDefault) {
				event.preventDefault();
				event.stopPropagation();
			} else {
				event.returnValue = false;
				event.cancelBubble = true;
			}
			return false;
		},
		
		getTarget : function(event){
			var targ;
			if (!event) var event = window.event;
			if (event.target) targ = event.target;
			else if (event.srcElement) targ = event.srcElement;
			if (targ.nodeType == 3) // defeat Safari bug
				targ = targ.parentNode;
			return targ;
		},
		
		getKey : function(event){
			return  event.which ? event.which : event.keyCode;
		},
		
		attachUnload : function(handler) {
			//var oldonload = window.onbeforeunload;
			if (typeof window.onbeforeunload != 'function') {
			
				var fireUnload = function(e){
					var e = e || window.event;
					var ret = '', sub_ret = '';
					if (portal.events._events_queue['unload'] != null){
						for (var i in portal.events._events_queue['unload']){
							sub_ret = portal.events._events_queue['unload'][i](e);
							if (sub_ret !== true)
								ret += sub_ret; 
						}
					}
					//portal.debug.dump(ret + '--- this is the end!');
					
					// if all ok - stop propagation and bubbling
					if (ret != '' && typeof e != 'undefined'){
						e.returnValue = ret;
						return ret;
					}
										
				}
				portal.events._events_queue['unload'] = new Array;
				
				var queue_id = 1;
				portal.events._events_queue['unload'][queue_id] = handler;
				
				window.onbeforeunload = fireUnload;
				
			}
			else {
				var queue_id = portal.events._events_queue['unload'].length;
				portal.events._events_queue['unload'][queue_id] = handler;
			}
			
			return queue_id;
		},
		
		
		removeUnload : function(ticket){
			if (typeof portal.events._events_queue['unload'][ticket] == 'function')
				delete portal.events._events_queue['unload'][ticket];
		},
		
		attachLoad : function(handler) {
			// check for always loaded
			if (typeof portal.events._events_queue['load'] == 'undefined'){//typeof window.onload != 'function') {
			
				var fireLoad = function(e){
					//alert('Sorry! But on load event fired!');
					var e = e || window.event;
					var ret = '', sub_ret = '';
					if (portal.events._events_queue['load'] != null){
						for (var i in portal.events._events_queue['load']){
							//alert(portal.events._events_queue['load'][i]);
							sub_ret = portal.events._events_queue['load'][i](e);
							if (sub_ret !== true)
								ret += sub_ret; 
						}
					}
					//portal.debug.dump(ret + '--- this is the end!');
					
					// if all ok - stop propagation and bubbling
					if (ret != '' && typeof e != 'undefined'){
						e.returnValue = ret;
						return ret;
					}
										
				}
				portal.events._events_queue['load'] = new Array;
				
				var queue_id = 1;
				// adding old eventer
				if (typeof window.onload == 'function'){
					portal.events._events_queue['load'][queue_id] = window.onload;
					queue_id++;
				}
				portal.events._events_queue['load'][queue_id] = handler;
				
				window.onload = fireLoad;
				
				//alert('attachLoad');
				
			}
			else {
				//alert(handler);
				
				var queue_id = portal.events._events_queue['load'].length;
				portal.events._events_queue['load'][queue_id] = handler;
			}
			
			return queue_id;
		},		
				
		defPosition : function(event) { 
			var x = 0, y = 0; 
			if (document.attachEvent != null) {
				x = event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft; 
				y = event.clientY + document.documentElement.scrollTop + document.body.scrollTop; 
			} 
			if (!document.attachEvent && document.addEventListener) { // Gecko 
				x = event.clientX + window.scrollX; 
				y = event.clientY + window.scrollY; 
			} 
			return {x:x, y:y}; 
		},
		
		addBlock : function(){
			window['bubble_block'] = true;
		},
		
		hasBlock : function(){
			// when found blocking - then release it and send to checker, that we can't move to the next step
			if (window['bubble_block']){
				window['bubble_block'] = false;
				return false;
			} else { return true; }
		},
		
		/** blocking and unblocking all childs **/
		blockSelect : function(parent){
			if (typeof parent == 'undefined' || !parent){
				parent = document;
			};
			var els = getLikeElements(null, null, null, parent);		
			if (els.length > 0){
				for (var i in els){
					els[i].onselectstart = function() { return false; }; // IE, Chrome, Safari
					els[i].unselectable = "on"; // IE, Opera
					if (typeof els[i].style == 'undefined' || !els[i].style){
						els[i].style = {};
					}
					if (typeof els[i].style != 'undefined'){
						els[i].style.MozUserSelect = 'none'; // FF
						els[i].style.KhtmlUserSelect = 'none';
						els[i].style.UserSelect = 'none';
					}
				}
			}
		},
		
		unblockSelect : function(parent){
			if (typeof parent == 'undefined' || !parent){
				parent = document;
			};
			var els = getLikeElements(null, null, null, parent);		
			if (els.length > 0){
				for (var i in els){
					els[i].onselectstart = function() {}; // IE, Chrome, Safari
					els[i].unselectable = "off"; // IE, Opera
					if (typeof els[i].style == 'undefined' || !els[i].style)
						els[i].style = {};
					if (typeof els[i].style != 'undefined'){
						els[i].style.MozUserSelect = 'auto'; // FF
						els[i].style.KhtmlUserSelect = 'auto';
						els[i].style.UserSelect = 'auto';
					}
				}
			}
		}
	},
	
	debug : {
		
		_level_del : "\t",
		
		_var_dump : function(data, level){
			
			// add protection for level recursion
			if (parseInt(level.length/portal.debug._level_del.length) > 4){return '...'};
			
			var out = level+"";
			try {
				if (typeof data != "object" && data != "array"){
					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+portal.debug._level_del);
						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+portal.debug._level_del);
						else
							out += data[i]+"\r\n";
					}
					out += " ]\r\n";
				}
			} catch (e) {
				// do nothing
			} finally {
				return out;
			}	
			
		},
	
		dump : function (data, inline){
			var out = portal.debug._var_dump(data, "");
			if (typeof inline != 'undefined' && inline){
				var ele = elem('pre', false, false, false);
				if (ele){
					ele.innerHTML = out;
					document.body.appendChild(ele);
				}
			} else if (typeof console == 'object' && typeof console.log == 'function'){
				console.log(out);
			} else {
				alert(out);
			}
		}
	},
	
	/* DOM tools */
	dom : {
		// append element after selected obj
		appendAfter : function(obj,node){
			if(typeof obj=="string"){obj=$_(obj);}; 
			if (obj.nextSibling){
				obj.parentNode.insertBefore(node,obj.nextSibling);
			} else {
				obj.parentNode.appendChild(node);
			}
		},
		
		getAbsPosition : function(obj){
			if(typeof obj=="string"){obj=$_(obj);}; 
			if (!obj) return {};
			
			var __isIE =  navigator.appVersion.match(/MSIE/);
			var __userAgent = navigator.userAgent;
			var __isFireFox = __userAgent.match(/firefox/i);
			var __isFireFoxOld = __isFireFox && 
			   (__userAgent.match(/firefox\/2./i) || __userAgent.match(/firefox\/1./i));
			var __isFireFoxNew = __isFireFox && !__isFireFoxOld;			
			
			var x = 0,y = 0, h = 0, w = 0;
			var source = obj;
			w = obj.offsetWidth;
			h = obj.offsetHeight;
			
			while(obj != null) {
				if (typeof obj == 'undefined' || obj == null || obj == window.document) break;
				
				if (typeof obj.offsetLeft != 'undefined'){
					//portal.debug.dump(obj.offsetLeft + 'x'+ obj.offsetTop);
					x += obj.offsetLeft; 
					y += obj.offsetTop;
				}
				
				/*var parentTagName = obj.tagName.toLowerCase();    

	            if ((__isIE && parentTagName != "table") || 
	                (__isFireFoxNew && parentTagName == "td")) {            
	                borderWidth = __getBorderWidth(offsetParent);
	                res.x += borderWidth.left;
	                res.y += borderWidth.top;
	            }
	            
	            if (obj != document.body && 
	                obj != document.documentElement) {
	                x -= obj.scrollLeft;
	                y -= obj.scrollTop;
	            }
				
				//portal.debug.dump(obj.nodeName + '----' + obj.id + ' | ' + (typeof obj.offsetParent) + ' | ' + (obj.offsetParent == null) + ' || ' + (typeof obj.parentNode) + ' | ' + (obj.parentNode == null) + ' | ' + getStyle(obj, 'position'));
				// for fixed - return!
				if (getStyle(obj, 'position') == 'fixed'){
					portal.debug.dump(self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop));
					break;
				}*/
				
				if ((getStyle(obj, 'position') == 'absolute' || getStyle(obj, 'position') == 'relative')|| getStyle(obj, 'position') == 'fixed'){
				//	$_('test').innerHTML += 'POSITION!<br>';
					obj = typeof obj.offsetParent != 'undefined' && obj.offsetParent != null ? obj.offsetParent : (typeof obj.parentNode != 'undefined' ? obj.parentNode : null);
				} else
					obj = typeof obj.offsetParent != 'undefined' && obj.offsetParent != null ? obj.offsetParent : (typeof obj.parentNode != 'undefined' ? obj.parentNode : null);
					
			}
			//$_('test').innerHTML += 'final: '+y+'<br>';
			var sY = self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
			var sX = self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
			
			while (source){
				if (typeof source.scrollLeft != 'undefined'){ 
					sX += source.scrollLeft; 
					sY += source.scrollTop;
				}
				source = source.parentNode; 
			}
			
			return  {'x' : x, 'scrollX' : sX, 'y' : y, 'scrollY' : sY, 'h' : h, 'w' : w};
		},
		
		/* getting in window position of the element */
		getInPos : function(obj, eX, eY, cPadding){
		
			if(typeof obj=="string"){obj=$_(obj);}; 
		
			var cW = getClientWidth();
			var cH = getClientHeight();
			var oW = obj.offsetWidth;
			var oH = obj.offsetHeight;
			
			if (typeof cPadding == "undefined")
				cPadding = 5;
				
			var res = {'x' : 0, 'y' : 0}
			
			if (eX + oW + cPadding > cW && eX - oW - cPadding > 0){
				res.x = eX - oW - cPadding;
			} else if (eX + oW + cPadding <= cW) {
				res.x = eX + cPadding;	
			} else {
				res.x = cW - oW - cPadding;	
			}
			
			if (eY + oH + cPadding > cH && eY - oH - cPadding > 0){
				res.y = eY - oH - cPadding;
			} else if (eY + oH + cPadding <= cH) {
				res.y = eY + cPadding;
			} else {
				res.y = cH - oH - cPadding;
			}
			
			return res;
		},
				
		clone : function(source, target, clonePosition){
		
			if(typeof source=="string"){source=$_(source);};
			if(typeof target=="string"){target=$_(target);};
			if(!source || !target){return null;};
			
			for (var i=0, max=source.childNodes.length; i < max; i++){
				var clone1=source.childNodes[i].cloneNode(true);
				target.appendChild(clone1);
			}
			
			// make clone position of the cloned element
			if (typeof clonePosition != 'undefined' && clonePosition){
				target.style.position = 'absolute';
				var offsets = portal.dom.getAbsPosition(source);
				
				//alert(offsets['y'] + '---' + offsets['scrollY']);
				target.style.top    = (offsets['y']) + 'px';
				target.style.left   = (offsets['x']) + 'px';
				target.style.width  = source.offsetWidth + 'px';
				target.style.height = source.offsetHeight + 'px'; 
			}
		},
		
		/* flag moveall helps to move always each element */
		swap : function(element1, element2, moveall){
		
			if(typeof element1=="string"){element1=$_(element1);};
			if(typeof element2=="string"){element2=$_(element2);};
			if(!element1 || !element2){return null;};
			
			// check - if we have swapping all objects - then simply swap places of it
			if (typeof moveall != 'undefined' && typeof moveall != 'null' && moveall){
				var nextSibling = element1.nextSibling;
				var parentNode = element1.parentNode;
				element2.parentNode.replaceChild(element1, element2);
				parentNode.insertBefore(element2, nextSibling);
			} else {
				for (var i=0, max=element1.childNodes.length; i < max; i++){
					var clone1=element1.childNodes[i].cloneNode(true);
					var clone2=element2.childNodes[i].cloneNode(true);
					var replaced1=element1.replaceChild(clone2, element1.childNodes[i]);
					var replaced2=element2.replaceChild(clone1, element2.childNodes[i]);
				}
			}
		}
	},
	
	hash : {
	
		set : function(hash){
			document.location.hash = hash;
		},

	
		parse : function(){
			var result = {};
			var url = document.location.hash;
	 		var parameters = url.slice(url.indexOf('#') + 1).split('&');
	 		
	 		if (parameters.length > 0){
		 		for(var i = 0;  i < parameters.length; i++) {
		 			var parameter = parameters[i].split('=');
		 			result[parameter[0]] = parameter[1];
		 		}
		 		
		 		// saving in protected property for future use
	 			// parsing full url 
	 			var domain;
	 			var re = new RegExp('^http:\/\/([^\/]+)', 'g');
	 			if (result['url'] && (domain = result['url'].match(re))){
	 				result['domain'] = domain[0];
	 				result['url'] = result['url'].slice(result['domain'].length, result['url'].length);
	 			}
	 			
	 			this._hash = result;
	 			
	 		}
	 		
	 		return result;
		},
		
		clear : function (){
		
			if (document.location.hash)
				document.location.hash = 'none'; //window.location = window.location.href.replace( /#.*/, "");
		
		}
	},
	
	
	ajax : {
		/**
		* load pages with parameters
		* you can set either id,lang_id or url params
		* @param object $e event object
		* @param int $params.id[option] id of the page for page
		* @param int $params.lang_id[option] language identifier of page
		* @param string $params.url[option] url of the page (it is preferedto user it) 
		* @param function $callback[option] function for callback 
		* @param bool $nohashadd[option] flag for adding hash to the URL 
		*/
		load : function(e, params, callback, nohashadd){
			var req = new JsHttpRequest();
			
			req.onreadystatechange = function (){
					
				if (req.readyState == 4){
		
					if (req.responseJS.status == 200){
					
						// checking for redirect
						if (typeof req.responseJS.redirect != 'undefined'){
							document.location.href = req.responseJS.redirect;
							return;
						}
					
						// calling callback function with closures
						if (typeof callback == 'function')
							callback(e, req.responseJS.response, params);
					}
						
				}
		
			}
			
			var hash = [];
			(params['pid'] ? hash[hash.length] = 'id='+params['id'] : '');
			(params['url'] ? hash[hash.length] = 'url='+params['url'] : '');
			(params['domain'] ? hash[hash.length] = 'domain='+params['domain'] : '');
			
			if (!(typeof nohashadd != 'undefined' && nohashadd))
				document.location.hash = hash.join('&');
			
			var url = /*(typeof params['domain'] != 'undefined'? params['domain'] : '')+*/'/ajax.php';
			
			//alert(url);
			
			req.open(typeof params['method'] != 'undefined' && params['method'] == 'post' ? 'POST' : 'GET', url, true);
			req.send({'resource' : 'content', 'params' : params});
		},
		
		/**
		* connect events to the forms on the page with flags
		* @param object $container consists of the standart structure for loading ajax content: {'clear' : function(), 'insert' : function() }
		*/
		attach_forms : function(container){
			var forms = getElementsByClass('frmAjx');
			if (forms.length > 0){
			
				var _always_submit = false;
				function sbForm(container, curr_form){
					if (_always_submit){
						return false;
					} else {
						//ev.returnValue = athis._checkForm(e, athis);
						
						/*if (!ev.returnValue){
							ev.preventDefault ? ev.preventDefault() : '';
						} else {*/
						
						_always_submit = true;
						var result = portal.hash.parse();
		 				if (result['id'] || result['url']){
		 					
		 					if (forms.length > 0){
		 						for (var i in forms){
		 							portal.events.remove(forms[i], "submit");
		 						}
		 					}
							result['form'] = curr_form;
							result['method'] = 'post';
					 		portal.ajax.load(null, result, container.insert);
					 		if (typeof container.clear == 'function')
						 		container.clear(null);
					 	}
						//}
					}
				
				}
				
				function fwk_submit(e){
					return portal.events.stop(e);
				}
			
				for (var i in forms){
					// check for always binded
					if (forms[i].getAttribute('binded') == null){
	
						forms[i].setAttribute('action', '');
						
						// for webkit browser we need to use another method
						if (navigator.userAgent.toLowerCase().indexOf('webkit') > -1){
							
							portal.events.attach(forms[i], 'submit', function(e){return fwk_submit(e);});
						
							var inputs = getLikeElements('input', 'type', 'submit', forms[i]);
						
							if (inputs && inputs.length > 0){
								for (var j in inputs){
									portal.events.attach(inputs[j], 'click', function(e){sbForm(container, forms[i]); return portal.events.stop(e);}, false);
								}
							}
						} else {
							portal.events.attach(forms[i], "submit", function(e){sbForm(container, forms[i]);	return portal.events.stop(e);}, false);
						}
					}
				}
			}
		},
		
		checkAjaxPage : function(is_ajax){
			if (parseInt(is_ajax) == 1){
				top.location.href = '/#url='+location.pathname;
			}
		}
	
	},
	
	
	/** script loader **/
	script : {
	
		load : function(src, params){
		
		    function async_load(){
		    	//alert('attaching script '+src);
		        var s = document.createElement('script');
		        s.type = 'text/javascript';
		        if (typeof params != 'undefined' && params.async)
			        s.async = true;
		        if (typeof params != 'undefined' && params.defer)
			        s.defer = true;
			       
		        s.src = src;
		        var x = document.getElementsByTagName('script')[0];
		        x.parentNode.insertBefore(s, x);
		    }

		    if (typeof params != 'undefined' && params.async)
			    portal.events.attachLoad(async_load);
			else
				async_load();
		
		}
	
	},
	
	css : {
	
		// class manipulation functions
		hasClass : function(ele,cls) {
			if (!ele || ele == null) return false;
			if(typeof ele=="string"){ele=$_(ele);};
			return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
		},
		
		addClass : function(ele,cls) {
			if (!ele || ele == null) return false;
			if(typeof ele=="string"){ele=$_(ele);};
			if (!portal.css.hasClass(ele,cls)) ele.className += " "+cls;
		},
		
		removeClass : function(ele,cls) {
			if (!ele || ele == null) return false;
			if(typeof ele=="string"){ele=$_(ele);};
			if (portal.css.hasClass(ele,cls)) {
				var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
				ele.className=ele.className.replace(reg,' ');
			}
		},
		
		load : function(src, params){
			if (src != ''){
				var cssNode = document.createElement('link');
			    cssNode.type = 'text/css';
			    cssNode.rel = 'stylesheet';
			    cssNode.href = src;
			    cssNode.media = typeof params != 'undefined' && params.media ? params.media : 'all';
			    document.getElementsByTagName("head")[0].appendChild(cssNode);
			}
		}
	
	},
	
	banners : {
		
	/** setuping banners **/
		load : function(id, async){
			var b_path = domain+'/js/project/banners/'
			
			if (typeof id != 'undefined' && $_('banner'+id)){
				async = typeof async != 'undefined' && async;
				var isIE = navigator.appName == 'Microsoft Internet Explorer';
				if ((isIE && !async) || (!isIE && async))
					document.write('<scri'+'pt type="text/javascript" '+(!isIE && async ? /*'async="async"'*/'' : '')+' src="'+b_path+'banner'+id+'.js"></scri'+'pt>');
			}
		},
		
		setup : function(){
			var sandbox = 'b-banners-sandbox';
			var isIE = navigator.appName == 'Microsoft Internet Explorer';
			if ($('#'+sandbox+' .bs').length > 0){
				$('#'+sandbox+' .bs').each(function(){
					var id = $(this).attr('id').replace('_in', '');
					if ($_(id)){
						if (!isIE)
							$_(id).innerHTML = $(this).html();
						$_(id).style.display = 	'block';		
					}
				})
			}
			if ($_(sandbox))
				$_(sandbox).parentNode.removeChild($_(sandbox));
		}
	},
	
	/** 
	* print page
	*/
	print : function (){
		
		var dochead = getLikeElements('head');
		if (dochead && dochead.length > 0){
			// append #print symbol to the hash for copying link and parsing it
			document.location.hash = 'print';
			dochead[0].appendChild(elem('link', {'rel' : 'StyleSheet', 'href' : '/css/project/print-src.css', 'type' : 'text/css'}));
			// now - print document
			if (window.print)
				window.print();
		}
			
	},
	
	session : {
		
		/** 
		* Session cookie prototype 
		* @version 0.2
		*
		* Changelog:
		*	0.2	10.11.08/goshi	remove bug with initial setting for session cookie
		*	0.1	10.11.08/goshi
		*/

		// because session data is dynamic - we must not set _values for long time - only for temporary cache for updating
		_session_name : 'webT',
		
		_values: false,
		
		_init: function(){
			this._values = portal.storage.get(this._session_name, 'object');
			
			if (typeof this._values == 'undefined' || this._values == false){				
				this._values = new Object();
				portal.storage.set(this._session_name, this._values, 'object');						
			}
			
			// setting up switchers			
			if (typeof this._values['switchers'] == 'undefined'){
			
				this._values['switchers'] = new Object();
				this.set('switchers', this._values['switchers']);
				
			}
		},
		
		get: function(valueName){
		
			if (this._values == false) this._init();
		
			this._values = portal.storage.get(this._session_name, 'object');
			if (typeof this._values[valueName] != 'undefined' && this._values[valueName] != ''){
				return this._values[valueName];
			}
		},
		
		set: function(valueName, value){
		
			if (this._values == false) this._init();

			this._values = portal.storage.get(this._session_name, 'object');
			this._values[valueName] = value;
			portal.storage.set(this._session_name, this._values, 'object');
		}
	
	}


};

var portal = new oPortal();
