var idioma = "es";
var vid = new Array();
var es = new Array();
var en = new Array();
var fistLoad = true;
jQuery().ready(function(){
						cargarEtiquetas();
						});
function asignarIdioma()
{
   //idimg = ""
    var ajax = objetoAjax();
    ajax.open("POST","idioma.php",true);
    
    ajax.onreadystatechange = function()
    {
        
		//if(ajax.readyState != 4)
			//$("#"+idimg).show('slow');
		
        if(ajax.readyState == 4)
        {
			
            var r = ajax.responseText;
			//alert(r)
	    	var rm = r.split("+");
			
			if(rm.length>0)
				var vr = rm[1].split("|");
			else
				vr[1]="-1"
            
			try
			{
				
				if(vr[1]=="-1")
            	{
                //alert(traducir2(17))	//error al actualzar
                //alert(traducir2(18))	//error al solicitar
            	}
            	else
				{
					 	if(scriptActual != "" && scriptActual!="musica.php")//from mandarina.js
							goto(scriptActual);
						if(idioma == "es")
						{
							$("#spanESP").css("color","#FFF");
							$("#spanENG").css("font-weight","bold");
							$("#spanESP").css("font-weight","normal");
							$("#spanENG").css("color","#F17F14");	
						}
						else
						{
							$("#spanESP").css("color","#F17F14");
							$("#spanENG").css("color","#FFF");
							$("#spanESP").css("font-weight","bold");
							$("#spanENG").css("font-weight","normal");
						}
						obtenerIdioma();
				}
				//$("#"+idimg).hide('slow');
			}
			catch(e)
			{}
            
        }
    }
    ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    ajax.send("funcion=asignarIdioma&idioma="+idioma)
	
}

function obtenerIdioma()
{
   //idimg = ""
   
    var ajax = objetoAjax();
    ajax.open("POST","idioma.php",true);
    
    ajax.onreadystatechange = function()
    {
        
		//if(ajax.readyState != 4)
			//$("#"+idimg).show('slow');
		
        if(ajax.readyState == 4)
        {
			
            var r = ajax.responseText;
			//alert(r)
	    	var rm = r.split("+");
			
			if(rm.length>0)
				var vr = rm[1].split("|");
			else
				vr[1]="-1"
            
			try
			{
				
				if(vr[1]=="-1")
            	{
                //alert(traducir2(17))	//error al actualzar
                //alert(traducir2(18))	//error al solicitar
            	}
            	else
				{
					idioma = vr[1];
					
						if(idioma == "es")
						{
							$("#spanESP").css("color","#FFF");
							$("#spanENG").css("font-weight","bold");
							$("#spanESP").css("font-weight","normal");
							$("#spanENG").css("color","#F17F14");	
						}
						else
						{
							$("#spanESP").css("color","#F17F14");
							$("#spanENG").css("color","#FFF");
							$("#spanESP").css("font-weight","bold");
							$("#spanENG").css("font-weight","normal");
						}
                    asignarEtiquetas(); 
				}
				//$("#"+idimg).hide('slow');
			}
			catch(e)
			{}
            
        }
    }
    ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	
    ajax.send("funcion=obtenerIdioma&filtro="+1)
	
}

function cargarEtiquetas()							 
{
		
	var csvURL = 'https://spreadsheets.google.com/pub?key=0As5BFdUOExUzdEplclhpWjlNWDNZQnpNU0JTaHVMZHc&hl=en&single=true&gid=0&output=csv';

	var yqlURL =	"http://query.yahooapis.com/v1/public/yql?q="+
					"select%20*%20from%20csv%20where%20url%3D'"+encodeURIComponent(csvURL)+"'%20and%20columns%3D'id%2Ces%2Cen'&format=json&callback=?";					
    yqlURL ="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%20%3D%20'https%3A%2F%2Fspreadsheets.google.com%2Fpub%3Fkey%3D0As5BFdUOExUzdEplclhpWjlNWDNZQnpNU0JTaHVMZHc%26hl%3Den%26single%3Dtrue%26gid%3D0%26output%3Dcsv'%20and%20columns%20%3D%20'id%2Ces%2Cen'&format=json&callback=?";
	$.getJSON(yqlURL,function(msg){
		
		$.each(msg.query.results.row,function(){
		try{
				var tid = this.id.replace(/""/g,'"').replace(/^"|"$/g,'');
				vid.push(tid);
				
					var tes = this.es.replace(/""/g,'"').replace(/^"|"$/g,'');
				es.push(tes);
					var ten = this.en.replace(/""/g,'"').replace(/^"|"$/g,'');
				en.push(ten);
		}
		catch( e)
		{
			//alert("error");
		}
		});
		
		if(en.length > 0)
		{
			obtenerIdioma();
		}
		else
			cargarEtiquetasPHP();
		
	});
	
}
function cargarEtiquetasPHP()
{
   //idimg = ""
    var ajax = objetoAjax();
    ajax.open("POST","idioma.php",true);
    
    ajax.onreadystatechange = function()
    {
        
		//if(ajax.readyState != 4)
			//$("#"+idimg).show('slow');
		
        if(ajax.readyState == 4)
        {
			
            var r = ajax.responseText;
			//alert(r)
	    	var rm = r.split("###");
			//alert(rm[1])
			if(rm.length>0)
				var vr = rm[1].split("|");
			else
				vr[1]="-1"
            //alert(vr);
			try
			{
				
				if(vr[1]=="-1")
            	{
                	alert("There was a problem loading the text. Text in english won't be available. Try refreshing the page by pressing F5.");
            	}
            	else
				{	
					vid = [];
					es = [];
					en = [];
					//alert(vr);
					for(var i=1;(vr.length-1)>=i;i=i+3)
					{
						//$("body").css("background-color","#FFFFFF");
						//$("body").html($("body").html()+vr[i]+vr[i+1]+vr[i+2])
						
						vid.push(vr[i]);
						es.push(vr[i+1]);
						en.push(vr[i+2]);
					}
					
					obtenerIdioma();
				}
			}
			catch(e)
			{}
            
        }
    }
    ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    ajax.send("funcion=labels");
	
}
function cambiarIdioma(d)
{
	idioma = d;
	asignarIdioma();

	
}
var t;

