/* Enjin.com v3 Core JS */

$(document).ready(function(){
	$('#layout-topbar .link.login').click(Core.showLoginBox);
	$('#layout-topbar .loginbox .login-button a').click(function(){$('#layout-topbar .loginbox .loginform').submit();});
	$('#layout-topbar .loginbox .forgot-button a').click(function(){$('#layout-topbar .loginbox .forgotform').submit();});
	$('#layout-topbar .loginbox .forgot-link, #layout-topbar .loginbox .backtologin-link').click(function(){$('#layout-topbar .loginbox .login').toggle(); $('#layout-topbar .loginbox .forgot-password').toggle();});
	$(".ghost_text").ghostText();
	
	$('#layout-topbar .loginbox .loginform').submit(function() {
		Core.login();
		return false;
	});
	$('#layout-topbar .loginbox .forgotform').submit(function() {
		Core.forgotPassword();
		return false;
	});
	
	if(location.pathname == '/index/index/showlogin') $('#layout-topbar .link.login').click();
	
	// links that open popup window
	$('a.new_window').click(Core.newWindow);
});

Core = { };

Core.login = function() {
	$.post("/ajax.php?s=auth", { cmd: "login", username: $('#layout-topbar .loginbox .loginform input[name=username]').val(), password: $('#layout-topbar .loginbox .loginform input[name=password]').val(), remember: $('#layout-topbar .loginbox .loginform input[name=remember]').attr('checked') }, function(data){
		if(data == 'success') location.reload(true);
		else $('#layout-topbar .loginbox .loginform .login-error').html(data.error).hide().fadeIn(150);
	}, 'json');
}

Core.forgotPassword = function() {
	$.post("/ajax.php?s=auth", { cmd: "forgot-password", email: $('#layout-topbar .loginbox input[name=email]').val() }, function(data){
		if(data == 'success') {
			$('#layout-topbar .loginbox .forgotform').hide();
			$('#layout-topbar .loginbox .forgot-success .forgot-email-sent').html($('#layout-topbar .loginbox input[name=email]').val());
			$('#layout-topbar .loginbox .forgot-success').show();
		}
		else $('#layout-topbar .loginbox .forgotform .forgot-error').html(data.error).hide().fadeIn(150);
	}, 'json');
}

Core.showLoginBox = function(event) {
	var loginBox = $('#layout-topbar .loginbox');
	loginBox.show()
	
	event.stopPropagation();
	$(document).one("click", function(event) {
		Core.hideLoginBox();
	});
	loginBox.bind('click', function(event) {
		event.stopPropagation();
	});
	
	$('#layout-topbar .loginbox .header').one("click", function(event) {
		Core.hideLoginBox();
	});
}

Core.hideLoginBox = function() {
	$('#layout-topbar .loginbox').hide();
}


Core.newWindow = function(evt) {
  var e = evt.target;
  if (e.tagName != "A") e = getParentByTag(e,"A");
  if (!e) return;

  var width = 500;
  var height = 640;
  
  // offset slightly from current window
  var screenX = (window.screenX ? window.screenX : window.screenLeft) + 40;
  var screenY = (window.screenY ? window.screenY : window.screenTop - 80) + 40;
  
  var w = window.open(e.href,e.target,"status,scrollbars,resizable,titlebar,left="+screenX+",top="+screenY+",width="+width+",height="+height);

  evt.preventDefault();
  evt.stopPropagation();

  w.focus();
}


/*
 * jQuery Plugins
 */

$.fn.ghostText = function() {
  return this.each(function(){
    var text = $(this).attr("placeholder");
    if(text!="") {
      if($(this).val()=="") $(this).addClass(text);
      $(this).focus(function(){
        if($(this).hasClass(text)) {
          $(this).removeClass(text);
        }
      });
      $(this).blur(function(){
        if($(this).val()=="") {
          $(this).addClass(text);
        }
      });
    }
  });
};