var language={
	myLang:'',
	defaultLang:'',
	supported:new Array(),
	names:new Array(),
	buttons:new Array(),
	defaultButtons:new Array(),
	chosenButtons:new Array(),
	langs:null,
	languages:null,
	serverName : location.hostname,
	pageName:'',
	tabs:new Array(),
	tab:function(){
		this.unid='',
		this.lang='',
		this.title=''
	},
	init:function(){
		dojo.require("dojo.cookie");
		this.defaultLang=Session.lang.defaultLang;
		this.supported=Session.lang.supported;
		this.names=Session.lang.names;
		this.myLang=this.getMyLang();
	},
	setMyLang:function(lang){
		dojo.require("dojo.cookie");
		this.page=this.getCurrentPage();
		console.debug(this.page);
		var insite=null;
		if(this.isNotSameLanguage(lang)){
		if(dojo.cookie("insite")==undefined){
		insite={
		lang:lang,
		tab:this.getStartPage(lang),
		area:'home'
		}
		}
		else{
		insite=dojo.fromJson(dojo.cookie("insite"));
		insite.lang=lang;
		insite.tab=this.getStartPage(lang)
		dojo.cookie("insite", "", {
			expires : -1
		});
		}
		
		dojo.cookie("insite", dojo.toJson(insite), {
			expires : 35,
			path:'/'
		});
		
		switch(this.getCurrentPage()){
		case '/index.xsp':
		document.location='index.xsp';
		break;
		case '/':
		document.location=document.location;
		break;
		case '/main.xsp':
		document.location='redirect.xsp?action=language&page='+this.page+'&lang='+lang+'&unid='+this.getQueryString('documentId');
		break;
		case '/content.xsp':
		document.location='redirect.xsp?action=language&page='+this.page+'&lang='+lang+'&cat='+this.getQueryString('cat')+'&unid='+this.getQueryString('documentId');
		break;
		case '/admin.xsp':
		document.location='redirect.xsp?action=language&page='+this.page+'&lang='+lang+'&unid='+this.getQueryString('documentId');
		break;
		case '/administration.xsp':
		document.location='admin.xsp?openDocument&documentId='+this.getAdminPage(lang);
		break;
		case '/widgets.xsp':
		document.location='admin.xsp?openDocument&documentId='+this.getAdminPage(lang);
		break;
		case '/template.xsp':
		document.location='admin.xsp?openDocument&documentId='+this.getAdminPage(lang);
		break;
		case '/searchparameter.xsp':
		document.location='admin.xsp?openDocument&documentId='+this.getAdminPage(lang);
		break;
		case '/parameter.xsp':
		document.location='admin.xsp?openDocument&documentId='+this.getAdminPage(lang);
		break;
		case '/navparameter.xsp':
		document.location='admin.xsp?openDocument&documentId='+this.getAdminPage(lang);
		break;
		case '/nparameter.xsp':
		document.location='admin.xsp?openDocument&documentId='+this.getAdminPage(lang);
		break;
		default: 
		document.location=document.location;
		}
		}
		
	},
	getMyLang:function(){
		dojo.require("dojo.cookie");
		var cookie = dojo.fromJson(dojo.cookie("insite"));
		if(cookie==undefined){			
			return Session.lang.defaultLang;
		}
		else{
			return cookie.lang;
		}
	},
	isNotSameLanguage:function(lang){
		if(this.getMyLang()==lang){
			return false;
		}
		else{
			console.debug(this.getMyLang(),lang,false);	
			return true;
		}
	},
	getStartPage:function(lang){
		for(i=0;Session.lang.pages.length;i++){
			var page=Session.lang.pages[i];			
			if(page.lang==lang){				
				return page.startpage;
			}
		}
	},
	getAdminPage:function(lang){
		for(i=0;Session.lang.adminpages.length;i++){
			var page=Session.lang.adminpages[i];			
			if(page.lang==lang){				
				return page.startpage;
			}
		}
	},
	checkLang:function(node){
		if(dijit.byId(node.id).checked){
			switch(this.pageName){
			case '/tabadmin.xsp':
			this.openTabAdmin(this.getAttributeByID(node.id,'alias'),this.getAttributeByID(node.id,'name'));
			break;
			case '/administration.xsp':
			this.openContent(this.getAttributeByID(node.id,'alias'),this.getAttributeByID(node.id,'name'));
			break;
			case '/content.xsp':
			this.openContent(this.getAttributeByID(node.id,'alias'),this.getAttributeByID(node.id,'name'));
			break;
			}
			}
	},
	setButtons:function(langListDiv,langs,languages,pageName){
	var langList=dojo.byId(langListDiv);
	this.langs=dojo.byId(langs);
	this.languages=dojo.byId(languages);	
	this.pageName=pageName;
	dojo.forEach(dojo.query('#langList button'),function(entry, i) {
	var langButton=new language.button();
	langButton.id=entry.id;
	langButton.name=language.names[i];
	langButton.alias=language.supported[i];
	language.buttons[i]=langButton;
	});
	},
	setLangs:function(value){
	this.langs.value=value
	},
	setLanguages:function(key){
	var value=language.getLanguage(key);
	this.languages.value=value;
	},
	getLanguage:function(key){
	dojo.forEach(this.langs,
	function(entry,i){
	if(entry.value==key){
	return this.names[i];
	}
	});
	},
	getAttributeByID:function(id,attribute){
	for(i=0;i< language.buttons.length;i++){
		var button=language.buttons[i];
		if(button.id==id){
		switch(attribute){
		case 'name':
		return button.name;
		break;
		case 'alias':
		return button.alias;
		break;
		}
		}
	}
	},
	button:function(){
	 this.id='',
	 this.name='',
	 this.alias=''
	},
	openContent:function(alias,name){
			var tabs = dijit.byId(contentContainer.id);
			var unid = this.getQueryString('documentId')
			var templateFrame = dijit.byId('template');
			var pane = dojo.clone(templateFrame);
			dojo.attr(pane, "id", "tab_" + alias);
			dojo.attr(pane, "title", name);
			dojo.attr(pane, "closable", true);
			tabs.addChild(pane);
			var iframe = pane.domNode.firstChild;
			var location = 'http://' + this.serverName + '/'
					+ Session.dbList.main
					+ '/contenttab.xsp?action=createDocument&parent='
					+ unid
					+ '&docLang='+alias+'&if=frame_'
					+ alias;
			dojo.attr(iframe, "src", location);
			dojo.attr(iframe, "id", "frame_" + alias);
			if (dojo.isIE) {
				tabs.selectChild(pane);
			} else {
				tabs.selectChild(pane);
			}
	}
	,
	openLangContent:function(unid,lang,title){
			var tabs = dijit.byId(contentContainer.id);
			var templateFrame = dijit.byId('template');
			var pane = dojo.clone(templateFrame);
			dojo.attr(pane, "id", "tab_" + lang);
			dojo.attr(pane, "title", lang+':'+title);
			tabs.addChild(pane);
			var iframe = pane.domNode.firstChild;
			var location = 'http://' + language.serverName + '/'
					+ Session.dbList.main
					+ '/contenttab.xsp?action=editDocument&documentId='
					+ unid
					+ '&docLang='+lang+'&if=frame_'
					+ lang;
			dojo.attr(iframe, "src", location);
			dojo.attr(iframe, "id", "frame_" + lang);
	},
	openTabAdmin:function(alias,name){
			var tabs = dijit.byId(contentContainer.id);
			var unid = this.getQueryString('documentId')
			var templateFrame = dijit.byId('template');
			var pane = dojo.clone(templateFrame);
			dojo.attr(pane, "id", "tab_" + alias);
			dojo.attr(pane, "title", name);
			dojo.attr(pane, "closable", true);
			tabs.addChild(pane);
			var iframe = pane.domNode.firstChild;
			var location = 'http://' + this.serverName + '/'
					+ Session.dbList.main
					+ '/admintab.xsp?action=createDocument&parent='
					+ unid
					+ '&docLang='+alias+'&if=frame_'
					+ alias;
			dojo.attr(iframe, "src", location);
			dojo.attr(iframe, "id", "frame_" + alias);
			if (dojo.isIE) {
				tabs.selectChild(pane);
			} else {
				tabs.selectChild(pane);
			}
	},
	openTab:function(langUNID,lang,title){
			var tabs = dijit.byId(contentContainer.id);
			var unid = this.getQueryString('documentId')
			var templateFrame = dijit.byId('template');
			var pane = dojo.clone(templateFrame);
			dojo.attr(pane, "id", "tab_" + lang);
			dojo.attr(pane, "title", lang+' : '+title);
			tabs.addChild(pane);
			var iframe = pane.domNode.firstChild;
			var location = 'http://' + this.serverName + '/'
					+ Session.dbList.main
					+ '/admintab.xsp?action=editDocument&documentId='
					+ langUNID
					+ '&docLang='+lang+'&if=frame_'
					+ lang;			
			dojo.attr(iframe, "src", location);
			dojo.attr(iframe, "id", "frame_" + lang);			
	},
	getQueryString : function(key, default_) {
		if (default_ == null)
			default_ = "";
		key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
		var qs = regex.exec(window.location.href);
		if (qs == null)
			return default_;
		else
			return qs[1];
	},
	getCurrentPage : function() {
		var temp = window.location.pathname.split('/');
		return ('/' + temp[temp.length - 1]);
	},
	addTab:function(unid,lang,title){
		var tmpTab=new language.tab();
		tmpTab.unid=unid;
		tmpTab.lang=lang;
		tmpTab.title=title;
		language.tabs.push(tmpTab);
	},
	writeTabs:function(){
		for(i=0;i< language.tabs.length;i++){
			var tmpTab=language.tabs[i];
			switch(this.pageName){
			case '/tabadmin.xsp':
			this.openTab(tmpTab.unid,tmpTab.lang,tmpTab.title);
			break;
			case '/administration.xsp':
			this.openLangContent(tmpTab.unid,tmpTab.lang,tmpTab.title);
			break;
			case '/content.xsp':
			this.openLangContent(tmpTab.unid,tmpTab.lang,tmpTab.title);
			break;
			}
			
		}
		
	}
}

