//JAVASCRIPT LANGUAGE SELECTOR BY madgus@gmail.com
/* This version of JavaScript Language Selector can be used by anyone who is not intented
to do profit directly with this code, it means you can not sale the code as is, 
you can use it ont your web or php project, or put in a CD Magazine, if you really
do profit with your project and my code consider sharing them :P.
If you want to include the selector in some web editor, do not remove this legend.
The lastest version can be found at
http://www.compunauta.com/forums/linux/programacion/java/gus_language_selector.html

This is Open and Free Software, madgus@gmail.com, gustavo@compunauta.com
bugfix - feb bad days.
*/
//DEFINE FIRST ALL STYLES STARTING WITH lang. LIKE lang.es lang.en AND SO ON
var idiomas = new Array();
idiomas[0]='es';
idiomas[1]='en';
idiomas[2]='fr';
idiomas[3]='de';
idiomas[4]='pt';
idiomas[5]='ru';
idiomas[6]='it';
DEFAULT_LANGUAGE='en';
TRANS = new Object();
TRANSLATION = new Object();
function autodetect_language(){
language=(navigator["language"])?navigator["language"]:navigator["userLanguage"];
language=language.toLowerCase();
language=language.substring(0,2);
language=getCookieLang('DEFAULT_LANGUAGE',language);
//alert("DEF:AUTO:"+DEFAULT_LANGUAGE);

auto=false;
	for(i=0; i<idiomas.length ; i++){
		if(idiomas[i] == language){auto=true;}
	}
	if (auto==false){
		set_lang(DEFAULT_LANGUAGE);
	}else{
		set_lang(language);
	}
}

function set_lang(idioma){
	for (m=0; m < idiomas.length; m++){
		if (idiomas[m] == idioma) {
		unhide_class('lang.'+idiomas[m]);
		}else{
		hide_class('lang.'+idiomas[m]);
		}
	}
	setCookieLang('DEFAULT_LANGUAGE',idioma);
	//alert("SET:LAN:"+idioma);
}

function hide_class(classname){
capas = document.getElementsByTagName('div');
   for(var i = 0;i < capas.length ;i++){
      var capa = capas.item(i);
      for(var j = 0; j < capa.attributes.length; j++){
          if(capa.attributes.item(j).nodeName == 'class'){
             if(capa.attributes.item(j).nodeValue == classname){
             capa.style.display = 'none';
             }
          }
      }
    }
}

function unhide_class(classname){
capas = document.getElementsByTagName('div');
   for(var i = 0;i < capas.length ;i++){
      var capa = capas.item(i);
      for(var j = 0; j < capa.attributes.length; j++){
          if(capa.attributes.item(j).nodeName == 'class'){
             if(capa.attributes.item(j).nodeValue == classname){
             capa.style.display = 'inline';
             }
          }
      }
    }
}

function toggle_class(classname){
capas = document.getElementsByTagName('div');
   for(var i = 0;i < capas.length ;i++){
      var capa = capas.item(i);
      for(var j = 0; j < capa.attributes.length; j++){
          if(capa.attributes.item(j).nodeName == 'class'){
             if(capa.attributes.item(j).nodeValue == classname){
             if(capa.style.display == 'inline'){
             capa.style.display = 'none';}
             else{capa.style.display = 'inline';}
             }
          }
      }
    }
}

//SIMPLE SETCOOKIE
function setCookieLang(name, value){
	setCookieFullLang(name, value, 0.1, "/", "", "");
}
function getCookieLang(name, defvalue){
	value=getCookieFullLang(name);
	if(value==null){return defvalue;}
	else{return value};
}
function getLang(){
	return getCookieLang('DEFAULT_LANGUAGE',language);
}
function delCookieLang(name){
	delCookieFullLang(name,"/","");
}
//COMPLEX COOKIE
function setCookieFullLang( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function getCookieFullLang( name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed name
		if ( cookie_name == name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function delCookieFullLang( name, path, domain ) {
if ( getCookieLang( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function autoTrans(word){
	return autoTrans(word,TRANS);
}
function autoTrans(word,TR){
	LANG=getLang();
	if(typeof(TR)=='undefined'){return word;}
	var t=TR[LANG+"."+word]
	if (typeof(t)!='undefined'){
	return t;
	}else{
	return word;
	}
}
