var boxLoaderClass = function(properties) { 
	// inicializa las propiedades
	if( properties )
		this.boxProperties = properties;
	else {
		this.boxProperties = {
			box_index: 0, 
			box_full_id: '', 
			box_key: ''
		};
	}
	// interval timer
	var timerId = 0;
	// instance of this class
	var thisObject;
	// cuenta de veces intentados cargar el js ó css
	var tries = 0;
	// carga js ó css externo
	this.loadjscssfile = function(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); 
		 fileref.setAttribute('id', 'script_' + this.boxProperties.box_full_id);
		}
		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);
	}; 
	// obtiene un objeto html mediante su id
	this.get_object = function(id){
	   var object = null;
	   if( document.layers ){
		object = document.layers[id];
	   } else if( document.all){
		object = document.all[id];
	   } else if(document.getElementById){
			object = document.getElementById(id);
	   }
	   return object;
	};
	// crea un iframe y le asigna todas sus propiedades
	this.makeiFrame = function(){

		var arrRuta = new Array();
		arrRuta = location.href.split("type/");
		//casos de typos de casillas
		
		if ( arrRuta.length == 2 ){
			var typeNt = arrRuta[1].substr(0,2);
		} else { // caso default
			var typeNt = 'wg';			
		}
		
		
		
		if(esmas_safe_boxattributes[thisObject.boxProperties.box_index][0].encuestas[0].status.toString() == '1' || esmas_safe_boxattributes[thisObject.boxProperties.box_index][0].encuestas[0].status.toString() == '2'){
		   // Se crea de esta forma, de lo contrario no respeta la transparencia
		   iframe_id = 'esmas_safe_simplepoll_iframe_' + esmas_safe_boxattributes[thisObject.boxProperties.box_index][0].box_guid.toString();
		   
		   //guardará en el Parent, un hash con la relación de poll_guids -> iframe_ids que encuentre en la página
		   parentBoxesArray[esmas_safe_boxattributes[thisObject.boxProperties.box_index][0].encuestas[0].enc_guid] = iframe_id;
		   
		   iframe_src = 'http://polls.esmas.com/esmassafeiframe/type/'+ typeNt +'/host/' + window.location.host + '/pollguid/' + esmas_safe_boxattributes[thisObject.boxProperties.box_index][0].encuestas[0].enc_guid; 


		   var box_continer = thisObject.get_object('esmas_safe_simple_poll_box_' + thisObject.boxProperties.box_key); 
		   box_continer.innerHTML = '<iframe id="' + iframe_id + '" allowtransparency="true" frameborder="0" scrolling="no" src="' + iframe_src + '" style="height:50px;"></iframe>'; 
		   
		} else {
			thisObject.get_object(thisObject.boxProperties.box_full_id).innerHTML = 'Check Poll (' + esmas_safe_boxattributes[thisObject.boxProperties.box_index][0].encuestas[0].enc_id.toString() + ') Status!';
		}
	};
	// Carga de datos Dinámica y de Encuesta dentro del Box
	this.loadAttributes = function() {
		this.loadjscssfile('http://pollsdynamic.esmas.com/resources/json/frontend/attributes/esmas_safe_boxattributes_' + this.boxProperties.box_key + '.js', 'js');	
		this.waitForLoad();
	};
	// Espera a la carga de del JS
	this.waitForLoad = function() {
		
		var arrRuta = new Array();
		arrRuta = location.href.split("type/");
		//casos de typos de casillas
		if ( arrRuta.length == 2 ){
			var typeNt = arrRuta[1].substr(0,2);
		} else { // caso default
			var typeNt = 'wg';
		}
		
		
		thisObject = this;
		thisObject.tries = 0;
		thisObject.timerId = setInterval(
		function()
		{
			new function(){
				if (thisObject.tries >= 240){
					clearInterval(thisObject.timerId);
					thisObject.get_object(thisObject.boxProperties.box_full_id).innerHTML = 'Poll Properties Not Found!';
				}
				if (esmas_safe_boxattributes[thisObject.boxProperties.box_index] != undefined) {
					clearInterval(thisObject.timerId);						
					if ( esmas_safe_boxattributes[thisObject.boxProperties.box_index][0].box_status.toString() == '1' ){
						var css_url = '';
						
						switch(typeNt){
							case "wg":
								css_url = esmas_safe_boxattributes[thisObject.boxProperties.box_index][0].box_css.toString();
							break;
							case "nt":
								css_url = esmas_safe_boxattributes[thisObject.boxProperties.box_index][0].box_css_nt.toString();
							break;
						}
						
						//esta se quitaría en un futuro
						thisObject.loadjscssfile(css_url, 'css');
						
						thisObject.makeiFrame(); 
					}
				}
				thisObject.tries++;
			}
		}, 250);
	}
}
//Obtiene todos los objetos mediante la clase especificada
document.getElementsByClassName = function(clsName){
	var retVal = new Array();
	var elements = document.getElementsByTagName('*');
	for(var i = 0;i < elements.length;i++){
		if(elements[i].className.indexOf(' ') >= 0){
			var classes = elements[i].className.split(' ');
			for(var j = 0;j < classes.length;j++){
				if(classes[j] == clsName)
				retVal.push(elements[i]);
			}
		}
		else if(elements[i].className == clsName)
		retVal.push(elements[i]);
	}
	return retVal;
}
var esmas_safe_boxattributes = []; 
var esmas_safe_box_objects = []; 
//hash para encontrar los iframes y resizearlos desde el Parent
var parentBoxesArray = new Array();

//Se obtienen todos los Boxes de la Página
var myBoxesArray = new Array();
myBoxesArray = document.getElementsByClassName('esmas_safe_simple_poll_box');
//Se obtiene la encuesta que le corresponde a cada Box y se le inserta
for(i=0;i<myBoxesArray.length;i++){
	poll_box_id = myBoxesArray[i].id; 
	poll_box_key = poll_box_id.substring(27,poll_box_id.length);
	
	esmas_safe_box_objects[i] = new boxLoaderClass({box_index: i, box_full_id: poll_box_id, box_key: poll_box_key});
	esmas_safe_box_objects[i].loadAttributes();
}

function updateIFrame(height,guid) {
	var iframe = document.getElementById(parentBoxesArray[guid]); 
	iframe.setAttribute( 'height', height );
	iframe.style.height = height + 'px'; 
}

