/*
* Coded By PM @ Green Labs MX
* Nov 2010
*/
$(function(){
//variables-------------------->
 var host = "http://www.lachainemexico.com/index.php/nacional/";
 var rootHost = "http://www.lachainemexico.com/index.php/";
//funciones-------------------->

//listeners-------------------->
	$('div#pics').cycle({
		speed:5000
	});
	$('input#nombre').focus(function() {
		if ($(this).val() == "Nombre...") {
			$(this).val('');
		};
	});
	$('input#nombre').blur(function() {
		if ($(this).val() == "") {
			$(this).val('Nombre...');
		};
	});
	$('input#email').focus(function() {
		if ($(this).val() == "Email...") {
			$(this).val('');
		};
	});
	$('input#email').blur(function() {
		if ($(this).val() == "") {
			$(this).val('Email...');
		};
	});
	$('textarea').focus(function() {
		if ($(this).val() == "Comentarios...") {
			$(this).val('');
		};
	});
	$('textarea').blur(function() {
		if ($(this).val() == "") {
			$(this).val('Comentarios...');
		};
	});

//main------------------------->
	$('input#submit').live('click', function(event) {
		$(this).attr('disabled', true);
		var bot = $(this);
		var pane = $(this).parents('form');
		pane.animate(
			{
				opacity: 0.25
			}, 'fast');
		$.ajax({
			url: host+'contactSend/',
			type: 'POST',
			dataType: 'json',
			data: {
				name: 		$('input#nombre').val(),
				email: 		$('input#email').val(),
				comments: 	$('textarea#comments').val()
			},
			complete: function(xhr, textStatus) {
				pane.animate({opacity: 1}, 'fast');
				bot.removeAttr('disabled');
			},
			success: function(data, textStatus, xhr) {
				$('input[type=text]').each(function(index) {
					$(this).val('');
				});
				if (data.msg) {
					$('textarea').val(data.msg);
				}else{
					$('textarea').val(data.msg);
				}
			}
		});
		event.preventDefault();
	});
	//login form animations
	$('a#activateLogin').live('click', function(event) {
		$('form#loginForm').animate({top: 0}, 'slow');
		event.preventDefault();
	});
	$('div#closeBot').live('click', function(event) {
		$('form#loginForm').animate({top: '-160px'}, 'slow');
		event.preventDefault();
	});
	//login form ajax
	$('input#submitLogin').live('click', function(event) {
		$('p.error span#msg').text('');
		var username = $('input#username').val();
		var password = $('input#password').val();
		var preloader = $('form#loginForm img');
		if (!username) {
			$('input#username').animate({opacity: 0.2}, 'fast', function() {
				$('input#username').animate({opacity: 1}, 'fast');
			})
		}else if(!password){
			$('input#password').animate({opacity: 0.2}, 'fast', function() {
				$('input#password').animate({opacity: 1}, 'fast');
			})
		}else{
			preloader.fadeIn('fast');
			$.ajax({
				url: rootHost+'login',
				type: 'POST',
				dataType: 'json',
				data: {
					username: username,
					password: password
				},
				complete: function(xhr, textStatus) {
					//preloader unload
				},
				success: function(data, textStatus, xhr) {
			    	if (data.response == true) {
						if (data.section == 1) {
							window.location.href = rootHost+'backOffice/';
						}else if(data.section){
								window.location.href = rootHost+'backOfficeBailliages/dashboard/'+data.section;
						}
					}else{
						preloader.fadeOut('slow', function() {
							$('p.error span#msg').text(data.msg);
						});
					}
				}
			});

		}
		event.preventDefault();
	});
	//clear forms
	$(window).load(function(){
		$("input[type=text]").val('');
		$('textarea').val('');
	});
});