function asignarEtiquetas()
{
	//var labels = document.getElementsByTagName("label");

	var labels = $("label");
	//alert("l: "+labels.length);
	//if(labels.length > 0)
	//Cufon.replace("#spanESP",{ fontFamily: 'GothamBold' });
	//Cufon.replace("#spanENG",{ fontFamily: 'GothamBold' });
	
	$("label").each(function (){
							  
							  
							  if($(this).attr("eid"))
							  	{
									
									//alert(typeof($(this).attr("eid")))
									
//									t  = e($(this).attr("eid"));
									if(idioma == "es")
									$(this).html(es[index($(this).attr("eid"))]);
									if(idioma == "en")
									$(this).html(en[index($(this).attr("eid"))]);
									

		var font = new String();
	  font = $(this).css("font-family");
	  font = font.replace('"',"");
	  font = font.replace('"',"");
	  //alert($(this).html())
	  //console.log($(this).html()+" "+font+" "+$(this).attr("eid"))
	  //Cufon.replace($(this),{ fontFamily: 'GothamBold' });
//	  alert(typeof(font)+" "+font)
	  switch (font)
	  {
		  case "GothamMediumRegular":
		  Cufon.replace($(this),{ fontFamily: 'GothamMedium' });
		  break;											
		  case "GothamMediumItalic":
		  Cufon.replace($(this),{ fontFamily: 'GothamMedium' });
		  break;
		  case "GothamUltraRegular":
		  Cufon.replace($(this),{ fontFamily: 'GothamUltra' });
		  case "GothamUltraItalic":
		  Cufon.replace($(this),{ fontFamily: 'GothamUltra' });
		  break;
		  case "GothamThinRegular":
		  Cufon.replace($(this),{ fontFamily: 'GothamThin' });
		  break;
		  case "GothamThinItalic":
		  Cufon.replace($(this),{ fontFamily: 'GothamThin' });
		  break;
		  case "GothamLightRegular":
		  Cufon.replace($(this),{ fontFamily: 'GothamLight' });
		  
		  break;										
		  case "GothamLightItalic":
		  Cufon.replace($(this),{ fontFamily: 'GothamLight' });
		  break;
		  case "GothamBookRegular":
		  Cufon.replace($(this),{ fontFamily: 'GothamBook' });
		  break;										
		  case "GothamBookItalic":
		  Cufon.replace($(this),{ fontFamily: 'GothamBook' });
		  break;
		  case "GothamBoldRegular":
		  Cufon.replace($(this),{ fontFamily: 'GothamBold' });
		  //alert("changed!")
		  //console.log("GothamBoldRegular changed!!");
		  break;
		  case "GothamBoldItalic":
		  Cufon.replace($(this),{ fontFamily: 'GothamBold' });
		  break;
		  case "GothamBlackRegular":
		  Cufon.replace($(this),{ fontFamily: 'GothamBlack' });
		  break;
		  case "GothamBlackItalic":
		  Cufon.replace($(this),{ fontFamily: 'GothamBlack' });
		  break;										
		  case "GothamExtraLightRegular":
		  Cufon.replace($(this),{ fontFamily: 'GothamXLight' });
		  break;		
		  case "GothamExtraLightItalic":
		  Cufon.replace($(this),{ fontFamily: 'GothamXLight' });
		  break;	
		  
	  }
								}
							  
							  });
	/*for(var i=0; labels.length > i; i++)
	{
		var t;
		if( $(labels.item(i)).attr("eid") != "" )
		{
			t  = e($(labels.item(i)).attr("eid"));
			if(t!="undefined" && t!="" && typeof(t)!="undefined")
			labels.item(i).innerHTML = t;
			else
			labels.item(i).innerHTML = "";
		}
	}*/
	//$(".letrasIdioma").css("font-weight","bold");
	
	//$(".letrasIdioma").css("visibility","visible");

	$(".letrasIdioma").css("visibility","visible");
    $(".musicShareBtn").attr("title",$("#tiptipSong").html());  
	if(navigator.appName != "Microsoft Internet Explorer")
    	{
			$(".hasTipTip").tipTip();
				Cufon.replace($("#spanESP"),{ fontFamily: 'GothamBold' });
				Cufon.replace($("#spanENG"),{ fontFamily: 'GothamBold' });
			}
	if(fistLoad)
	{
			/*$("#container").fadeIn(2000);							
		$("#loader").fadeOut(500);		*/			   
		document.title = "Mandarina Lounge";							   
	//song("s.mp3");
	fistLoad = false;
		}
	
	
	$(".opcion").hover(function(){
		}, function(){
			Cufon.refresh();
		});

	//Cufon.replace(".opcion");
	//alert(1)
	//alert(" -- DEMO -- ");
}



function e(id)
{
	
	//var iid = parseInt(id);
	//alert(id);
	//id = id*1 - 1;
	//alert(idioma);
	
	if(idioma == "es")
		return es[index(id)];
	if(idioma == "en")
		return en[index(id)];
}

function index(id)
{
	
	
	for(var ss=0; vid.length > ss; ss++)
	{
		if(id==vid[ss])
		return ss;
	}
}
