

red.prototype.tab = {
	
	container : '',
	
	tabobject : new Array(),
	
	active_id : '',
	
	siteid_ : '',
		
	init : function(containerid) {
		red.prototype.tab.container = red.prototype.dom.byid(containerid);
		red.prototype.tab.checksite();
    },
	
	checksite : function(){
		var urlarray = document.URL.split('/');
		red.prototype.tab.save(document.title,urlarray[urlarray.length-1]);
		red.prototype.tab.fillcontainer();
	},
	
	fillcontainer : function(){
		var output = '';
		for(var i=0;i<red.prototype.tab.tabobject.length;i++){
			if(red.prototype.tab.tabobject[i].title != 'false'){				
				var delbutn = '&nbsp;&nbsp;<a href="#" onclick="r.tab.remove(\''+i+'\');" style="color:#666;">x</a>';
				
				if(red.prototype.tab.tabobject[i].siteid==red.prototype.tab.siteid_){
					output += '<span class="tab_aktiv">'+red.prototype.tab.tabobject[i].title+delbutn+'</span>';
				}
				else{
					output += '<span class="tab_inaktiv"><a href="'+red.prototype.tab.tabobject[i].URL+'">'+red.prototype.tab.tabobject[i].title+delbutn+'</a></span>';
				}
			}
		}
		var deletebutton = '<span title="alle Tabs l&ouml;schen" class="tab_inaktiv"><a href="#" onclick="r.tab.removeall();">x</a></span>';
		
		red.prototype.tab.container.innerHTML = output+" "+deletebutton;
	},
	
	removeall : function(){
		red.prototype.cookie.remove('tabs');
		red.prototype.tab.tabobject = new Array();
		red.prototype.tab.container.innerHTML = "";
	},
	
	remove : function(tabid){		
		var newtabobj = new Array();		
		var j=0;
		for(var i=0;i<red.prototype.tab.tabobject.length;i++){
			if(i != tabid){
				newtabobj[j] = red.prototype.tab.tabobject[i];
				j++;
			}
		}
		if(tabid==red.prototype.tab.active_id){
			red.prototype.tab.active_id='';
		}
		red.prototype.tab.tabobject = newtabobj;
		red.prototype.tab.fillcontainer();
		red.prototype.tab.tabobject2cookie();
	},
	
	tabobject2cookie : function(){
		var cookie_temp = new Array();
		for(var i=0;i<red.prototype.tab.tabobject.length;i++){
			cookie_temp[i] = red.prototype.tab.tabobject[i].title+'},{'+red.prototype.tab.tabobject[i].URL+'},{'+red.prototype.tab.tabobject[i].siteid;
		}
		var cookie_str = escape(cookie_temp.join('],['));
		red.prototype.cookie.set('tabs',cookie_str);
	
	},
	
	save : function(title,URL){
		var tabobject = red.prototype.tab.load();
		var len = tabobject.length;
		
		var siteid = red.prototype.tab.siteid(URL);		
		red.prototype.tab.siteid_ = siteid;
		
		var obj_id = red.prototype.tab.inobject(siteid,URL,tabobject);
		
		if(obj_id[0] === false){
			tabobject[len] 			= new Object();
			tabobject[len].title 	= title;
			tabobject[len].URL 		= URL;
			tabobject[len].siteid	= siteid;
			red.prototype.tab.active_id = len;
			red.prototype.tab.tabobject = tabobject;
			// cookie save
			red.prototype.tab.tabobject2cookie();
		}
		else if(obj_id[0] == 'url'){
			tabobject[obj_id[1]].URL 	= URL;
			red.prototype.tab.active_id = len;
			red.prototype.tab.tabobject = tabobject;
			// cookie save
			red.prototype.tab.tabobject2cookie();
		}
		else{
			red.prototype.tab.active_id = obj_id;
			red.prototype.tab.tabobject = tabobject;
		}
	},
	
	inobject : function(siteid,URL,obj){		
		for(var i=0;i<obj.length;i++){
			if(obj[i].siteid == siteid){
				if(obj[i].URL != URL){
					return Array('url',i);	
				}
				return i;
			}
		}
		return Array(false);
	},
	
	siteid : function(URL){
		var urlarray = URL.split('?');
		if(urlarray.length==1){return '1';}
		var urlarray = urlarray[1].split('&');
		for(var i=0;i<urlarray.length;i++){
			var array = urlarray[i].split('=');
			if(array[0] == 'siteid'){
				return array[1];
			}
		}
		return '1';
	},
	
	load : function(){
		var tabs = unescape(red.prototype.cookie.get('tabs'));
				
		var tabobject = new Array();
		if(tabs == false){
			return tabobject;
		}		
		var tabsarray = tabs.split('],[');
		for(var i=0;i<tabsarray.length;i++){
			tabsarray2 = tabsarray[i].split('},{');
			tabobject[i] = new Object();
			tabobject[i].title 	= tabsarray2[0];
			tabobject[i].URL 	= tabsarray2[1];
			tabobject[i].siteid	= tabsarray2[2];
		}
		return tabobject;
	}
	

}