Studs = function() {
    return {
        Postman: {},
        Functions: {},
        MessageManager: {},
        UIManager: {},
        Settings: {},
        debugA: {},
        debugB: {}
    };
} ();   

Studs.UIManager = function() {
	return {
	}
} ();

Studs.MessageManager = function() {
	return {
		SendMessage: function(senderName, senderEmail, message) {
			Studs.Postman.DoRequest('', {senderName: senderName, senderEmail: senderEmail, message: message});
		},
		MessageSent: function() {
			$('#dialog').dialog('close');
			$('#dialog-sent').dialog('open');
		}
	}
} ();


Studs.Postman = function() {
    
    return {
    Actions: {
		SendMessage : 11
    },

        ResponseHandler: function(response) {
            if(response.success) {
                try {
                    switch(response.action) {
						case Studs.Postman.Actions.SendMessage:
							Studs.MessageManager.MessageSent();
							break;
                        default:
                            break;
                    }
                } catch(e) {
                    alert(e);
                }
            }
        },
        
        DoRequest: function(page, params) {
            $.ajax({
                type: 'POST',
                url: Studs.Settings.SiteRoot + '/' + page,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                data: $.toJSON(params),
                success: function(response) {
                    Studs.Postman.ResponseHandler(response);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    Studs.debugA = textStatus;
                    Studs.debugB = errorThrown;
                    alert(Studs.Settings.GeneralError + errorThrown );
                }
            })
        }
    };
} ();

Studs.Settings = function() {
	return {
		GeneralError: "Fel: ",
		//SiteRoot: "http://localhost:8082"
		SiteRoot: "http://studsmedia.appspot.com"
	}
} ();

Studs.Functions = function() {
	return {
		GetToken: function() {
			return Studs.Functions.GetQuerystring('token');
		},
		GetQuerystring: function(key, default_) {
		  if (default_==null) default_='';
		  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
		  var qs = regex.exec(window.location.href);
		  if(qs == null)
		    return default_;
		  else
		    return qs[1];
		}
	}
} ();
