
function showThickbox(msg,width,height) {
	if (width == undefined && height == undefined) {
		auto = true;
	} else {
		auto = false;
		if (width == undefined) {
			width = 'auto';
		} else {
			width = width+'';
		}
		if (height == undefined) {
			height = 'auto';
		} else {
			height = height+'';
		}
	}
	
	jQuery(document).ready(function() {
		$.fancybox(
			msg,
			{
				'scrolling'			: 'no',
	        	'autoDimensions'	: auto,
				'width'         	: width,
				'height'        	: height,
				'transitionIn': 'none'
			}
		);
	});
}


function showThickboxUrl(url,width,height) {
		
	$.ajax({
		url: url, 
		type: 'get',
		success: function(data) {
			showThickbox(data,width,height);
		}
	});

}

function cerrarThickbox() {
	$.fancybox.close();
}

/* FUNCIONES DE USUARIO */

function formNewsletter() {
	//$('respuestapassword').style.display='';
	var form = $('#formnovedades');
	form.hide();
	var log = $('#pnovedades');
	var log = log.empty().addClass('ajax-loading-20');
	// Envio el formulario
	$.ajax({
		url: '/newsletter.envio?'+form.serialize(), 
		type: 'get',
		success: function(data) {
			log.removeClass('ajax-loading-20');
			log.html(data);
		}
	});

	// Analytics	
	pageTracker._trackPageview("/newsletter.envio");
}

// Formulario para registro de usuario
function envioUsuario() {
	checkForm('usuario',true);
}


/* MISCELANEA DE FUNCIONES */

function copiaFrase() {
	var frase = $('#batch_frase').html();
	$('.frase').html(frase.substring(frase.indexOf('<p>')));
}

function checkMailComentario() {
	var email = $('#email_comentario');
	if (email.attr("disabled")) {
		email.removeAttr("disabled"); 
	} else {
		email.attr("disabled", "disabled");
	}
}

function closeDiv(name) {
	$(name).style.display = 'none';
}

function openDiv(id, url) {
	ajaxDiv(id);
	
	$.ajax({
		url: url+'?id='+id, 
		type: 'get',
		onComplete: function() {
			dataDiv(id, this.response.text);
		}
	});
}

function dataDiv(id, text) {
	var nndiv = new Element('div');
	nndiv.setHTML(text);

	var ndiv = $('meta'+id)
	while (ndiv.hasChildNodes()) {
  		ndiv.removeChild(ndiv.firstChild);
  	}
	ndiv.setStyle('display','');
	ndiv.empty().removeClass('data-loading');
	ndiv.appendChild(nndiv);
}

function ajaxDiv(id) {

	var nndiv = new Element('div');
	nndiv.setHTML('<form class="ajax-loading">&nbsp;</form>');

	var ndiv = $('meta'+id)
	while (ndiv.hasChildNodes()) {
  		ndiv.removeChild(ndiv.firstChild);
  	}
	ndiv.empty().addClass('data-loading');
	ndiv.appendChild(nndiv);
}

// Envio de formulario
function openForm(id, nameForm, urlLoad, param) {
	//ajaxDiv(id);
	
	if (checkForm(nameForm,false)) {
	
	  	// Realizo peticion
		var miAjax = new Ajax(urlLoad+'?'+param, 
			{
			method: 'get',
			data:$(nameForm),
			onComplete: function() {
				dataDiv(id, this.response.text);
				//respuestaForm(id, this);
			}
			}
		)
		miAjax.request();
	}
}

// Respuesta de formulario
function respuestaForm(id, obj) {
	dataDiv(id, this.response.text);
}

// Formulario para enviar contactos
function envioContacto() {
	checkForm('contacto',true);
	
	// Analytics	
	pageTracker._trackPageview("/contacto.envio");
}

// Formulario para enviar comentarios
function envioComentario() {
	checkForm('comentario',true);

	// Analytics	
	pageTracker._trackPageview("/common.comentarios.envio");
}

// Funcion generica para enviar formularios
function envioForm(nameForm) {
	checkForm(nameForm,true);

	// Analytics	
	pageTracker._trackPageview("/"+nameForm);
}

// Enlace pagina amiga (Adaptar para descargas)
function clickEnlace(id) {
	var url = "/ver.enlace/"+id+"/?r="+Math.random();
	$.ajax({
		url: url,
		type: 'get'
	});
	
	// Analytics	
	pageTracker._trackPageview("/enlace");	
}

// Agregar pagina a Favoritos
function agregarFavoritos() {
	if (document.all) { window.external.AddFavorite("http://www.tierradeatomos.com", "Tierra de Atomos"); }
	if (window.sidebar) { window.sidebar.addPanel("Tierra de Atomos", "http://www.tierradeatomos.com", ""); }

	// Analytics	
	pageTracker._trackPageview("/favoritos");	
}


