var CMxmHeader = Class.create(CSefMxm,{
			
	init : function( options ){
			
		this.setOptions( options );
		
		//Init Information
		this.iter    = 0;
		this.Mode 	 = this.options.Mode;
		this.Source  = this.options.Source;
		this.Timer   = this.Mode == 'Debug' ? 20 : this.options.Timer;
		this.Url 	 = this.options.Url;				
		this.SlideDown  = this.options.SlideDown;
		this.idData  = 'headerjs' + this.iter;
		
		this.returLabel = 'Global:'	;
		
		this.MinuteHold = null;
		
		this.ILoader = $('MxMLoaderUpdate');
		
		this.GameType   = null;
		this.GameStatus = null;
		
		this.loadGuiHeader();
	},
	
	setOptions : function( options ){							  
 		
		this.options = Object.extend({
									 
			Mode :  'Secure',					  
			Server : '',	
			Timer  : 50,
			Source : null,
			Url : '',
			onComplete : Prototype.emptyFunction
			
		}, options || {});
		
		
	},
	
	loadGuiHeader : function( ){
	
		ActiveHeader = null;		
		this.loadJs();
	},
	
	
	/*
	*  function : initDataPageLoad 
	*  description: actualiza en el api los datos que se requieren 
	*/
	initDataPageLoad : function (){
		
		if( this.SlideDown ) {
		
			$('MxMHeaderApi').slideDown({duration:1});
		
		}else{
				
			$('MxMHeaderApi').show();
		}
		
		this.makeNavHeader();
		
		this.liveTransmition();
		this.liveTransmitionUsa();
		
		this.updateTeam1();
		this.updateTeam2();
		
		
		this.updateDate();
		
		
	},
		
	/*
	*  function : executer
	*  description: loadPeriodicamente el nuevo js cada 60 segundos
	*
	*/
	
	executer : function( request ){
		
		this.removeLoader();
		
		this.DataMxmHeader   = this.DefineData( request ); 		
	
		this.MinuteHold 	= this.DataMxmHeader.minuto;
		
		this.guidPartido	= this.DataMxmHeader.guidPartido;
		this.tiempo			= this.DataMxmHeader.tiempo;
		
		this.DefineGameStatus();
		this.updateLiguillaData();
		
		
				
		this.HandLoadJs 	= this.loadJs.bindAsEventListener(this);
		
		this.ILoader.fade();	 
		
		///////////////////////////////////////////////////////////////
		this.updateHeader();
		this.updateTimmer();
		this.updateGoals();
		this.updateOnPenalties( true );
		
		////////////////////////////////////////////////////////////////////////
		
		if( this.iter == 1 ){
			
			new PeriodicalExecuter( this.HandLoadJs,this.Timer );
			this.initDataPageLoad();
			this.notify('onComplete',this);
		
		}
		else
		{
			var thisUrl = new String();
               	//var thisUrl = window.location.toString();
               	//var auxStr = thisUrl.replace('http://mxm2.televisadeportes.com/', '');
               	//urchinTracker(auxStr);
           	var pix = new Image();
       		pix.src = doStats('return');
		}
	},
	
	
	DefineGameStatus : function(){
		
		$('MxMHeaderGolLabelTeam1').hide();			
		$('MxMHeaderGolLabelTeam2').hide();
				
		var status = (this.DataMxmHeader.tiempo).toLowerCase();
	
		switch( status ){
			
			case 'penales': case 'penal':
			
				this.GameStatus = 'penal';				
				//this.updateOnPenalties( 2 );
			
			break;
			case 'final':
				
				this.GameStatus = 'final';		
				$('MxMHeaderMinuteGame').hide();
			
			break; 			
			case 'primer tiempo extra': case 'segundo tiempo extra': case 'te': case 'tiempo extra':
				
			
				this.GameStatus = 'extras';				
				this.showGlobalScore();
			
			break;
			case 'descanso':
				
				
				this.GameStatus = 'descanso';
			
			break;
			default:
				
				this.GameStatus = 'normal';				
				
				
			break;
			
		}
		
	},
	
	
///////////////////////////////////////////////// UPDATERS //////////////////////////////////////////////////////
	
	
	updateLiguillaData : function(){
		
		
		if( this.DataMxmHeader.partidoIda || this.DataMxmHeader.partidoVuelta ) {
			
		this.GameType = 'liguilla';
		
		var MxMDataLiguilla     = $('MxMDataLiguilla');
		var MxMDataLiguillaText = $('MxMDataLiguillaText');
		
		str = '<h6>';
		
		if( isNoEmpty( this.DataMxmHeader.partidoIda ) ) {
			
			str += 'Partido ida: <b> ' + 
			this.DataMxmHeader.partidoIda.local + '  ' + 
			this.DataMxmHeader.partidoIda.golesLocal + '  -  ' + 
			this.DataMxmHeader.partidoIda.golesVisitante + '  ' +  
			this.DataMxmHeader.partidoIda.visitante + '</b> '; 
		}
		
		if( isNoEmpty( this.DataMxmHeader.partidoVuelta ) ) {
			
			this.returLabel =  this.isExtraOrFinal() ? 'Vuelta: ' :  'Global: ' ;
			var goalLocal   =  this.isExtraOrFinal() ? this.DataMxmHeader.partidoVuelta.golesLocal : this.DataMxmHeader.equipoLocal.golesGlobal ;
			var goalVisit   =   this.isExtraOrFinal() ? this.DataMxmHeader.partidoVuelta.golesVisitante : this.DataMxmHeader.equipoVisitante.golesGlobal ;

		   str +=  " | "+this.returLabel+ '<b> ' + 
			this.DataMxmHeader.partidoVuelta.local + '  ' + 		  
			goalLocal + '  -  ' + 			
			goalVisit + '  ' + 
			this.DataMxmHeader.partidoVuelta.visitante + '</b> ';			
			
		}
		
		if( isNoEmpty( this.DataMxmHeader.partidoTE ) ) {
			
		str +=  ' | TE: <b> ' + 
			this.DataMxmHeader.partidoTE.local + '  ' + 
			this.DataMxmHeader.partidoTE.golesLocal + '  -  ' + 
			this.DataMxmHeader.partidoTE.golesVisitante + '  ' +  
			this.DataMxmHeader.partidoTE.visitante + '</b> '; 
			
			this.showGlobalScore();
			
		}
		
		str += '</h6>';
		
		MxMDataLiguillaText.update( str );
		MxMDataLiguilla.show();
		
		
		
		}
	},
	
	
	isExtraOrFinal : function(){
		
		 return isNoEmpty( this.DataMxmHeader.partidoTE ); //&&  this.GameStatus == 'final';
		
	},
	
	TimeAndPlace : function(){
		
		str  = '<img src="http://i2.esmas.com/deportes30/img/bullet_white_disc.gif" style="margin:0 5px">  Estadio: ';
		str +=  this.DataMxmHeader.estadio.nombre;
		
		str +=  isNoEmpty(DataMxmHeader.estadio.ciudad) ? ", " + DataMxmHeader.estadio.ciudad : '';
		str +=  isNoEmpty(DataMxmHeader.estadio.pais) ? ", " + DataMxmHeader.estadio.pais : '';
		return str;
		
	},
	
	updateHeader : function(){
		
		
		
		//Div MxMHeaderUrlApi
		var MxMHeaderUrlApi  = $('MxMHeaderUrlApi');
		if(this.DataMxmHeader.leyenda!=''){
		MxMHeaderUrlApi.href = this.DataMxmHeader.url;
		MxMHeaderUrlApi.update( this.DataMxmHeader.leyenda);
		$('MxMHeaderUrlApiConta').show();
		}else{
		$('MxMHeaderUrlApiConta').hide();
		MxMHeaderUrlApi.href = this.DataMxmHeader.url;
		MxMHeaderUrlApi.update( this.DataMxmHeader.leyenda);
		}
		
		
		
		$('MxMHeaderCoupName').update( this.DataMxmHeader.torneo.nombre);
		$('MxMHeaderStadiumCityName').update( this.TimeAndPlace() );
		
		
	},
	
	
	showGlobalScore : function(){
		
		if( this.DataMxmHeader.partidoIda || this.DataMxmHeader.partidoVuelta ) {				
		
				$('MxMHeaderGolLabelTeam1').show();
				$('MxMHeaderGolLabelTeam2').show();
			
			}
		
	},
	
	updateDate : function(){
			
			var str = '<strong>';
			
			str = this.DataMxmHeader.jornada.nombre;			
			str += ' - ' + this.DataMxmHeader.fechaPartidoLetra.replace(/-/g,' ');
			str += " / " + this.DataMxmHeader.horaPartido + '</strong> ';
			
			
			$( 'MxMDateInfoGame' ).update( str );
			
	},
	
	updateTimmer : function(){ 
								
		$('MxMHeaderMinuteGame').update( this.MinuteHold );
		if ($('mxmThcurrentTimeDiv'))
			$('mxmThcurrentTimeDiv').update( this.MinuteHold+'\'');
		$('MxMHeaderStatusGame').update( this.DataMxmHeader.tiempo );
		
	},
	
	
	updateGoals : function(){
		
		var LocalGoal =  this.isExtraOrFinal() ? this.DataMxmHeader.equipoLocal.golesGlobal : this.DataMxmHeader.equipoLocal.goles ;
		var VisitGoal =  this.isExtraOrFinal() ? this.DataMxmHeader.equipoVisitante.golesGlobal : this.DataMxmHeader.equipoVisitante.goles;
		
		$('MxMHeaderGolTeam1').update( LocalGoal );
		$('MxMHeaderGolTeam2').update( VisitGoal ); 
		
	},
	
	isPenalOrFinal : function(){
		
		var local = isNoEmpty( this.DataMxmHeader.equipoLocal.penales ) && this.DataMxmHeader.equipoLocal.penales > 0;
		var visit  = isNoEmpty( this.DataMxmHeader.equipoVisitante.penales ) && this.DataMxmHeader.equipoVisitante.penales > 0
		var tiempo = this.GameStatus == 'penal';
		
		return  local || visit || tiempo;
	},
	
	emptyToCero : function( value ){
			
			
			if( typeof(value) == 'string' ) {
				
			if( value.empty() ){
			
				return 0;
				
			}
			
		 }
			return value;
		
	},
	
	updateOnPenalties : function( type ){
	
	
	
		if( this.isPenalOrFinal() ) {
			
			
			var pLocal = this.emptyToCero( this.DataMxmHeader.equipoLocal.penales );
			var pVisit = this.emptyToCero( this.DataMxmHeader.equipoVisitante.penales );
			
			
			
			$('MxMHeaderMinuteGame').hide();			
			$('MxMHeaderGolPenaltyTeam1').update( '('+ pLocal + ')' ).show()
			$('MxMHeaderGolPenaltyTeam2').update( '('+ pVisit + ')').show()
			$('MxMHeaderPenaltyLabelTeam1').show();	
			$('MxMHeaderPenaltyLabelTeam2').show();
		  
			$("MxMHeaderGolTeam1Global").removeClassName('complete');
			$("MxMHeaderGolTeam2Global").removeClassName('complete');
			
			
	
		
		}else{
			
			$("MxMHeaderGolTeam1Global").addClassName('complete');
			$("MxMHeaderGolTeam2Global").addClassName('complete');
			$('MxMHeaderContainerGolPenaltyTeam1').hide();
			$('MxMHeaderContainerGolPenaltyTeam2').hide();
			
			$('MxMHeaderGolPenaltyTeam1').hide()
			$('MxMHeaderGolPenaltyTeam2').hide()
			$('MxMHeaderPenaltyLabelTeam1').hide();	
			$('MxMHeaderPenaltyLabelTeam2').hide();
			
		}
		
	},
	
	updateTeam1 : function(){
		
		
		
		//-----------Team1
		var MxMHeaderNameTeam1 = $('MxMHeaderNameTeam1');
		MxMHeaderNameTeam1.href = this.DataMxmHeader.equipoLocal.url;
		MxMHeaderNameTeam1.target = '_blank';
		MxMHeaderNameTeam1.update( this.DataMxmHeader.equipoLocal.nombre );
		
		$('AMxMHeaderLogoTeam1').href = this.DataMxmHeader.equipoLocal.url;
		$('AMxMHeaderLogoTeam1').target = '_blank';
		if( isNoEmpty( this.DataMxmHeader.equipoVisitante.logo ) )
			$('MxMHeaderLogoTeam1').src   = this.DataMxmHeader.equipoLocal.logo;
		else
			$('MxMHeaderLogoTeam1').src   = 'http://i2.esmas.com/canal30/img/spacer.gif';
	 },
	 
	 updateTeam2 : function(){
		
		//-----------Team2
		var MxMHeaderNameTeam1 = $('MxMHeaderNameTeam2');
		MxMHeaderNameTeam1.href = this.DataMxmHeader.equipoVisitante.url;
		MxMHeaderNameTeam1.target = '_blank';
		MxMHeaderNameTeam1.update( this.DataMxmHeader.equipoVisitante.nombre );
		
		
		$('AMxMHeaderLogoTeam2').href = this.DataMxmHeader.equipoVisitante.url;
		$('AMxMHeaderLogoTeam2').target = '_blank';
		if( isNoEmpty( this.DataMxmHeader.equipoVisitante.logo ) )
			$('MxMHeaderLogoTeam2').src   = this.DataMxmHeader.equipoVisitante.logo;
		else
			$('MxMHeaderLogoTeam2').src   = 'http://i2.esmas.com/canal30/img/spacer.gif';
		
		
	 },
	 
	 liveTransmition : function(){
		 
		if( isNoEmpty( this.DataMxmHeader.transimisionVivo ) ){
			
			//$('MxMImgliveTransmition').src = this.DataMxmHeader.transimisionVivo;
			$('MxMImgliveTransmition').setStyle({cursor:'pointer'});
			$('MxMImgliveTransmition').show();
			$('MxMImgliveTransmition').onclick = function(){
					
					if(this.DataMxmHeader.transimisionVivo.match("javascript")!="javascript"){					
						window.open(this.DataMxmHeader.transimisionVivo);
					}else{
					var jsfun=this.DataMxmHeader.transimisionVivo.replace("javascript:","");
					if(jsfun.substring(jsfun.length-1)!=";")
						jsfun+=";";
					eval(jsfun);
					}
				
			}.bind(this);
				
		}
		
	 },
	 liveTransmitionUsa : function(){
		if($('MxMImgliveTransmitionUsa')){
			if( isNoEmpty( this.DataMxmHeader.transimisionVivoUsa ) ){
				
				//$('MxMImgliveTransmitionUsa').src = this.DataMxmHeader.transimisionVivo;
				$('MxMImgliveTransmitionUsa').setStyle({cursor:'pointer'});
				$('MxMImgliveTransmitionUsa').show();
				$('MxMImgliveTransmitionUsa').onclick = function(){
					if(this.DataMxmHeader.transimisionVivoUsa.match("javascript")!="javascript"){					
						window.open(this.DataMxmHeader.transimisionVivoUsa);
					}else{
					var jsfun=this.DataMxmHeader.transimisionVivoUsa.replace("javascript:","");
					if(jsfun.substring(jsfun.length-1)!=";")
						jsfun+=";";
					eval(jsfun);
					}
				}.bind(this);
					
			}
		}
	 },	 
	 makeNavHeader  : function(){
		 
		 	var str = '';
			var data = this.DataMxmHeader.paginas;
			
			var M = 1;
			for( i in data ){
					switch(i)
					{
					case 'previo':
					  M=1;
					  break;
					case 'alineacion':
					  M=2;
					  break;
					case 'mxm':
					  M=3;
					  break;
					case 'campo':
					  M=4;
					  break;
					case 'cronica':
					  M=5;
					  break;
					case 'votaciones':
					  M=6;
					  break;
					case 'datoExperto':
					  M=7;
					  break;					  
					}
				var vId    = 'id="option-'+M+'"';
				var vclass = i == 'previo' ? 'class="first "' : '' ;
					if(i!='datoExperto'){
						str += '<li '+ vId +' '+vclass + '>';
						str += '<a href="'+ data[i] +'">';
						switch(i)
						{
						case 'cronica':
						  str += 'CR&Oacute;NICA';
						  break;
						case 'alineacion':
						  str += 'ALINEACI&Oacute;N';
						  break;
						case 'campo':
						  str += 'CAMPO DE JUEGO';
						  break;
						case 'datoExperto':
						  str += 'DATO EXPERTO';
						  break;					  
						default:
						  str +=  i.toUpperCase();
						}
						str += '</a>';
						str += '</li>'; 
					}
			}
			
			$('MxMenubarMinute').update( str );
		 
	 }
		
		
	
  
 })
