function tabbedContent() {

	var obj = this;
	
	obj.name = null;
	obj.el = null;
	obj.menu = null;
	obj.tabList = {};
	obj.content = null;
	obj.tabs = [];
	obj.tabNames = [];
	obj.selectedTab = 0;
	obj.currentTab = 0;
	obj.rows = [];
	obj.lis = [];
	obj.index = 0;
	obj.loaded = true;
	obj.preloader = null;
	obj.query = "";
	obj.title = "";

	obj._initialize = function() {

		var div = obj.$(obj.name);
		if (!div) return false;

		obj.title = document.title;
		obj.el = div;
		obj.el.style.position = 'relative';
		obj.menu = obj.$('tabs');
		obj.content = obj.$('tabCont');
		if (obj.menu) {
			var ac = 'read';
		} else {
			var ac = 'set';
			obj.menu = createEl('div', 'className=menu', obj.el, obj.el.firstChild);
			var head = createEl('div', 'className=tabHeader', obj.el);
			head.innerHTML = '<div class="left"></div><div class="right"></div>';
			obj.content = createEl('div', 'id=tabCont', obj.el);
			obj.content.style.height = 60 + 'px';
			var foot = createEl('div', 'className=tabFooter', obj.el);
			foot.innerHTML = '<div class="left"></div><div class="right"></div>';
			showPreloader(obj.el, 528, 60);
		}
		setTabs(ac);
	};
	function createEl(_type, _style, parent, pos) {
		if (!parent) parent = obj.parent;
		var el = document.createElement(_type);
		if(_style && _style != '') {
			var styles = _style.split('=');
			var x = styles[0];
			el[x] = styles[1];
		}
		if(pos)
			parent.insertBefore(el, pos);
		else
			parent.appendChild(el);
		return el;
	};
	obj.$ = function(eid) {
		if (x = document.getElementById(eid))
			return x;
		return null;
	};
	obj.setTabNames = function(x) {
		obj.tabNames = x;
	}
	function setTabs(ac) {
		while(obj.index < obj.tabNames.length) {
			if (ac == 'read')
				readRow();
			else
				setRow();
			++obj.index;
		}
		getLink();
		addClicks();
	};
	function getLink() {
		var _l = new String(document.location.href);
		q = _l.substring(_l.indexOf('#') + 1);
		if (q.indexOf('tabs=') == 0) {
				var tab = q.split('=');
				var tmp = parseInt(tab[1],10);
				if (!isNaN(tmp))
					obj.selectedTab = tmp;
		}
	};
	function readRow() {
		var tabUl = obj.$('tabs');
		var row = tabUl.parentNode;
		var tabList = tabUl.getElementsByTagName('li');
		var width = 0;
		var rowStart = obj.index;
		for (var a = rowStart; a < tabList.length/3; a++) {

			var tmpArr = [];
			obj.index = a;

			var el = tabList[a * 3];
			var tmp = tabList[a * 3 + 1];
			el.num = tmp.num = a;
			var inner = tmp.firstChild;
			inner.onclick = function() {
				if (obj.loaded) {
					this.blur();
					obj.selectedTab = this.parentNode.num;
					IC.util.History.navigate('tabs',obj.selectedTab.toString());
					document.title = obj.title;
				}
				return false;
			};
			for (var x in obj.tabNames[a]) {
				tmp.path = x;
				tmp.row = obj.rows.length;
			}
			var el2 = tabList[a * 3 + 2];
			el2.num = a;

			tmpArr[0] = el;
			tmpArr[1] = tmp;
			tmpArr[2] = el2;

			obj.tabs.push(tmp);
			obj.lis.push(tmpArr);
		}
		obj.rows.push(row);
		return tabUl;
	};
	function setRow() {
		var row = createEl('div', 'className=row', obj.menu, obj.menu.firstChild);
		var tabList = createEl('ul', 'id=tabs', row);
		var width = 0;
		var rowStart = obj.index;
		for (var a = rowStart; a < obj.tabNames.length; a++) {

			var tmpArr = [];
			obj.index = a;

			var _class = (obj.selectedTab == a) ? "selected" : "";
			var _name = (a > rowStart) ? 'Tab' : 'Tabs';
			var el = createEl('li', 'className='+_class+_name+'Start', tabList);
			var tmp = createEl('li', 'className='+_class, tabList);
			el.num = tmp.num = a;
			var inner = createEl('a', '', tmp);
			inner.href = 'javascript:void(0);';
			inner.onclick = function() {
				if (obj.loaded) {
					this.blur();
					obj.selectedTab = this.parentNode.num;
					IC.util.History.navigate('tabs',obj.selectedTab.toString());
					document.title = obj.title;
				}
				return false;
			};
			for (var x in obj.tabNames[a]) {
				inner.innerHTML = obj.tabNames[a][x];
				tmp.name = obj.tabNames[a][x];
				tmp.path = x;
				tmp.title = obj.tabNames[a][x].replace(/<br( *\/)?>/,' ');
				tmp.row = obj.rows.length;
			}
			_name = (a < obj.tabNames.length - 1) ? 'Tab' : 'Tabs';
			var el2 = createEl('li', 'className='+_class+_name+'End', tabList);
			el2.num = a;

			tmpArr[0] = el;
			tmpArr[1] = tmp;
			tmpArr[2] = el2;
			width += el.offsetWidth + tmp.offsetWidth + el2.offsetWidth;
//			if (width > tabList.offsetWidth - 10) {
//				tabList.removeChild(el);
//				tabList.removeChild(tmp);
//				tabList.removeChild(el2);
//				var li = obj.lis[obj.lis.length - 1][2];
//				li.className = li.className.replace('Tab','Tabs');
//				break;
//			}

			obj.tabs.push(tmp);
			obj.lis.push(tmpArr);
		}
		obj.rows.push(row);
		return tabList;
	};
	obj._changeSelected = function() {

		var startTab = obj.lis[obj.currentTab];
		var prev = obj.currentTab == 0 ? false : obj.lis[obj.currentTab - 1][2];
		if (prev) {
			prev.className = prev.className.replace('prevSel','');
		}
		for(var i = 0; i < startTab.length; i++) {
			var li = startTab[i];
			li.className = li.className.replace('selected','');
		}
		var targetTab = obj.lis[obj.selectedTab];
		var prev = obj.selectedTab == 0 ? false : obj.lis[obj.selectedTab - 1];
		if (prev && prev[1].row == targetTab[1].row) {
			prev[2].className = 'prevSel' + prev[2].className;
		}
		for(var i = 0; i < targetTab.length; i++) {
			var li = targetTab[i];
			li.className = 'selected' + li.className;
		}
		if (startTab[1].row != targetTab[1].row) {
			var tmp = obj.rows[targetTab[1].row];
			obj.menu.replaceChild(obj.rows[startTab[1].row], tmp);
			obj.menu.appendChild(tmp);
		}
		getTabContent();
	};
	obj.removeTab = function() {
		var rem = obj.tabs.length  - 1;
//		if (rem == obj.tabs.length - 1) {
			var last = obj.lis[rem - 1][2];
			last.className = last.className.replace('TabEnd', 'TabsEnd');
//		}
//		if (rem == 0) {
//			var first = obj.lis[0][0];
//			first.className = first.className.replace('TabStart', 'TabsStart');
//		}
		for(var i = 0; i < 3; i++) {
			obj.menu.removeChild(obj.lis[rem][i]);
		}
		obj.lis.splice(rem,1);
		obj.tabs.splice(rem,1);
	};
	obj.getContent = function(query) {
		getTabContent(query);
		return false;
	};
	function publish(HTML) {
		obj.currentTab = obj.selectedTab;
		obj.content.style.height = '100%';
		obj.content.innerHTML = HTML;
		evalScripts(HTML);
		obj.loaded = true;
		obj.preloader = null;
		addClicks(obj.content);
		document.title = obj.title;

		initTooltip(obj.el.id);//tooltip initialization 31/12/09
	};
	function getTabContent(query) {
		if (!query)
			obj.query = (document.location.search.length > 1) ? document.location.search : "";
		else
			obj.query = '?' + query;
		advAJAX.get({
			url : obj.tabs[obj.selectedTab].path + obj.query,
			mimeType : 'text/plain',
			onInitialization : function() {
				obj.loaded = false;
				showPreloader(obj.content, 528, obj.content.offsetHeight - 20);
	    },
			onSuccess : function(res) { publish(res.responseText); },
			onError :	function() { obj.loaded = false; var tmp = obj.selectedTab; obj.selectedTab = obj.currentTab; obj.currentTab = tmp; obj._changeSelected(); }
		});
	};
	function showPreloader(el, w, h) {
		if (obj.preloader) {
			obj.preloader.parentNode.removeChild(obj.preloader);
		}
		var pre = document.createElement('div'); 
		pre.id = 'preloader';
		pre.style.top = '10px';
		pre.style.left = '10px';
		pre.style.width = (!w ? 530 : w) + 'px';
		pre.style.height = ((!h || h < 0) ? 50 : h) + 'px';
		obj.preloader = pre;
		el.insertBefore(pre, el.firstChild);
	};
	function evalScripts (html) {	
		return html.extractScripts ().map (function (script) {
			try {
				if (script.indexOf('document.write') != -1) return false;
				return eval (script);
			} catch(e) {} 
		});
	};
};
tabbedContent.init = function(_name, _var, _tabs) {
	var _tc = new tabbedContent();
	_tc.name = _name;
	window[_var] = _tc;
	_tc.setTabNames(_tabs);
	return _tc._initialize();
};

if (!Array.prototype.map) {
  Array.prototype.map = function (fun) {
    var len = this.length;
    if (typeof fun != 'function')
      throw new TypeError ();
    var res = new Array (len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
      if (i in this)
        res[i] = fun.call (thisp, this[i], i, this);
    }
    return res;
  };
}

String.prototype.extractScripts = function () {
  var scriptFragment = '<script[^>]*>([\\S\\s]*?)<\/script>';
  var matchAll = new RegExp (scriptFragment, 'img');
  var matchOne = new RegExp (scriptFragment, 'im');
  return (this.match (matchAll) || []).map (function (scriptTag) {
					      return (scriptTag.match (matchOne) || ['', ''])[1];
					    });
};

function addClicks(el) {
  if (!el) el = document.getElementById('contentContainer');
  var obj = el.getElementsByTagName('a');
  for (var i = 0; i < obj.length; i++) {
    var anchor = obj[i];
    if (anchor.href.indexOf(document.location.pathname) != -1) {
      var b = anchor.href.indexOf('#tabs=');
      if (b != -1) {
        anchor.tabNumber = anchor.href.substr (b + 6, 1);
        anchor.onclick = function() { IC.util.History.navigate('tabs', this.tabNumber); return false; };
      }
    }
  }
}