var baseUrl = 'http://' + document.domain; 
baseUrl = baseUrl.replace('abacus', 'devsite');

$(document).ready(function() {
    var button = $('#loginButton');
    var box = $('#loginBox');
    var form = $('#loginForm');
    button.removeAttr('href');
    
    button.click(function(e) {
        /* check if the user has logged in */
    	$.ajax({
			  url: baseUrl + "/loginform.html?action=checklogin&callback=?",
			  dataType: "jsonp",
			  success: function(user){
				 if (user.loggedIn == true) {
					 box.show();
					 $('#loginForm').hide();
					 $('#loggedInUser .name').html(user.name);
					 if (user.type == 'merchantuser') {
						 $('#loggedInUser .affiliate').hide();
						 $('#loggedInUser .merchant').show();  
					 } else {
						 $('#loggedInUser .merchant').hide();
						 $('#loggedInUser .affiliate').show();
					 }
					 
					 $('#loginCloseBox .login-text').hide();
					 $('#loginCloseBox .account-text').show();
					 $('#loggedInUserBox').slideDown();
				 } else {
					 $('#loginChooser').hide();
					 $('#loginCloseBox .account-text').hide();
					 $('#loginCloseBox .login-text').show();
					 $('#loggedInUserBox').hide();
					 $('#loginForm').show();
					 $('#searchBox').hide();
					 box.show();
                                         button.hide();
				 }
			  }
		});
    	//Cancel the link behavior
        e.preventDefault();
        
    	var mask    = $('#mask');
    	var subMask = $('#subMask');

    	$('#contentWrapper').hide();
    	$('#footerWrapper').hide();
    	$('#linkstrip').hide();
    	//Get the screen height and width
        var maskHeight = $(document).height() - 128	;
        var maskWidth = $(document).width();

        $('#contentWrapper').show();
    	$('#contentWrapper').css({'height':maskHeight, 'overflow': 'hidden'});

        //Set height and width to mask to fill up the whole screen
        mask.css({'width':maskWidth,'height':maskHeight});
        mask.addClass('mask');

        subMaskHeight = maskHeight - 600;
        if (subMaskHeight < 0) {
        	subMaskHeight = 0;
        }
        subMask.css({'height':subMaskHeight});

        //transition effect           
        mask.fadeIn("fast");
    });
    form.mouseup(function() { 
        return false;
    });
    
    var loginAction = function() {
        /* validate */
		var username = $('#username').val(); 
		var password = $('#password').val();
		var valid    = true;
		if (username == '') {
			$('#error-username').html('Please enter user name');
			valid = false;
		} else {
			$('#error-username').html('');
		}
		if (password == '') {
			$('#error-password').html('Please enter password');
			valid = false;
		} else {
			$('#error-password').html('');
		}

		if (valid == false) {
			return (false);
		} 

		$('#login').val('Loading..');
		$.ajax({
			  url: baseUrl + "/loginform.html?action=checkauth&callback=?",
			  type: 'POST',
			  data: { username: username, password: password },
			  dataType: "jsonp",
			  success: function(result){
				  var usertype = 'none';
				  if (result.merchantuser == true) {
						usertype = 'merchantuser';
				  }

				  if (result.affiliateuser == true) {
						usertype = 'affiliateuser';
				  }

				  if (result.merchantuser == true && result.affiliateuser == true) {
						usertype = 'both';
				  }

				  if (usertype == 'none') {
					  $('#error-login').html('Incorrect username/password');
					  return (false);
				  } else if (usertype == 'both') {
					  $('#loginForm').hide();
					  $('#loginChooser').show();
				  } else {
						login(username, password, usertype);
				  }
			  }
		});
		$('#login').val('Login');
	}
    
    $('#login').click(loginAction);
    form.keypress(function(e) {
    	if(e.keyCode == 13) {
    		loginAction();
    	}
    });
    $('#loginClose').click(function(){
    	$('#searchButton').show();
    	box.hide();
    	$('#mask').hide();
        button.show();
    	$('#contentWrapper').css({'height': '100%', 'overflow': 'visible'});
    	$('#footerWrapper').show();
    	$('#linkstrip').show();
    });
    $('#merchantLogin').click(function(){
    	$('#loginChooser').html('Loading..');
    	var username = $('#username').val(); 
		var password = $('#password').val();
		login(username, password, 'merchantuser');
    });
    $('#affiliateLogin').click(function(){
    	$('#loginChooser').html('Loading..');
    	var username = $('#username').val(); 
		var password = $('#password').val();
		login(username, password, 'affiliateuser');
    });
    
    $('#logout').click(function(){
    	$.ajax({
    		url: baseUrl + "/logout.html?callback=?",
    		dataType: "jsonp",
    		complete: function() {
    			box.hide();
    			$('#mask').hide();
    	        button.show();
    		}
    	});
    });
    
    /** search box ***/
    $('#searchButton').click(function(e) {
    	$('#searchButton').hide();        
    	//Cancel the link behavior
        e.preventDefault();

        box.hide();
        $('#searchBox').show();
        $('#searchinput').focus();
    });
    $('#searchClose').click(function(){
    	$('#searchBox').hide();
    	$('#mask').hide();
    	$('#searchButton').show();
    });
    $('#search').click(function() {
    	$('#searchForm').submit();
    });
});
function login(username, password, usertype){
	$('#loginForm').attr('action', baseUrl + '/loginform.html?action=login');
	$('#user_type').val(usertype);
	$('#loginForm').submit();
}

