var radioHTML = null;

function dropRadio()
{
	document.getElementById('radio').style.display='none';
	radioHTML = document.getElementById('radio').innerHTML;
	document.getElementById('radio').innerHTML='';
}

function popup()
{
	//$.post('?ac=radio_popup');
	dropRadio();
	var width = 615;
	var height = 296;
	var left = ((screen.width - 10)/2) - width/2;
	var top = ((screen.height - 56)/2) - height/2;
	var ventana = window.open("?ac=radio_popup", "radio", "scrollbars=no, width="+width+", height="+height+", resizable=no, left="+left+", top= "+top);
}

var down = false;

function popdown()
{
	$.post('?ac=radio_popup');

	var divradio = document.getElementById('radio');
	if(divradio != null && divradio.style.display == 'none'){
		divradio.style.display = 'block';
		divradio.innerHTML = radioHTML;	
	}

	else if(divradio == null && down == true){
		document.location = '?c=radio';
	}
}


function swf()/*filename, width, height, wmode, menu, container*/
{
	var p = arguments[0] || {};
	var html = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + p.width + '" height="' + p.height + '">';
	html += '<param name="movie" value="' + p.filename + '" />';
	html += '<param name="quality" value="high" />';
	html += '<param name="wmode" value="' + p.wmode + '" />';
	html += '<param name="menu" value="' + p.menu + '" />';
	html += '<embed src="' + p.filename + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + p.width + '" height="' + p.height + '" wmode="'+p.wmode+'" menu="'+p.menu+'"></embed>';
	html += '</object>';
	document.getElementById(p.container).innerHTML = html;
}

// seudo clase Indicador de Carga
var LI = function()
{
	this.indicator = document.createElement('div');
	this.indicator.id				= 'divLI';
	this.indicator.style.position	= 'absolute';
	this.indicator.style.width		= '125px';
	this.indicator.style.height		= '14px';
	this.indicator.style.border		= '1px solid #F9F7ED';
	this.indicator.style.borderTop	= 'none';
	this.indicator.style.backgroundColor = '#FF7400';
	this.indicator.style.color		= '#F9F7ED';
	this.indicator.style.fontFamily	= 'Verdana, Arial, Helvetica, sans-serif';
	this.indicator.style.fontSize 	= '10px';
	this.indicator.style.padding	= '2px';
	this.indicator.innerHTML		= 'Espere por favor...';

	this.showed = false; // indicador mostrado
	this.moreShow = '';
	this.moreHide = '';

	this.show = function()
	{
		//if(!this.showed){
		if(document.getElementById(this.indicator.id) == null){
			this.indicator.style.left	= (document.documentElement.clientWidth - 131)/2 + 'px';
			this.indicator.style.top	= document.documentElement.scrollTop + 'px';
			document.body.appendChild(this.indicator);
			this.showed = true;
		}

		eval(this.moreShow);
		this.moreShow = '';
	}

	
	this.move = function()
	{
		if(this.showed){
			this.indicator.style.left	= (document.documentElement.clientWidth - 131)/2 + 'px';
			this.indicator.style.top	= document.documentElement.scrollTop + 'px';
		}

	}

	this.hide = function()
	{
		if(document.getElementById(this.indicator.id) != null/*this.showed*/){
			document.body.removeChild(this.indicator);
			this.showed = false;
		}
		eval(this.moreHide);
		this.moreHide = '';
	}

	this.add = function(showcode, hidecode)
	{
		this.moreShow += (showcode + '\n');
		this.moreHide += (hidecode + '\n');
	}

}

