function updateSlide(data)
{
	if(data.sliderActual==1){
		data.btnPrev.css('background-position','0 -40px');
	} else	{
		data.btnPrev.css('background-position','0 0px');
	}
	
	if(data.sliderActual==data.sliderTotal){
		data.btnNext.css('background-position','0 -40px');
	} else {
		data.btnNext.css('background-position','0 0px');
	}
}

function inputMailHover(data, classOff, classOn)
{
	$(data).removeClass(classOff);
	$(data).addClass(classOn);
}

function inputMailOut(data, classOff, classOn)
{
	if($(data).val()==''){
		$(data).removeClass(classOn);
		$(data).addClass(classOff);
	}
}

function validateMail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email;
   if(reg.test(address) == false) {
      return false;
	} else {
		return true;
	}
}

function ajaxAddMail(qstring)
{
	var targetElement 	= '#sectionMainText';

	$.ajax({
		type: 		'POST',
		url: 		'ajax.agregarMail.php',
		data: 		qstring,
		cache: 		false,
		beforeSend: function(){
			$('#mailing td div').toggle();
		},
		success: function(generado){
			$('#mailingLoading').hide();
			$('#mailingTextContainer').html('mailing list: <span>' + generado + '</span>').show();
		},
		error: function(){
			$('#mailingLoading').hide();
			$('#mailingTextContainer').html('mailing list: <span>No disponible</span>').show();
		}
	});
}

function popUp(windowUri, windowName, windowHeight, windowWidth)
{
	var centerWidth = (window.screen.width - windowWidth) / 2;
	var centerHeight = (window.screen.height - windowHeight) / 2;

	newWindow = window.open(windowUri, windowName, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=' + windowWidth + ',height=' + windowHeight + ',left=' + centerWidth + ',top=' + centerHeight);

	newWindow.focus();
	return newWindow.name;
}

(function($) { 
	$.fn.extend({ 
		antiSpam: function(options){
			
			var defaults = { 
				attr: 		'rel', 
				pattern: 	'/',
				atChar:		'@',
				dotChar:	'.'
			}; 

			var options = $.extend(defaults, options);

			return this.each(function(c){
				var obj = $(this);
				
				if(obj.attr(options.attr) && obj.attr(options.attr).indexOf(options.pattern) != -1 ){
					var value 	= (obj.attr(options.attr)).split(options.pattern);
					if(value.length==3 || value.length==4)
					{
						value = (value[3]) ? value[0]+options.atChar+value[1]+options.dotChar+value[2]+options.dotChar+value[3] : value[0]+options.atChar+value[1]+options.dotChar+value[2];
						obj.html(value).attr('href', 'mailto:'+value);
					}
				}
				
			});	
		} 
	}); 
})(jQuery);

$(document).ready(function(){


	// --- Form Mailing --- //
	
	$('#btn')
		.hover(function(){
				$(this).css('background-position','bottom left');
			},function(){
				$(this).css('background-position','top left');
		})
		.click(function(){
			if(validateMail($('#mailingMail').val()))
			{
				ajaxAddMail('jsTest=1&email=' + $('#mailingMail').val());
			}
		});

	$('#mailingMail')
		.bind('focus', function(event){ inputMailHover(this, 'hoverMailingMail', 'hoverMailingMailIn'); })
		.bind('blur', function(event){ inputMailOut(this, 'hoverMailingMail', 'hoverMailingMailIn'); });


	// --- Footer --- //
	
	$('#footerSquare')
		.hover(function(){
				$(this).css('background-position','0 -20px');
			},function(){
				$(this).css('background-position','0 0px');
		})
		.click(function(){
			window.location = 'index.php';
		});
		
		
	// --- Menu --- //
	
	$('.menuLink a')
		.each(function(){
			$(this)
				.hover(function(t){
						$(this).children('img').addClass('hover');
					},function(t){
						$(this).children('img').removeClass('hover');
				});
		});


	// --- Proteccion de Mails --- //
	/*
	$('a[rel]').each(function(){
		var value 	= ($(this).attr('rel')).split('/');
		value		= (value[3]) ? value[0]+'@'+value[1]+'.'+value[2]+'.'+value[3] : value[0]+'@'+value[1]+'.'+value[2];
		$(this).html(value).attr('href', 'mailto:'+value);
	});
	*/
	
	$('a').antiSpam();
		
});
