var refresh = false; 

$(document).ready(function () {	
	// Login form handlers.	
	$('#account-auth-loginForm-submit').bind('click', function () {
		$('#account-auth-loginForm').submit();
		return false;
	});
	
	$('#account-passwordRecovery-dialog').dialog({
        autoOpen: false,
        width: 440,
		minWidth: 440,
        height: 130,
		minHeight: 130
    });

	$('#account-auth-loginForm-forgotPassword').bind('click', function () {
		if (refresh) {
			$.post('/password-recovery/ajax-form/', {}, showRecoveryDialog);
		} else {
			showRecoveryDialog();
		}		
		return false;
	});
});

function showRecoveryDialog(data)
{ 
	refreshRecoveryDialog(data);
	$('#account-passwordRecovery-dialog').dialog('open');
}

function refreshRecoveryDialog(data)
{
	if (data != undefined) {
		$('#account-passwordRecovery-dialog').html(data);
	}
	
	// Add handlers for buttons and keys.
	$('#account-passwordRecovery-cancel').bind('click', function () {
		$('#account-passwordRecovery-dialog').dialog('close');
		return false;		
	})
	$('#account-passwordRecovery-getPassword').bind('click', sendRecoveryDialog);
	$('#account-passwordRecovery-recoveryForm').bind('submit', sendRecoveryDialog);
} 

function sendRecoveryDialog()
{
	refresh = true;
	$.post('/password-recovery/ajax-send/', {email: $('#account-passwordRecovery-email').val()}, refreshRecoveryDialog);		
	return false;
} 
/*
	Author: MindFreakTheMon.com
	jQuery 1.3.* Required
	Script was tested on IE7,8; FF3.5; Opera 10.01;
	Chrome ans Safari understands HTML5-placeholder attribute, so there is no need in this script for them.
*/

(function($)
{
	if(!$) return false;

	$.fn.extend({
		storeEvents: function(b) {
			return this.each(function() {
				var copy = function(j) {
					var o = {};
	
					for(i in j) {
						o[i] = typeof(j[i]) == 'object' ? arguments.callee(j[i]) : j[i];
					}
	
					return o;
				};
				
				$.data(this, 'storedEvents', copy($(this).data('events')));
				
				if(b) {
					$(this).unbind();
				}
			});
		},
		restoreEvents: function(b) {
			return this.each(function() {
				var events = $.data(this, 'storedEvents');
	
				if(events) {
					if(!b) {
						$(this).unbind();
					}
	
					for(var type in events) {
						for(var handler in events[type]) {
							$.event.add(this, type, events[type][handler], events[type][handler].data);
						}
					}
				}
			});
		},
		copyAttr: function(e, p, v) {
			return this.each(function() {
				var a = {}, i, n, m;
				
				if(p === true) {
					for(i = 0, v = $.makeArray(v); i < this.attributes.length; i++) {
						n = this.attributes[i].nodeName;
						m = this.attributes[i].nodeValue;
	
						if(m && $.inArray(n, v) == -1) {
							a[n] =m;
						}
					}
				} else {				
					for(i = 0, p = $.makeArray(p); i < p.length; i++) {
						if(typeof $(this).attr(p[i]) == 'string') {
							a[p[i]] = $(this).attr(p[i]);
						}
					}
				}
				
				$(this).attr(a);
			});			
		},
		blurfocus: function(options) {
			return this.each(function() {
				if($.browser.safari) {
					return false;
				}
				
				var text = $(this).attr('placeholder');
				options = $.extend({
					className: 'placeholded',
					classNameHide: 'placeholded-hide',
					handle_send: true,
					handle_password: true},
				options || {});
				
				if($(this).is(':password') && options.handle_password) {
					$(this).data('placeholded_type', 'password');
				}
				
				var blur = function(e) {
					if($(this).val().length == 0) {
						if($(this).data('placeholded_type') == 'password') {
							var input = $('<input type="text" name="' + $(this).attr('name') + '" />'), events = $(this).storeEvents(true).data('storedEvents');
							$(this).copyAttr(input, true, ['type', 'name']).replaceWith(input);
							input.blur().data('placeholded_type', 'password').data('storedEvents', events).restoreEvents();
							
							/**
							 * Save attribute class
							 * 
							 * @author Roman Makarov <roman.makarov@gtmdevelopments.com>
							 */
							input.addClass($(this).attr('class'));
						} else {
							var input = $(this);
						}

						/**
						 * Old version
						 */
						//input.attr('readonly', true).val(text).addClass(options.className);
						
						/**
						 * New version
						 * 
						 * @author Roman Makarov <roman.makarov@gtmdevelopments.com>
						 */
						input.val(text).addClass(options.className).removeClass(options.classNameHide);
					}
				}, focus = function(e) {
					if($(this).val() == text && $(this).hasClass(options.className)) {
						if($(this).data('placeholded_type') == 'password') {
							var input = $('<input type="password" name="' + $(this).attr('name') + '" />'), events = $(this).storeEvents(true).data('storedEvents');
							$(this).copyAttr(input, true, ['type', 'name']).replaceWith(input);
							input.focus().data('placeholded_type', 'password').data('storedEvents', events).restoreEvents();
							
							/**
							 * Save attribute class
							 * 
							 * @author Roman Makarov <roman.makarov@gtmdevelopments.com>
							 */
							input.addClass($(this).attr('class'));
						} else {
							var input = $(this);
						}
						
						/**
						 * Old version
						 */
						//input.val('').removeAttr('readonly').removeClass(options.className);
						
						/**
						 * New version for equatorlearning
						 * 
						 * @author Roman Makarov <roman.makarov@gtmdevelopments.com>
						 */
						input.val('').addClass(options.classNameHide).removeClass(options.className);
					}
				}, click = function() {
					focus.call(this);
					$(this).storeEvents(true).focus().restoreEvents();
				}, keydown = function(e) {
					if(e.keyCode == 9) {
						if(!$.browser.msie) {
							blur.call(this);
						}
	
						e.stopPropagation();				
					}
				};
				
				if($(this).is(':password, :text, textarea')) {
					$(this).val($(this).val() == text ? '' : $(this).val()).blur(blur).focus(focus).click(click).keydown(keydown).blur();
					
					if(options.handle_send) {
						$(this).parents('form:eq(0)').each(function() {
							if($(this).data('placeholder_form') != 'handled') {
								events = $(this).data('placeholder_form', 'handled').storeEvents().data('storedEvents');
								eval('var anonfunc = function(){ ' + ($(this).attr('onsubmit') || '') + ' };');
								events.submit = $.extend(true, {'spec_sub': function(){ $(this).find('.' + options.className).focus(); }},  events.submit, {'anonfunc': anonfunc, 'spec_aft': function(e){ if(e.isDefaultPrevented()) $(this).find(':input').blur(); }});
								$(this).removeAttr('onsubmit').data('storedEvents', events).restoreEvents();
							}
						});
					}
				}
			});
		}
	});
	
	$(function() {
		$(":input[placeholder]").each(function(){ $(this).blurfocus($(this).attr('placeholder')); });
	});
})(jQuery);