Enjin_System_Profile_Hover = function(options) {
	this.init(options);
}


Enjin_System_Profile_Hover.prototype = {
	enabled: true,
	el: null,
	el_hover: null,
	el_link: null,
	options: null,
	
	init: function(options) {
		var default_options = {
			el: null,
			el_hover: null,
			text: 'Edit',
			callback: function() {},
			callback_scope: this,
			callback_params: [],
			mouse_callback: true
		};
			
		jQuery.extend(default_options, options);
		
		if (default_options.el_hover == null)
			default_options.el_hover = default_options.el;
		
		this.options = default_options;
		
		this.el = $(default_options.el);
		this.el_hover = $(default_options.el_hover);
		this.render();
	},	
	
	render: function() {
		var self = this;
		var el_link = document.createElement('div');
		var el_link_a = document.createElement('a');		
		
		el_link.className = 'element_tinypopup';
		el_link_a.href = "javascript:void(0);";
		el_link_a.onclick = function(evt) {
			if (!evt) var evt = window.event;
			evt.cancelBubble = true;
			if (evt.stopPropagation) evt.stopPropagation();
			
			self.hide();
			self.options.callback.apply(self.options.callback_scope, self.options.callback_params);
		}
		el_link_a.innerHTML = this.options.text;
		
		el_link.appendChild(el_link_a);		
		$(el_link).appendTo(this.el);
		this.el_link = $(el_link);
		
		//add to element the callbacks
		if (this.options.mouse_callback) {		
			this.el_hover.bind('mouseover', function(){
				if (self.enabled)
					self.show();
			});
			
			//add to element the callbacks
			this.el_hover.bind('mouseout', function(){
				self.hide();
			});
		}		
	},
		
	disable: function() {
		this.enabled = false;
	},
	
	enable: function() {
		this.enabled = true;
	},
	
	show: function() {
		this.el_link.show();
	},
	
	hide: function() {
		this.el_link.hide();
	}
}

Enjin_Profile_Common = function() {
	this.init();
}

Enjin_Profile_Common.__instance = null;
Enjin_Profile_Common.getInstance = function() {
	if (Enjin_Profile_Common.__instance == null) {
		Enjin_Profile_Common.__instance = new Enjin_Profile_Common();
	}
	
	return Enjin_Profile_Common.__instance;
}

Enjin_Profile_Common.prototype = {
	link_hovers: null,
	editors: null,
	data: null,
	
	init: function() {
	},
	
	initProfile: function() {
		var editor;
		var link_hover;
		var self = this;
		var el_about = '.group-about textarea[name=about]';
	
		this.link_hovers = {};
		this.editors = {};
		this.data = {
			quote: {
				default_quote: false
			}
		};
		this.checkDefaultQuote();
		
		
		//prepare about
		$(el_about).bind('keyup', function() {
			Enjin_Core.checkMaxChars(el_about, profile_limits.about);
		});
		
		//create links for quote
		editor = new Enjin_Editor_Inline({
			el: '#profile-panel-quote .block-content',
			url_save: absoluteUrl+'/ajax.php?s=profile_info',
			url_params: {cmd: 'set-quote'},
			save_response_mode: Enjin_Editor_Inline.SAVE_RESPONSE_AJAX,
			editor_attrs: {maxlength: profile_limits.quote}
		});	
		
		
		$(editor).bind('beforeRenderEditor', function() {
			if (self.data.quote.default_quote) {
				$('#profile-panel-quote .block-content').html('');
			}			
		});
		
		$(editor).bind('startEdit', function() {		
			self.link_hovers['quote'].disable();
		});
		
		$(editor).bind('stopEdit', function() {
			self.link_hovers['quote'].enable();
			self.checkDefaultQuote();			
		});
		
		link_hover = new Enjin_System_Profile_Hover({
			el: '#profile-panel-quote',
			text: 'Edit Quote',
			callback: function(editor) {
				editor.showEditor();
			},
			callback_params: [editor]
		});	
		
		//assign to
		this.link_hovers['quote'] = link_hover;
		this.editors['quote'] = editor;
	},
	
	checkDefaultQuote: function() {
		//check if the quote is white then replace with default
		var el = $('#profile-panel-quote .block-content');
		
		if (jQuery.trim(el.html()) == '') {
			this.data.quote.default_quote = true;
			el.html(profile_defaults_general.quote);
		} else {
			this.data.quote.default_quote = false;
		}
	},
	
	communityConfirmationLeave: function(evt, type, baseurl) {
		var params = {
			message: 'Leave confirm',
			message1: '',
			button_continue: 'Ok',
			scope: this,
			callback: function() {
				window.location = baseurl;
			}
		};
		
		if (type == 1)
			params.message1 = 'If you leave this community you will lose your administrator access';
		if (type == 2)
			params.message1 = 'Are you sure you want to leave this community?';
		
		//prevent close due same click event
		if (!evt) var evt = window.event;
		evt.cancelBubble = true;
		if (evt.stopPropagation) evt.stopPropagation();		
		
		Enjin_Core.showPopup(params);
	},
	
	communityConfirmationJoin: function(evt, baseurl) {
		var params = {
				message: 'Confirm Join',
				message1: 'Do you want to join to this community?',
				button_continue: 'Ok',
				scope: this,
				callback: function() {
					window.location = baseurl;
				}
			};
			
			//prevent close due same click event
			if (!evt) var evt = window.event;
			evt.cancelBubble = true;
			if (evt.stopPropagation) evt.stopPropagation();		
			
			Enjin_Core.showPopup(params);		
	}
}

$(document).ready(function() {	
	Enjin_Profile_Common.getInstance();
	
	$('.m_system-profile .profile-left .menu a, .m_system-profile .profile-main .menu a').click(function() {
		$('.m_system-profile .profile-left .menu .tab, .m_system-profile .profile-main .menu .tab').removeClass('selected');
		$(this).children('.tab').addClass('selected');
		$('.profile-main .block.main').children().css({ opacity: 0.4 });
	});

	$('.m_system-profile .profile-right .profile-sidebar-block-games .element_gamebox, .m_system-profile .widget-games .items .element_gamebox').each(function(){
		Enjin_Core.bindTooltip($(this), $(this).attr('data-title'), 'dashboard-game-tooltip');
	});
});


