var d3mls_user = 
{
	changeSubmitState : function(state, checkbox, submit)
	{
		if (typeof checkbox == "undefined" || checkbox == null){
			var $d3mls_userSubmitButton = $('#d3mls_userSubmitButton');
			if (state == 'register')
			{
				if ($('#nameRow').hasClass('hidden'))
				{
					$d3mls_userSubmitButton.attr("name","register").val("Register");
					$('#d3mls_userSubmitLink span').html('Register');
					$('#nameRow').removeClass('hidden');
				}
				else
				{
					$d3mls_userSubmitButton.attr("name","login").val("Login");
					$('#nameRow').addClass('hidden');
				}
			}
			else if (state == 'forgot')
			{
				$d3mls_userSubmitButton.attr("name","forgot").val("Get Password");
				$('#popupSubmitButton').attr("name","forgot").val("Get Password");
				$('#d3mls_userSubmitLink span').html('Get Password');
			}
			else if (state == 'update')
				$d3mls_userSubmitButton.attr("name","update").val("Update");
			else if (state == 'logout')
				$d3mls_userSubmitButton.attr("name","logout").val("Logout");
		}
		else if (typeof checkbox != "undefined" || checkbox != null)
		{
			if ($(checkbox).is(":checked"))
			{
				$(checkbox.form).find("a.button > span").html("Get password").end().find("#d3mls_userSubmitButton").attr("name","forgot").end().find('#popupSubmitButton').attr("name","forgot").val("Get Password").end();
			}
			else
				$(checkbox.form).find("a.button > span").html("Login").end().find("#d3mls_userSubmitButton").attr("name","login");
		}
		if (submit === true)
		{
			document.forms.d3mls_usersForm.submit();
			return false;
		}
	},

	changePasswordFieldType : function(link){
		$(link).html( ($(link).html()=='hide') ? "show" : "hide" );
		$passwordField = $('#d3mls_user form input[name=password]').clone();
		$('#d3mls_user form input[name=password]')
			.replaceWith(
				"<input type='" + (($passwordField.attr("type")=="password") ? "text" : "password")+
				"' value='" + $passwordField.attr("value") +
				"' name='password' autocomplete='off' class='frm-txt'>");
	},

	facebook : function(system_id, liked)
	{
		if (typeof system_id != 'undefined' && system_id != null)
		{
			$.post('/app/modules/d3mls/ninja_code/facebook/like.php',
				{
					'system_id' : system_id,
					'liked' : parseInt(liked)
				},
				function(data){ /*console.log(data);*/ }
			);
		}
	},

	removeWishlist : function(system_id, link, addText, removeText)
	{
		if (typeof system_id != 'undefined' && system_id != null ){
			$.post("/app/modules/d3mls/ninja_code/wishlists.php", {'system_id' : system_id}, function(data){
				if (typeof link != "undefined" && link != null)
				{
					//site uses cufon fonts in anchor buttons
					var $fontWords = $(link).find(".cufon-canvas");
					if ($fontWords.length)
					{
						var $addRemoveWord = $($fontWords[0]);
						if ($addRemoveWord.attr("alt").match(/Add/))
							$(link).find('span').html(removeText);
						else if ($addRemoveWord.attr("alt").match(/Remove/))
							$(link).find('span').html(addText);
						Cufon.replace('span#addRemoveWishlistButton');
					}
					else //site uses regular text anchors for buttons
					{
						$(link).find('span').html( ($(link).find('span').html().match(/Add/)) ? removeText : addText  );
					}
				}
				else if (data == system_id)
					$(".wishlist_"+system_id).remove();
			});
		}
		return false;
	},

	qtipOptions : {
		position: {
			corner: {
				tooltip: 'topRight',
				target: 'bottomLeft'
			}
		},
		show: {
			when: false,
			ready: true
		},
		hide: {
			when: {event: 'unfocus'},
			effect: function() { 
				$(this).qtip("destroy");
			}
		},
		style: {
			padding : '0px',
			background: 'none',
			border: {
				width: 5,
				radius: 10,
				color: '#385168'
			},
			tip: true
	   	},
		api: {
			onRender : function () {
				$('#qtip_close_button').live('click',this.destroy);
			}
		}

	},
	

	popupSubmit : function(popupType)
	{
		if (popupType == 'register' || popupType == 'login'){
			$.post("/app/modules/d3mls/ninja_code/d3mls_user_popup.php",
				$(".qtip-active form[name=popupForm]").serialize(),
				function(data)
				{
					//logged in
					if (data.match(/error/)==null && data.match(/loggedout/)==null)
					{
						//logged in, populate form fields
						var userInfo = data.split("|")
						var name = userInfo[0];
						var email = userInfo[1];
						var passwordHash = encodeURIComponent(userInfo[2]);
						var passwordText = userInfo[3];
						$("#d3mls_usersPopupRegister .d3mls_usersPopup .popupForm input[name=name]").val(name);
						$('#d3mls_usersPopupRegister .d3mls_usersPopup .popupForm input[name=email]').val(email);
						$('#d3mls_usersPopupRegister .d3mls_usersPopup .popupForm input[name=password]').val(passwordText);
						//replace text in the welcome message
						var loginText = $('#d3mls_usersPopupLogin .d3mls_usersPopup .popupLoggedin').html().replace(/Welcome.*, /, "Welcome "+ name+", ");
						$('#d3mls_usersPopupLogin .d3mls_usersPopup .popupLoggedin').html(loginText);
						var registerText = $('#d3mls_usersPopupRegister .d3mls_usersPopup .popupLoggedin').html().replace(/Welcome.*, /, "Welcome "+ name+", ");
						$('#d3mls_usersPopupRegister .d3mls_usersPopup .popupLoggedin').html(registerText);
						//change the registration button to update
						$('#d3mls_usersPopupRegister .d3mls_usersPopup .popupForm a.button span').html("Update");
						//hide all the headers, show all the loggedin text
						$('.qtip-active .d3mls_usersPopup .popupHeader').hide().next().show();
						$('#d3mls_usersPopupLogin .d3mls_usersPopup .popupHeader').hide().next().show();
						$('#d3mls_usersPopupRegister .d3mls_usersPopup .popupHeader').hide().next().show();
						//hide the login form
						$('#d3mls_usersPopupLogin .d3mls_usersPopup .popupForm').hide();
						//set content in the active qtip
						if (popupType == 'register')
						{
							$('.qtip-active .d3mls_usersPopup .popupForm a.button span').html("Update");
							$('.qtip-active .d3mls_usersPopup .popupLoggedin').html(registerText);
						}
						else if (popupType == 'login')
						{
							$('.qtip-active .d3mls_usersPopup .popupLoggedin').html(loginText);
							$('.qtip-active .d3mls_usersPopup .popupForm').hide();
						}
					}
					//input error
					else if ((data.match(/error/)!=null))
						$(".qtip-active .d3mls_usersPopup .popupMessages").show().html(data);
					//logged out
					else if ((data.match(/loggedout/)!=null))
						window.location.href='/';
				}
			);
		}
		else if (popupType == 'logout' )
		{
			$.post("/app/modules/d3mls/ninja_code/d3mls_user_popup.php",
				{d3mls_usersForm: 1, logout:1},
				function(data)
				{
					if ((data.match(/error/)!=null))
						$(".qtip-active .d3mls_usersPopup .popupMessages").show().html(data);
					else if ((data.match(/loggedout/)!=null))
					{
						var options = { path: '/', expires: new Date(99999).toGMTString() };
						$.cookie('email', null, options);
						$.cookie('password', null, options);
						$.cookie('persist', null, options);
						window.location.href='/';
					}
				}
			);
			
		}
		return false;
	}
	
}
