var howis = new function()
{
    this.accountCreateViaTwitterLogin = function(accessToken, callBack)
    {
        $.post('/ajax/accountCreateViaTwitterLogin', {'accessToken':accessToken}, function(data)
        {
	    if(data && data.length > 0)
	    {
		_gaq.push(['_trackEvent', 'accountType', 'create', 'twitter', data[0].newAccount]);
		if(httpReferer) _gaq.push(['_trackEvent', 'accountReferer', 'create', httpReferer]);
	    }

	    if(callBack)
		callBack(data);
        }, 'json');
    }

    this.accountCreateViaFacebookLogin = function(userId, accessToken, callBack, signedRequest)
    {
        $.post('/ajax/accountCreateViaFacebookLogin', {'userId':userId, 'accessToken':accessToken, 'signedRequest':signedRequest}, function(data)
        {
	    if(!data)
		return;

	    if(data && data.length > 0)
	    {
		_gaq.push(['_trackEvent', 'accountType', 'create', 'facebook', data[0].newAccount]);
		if(httpReferer) _gaq.push(['_trackEvent', 'accountReferer', 'create', httpReferer]);
	    }

	    if(data.twitterAccessToken) twitterAccessToken = data.twitterAccessToken;
	    if(data.twitterUserId) twitterUserId = data.twitterUserId;
	    if(data.twitterUsername) twitterUsername = data.twitterUsername;

	    if(callBack)
		callBack();
        }, 'json');
    }

    this.emotionCreateViaTwitterLogin = function(userId, accessToken, basic, reason, imagePath, postToFaceBook, postToTwitter, callBack)
    {
        $.post('/ajax/emotionCreateViaTwitterLogin', {'userId':userId, 'accessToken':accessToken, 'basic':basic, 'reason':reason, 'imagePath':imagePath, 'postToTwitter':postToTwitter, 'postToFaceBook':postToFaceBook}, function(data)
        {
	    if(data && data.length > 0)
	    {
		_gaq.push(['_trackEvent', 'emotionType', 'create', 'twitter']);
		if(httpReferer) _gaq.push(['_trackEvent', 'emotionReferer', 'create', httpReferer]);
	    }

	    if(callBack)
		callBack(data);
        }, 'json');
    }

    this.emotionCreateViaFacebookLogin = function(userId, accessToken, basic, reason, imagePath, postToFaceBook, postToTwitter, callBack)
    {
        $.post('/ajax/emotionCreateViaFacebookLogin', {'userId':userId, 'accessToken':accessToken, 'basic':basic, 'reason':reason, 'imagePath':imagePath, 'postToTwitter':postToTwitter, 'postToFaceBook':postToFaceBook}, function(data)
        {
	    if(data && data.length > 0)
	    {
		_gaq.push(['_trackEvent', 'emotionType', 'create', 'facebook']);
		if(httpReferer) _gaq.push(['_trackEvent', 'emotionReferer', 'create', httpReferer]);
	    }

	    if(callBack)
		callBack(data);
        }, 'json');
    }

    this.emotionCommentSearchViaTwitterLogin = function(userId, accessToken, username, _id, fbPostId, callBack)
    {
	var c = getDistributedHost();

	if(!fbPostId)
	    fbPostId = '';

        $.ajax(
        {
                url: 'http://'+c+'.how.is/ajax/emotionCommentSearchViaTwitterLogin/'+username+'/'+userId+'/'+encodeURIComponent(encodeURIComponent(accessToken))+'/'+_id+'/'+fbPostId,
                dataType: "jsonp",
                timeout: 30000,
                data:
                {
                },
                success: function( data )
                {
	    	    data.fbPostId = fbPostId;
	    	    data._id = _id;

	    	    if(callBack)
			callBack(data);
                },
             	error: function(jqXHR, textStatus, errorThrown)
                {
	    	    if(callBack)
			callBack(null);
         	}
        });
    }

    this.emotionCommentSearchViaFacebookLogin = function(userId, accessToken, username, _id, fbPostId, callBack)
    {
	var c = getDistributedHost();

	if(!fbPostId)
	    fbPostId = '';

        $.ajax(
        {
                url: 'http://'+c+'.how.is/ajax/emotionCommentSearchViaFacebookLogin/'+username+'/'+userId+'/'+accessToken+'/'+_id+'/'+fbPostId,
                dataType: "jsonp",
                timeout: 30000,
                data:
                {
                },
                success: function( data )
                {
	    	    data.fbPostId = fbPostId;
	    	    data._id = _id;

	    	    if(callBack)
			callBack(data);
                },
             	error: function(jqXHR, textStatus, errorThrown)
                {
	    	    if(callBack)
			callBack(null);
         	}
        });
    }

    this.emotionSearchViaTwitterLogin = function(userId, accessToken, username, basic, reason, dbUsername, callBack)
    {
        $.post('/ajax/emotionSearchViaTwitterLogin/'+username, {'userId':userId, 'accessToken':accessToken, 'basic':basic, 'reason':reason, 'dbUsername':dbUsername}, function(data)
        {
	    data.friendId = username;
	    if(callBack)
		callBack(data);
        }, 'json');
    }

    this.emotionSearchViaFacebookLogin = function(userId, accessToken, username, basic, reason, callBack)
    {
        $.post('/ajax/emotionSearchViaFacebookLogin/'+username, {'userId':userId, 'accessToken':accessToken, 'basic':basic, 'reason':reason}, function(data)
        {
	    data.friendId = username;
	    if(callBack)
		callBack(data);
        }, 'json');
    }

    this.getActiveFriendsViaTwitterLogin = function(userID, accessToken, callBack)
    {
        $.post('/ajax/getActiveFriendsViaTwitterLogin', {'userId':userID, 'accessToken':accessToken}, function(data)
        {
	    if(callBack)
		callBack(data);
        }, 'json');
    }

    this.getActiveFriendsViaFacebookLogin = function(userID, accessToken, callBack)
    {
        $.post('/ajax/getActiveFriendsViaFacebookLogin', {'userId':userID, 'accessToken':accessToken}, function(data)
        {
	    if(callBack)
		callBack(data);
        }, 'json');
    }

    this.emotionCommentViaTwitterLogin = function(userId, accessToken, _id, comment, dbUsername, callBack)
    {
        $.post('/ajax/emotionCommentViaTwitterLogin', {'userId':userId, 'accessToken':accessToken, '_id':_id, 'comment':comment, 'dbUsername':dbUsername}, function(data)
        {
	    if(data && data.length > 0)
	    {
		_gaq.push(['_trackEvent', 'emotionType', 'comment', 'twitter']);
		if(httpReferer) _gaq.push(['_trackEvent', 'emotionReferer', 'comment', httpReferer]);
	    }

	    if(callBack)
		callBack(_id, 0, data);
        }, 'json');
    }

    this.emotionCommentViaFacebookLogin = function(userId, accessToken, _id, comment, fbpostid, callBack)
    {
        $.post('/ajax/emotionCommentViaFacebookLogin', {'userId':userId, 'accessToken':accessToken, '_id':_id, 'comment':comment, 'fbpostid':fbpostid}, function(data)
        {
	    if(data && data.length > 0)
	    {
		_gaq.push(['_trackEvent', 'emotionType', 'comment', 'facebook']);
		if(httpReferer) _gaq.push(['_trackEvent', 'emotionReferer', 'comment', httpReferer]);
	    }

	    if(callBack)
		callBack(_id, fbpostid, data);
        }, 'json');
    }

    this.getSettingsViaFacebookLogin = function(accessToken, username)
    {
	if(!username) username = '';
        $.post('/ajax/getSettingsViaFacebookLogin/'+accessToken+'/'+username, {}, function(data)
        {
	    var css = '';
	    if(data)
	    {
                css = data['css'];
		if(!isMobile) applyCssObject(css);
	    }
        }, 'json');
    }

    this.getSettingsViaLogin = function(type, accessToken, username)
    {
        $.post('/ajax/getSettingsViaLogin', {type: type, accessToken: accessToken, username: username}, function(data)
        {
	    var css = '';
	    if(data)
	    {
                css = data['css'];
		if(!isMobile) applyCssObject(css);
	    }
        }, 'json');
    }

    this.getUserSettings = function(username)
    {
        $.post('/ajax/getUserSettings/'+username, {}, function(data)
        {
	    var css = '';
	    if(data)
	    {
                css = data['css'];
		if(!isMobile) applyCssObject(css);
	    }
        }, 'json');
    }

    this.accountSearch = function(username, facebookUserId, twitterUserId, callBack)
    {
	var postObject = {};

	if(username)
	    postObject.username = username;
	else if(facebookUserId)
	    postObject.facebookUserId = facebookUserId;
	else if(twitterUserId)
	    postObject.twitterUserId = twitterUserId;

        $.post('/ajax/accountSearch', postObject, function(data)
        {
	    if(callBack)
		callBack(data);
        }, 'json');
    }

    this.userMetaViaFacebookLogin = function(userId, accessToken, username, callBack)
    {
	var c = getDistributedHost();

        $.ajax(
        {
                url: 'http://'+c+'.how.is/ajax/userMetaViaFacebookLogin/'+username+'/'+userId+'/'+accessToken,
                dataType: "jsonp",
                timeout: 30000,
                data:
                {
                },
                success: function( data )
                {
	    	    if(callBack)
			callBack(data);
                },
             	error: function(jqXHR, textStatus, errorThrown)
                {
	    	    if(callBack)
			callBack(null);
         	}
        });
    }

    this.emotionDeleteViaLogin = function(type, userId, accessToken, _id, callBack)
    {
        $.post('/ajax/emotionDeleteViaLogin', {'type':type, 'userId':userId, 'accessToken':accessToken, '_id':_id}, function(data)
        {
	    if(data && data.length > 0)
	    {
		_gaq.push(['_trackEvent', 'emotionType', 'delete', type]);
		if(httpReferer) _gaq.push(['_trackEvent', 'emotionReferer', 'delete', httpReferer]);
	    }

	    if(callBack)
		callBack(data);
        }, 'json');
    }

    this.hello = function(firstName, lastName, title)
    {
        $.post('/ajax/hello/'+firstName+'/'+lastName+'/'+title, {}, function(data)
        {
	    alert(JSON.stringify(data));
        }, 'json');
    }
}

