function loadjscssfile(filename, filetype){
	
	 if (filetype=="js"){ //if filename is a external JavaScript file
	  var fileref=document.createElement('script')
	  fileref.setAttribute("type","text/javascript")
	  fileref.setAttribute("src", filename)
	 }
	 else if (filetype=="css"){ //if filename is an external CSS file
	  var fileref=document.createElement("link")
	  fileref.setAttribute("rel", "stylesheet")
	  fileref.setAttribute("type", "text/css")
	  fileref.setAttribute("href", filename)
	 }
	 if (typeof fileref!="undefined")
	  document.getElementsByTagName("head")[0].appendChild(fileref)
	  
}


///////////////////////////////////////////////////////////////

/*
* 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 totalCOnforms = HomeConforms.length;
	
	for( j=0; j < totalCOnforms; j++){
			
			var json = eval( 'home_'+ HomeConforms[j] );
			for( i in json ){	
				 if( json[i].id ){
    				
					var idViews    	   = json[i].id + '_views';
					var idComments	   = json[i].id + '_comments';
					var idPositive	   = json[i].id + '_positive_votes';
					var idNegative	   = json[i].id + '_negative_votes';
					
					var views 	 = json[i].views;
					var comments = json[i].comments;
					var positive = json[i].positive_votes;
					var negative = json[i].negative_votes;				
				    
					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 );	
					
					}

				 }
				 
			    i++;
		    }
			 
	}
	
}
	
	/*
	* 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;
}

/*
* esmasGalaxyGet
* Ejecuta el request de javascript
* Devuelve los datos  la función 
* esmasUpdatePageGalaxy
*/
function esmasGalaxyGet	( strURL	) {
	
	
	loadjscssfile( strURL, 'js' );
		
} 

/*
* ConformIdSolutions
* toma los ids unicos y forma una colección
* Devuelve un arreglo de ids de conforms
* 
*/

function esmasGalaxyGetCommunityData(){
	
	var Collect = getElementsByClass('viewsGalaxyComments',document,'div');
	var Conforms = Array();
	
	for( x in Collect ){
		
		  if( Collect[x].id ) {
		
				 var Astr = ( Collect[x].id ).split("_");
				 var Conf = ( Astr[1].replace(/C/,'') );
				 
				 
				if ( !Aexists( Conforms, Conf ) ) {
						
						Conforms.push( Conf );
						
				}
		
		  }
			
	}
	

	
	var IdConforms     = Conforms.toString();
	var RandomToString = Math.random( ).toString();
	var RandomNumver   = '&rand='+ RandomToString.replace('0.','');
	var url = "http://comentarios.esmas.com/apis/jsonHomes.php?id="+IdConforms + RandomNumver; 	
	
	HomeConforms       = Conforms;	
	esmasGalaxyGet( url );
	
}

//Ejecutando la funcion desde el inicio
esmasGalaxyGetCommunityData();