/// seudo clase FORM
var FORM = function(form)
{
	this.form = form;
	this.disablings = new Array();
	
	this.getArgs = function()
	{
		var form = this.form;
		var vars = new Array();
		var values = new Array();
		var k = 0; // indice de vars y values, se incrementa solo cuando el elemento es válido
			
		for(var i = 0; i < form.elements.length; i++){		
			if(form.elements[i].tagName == "INPUT"){
				if(form.elements[i].type == "text" || form.elements[i].type == "hidden" || form.elements[i].type == "password"){
					vars[k] = form.elements[i].name;
					values[k] = form.elements[i].value;
					k++;
				}
				if(form.elements[i].type == "checkbox"){
					if(form.elements[i].checked){
						vars[k] = form.elements[i].name;
						values[k] = form.elements[i].value;
					}
					else{
						vars[k] = form.elements[i].name;
						values[k] = "";
					}
					k++;
				}
				if(form.elements[i].type == "radio"){
					if(form.elements[i].checked){
						vars[k] = form.elements[i].name;
						values[k] = form.elements[i].value;
						k++;
					}
				}
			}

			if(form.elements[i].tagName == "SELECT"){
				var sel = form.elements[i];
				if(sel.type != "select-one"){
					for(j = 0; j < sel.options.length; j++){
						if(sel.options[j].selected){
							vars[k] = sel.name + "[]";
							values[k] = sel.options[j].value;
							k++;
						}
					}
				}

				else{
					vars[k] = sel.name;
					values[k] = sel.options[sel.selectedIndex].value;
					k++;
				}		
			}

			if(form.elements[i].tagName == "TEXTAREA"){
				vars[k] = form.elements[i].name;
				values[k] = form.elements[i].value;
				k++;
			}
		} // end for

		var args = new Array();
		for(i = 0; i < k; i++){
			values[i] = escape(values[i]);
			args[i] = vars[i] + "=" + values[i];
		}
		return args.join('&');
	} // en getArgs

	this.disable = function()
	{
		var form = this.form;
		for(i = 0; i < form.elements.length; i++){
			this.disablings[i] = form.elements[i].disabled;
			form.elements[i].disabled = true;
		}
	} // end disable

	this.enable = function()
	{
		var form = this.form;
		for(i = 0; i < form.elements.length; i++){
			form.elements[i].disabled = this.disablings[i];
		}
	} // end enable

	this.clean = function()
	{
		var form = this.form;
		form.reset();
	}
} // end seudo clase form

/// seudo clase ajax
var ajaxForm = null; // variable global
var ajaxInd = new LI();

function moveAjaxInd()
{
	ajaxInd.move();	
}

window.onscroll = moveAjaxInd;
window.onresize = moveAjaxInd;

var AJAX = function()
{
	// atributos
	var params		= arguments[0] || {};
	this.url		= params.url || 'index.php';
	this.args		= params.args || '';
	this.container 	= params.container || 'contenido';
	this.type		= params.type || 'content';

	// manejador
	this.req = false;

	try{
		this.req = new ActiveXObject('Msxml2.XMLHTTP');
	}

	catch(e){
		try{
			this.req = new ActiveXObject('Microsoft.XMLHTTP');
		}

		catch(e){
			this.req = false;
		}
	}
	
	if(!this.req && typeof XMLHttpRequest != 'undefined'){
		this.req = new XMLHttpRequest();
	}


	//////////////////////// funciones
	this.postForm = function(form)
	{
		ajaxForm = new FORM(form);
		var args = ajaxForm.getArgs();
		ajaxInd.add('ajaxForm.disable();', 'ajaxForm.enable();');
		this.getResponse({args:args, type:'script'});
	}

	this.getResponse = function()
	{
		var params	= arguments[0] || {};
		var url		= params.url || this.url;
		var args	= params.args || this.args;
		var container = params.container || this.container;
		var type	= params.type || 'content';

		this.post(url, args, container, type);
	}

	this.post = function(url, args, container, type)
	{
		var request = this.req;
		request.open('POST', url, true);
		request.onreadystatechange = function(){
			if(request.readyState == 1){ // conexión abierta
				ajaxInd.show(); // mostrar indicador
			}
			if(request.readyState == 4){ // data cargada
				ajaxInd.hide(); // ocultar indicador
				if(request.status == 200){
					try{ // exito
						if(type == 'content'){// insertar codigo

							var element = document.getElementById(container);
							element.innerHTML = request.responseText;

							var scripts = element.getElementsByTagName('script');
							var head	= document.getElementsByTagName('head')[0];

							for(var i = 0; i < scripts.length; i++){
								if (scripts[i].src){
									var scriptObj = document.createElement("script");            
									scriptObj.setAttribute("type", "text/javascript");
									scriptObj.setAttribute("src", scripts[i].src);
									head.appendChild(scriptObj);
								}

								else{
									eval(scripts[i].innerHTML);
								}
							}
						}

						if(type == 'script'){
							eval(request.responseText);
						}
					}

					catch(e){ // error de resultado
						alert('error de resultado');
						alert(request.responseText);
					}
				}

				else{ // error de conexión
					alert('Error (' + request.status + "): " + request.statusText);
				}
			}	
		} // end onreadystatechange
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.send(args);
	} // end post
} // end AJAX


