
var SK__LanguageSelection=new Class({DB:null,settings:{},remoteAccessData:{},initialize:function(options){this.remoteAccessData=JSON.decode(options.properties.Data);this.settings={placeHolder:$(options.placeholder),alignAt:options.properties.AlignAt||'left',showOnPage:options.properties.ShowOnPage||'label'}
$(window).addEvent('load',this.init.bind(this));},init:function(){this.getAllLanguages();},getAllLanguages:function(){new SK.API(this.remoteAccessData.auth_token,{encoding:SK.Util.getEncoding()}).execute('db.query.get_all_rows',{db_id:this.remoteAccessData.languages,index_only:0,filter:JSON.encode({sort:{column:this.remoteAccessData.order,order:'asc'}})},function(success,message,data){if(!success){return;}
var domain_name=window.location.host,current_lang;var current_path=window.location.pathname,current_page=current_path.substr(1);var col_id=0;for(var i=0,l=data.rows.length;i<l;i+=1){var row=data.rows[i];if(row.cells[this.remoteAccessData.domain].indexOf(domain_name)!==-1){col_id=row.cells[this.remoteAccessData.colid];current_lang=row.cells[this.remoteAccessData.language];row.cells[this.remoteAccessData.domain]=domain_name;break;}}
if(SK.Singletons.env.get('user_id')){if(data.rows.length){this.hideZeroFootprint();}
this.printLanguageMenu({languages:data.rows,current_lang:current_lang,current_page:current_page,col_id:col_id,onclick:"alert('Language Selection toolbar is not active in Admin mode.'); return false;"});}else{this.getUrlMapping({languages:data.rows,current_lang:current_lang,current_page:current_page,col_id:col_id});}}.bind(this));},getUrlMapping:function(data){new SK.API(this.remoteAccessData.auth_token,{encoding:SK.Util.getEncoding()}).execute('db.query.get_filtered_rows',{db_id:this.remoteAccessData.url_mapping,index_only:0,filter:JSON.encode({"where":[[data.col_id,"=",[data.current_page]]],"limit":{"from":"0","count":"1"}})},function(success,message,result){if(!success){return;}
if(result.rows.length){data.pages=result.rows[0].cells;}else{data.pages=null;}
this.printLanguageMenu(data);}.bind(this));},printLanguageMenu:function(data){var menu=links='';menu='<div style="text-align:'+this.settings.alignAt+'">';data.languages.each(function(lang){var page=data.pages!=null?(data.pages[lang.cells[this.remoteAccessData.colid]]!=''?data.pages[lang.cells[this.remoteAccessData.colid]]:data.current_page):data.current_page;var onclick=data.onclick?' onclick="'+data.onclick+'"':'';switch(this.settings.showOnPage){case'both':var label=lang.cells[this.remoteAccessData.icon]!=''?'<img src="'+lang.cells[this.remoteAccessData.icon]+'" border="0"'
+' alt="'+lang.cells[this.remoteAccessData.language]+'"'
+' title="'+lang.cells[this.remoteAccessData.language]+'" /> '
+lang.cells[this.remoteAccessData.language]:lang.cells[this.remoteAccessData.language];break;case'icon':var label=lang.cells[this.remoteAccessData.icon]!=''?'<img src="'+lang.cells[this.remoteAccessData.icon]+'" border="0"'
+' alt="'+lang.cells[this.remoteAccessData.language]+'"'
+' title="'+lang.cells[this.remoteAccessData.language]+'" />':lang.cells[this.remoteAccessData.language];break;case'label':var label=lang.cells[this.remoteAccessData.language];break;}
links+=(links!=''?'&nbsp;':'')+' <a href="http://'+lang.cells[this.remoteAccessData.domain].split(/\r?\n/)[0]+'/'+page+'"'+onclick+'>'
+label+'<\/a> ';}.bind(this));menu+=links+'</div>';this.settings.placeHolder.innerHTML=menu;},hideZeroFootprint:function(){this.settings.placeHolder.getParent().addClass('sk-remove-zero-footprint');}});
