$(document).ready(function(){
	$("input[type=submit], button").mousedown(function(){
		$(this).addClass('clicked');
	}).mouseup(function(){
		$(this).removeClass('clicked');
	}).mouseleave(function(){
		if($(this).hasClass('clicked')){
			$(this).removeClass('clicked');
		}
	});
	window.flashAlert = new flashAlert();
});

function logout(){
	$.ajax({
		type: 'get',
		url: url+'/users/logout',
		dataType: 'json',
		success: function(response){
			window.location.reload();
		}
	});
}

function initLoginBox() {
	var validator = $('.loginBox #UserLoginForm').validate({
		rules: {
			'data[User][login_email]': {
				email: true,
				required: true
			},
			'data[User][login_password]': {
				required: true,
				minlength: 6
			}
		},
		messages: {
			'data[User][login_email]': {
				email: 'por favor escribe correctamente el email',
				required: 'este campo es obligatorio'
			},
			'data[User][login_password]': {
				required: 'este campo es obligatorio',
				minlength: jQuery.format("ingresa al menos {0} caracteres")
			}
		}
	});
	$('.loginBox #UserLoginForm').ajaxForm({
		type: 'post',
		dataType: 'json',
		beforeSubmit: function(){
			if(validator.numberOfInvalids()) return false;
			$('#UserLoginForm div.submit input').hide();
			$('.loginLoader').show();
		},
		success: function(response){
			if(response.code == 'html'){
				if(window.location.href.match(/^(.*)registrate$/)){
					console.log('test');
					window.location.href = '/foros';
				} else {
					window.location.href = window.location.href;
				}
			} else if(response.code == 'fail'){
				$('.loginLoader').hide();
				$('#UserLoginForm div.submit input').show();
				flashAlert.error(response.message);
			}
		}
	});	
}

function flashAlert(){
	this.inProgress = false;
	this.queue = new Array();
	this.messageBoxId = 'flashAlert';
	
	this.error = function(message){
		this._flash('flashErrorMessage', message);
	};

	this.success = function(message){
		this._flash('success', message);
	};
	
	this._flash = function(cssClass, message){
		this.queue.push({cssClass: cssClass, message: message});
		this._displayCycle();
	};
	
	this._displayCycle = function(){
		if(this.inProgress) return true;
		this.inProgress = true;
		this._fetchAndDisplay(function(){window.flashAlert.inProgress = false;});
	};
	
	this._fetchAndDisplay = function(callback){
		var item = this.queue.pop();
		if(typeof(item) != 'undefined'){
			this._displayItem(item, function(){
				window.flashAlert._fetchAndDisplay(callback);
			});
		} else {
			if(typeof(callback) == 'function')	callback();
		}
	};
	
	this._displayItem = function(item, callback){
		$("#"+this.messageBoxId).remove();
		var element = $('<div class="'+item.cssClass+'" id="'+this.messageBoxId+'" style="display: none;">'+item.message+'</div>');
		$('#fixedFlashContainer').append(element);
		element.fadeIn(500, function(){
			$.sleep(2, function(){
				element.fadeOut(500, function(){
					if(typeof(callback) == 'function') callback();
					return true;
				});
			});
		});
	};
	
	
	this._init = function(){
		var self = this;
	};
	this._init();
}
	
/*
	Sleep by Mark Hughes
	http://www.360Gamer.net/

	Usage:
		$.sleep ( 3, function()
		{
			alert ( "I slept for 3 seconds!" );
		});
	Use at free will, distribute free of charge
*/
;(function($)
{
	var _sleeptimer;
	$.sleep = function( time2sleep, callback )
	{
		$.sleep._sleeptimer = time2sleep;
		$.sleep._cback = callback;
		$.sleep.current_i = 1;
		$.sleep.timer = setInterval('$.sleep.count()', 1000);
	};
	$.extend ($.sleep, {
		current_i : 1,
		_sleeptimer : 0,
		_cback : null,
		timer : null,
		count : function()
		{
			if ( $.sleep.current_i === $.sleep._sleeptimer )
			{
				clearInterval($.sleep.timer);
				$.sleep._cback.call(this);
			}
			$.sleep.current_i++;
		}
	});
})(jQuery);

jQuery.validator.addMethod('ckeditor', function(value, element){
	// Tests if the editor's container is empty
	var data = $(element).val();
	
	if(data == null || data == '' || $(data).text().match(/^\s*$/gi)){
		return false;
	} else {
		return true;
	}
}, 'Por favor, ingresa contenido');