/* superjq */
$(document).ready(function(){
	/*
	$.post("?ac=weather", function(xml){
		$("span#clima").html(xml);
	});
	*/
	$('#botonbuscar').click(function(){
		$('#formbuscar').submit();
		return;
	});
	
	$('#email').focus(function(){ $(this).select(); });
});

$(document).ready(function(){
	// Reset Font Size
	/*var originalFontSize = $('html').css('font-size');
	$(".resetFont").click(function(){
		$('html').css('font-size', originalFontSize);
	});*/
	var selector = '#resizableText';
	// Increase Font Size
	$(".increaseFont").click(function(){
		var currentFontSize = $(selector).css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.2;
		$(selector).css('font-size', newFontSize);
		return false;
	});
	// Decrease Font Size
	$(".decreaseFont").click(function(){
		var currentFontSize = $(selector).css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.8;
		$(selector).css('font-size', newFontSize);
		return false;
	});
});


var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

$(document).ready(function(){
  headline_count = $("div.headline").size();
  $("div.headline:eq("+current_headline+")").css('top','0px');
  
  headline_interval = setInterval(headline_rotate,10000); //time in milliseconds
  
  $('#scrollup').hover(function() {
    clearInterval(headline_interval);
  }, function() {
    headline_interval = setInterval(headline_rotate,10000); //time in milliseconds
    //headline_rotate();
  });
  
});

function headline_rotate() {
  current_headline = (old_headline + 1) % headline_count; 
  $("div.headline:eq(" + old_headline + ")").animate({top: -205},"slow", function() {
    $(this).css('top','210px');
    });
  $("div.headline:eq(" + current_headline + ")").show().animate({top: 0},"slow");  
  old_headline = current_headline;
}

var enlace_cont;
var enlace_intervalo;
var enlace_anterior = 0;
var enlace_actual = 0;

$(document).ready(function(){
  enlace_cont = $("div.enlace").size();
  $("div.enlace:eq("+enlace_actual+")").css('top','0px');
  
  enlace_intervalo = setInterval(enlace_rotate,5000); //time in milliseconds
  $('#scrollupe').hover(function() {
    clearInterval(enlace_intervalo);
  }, function() {
    enlace_intervalo = setInterval(enlace_rotate,5000); //time in milliseconds
    enlace_rotate();
  });
});

function enlace_rotate() {
  enlace_actual = (enlace_anterior + 1) % enlace_cont; 
  $("div.enlace:eq(" + enlace_anterior + ")").animate({top: -205},"slow", function() {
    $(this).css('top','210px');
    });
  $("div.enlace:eq(" + enlace_actual + ")").show().animate({top: 0},"slow");  
  enlace_anterior = enlace_actual;
}

/* poll */
$(document).ready(function(){
	//$.post("index.php?ac=poll&vote=0",function(xml){
		/*$("#poll-container").append(unescape(xml)).fadeIn("slow",function(){*/animateResults(); $("#poll").submit(formProcess);//});
	//});
});

function formProcess(event){
  event.preventDefault();
  
  var id = $("input[@name='poll']:checked").attr("value");
 
  if(id != null){
 	 id = id.replace("opt",'');
  
	$("#poll-container").fadeOut("slow",function(){
	$(this).empty();
	$.post("index.php?ac=poll&vote="+id,function(xml){
		$("#poll-container").append(unescape(xml)).fadeIn("slow",function(){animateResults();});
	});
	});
  }
}

function animateResults(){
  $("#poll-results div").each(function(){
      var percentage = $(this).next().text();
      $(this).find('span.bar').css({width: "0%"}).animate({width: percentage}, 'slow');
  });
}