/**
 * Functions for user profile
 *
 */
var UserProfile = new Object();
UserProfile.initialize = function()
{
	this.profile = $('#profile');
	this.editing = false;
	$.editable.addInputType('password', {
		element : function(settings, original) {
			var input = $('<input type="password">');
			$(this).append(input);
			return(input);
		}
	});
    $('.change_link').each(function(){
		jQuery.data(this, 'target_id', $(this).attr('title'));
		$(this).attr('title','');
		$(this).click(function(){
			UserProfile.editProfile(this);
			return false;
		});
	});
}
UserProfile.editProfile = function() {
	var element = arguments[0];
	if(this.editing)
	{
		UserProfile.cancelEditing(this.editing);
	}
	this.editing = element;
	var ids = jQuery.data(element,'target_id');
	var selector = '#' + ids;
	if(ids == 'pass')
	{
		var type = 'password';
	}
	else
	{
		var type = 'text';
	}
	$(selector, this.profile).editable(UserProfile.submitData, { 
      type   : type,
      select : true,
      width  : '120px',
	  onblur : 'ignore',
	  placeholder : 'Click change to edit this',
	  cancel : "<button type='cancel' style='display:none' />",
	  submit : "<button type='submit' style='display:none' />",
	  onreset : function(value, settings) {
         $(this).unbind(settings.event);
		 UserProfile.destroyButtons(UserProfile.editing);
		 $(this).editable('destroy');
     }
	});
	$(selector, this.profile).click();
	UserProfile.createButtons(element, ids);
}

UserProfile.createButtons = function(element, ids)
{
	var saveButton = $("<a>Save</a>").attr('href','#').attr('id','save_' + ids);
	var text = $("<span></span>").text("|").attr('id','delimiter_' + ids);;
	var cancelButton = $("<a>Cancel</a>").attr('href','#').attr('id','cancel_' + ids);;
	var buttons = new Array(saveButton, cancelButton, text);
	saveButton.click(function(){
		UserProfile.saveData(element, ids, buttons);
		return false;
	});
	cancelButton.click(function(){
		UserProfile.cancelEditing(element, ids, buttons);
		return false;
	});
	$(element).after(cancelButton).after(text).after(saveButton);
	$(element).hide();
	$('#' + ids, UserProfile.profile).find('input').bind('keypress', function(e) {
			if(e.keyCode==13){
					UserProfile.saveData(element, ids, buttons);
					return false;
			}
		});
	
}

UserProfile.saveData = function(element, ids, buttons){
	var element = arguments[0];
	var ids = arguments[1];
	var data = {X_AJAX_INFO : 'users_profile'};
	
	data[ids] =  $('#' + ids, UserProfile.profile).find('input').val();
	

	$.post(url, data, UserProfile._ajaxAccountCallback, 'json');
	
	UserProfile.destroyButtons(element);
	
	//send data based on arguments
}
UserProfile._ajaxAccountCallback = function(data, status, form)
{
	//show message
	if(data.status == 'succes')
	{
		$('#message').text('profile updated');
	}
	else
	{
		$('#message').text('update failed');
	}
	UserProfile.saveEditing(UserProfile.editing);
}
UserProfile.saveEditing = function(element)
{
	var ids = jQuery.data(element,'target_id');
	var selector = '#' + ids;
	var form = $(selector, this.profile).find('form');
	form.bind("submit", function(e){
		return false;
	});
	form.submit();
	$(selector, this.profile).editable('destroy');
	UserProfile.destroyButtons(element);
	$(selector).text($(selector, this.profile).text());
}
UserProfile.cancelEditing = function(element) {
	var ids = jQuery.data(element,'target_id');
	var selector = '#' + ids;
	$(selector, this.profile).find('form').find("button[type='cancel']").trigger('click');
	UserProfile.editing = false;
}		

UserProfile.destroyButtons = function(element) {
	var ids = jQuery.data(element,'target_id');
	var buttons = new Array($('#save_' + ids), $('#delimiter_' + ids), $('#cancel_' + ids));
	$.each(buttons, function(){
		$(this).remove();
	});
	$(element).show();	
}

UserProfile.submitData = function(value, settings)
{
	if(settings.type == 'password')
	{
		return '******';
	}
	else
	{
		return value;
	}
}

$(document).ready(function() {
	UserProfile.initialize(); 
});