//Libreria js para ciudades

var ciudad = {
	name:"",
	tid:"",
	themeimg:"",
	base_url:"",
	
	init:function(name, tid, themeimg, base_url){
		var _this = this;
		
		_this.name = escape(name);
		_this.tid = tid;
		_this.themeimg = themeimg;
		_this.base_url = base_url;
		
		_this.selectReady();
		$("select.select_ciudad").val(tid);
		$("#header_content").css("background-image", "url(" + escape(_this.themeimg) + ")");
		
	},
	
	set:function(name, tid){
		var _this = this;
		
		cookie.set("ciudad_name", escape(name));
		cookie.set("ciudad_tid", tid);
		
		_this.name = escape(name);
		_this.tid = tid;
	},
	
	selectReady:function(){
		var _this = this;
		
		$("select.select_ciudad").change(function(){
			selector = "select.select_ciudad option:selected";
			
			$("div.select span").html($(selector).text());
			_this.set($(selector).text(), $(selector).val());
			
			
			if ($("#FrontHome").val()) {
				//var url_ciudad = $("#FrontHome").val();
		
				location.href = _this.base_url + "/" + $(selector).attr("title");
				//alert("go");
				
			}
			else {
			//	window.location.reload();
			var Cad=document.URL;
			var f= Cad.split("/");
			var fxe=f[3]+"?123";
			var fxe=fxe.split("?");
			var fxe=fxe[0]+"?"+fxe[1];

			var fxe2=f[4]+"?123";
			var fxe2=fxe2.split("?");
			var fxe2=fxe2[0]+"?"+fxe2[1];
			
				if(f[4]==null){
					//header(<META HTTP-EQUIV="Expires" CONTENT="0">);
					location.href = _this.base_url+"/"+fxe;
					//alert("entro otavez "+f[3]); //obtenemos el nombre de la url la parte final ej: lamega/audios --> audios
				}
				else{
					//alert("entro otavez "+f[4]);
					window.location.reload();
					//location.href = _this.base_url+"/"+fxe2;
				}
			}
			
		});
	},
	
	//Mobile Section --------------
	mobileInit:function(base_url){
		var _this = this;
		
		_this.base_url = base_url;
		//_this.name = escape(name);
		//_this.tid = tid;
		//_this.themeimg = themeimg;
		
		_this.mobileSelectReady();
		//$("select.select_ciudad").val(tid);
		//$("#header_content").css("background-image", "url(" + escape(_this.themeimg) + ")");
	},
	
	mobileSelectReady:function(){
		var _this = this;
		
		$("#btn_ciudad").click(function(){
			//var data = "cnid=" + $(this).attr("cnid") + "&tnid=" + $(this).attr("tnid");
			var data = {};
			overlay.ajax(_this.base_url + "/ciudades_m", data , "POST", 300, "", function(){
				$("#overlayBox .sel_ciudad").click(function(){
					alert($(this).attr("value") + " - " + $(this).text());
				});
			});
			
			return false;
		});
	}
	
}
