if(!thrupons) var thrupons = {};

thrupons.auth = {
	algo : null,
    base_url: null,

	init : function()
	{
		$(function(){
			thrupons.auth.autoreplace();
		});
	},

	dispatcher : function()
	{
		thrupons.connect.popup(thrupons.config.url.base + 'home/auth/popup', 'Login');
	},	

	autoreplace : function()
	{
		$("input.autoreplace").each(function(){
			var value = $(this).val();
			$(this).css({"font-style" : "italic"});
			$(this).focus(function(){
		    	if($(this).val() == value)
		    	{
		    		$(this).val("");
		    		$(this).css({"font-style" : "normal"});
		    	}
		    });
			$(this).blur(function(input){
		    	if($.trim($(this).val()) == "")
		    	{
		    		$(this).val(value);
		    		$(this).css({"font-style" : "italic"});
		    	}
		    });
		});
	},
	
	showPanel : function(panel)
	{
		if(panel == "login_tp")
		{
			$('#login_tp_title, #login_tp_cont').show();
			$('#login_fb_title, #login_fb_cont, #register_title, #register_cont, #confirmation_title, #confirmation_cont, #forgot_identify_title, #forgot_identify_cont, #forgot_reset_title, #forgot_reset_cont').hide();
		}

		if(panel == "register")
		{
			$('#register_title, #register_cont').show();
			$('#login_tp_title, #login_tp_cont, #login_fb_title, #login_fb_cont, #confirmation_title, #confirmation_cont, #forgot_identify_title, #forgot_identify_cont, #forgot_reset_title, #forgot_reset_cont').hide();
		}

		if(panel == "confirmation")
		{
			$('#confirmation_title, #confirmation_cont').show();
			$('#login_tp_title, #login_tp_cont, #login_fb_title, #login_fb_cont, #register_title, #register_cont, #forgot_identify_title, #forgot_identify_cont, #forgot_reset_title, #forgot_reset_cont').hide();
		}

		if(panel == "forgot_identify")
		{
			$('#forgot_identify_title, #forgot_identify_cont').show();
			$('#login_tp_title, #login_tp_cont, #login_fb_title, #login_fb_cont, #register_title, #register_cont, #confirmation_title, #confirmation_cont, #forgot_reset_title, #forgot_reset_cont').hide();
		}

		if(panel == "forgot_reset")
		{
			$('#forgot_reset_title, #forgot_reset_cont').show();
			$('#login_tp_title, #login_tp_cont, #login_fb_title, #login_fb_cont, #register_title, #register_cont, #confirmation_title, #confirmation_cont, #forgot_identify_title, #forgot_identify_cont').hide();
		}
	},

	register : function()
	{
        var name = $.trim($("#name").val());
        var email = $.trim($("#email").val());
        var password = $.trim($("#password").val());
        var rewrite_password = $.trim($("#rewrite_password").val());

		if(!name.length || !email.length || !password.length)
		{
			$("#register_cont p.error").html("Todos los campos deben estar completos.").show();
			return;	
	    }

		var validateMail = function(mail)
		{
			var exr = /^[0-9a-z_\-\.]+@[0-9a-z\-\.]+\.[a-z]{2,4}$/i;
			return exr.test(mail);
		};

		if(!validateMail(email))
		{
			$("#register_cont p.error").html("La direccion de email no es valida").show();
			return;	
	    }

		if(password != rewrite_password)
		{
			$("#register_cont p.error").html("La repeticion de la contraseña no coincide").show();
			return;	
	    }

	    $("#register_cont p.error").hide();
		$("#register_cont a.show_hide").toggle();
        $.ajax({
            url : 'home/register',
            dataType:'json',
  	        data : {
	            name : $("#name").val(),
	            email : $("#email").val(),
	            password : $("#password").val()
        	},
            success:function(data)
            {
        		if(data.error)
        		{
        			$("#register_cont p.error").html(data.error).show();
        			$("#register_cont a.show_hide").toggle();
        		}
        		else
        		{
        			thrupons.auth.showPanel('confirmation');
        			$(".confirmation_link").show();
    	            $("#tp_login_btn").hide();
    	            $("#dis_tp_login_btn").show();
        			$("#tp_register_form").reset();
        			$("#location").hide();
        		}
            }
        });
	},

	login : function()
	{
		$("#login_tp_cont p.error").hide();
		$("#login_tp_cont a.show_hide").toggle();
        $.ajax({
            url : 'home/login',
            dataType:'json',
  	        data : {
    		    email : $("#user").val(),
    		    pass : $("#pass").val()
        	},
            success:function(data)
            {
        		if(data.error)
        		{
        			$("#login_tp_cont a.show_hide").toggle();
        			$("#login_tp_cont p.error").html(data.error).show();
        		}
        		else
        		{
        			if(thrupons.config.mode == "popup")
        			{
        			    window.opener.location.reload();
        			    window.close();
        			}
        			else
        				top.location.href = thrupons.config.url.base + "home/page/index";
        		}
            }
        });
	},

    logout : function()
    {
		thrupons.connect.popup(thrupons.config.url.base + "home/logout/thrupons", 'Logout_by_Thrupons');
	},

	confirmation : function()
	{
		$("#confirmation_form p.error").hide();
		$("#confirmation_form a.show_hide").toggle();
        $.ajax({
            url : 'home/confirmation',
            dataType:'json',
  	        data : {
        	    code : $("#code").val()
        	},
            success:function(data)
            {
        		if(data.error)
        		{
        			$("#confirmation_form a.show_hide").toggle();
        			$("#confirmation_form p.error").html(data.error).show();
        		}
        		else
        		{
        			$("#acount_activated").show();
        			$(".confirmation_link").hide();
        			thrupons.auth.showPanel('login_tp');
        		}
            }
        });
	},

	clearInput : function(obj)
	{
	    if(document.getElementById(obj).value=='Tu nombre' || document.getElementById(obj).value=='tuemail@ejemplo.com'){
            document.getElementById(obj).value='';
        }
	},
	
	showDefaultTxt : function(obj)
	{
        var txt='';
        switch(obj){
            case 'name':
                txt="Tu nombre";
            break;
            case 'email':
                txt="tuemail@ejemplo.com";
            break;
        
        }
        if(document.getElementById(obj).value==''){
            document.getElementById(obj).value=txt;
        }
	},

	disableSubmit : function()
	{
		$("#submit").attr("submit","submit");
	},

	forgot :
	{
		identify : function()
		{
			$("#forgot_identify_form p.error").hide();
			$("#forgot_identify_form a.show_hide").toggle();
	        $.ajax({
	            url : 'home/forgot_identify',
	            dataType:'json',
	  	        data : {
	        	    email : $("#forgot_email").val()
	        	},
	            success:function(data)
	            {
	        		if(data.error)
	        		{
	        			$("#forgot_identify_form a.show_hide").toggle();
	        			$("#forgot_identify_form p.error").html(data.error).show();
	        		}
	        		else
	        			thrupons.auth.showPanel('forgot_reset');
	            }
	        });
		},

		reset : function()
		{
			if($("#new_password").val() != $("#rewrite_new_password").val())
			{
				$("#forgot_reset_form p.error").html("La contraseña es incorrecta").show();
				return;
			}
			$("#forgot_reset_form p.error").hide();
			$("#forgot_reset_form a.show_hide").toggle();
	        $.ajax({
	            url : 'home/forgot_reset',
	            dataType:'json',
	  	        data : {
	        		code : $("#code_reset").val(),
	        		password : $("#new_password").val()
	        	},
	            success:function(data)
	            {
	        		if(data.error)
	        		{
	        			$("#forgot_reset_form a.show_hide").toggle();
	        			$("#forgot_reset_form p.error").html(data.error).show();
	        		}
	        		else
	        			thrupons.auth.showPanel('login_tp');
	            }
	        });
		}
	}
};
thrupons.auth.init();


