window.addEvent('domready', function() {
	var whereiam = window.location;
	whereiam = whereiam.toString();
	if(!$defined(sesion)){
		$('bnregistrate').set('href','http://www.tvazteca.com/registro/create?redirectTo=' + escape(window.location) + '&dom=tvazteca&ss=' + programa.subseccion);
		$('bn_iniciasesion').set('href','http://www.tvazteca.com/registro/login?redirectTo=' + escape(window.location) + '&dom=tvazteca&ss=' + programa.subseccion);
	}else{
		var divprofile = new Element('div',{'id':'profilepanel','class':'divprofile borderRadiusBL10 borderRadiusBR10'});
		var paclose = new Element('a',{'id':'bnprofclose','class':'fright','href':'javascript:void(0);'});
		var pimgclose = new Element('img',{'src':'http://www.statictvazteca.com/navegacion/barra/images/profclose.jpg','alt':''});
		var profavatar;
		if((sesion.avatar).length > 15)profavatar = sesion.avatar;
		else profavatar = 'http://www.statictvazteca.com/navegacion/barra/images/' + (sesion.sexo=='1'?'male':'female') + '/' + sesion.avatar + '.jpg';
		var profimg = new Element('img',{'src':profavatar,'class':'imgtopprofile'});
		var tmpNombre = sesion.nombre.trim() + ' ';			
		tmpNombre = tmpNombre.substr(0,tmpNombre.indexOf(' '));
		if(tmpNombre.length>15)tmpNombre = tmpNombre.substr(0,15) + '...';
		var profnombre = new Element('p',{'class':'ptop bnhighlight','html':tmpNombre});
		var profmail = new Element('p',{'class':'ptop highlight2','html':sesion.cCorreo});
		var location = "";
		var latlng = "";
		if($defined(sesion.latlng)){
			location = sesion.latlng;
			latlng =  sesion.latlng;
			location = location.slice(location.lastIndexOf('|') + 1);
			var profloc = new Element('p',{'id':'bnplocation','class':'ptop','html':location});
		}
		var profuploc = new Element('a',{'id':'bnubicacion','href':'/scripts/maps/delete.php?close=closeBumpBox&callback=callbackMyCountry&direccion='+escape(latlng),'title':'TV Azteca - Busca o selecciona tu ubicaci&oacute;n exacta en el mapa','rel':'660,440','class':'bumpbox bnatop','html':'Actualiza tu ubicaci&oacute;n geogr&aacute;fica'});
		pimgclose.inject(paclose);paclose.inject(divprofile);profimg.inject(divprofile);profnombre.inject(divprofile);profmail.inject(divprofile);if($defined(profloc))profloc.inject(divprofile);profuploc.inject(divprofile);divprofile.inject($('bndivlogin'));
		initBumpBox();
		$('bn_spanregtxt').set('html',tmpNombre);
		$('bn_sesiontoptxt').set('html','CERRAR SESIÓN');
		$('bn_iniciasesion').set('href','javascript:endSesion();')
		$('bn_spanbtnsesionimg').setStyle('background-position','0px -60px');
		$('bnregistrate').addEvent('click',function(e){
			e.stop();
			showBnProfile();
		});
		$('bnprofclose').addEvent('click',function(e){
			e.stop();
			closeProfilePanel();
		});
		$('bnubicacion').addEvent('mouseup',function(e){
			e.stop();
			closeProfilePanel();
		})
	}
//DETECT CONFIRM
	detectConfirm();
	function detectConfirm(){
		var confirm = Cookie.read('confirm');
		if(confirm != null){
			showTopAlert('Tu proceso de registro ha sido completado con ÃƒÂ©xito, para iniciar sesiÃƒÂ³n, ingresa tu e-mail y contraseÃƒÂ±a en la secciÃƒÂ³n de <strong>Ingresa</strong> en el panel de <strong>Participa</strong> de la barra de menÃƒÂº.');
			Cookie.dispose('confirm',{domain:'.tvazteca.com'});
		}
	}
//TOP PANEL CLOSE
	$('bnatopalertaclose').addEvent('click',function(e){
		e.stop();
		$('bntopalert').setStyle('display','none');
	});
});
var sesion;
var redirectToDefault;
function showTopAlert(x){
	$('ptopalert').set('html',x);
	$('bntopalert').setStyle('display','block');
}
function endSesion(){
	window.location = 'http://www.tvazteca.com/registro/login/logout?redirectTo=' + escape(window.location) + '&dom=tvazteca&ss=' + programa.subseccion;
}
function scrollToTop(){
	var toTop = new Fx.Scroll(window, {
    	duration: 1000,
    	wait: true
	}).toTop();
}
function flashReg(item){
	var fx = new Fx.Tween(item);
    fx.start('color', '#AF0812').chain(function(){
        this.start('color', '#FFF')
    }).chain(function() {
        this.start('color', '#AF0812')
    }).chain(function() {
        this.start('color', '#FFF')
    }).chain(function() {
        this.start('color', '#AF0812')
    }).chain(function() {
        this.start('color', '#FFF')
    }).chain(function() {
        this.start('color', '#AF0812')
    }).chain(function() {
        this.start('color', '#FFF')
    }).chain(function() {
        this.start('color', '#AF0812')
    }).chain(function() {
        this.start('color', '#FFF')
    }).chain(function() {
        this.start('color', '#AF0812')
    }).chain(function() {
        this.start('color', '#FFF')
    });
}
function setForReg(){setForLogin();}
function setForLogin(){
	 scrollToTop();
	 flashReg($('bn_spanregtxt'));
	 flashReg($('bn_sesiontoptxt'));
}
function showBnProfile(){
	$('profilepanel').setStyle('display','block');
	$('profilepanel').fade('hide');
	var showProf = new Fx.Morph('profilepanel', {duration: 600, transition: Fx.Transitions.Quad.easeOut}); 
	showProf.start({
	    'top': [73, 60], 
	    'opacity': 1
	});
}
function closeProfilePanel(){
	$('profilepanel').fade(0);
}
function updateLocation(){
	if(!$('bnplocation')){
		var profloc = new Element('p',{'id':'bnplocation','class':'ptop','html':''});
		var bnlinkref = $('bnubicacion');
		profloc.inject(bnlinkref,'before');
	}
	var newloc = sesion.latlng;
	newloc = newloc.slice(newloc.lastIndexOf('|') + 1);
	$('bnplocation').set('html',newloc);
	$('bnubicacion').set('href','/scripts/maps/delete.php?close=closeBumpBox&callback=callbackMyCountry&direccion='+escape(sesion.latlng));
}
/*BUMPBOX*/
function callbackMyCountry(response){ 
 	updateMyCountry(response);
	closeBumpBox();
}
function updateMyCountry(response){
 	var request = new Request.JSON({
		url: '/registro/update',
		method: "post",
		onComplete: function(jsonRes) {
					sesion.pais = response.pais;
					sesion.estado = response.estado;
					sesion.latlng = response.latlng;
				updateLocation();
		}
}).post(response);
}
