/*
* Creado por: Fernando Soto
* erickfernando@gmail.com
* Para Esmas.com
* AÃ±o 2009
* View, Comments & Votes

*/

//////////////////////////////////////////////////////////////

Function.prototype.bind = function (obj) {
  
      var fn = this;
   
      return function () {
  
      var args = [this];
   
      for (var i = 0, ix = arguments.length; i < ix; i++) {
  
      args.push(arguments[i]);
  
      }
   
      return fn.apply(obj, args);
   
      };
  
 };
	  
/*
* LibraryOnDemand
* Ejecuta el request de javascript
*/  
LibraryOnDemand = {
	
	 load : function (component,options,type){
		 
		this.options = options;
			
		var fileType = component.substring(component.lastIndexOf('.'));
		var head     = document.getElementsByTagName("head")[0];
		done         = false;
		
		
		switch( type ) {
						
			case 'css':
			
				var fileRef = document.createElement("link");
					fileRef.setAttribute("type", "text/css");
					fileRef.setAttribute("rel", "stylesheet");
					fileRef.setAttribute("href", component);
				
			
			break;
			default:
				
					var fileRef = document.createElement('script');
					
					fileRef.setAttribute("type", "text/javascript");
					fileRef.setAttribute("src", component);
					fileRef.onload = fileRef.onreadystatechange = this.onComplete.bind(this);
					
					
			break;
		
		}
			
			
		if (typeof fileRef != "undefined") {
			head.appendChild(fileRef);
		}
			
	},
	
	onComplete : function ( ){
		
		
		if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete") ) {
			
			done = true;			
			this.notify('onCompletedLoad',this);
			
		}
		
		
		
	},

	
	 notify : function(event_name){
	
		try{
			
			if(this.options[event_name]){
			
				return [this.options[event_name].apply()];
				
			}
				
		}catch(e){
			
			return false;
			
		}
	 }
	
};


///////////////////////////////////////////////////////////////

	/*
	* Aexists
	* recibe un arreglo y un string 
	* Cerifica si existe el string como 
	* elemento del arreglo proporcionado
	* devuelve @bolean
	*
	*/
	function Aexists ( collect , element ) {
	
		for (v in collect ) {
			
			if ( collect[v] == element) {
					  return true;
			  }
			  
		  }
		  
		return false;
		  
	};

	/*
	* esmasUpdatePageGalaxy
	* recibe los datos del servidor y parsea los datos 
	* y actuliza los divs del template
	*
	*/
	function esmasUpdatePageGalaxy(){
				 		
		var HomeConforms = getAllTags();
		var totalCOnforms = HomeConforms.length;
		
	
		 var gjson    = json;
		 var elements = gjson.elements;
		 var items    = elements.item;
		 
		// document.getElementById( 'dataRecived' ).innerHTML = (  json );
		 
				var idViews    	   = HomeConforms[0] + '_views';
				var idComments	   = HomeConforms[0] + '_comments';
				var idPositive	   = HomeConforms[0] + '_positive_votes';
				var idNegative	   = HomeConforms[0] + '_negative_votes';
				var idFans		   = HomeConforms[0] + '_fans';
				
				var views 	 = items.views;
				var comments = items.comments;
				var positive = items.votes;
				var negative = items.negative_votes;
				var fans = items.fans;
			
				if( document.getElementById( idViews ) ) {
				//Views				
				document.getElementById( idViews ).innerHTML = ( views );
				
				//console.debug( 'Vistas ->' +idViews );
				
				}
				
				if( document.getElementById( idComments ) ) {
				//Comments
				document.getElementById( idComments ).innerHTML = ( comments );
					
				//console.debug( 'Comentarios ->' + idComments );	
				
				}
				
				if( document.getElementById( idPositive ) ) {
				//Positive
				document.getElementById( idPositive ).innerHTML = ( positive );
					
				//console.debug( 'Votos positivos ->' + idPositivos );	
				
				}
				
				if( document.getElementById( idNegative ) ) {
				//Negative
				document.getElementById( idNegative ).innerHTML = ( negative );
					
				//console.debug( 'Votos negativos->' + idNegative );	
				
				}
				
				if( document.getElementById( idFans ) ) {
				
				//Fans
				document.getElementById( idFans ).innerHTML = ( fans );
					
				//console.debug( 'Votos negativos->' + idNegative );	
				
				}
					 
		
	}
	
	/*
	* getElementsByClass
	* optiene los divs por medio del class tag
	* devuelve una colecciÃ³n de elementos
	* 
	*/
function getElementsByClass(searchClass,node,tag) {
		
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
		
	}
	
	return classElements;
}


function getAllTags(){
		
	var Collect = getElementsByClass('viewsGalaxyComments',document,'div');
	var Conforms = Array();
	
	for( x in Collect ){
		
		  if( Collect[x].id ) {
		
				 var tmp  = Collect[x].id.split('_');
				 var ID   = tmp[0] +'_'+tmp[1] +'_'+tmp[2];
				 
				 
				if ( !Aexists( Conforms, ID ) ) {
						
						Conforms.push( ID );
						
				}
		
		  }
			
	}
	
	HomeConforms = Conforms
	
	return HomeConforms;
}


/*
* ConformIdSolutions
* toma los ids unicos y forma una colecciÃ³n
* Devuelve un arreglo de ids de conforms
* 
*/

function esmasGalaxyGetCommunityDataNote(){
 
	var alink   = (window.location).toString();
	var constan = '?type=TVprograms&url=';
	var server  = 'http://comentarios.esmas.com/apis/topUrl.php';
	
	var url     = server + constan + alink + '&format=json'; 	
	
	LibraryOnDemand.load(url,{
				
		onCompletedLoad : function(  ){
	
				setTimeout('esmasUpdatePageGalaxy()',1000)
		
		}

	});
	
}

//Ejecutando la funcion desde el inicio
esmasGalaxyGetCommunityDataNote();