if(!thrupons.template) thrupons.template = {};

thrupons.connect = {
	init : function()
	{
		window.fbAsyncInit = function(){
			FB.init({
				appId: thrupons.config.facebook.appId,
				status:true,
				cookie:true,
				xfbml:true
			});
		};
	
		(function() {
			var e = document.createElement('script');
			e.async = true;
			e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
			document.getElementById('fb-root').appendChild(e);
		}());
	},

    login : function()
    {
		if(thrupons.config.mode == "popup")
			top.location.href = thrupons.config.url.base + "home/forward/login";
		else
			thrupons.connect.popup(thrupons.config.url.base + "home/forward/login", 'logout_by_facebook');
    },

    logout : function()
    {
		if(thrupons.config.mode == "popup")
			top.location.href = thrupons.config.url.base + "home/forward/logout";
		else
			thrupons.connect.popup(thrupons.config.url.base + "home/forward/logout", 'logout_by_facebook');
	},

    popup : function(url, name)
    {
        var screenX = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft;
        var screenY = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop;
        var outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth;
        var outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22);
        var width = 550;
        var height = 350;
        var left = parseInt(screenX + ((outerWidth - width) / 2), 10);
        var top = parseInt(screenY + ((outerHeight - height) / 2.5), 10);
        var features = 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',resizable=no,scrollbars=yes,directories=no,location=no,menubar=no,toolbar=no,titlebar=no';

        var newwindow = window.open(url, name, features);

    	if(window.focus)
    		newwindow.focus();
    }
};

thrupons.template.connect = thrupons.connect;
