// var e = document.createElement("script"); 
// e.src = "http://static.terra.com.ar/js/JavascriptCookieScript/1.1.1/javascript_cookies.js"; 
// e.type="text/javascript"; 
// document.getElementsByTagName("head")[0].appendChild(e);

var Poll = {

	application_url: '/encuesta/',
	random_seed: Math.floor(Math.random()*1000),

	jInfo: '',

	opciones: '',
	
	land: '',
	
	templates: {
		preguntas: '',
		resultados: ''
	},

	voted: false,
	ver_resultado: false,

	reportError: function() {
		alert("Se ha producido un error. Por favor, reintentelo mas tarde.");
	},
	
	htmlspecialchars: function(unescapedString) {

	  //unescapedString = unescapedString.replace(/&/g, '&amp;');
	  //unescapedString = unescapedString.replace(/</g, '&lt;');
	  //unescapedString = unescapedString.replace(/>/g, '&gt;');
	  //unescapedString = unescapedString.replace(/"/g, '&quot;');
	  //unescapedString = unescapedString.replace(/'/g, '&#039;');
/*	  unescapedString = unescapedString.replace(/á/g, '&aacute;');
	  unescapedString = unescapedString.replace(/é/g, '&eacute;');
	  unescapedString = unescapedString.replace(/í/g, '&iacute;');
	  unescapedString = unescapedString.replace(/ó/g, '&oacute;');
	  unescapedString = unescapedString.replace(/ú/g, '&uacute;');
	  unescapedString = unescapedString.replace(/Á/g, '&Aacute;');
	  unescapedString = unescapedString.replace(/É/g, '&Eacute;');
	  unescapedString = unescapedString.replace(/Í/g, '&Iacute;');
	  unescapedString = unescapedString.replace(/Ó/g, '&Oacute;');
	  unescapedString = unescapedString.replace(/Ú/g, '&Uacute;');
	  unescapedString = unescapedString.replace(/ñ/g, '&ntilde;');
	  unescapedString = unescapedString.replace(/Ñ/g, '&Nacute;');
	  unescapedString = unescapedString.replace(/ü/g, '&uuml;');
	  unescapedString = unescapedString.replace(/Ü/g, '&Uuml;');
	  unescapedString = unescapedString.replace(/¿/g, '&iquest;');
	  unescapedString = unescapedString.replace(/¡/g, '&iexcl;');*/
	  return unescapedString;
	},
	
	checkboxRadiobutton: function() {
			/* Radio replace */
			var rdoReplace = $("input:radio.chk-rdo-replace");
		
			rdoReplace.css("opacity","0");
			rdoReplace.before("<span class='rdo-replace'>Radio</span>");
			
			rdoReplace.change(function() {
				rdoClick($(this).attr("name"));
			});
			/* IE fix */
			rdoReplace.click(function() {
				rdoReplace.change();
			});
		
			$(".rdo-replace").click(function() {
				$(this).next().attr("checked","checked");
				rdoReplace.change();
			});
		
			function rdoClick(name) {
				$("input:radio.chk-rdo-replace[name='"+name+"']").each(function() {
					if ($(this).attr("checked")) {
						$(this).prev().addClass("rdo-checked");
					} else {
						$(this).prev().removeClass("rdo-checked");
					}
				});
			}
			/* // Radio replace */
		
		
			/* Checkbox replace */
			var chkReplace = $("input:checkbox.chk-rdo-replace");
			
			chkReplace.css("opacity","0");
			
			chkReplace.each(function() {
				if ($(this).attr("checked")) {
					$(this).before("<span class='chk-replace chk-checked'>Checkbox</span>");
				} else {
					$(this).before("<span class='chk-replace'>Checkbox</span>");
				}
			});
			
			chkReplace.each(function() {
				$(this).change(function() {
					if ($(this).attr("checked")) {
						$(this).prev().addClass("chk-checked");
					} else {
						$(this).prev().removeClass("chk-checked");
					}
				});
			});
			/* IE fix */
			chkReplace.click(function() {
				$(this).change();
			});
			
			$(".chk-replace").click(function() {
				if ($(this).next().attr("checked")) {
					$(this).next().removeAttr("checked");		
				} else {
					$(this).next().attr("checked","checked");
				}
				$(this).next().change();
			});
			/* // Checkbox replace */
		},
	
	getLastPoll: function(){
		
		if (Poll.ver_resultado) {
			var from_uri = document.location.search.split('=');
			var land = from_uri[1];
			
			if (land) {
				Poll.land = land;
			}
		}

		var url = Poll.land ? 'ver/landing/' + Poll.land : 'ver/ultima';
		url = Poll.application_url + url ;

		if (Poll.voted || Poll.ver_resultado) {
			var url2 = Poll.application_url + 'static/templates/resultados.html';
			$.get(url2, function(data){
				Poll.templates.resultados = data;
				$.getJSON(url, function(r){
					Poll.jInfo = r;
					Poll.viewResultados();
				});
			});
		} else {
			var url1 = Poll.application_url + 'static/templates/preguntas.html';
			$.get(url1, function(data){
				Poll.templates.preguntas = data;
				$.getJSON(url, function(r){
					Poll.jInfo = r;
					$('#encuestas').html(Poll.templates.preguntas.process(Poll.jInfo));
					Poll.checkboxRadiobutton();
				});
			});
		}
	},

	resizePopup: function() {
		if ($(".mod-poll-result").is(":visible")) {
			if (window.innerHeight) {
				window.resizeTo($(".mod-poll-result").width() + 30 + window.outerWidth - window.innerWidth,$(".mod-poll-result").height() + 30 + window.outerHeight - window.innerHeight);
			} else {
				window.resizeTo(274,$(".mod-poll-result").height() + 90);
			}
		}
	},

	addVote: function() {
		var url = Poll.application_url + 'ver/vote';
		var option_id = $('input[name=rdo-poll]:checked').val();
		var error = 0;

		if (! option_id ) {
			error = 1;
			return;
		}
		
		var flag =  Get_Cookie( 'ya_votado_' + Poll.land);
		if ( Get_Cookie( 'ya_votado_' + Poll.land)) {
			error = 2;
			Poll.voted = true;
		}

		if ( error == 0) {
			$.post(url, { poll_id: Poll.jInfo.id, option_id: option_id }, function(r){ },'json');
			Set_Cookie( 'ya_votado_'+ Poll.land ,  Poll.jInfo.id +'-0', '', '/', '', '' );
 			Poll.voted = true;
		}

		var w = window.open(Poll.application_url + 'static/popup.html?land=' + Poll.land, 'resultado_encuesta', 'width=300,height=450,scrollbars=NO');

		if (w && !w.closed) {
			w.focus();
		}
	},

	viewResultados: function() {
		$('#encuestas').html(Poll.templates.resultados.process(Poll.jInfo));
		var flag = Get_Cookie( 'ya_votado_' + Poll.land)

		if ( flag == Poll.jInfo.id + '-0') {
			Set_Cookie( 'ya_votado_'+ Poll.land , Poll.jInfo.id + '-1', '', '/', '', '' );
		} else if ( flag == Poll.jInfo.id + '-1'){
			$('#ya_has_votado').show();
		}

		Poll.resizePopup();
	},

	start: function() {

		Poll.getLastPoll();
  	},
	
	resultado: function() {
		Poll.ver_resultado = true;
		Poll.start();
	}
}

