$(document).ready(function() {
	$('#username').each(function() {
	    var default_value = this.value;
	    $(this).css('color', '#880000');
	    $(this).focus(function() {
	        if(this.value == default_value) {
	            this.value = '';
	            $(this).css('color', '#fff');
	        }
	    });
	    $(this).blur(function() {
	        if(this.value == '') {
	            $(this).css('color', '#880000');
	            this.value = default_value;
	        }
	    });
	});

	$('#password-clear').css('color', '#880000');

	$('#password-clear').show();
	$('#password').hide();

	$('#password-clear').focus(function() {
		$('#password-clear').hide();
		$('#password').show();
		$('#password').focus();
	});
	$('#password').blur(function() {
		if($('#password').val() == '') {
			$('#password-clear').show();
			$('#password').hide();
		}
	});


	$('#newmembership').click(function(event){
		$.fn.colorbox({
		    href:"/arabic/odn/register/user",
		    onOpen:function(){
			if($('#cboxContent #closebutton').length==0)
			{
			    $('#cboxContent').css('position', 'relative');
			    $('#cboxContent').append('<a id="closebutton" href="#close" onclick="$.fn.colorbox.close();return false;" style="position: absolute; top: -5px; right: -5px;"><img src="/arabic/odn/images/close.png" border="0" /></a>')
			}
		    }});

		event.preventDefault();
	});
	$('#gobtn').parents('form').submit(function(e){
	    $('#gobtn').click();
	    e.preventDefault();
	});
	$('#gobtn').click(function(event){
		if ($('#username').val() == '' || $('#password').val() == '')
		{
			$('#statusmessage').html('ادخل اسم المستخدم وكلمة المرور!').addClass('ajaxloginerror');
		}
		else
		{
			$('#login').hide();
			$('#logging').show();

			$.ajax({
				type: "POST",
				url: "http://www.orioncities.com/arabic/odn/login/check",
				data: "email=" + $('#username').val() + "&password=" + $('#password').val(),
				success: function(data){
					if (data == 'NO_USERNAME')
					{
						$.fn.colorbox({href:"/arabic/odn/login/chooseusername"});

						$('#logging').hide();
						$('#loggedin').show();
					}
					else if (data == 'OK')
					{
						$('#loginback').css('background-image', 'url(http://www.orioncities.com/arabic/odn/images/odn_logged_bg.png)').css('height', '47px');
						$('#usernamelabel').html($('#username').val());
						$('#logging').hide();
						$('#loggedin').show();
						
						$('#addreplyloginbtnbottom').hide();
						$('#addreply').show();
					}
					else if (data == 'INACTIVE')
					{
						$('#logging').hide();
						$('#statusmessage').html('');
						//$('#statusmessage').html('هذه العضوية لم يسبق لك تنشيطها، في حال رغبتك باستلام رسالة التنشيط مرة أخرى <a href="#" id="resendactivation">انقر هنا</a>.').addClass('ajaxloginerror');
						$('#login').show();
						$('#inactiveuser').show();
					}
					else
					{
						$('#logging').hide();
						$('#statusmessage').html('البيانات التي قمت بادخالها غير صحيحة!').addClass('ajaxloginerror');
						$('#forgotpwd').show();
						$('#login').show();
						//$('#wronglogin').show();
					}
				}
			});
		}
	});

	$('#logoutlink').click(function(event){
		$('#inactiveuser').hide();
		$('#forgotpwd').hide();
		$('#loggedin').hide();
		$('#loggingout').show();

		$('#password').val('');

		$.post("/arabic/odn/logout/ajax", function(data){
		if (data == 'OK')
			$('#add').hide();
			$('#addreply').hide();
			$('#addloginbtntop').show();
			$('#addloginbtnbottom').show();
			$('#addreplyloginbtnbottom').show();
			
			$('#loggingout').hide();

			$('#statusmessage').html('سجل دخولك للاستفادة من مميزات العضوية المجانية').removeClass('ajaxloginerror').removeClass('ajaxloginok');
			$('#loginback').css('background-image', 'url(http://www.orioncities.com/arabic/odn/images/odn_login_bg.png)').css('height', '87px');
			$('#login').show();
		});

		event.preventDefault();
	});

	$('#profilelink').click(function(event){
		$.fn.colorbox({href:"/arabic/odn/profile",
				onOpen:function(){
				    if($('#cboxContent #closebutton').length==0)
				    {
					$('#cboxContent').css('position', 'relative');
					$('#cboxContent').append('<a id="closebutton" href="#close" onclick="$.fn.colorbox.close();return false;" style="position: absolute; top: -5px; right: -5px;"><img src="/arabic/odn/images/close.png" border="0" /></a>')
				    }}});

		event.preventDefault();
	});

	$('#forgotpwdlink').click(function(event){
		$.fn.colorbox({href:"/arabic/odn/lostpw"});

		event.preventDefault();
	});

	$('#resendactivation').click(function(event){
		$('#inactiveuser').hide();

		$('#statusmessage').html('<img src="/arabic/odn/arabic/images/loading.gif" /> جاري إعادة الإرسال...').removeClass('ajaxloginerror');

		$.ajax({
			type: "POST",
			url: "http://www.orioncities.com/arabic/odn/register/resend_activation",
			data: "username=" + $('#username').val(),
			success: function(data){
				if (data == 'OK')
				{
					$('#statusmessage').html('تم إرسال رسالة التنشيط إلى بريدكم المعتمد لدينا.').addClass('ajaxloginok');
				}
				else
				{
					$('#statusmessage').html('فشلت عملية إعادة بعث رسالة التنشيط، اتصل بإدارة شبكة مطوري الجوزاء لتصحيح الأمر!').addClass('ajaxloginerror');
				}
			}
		});

		event.preventDefault();
	});
});
