
(function(){
    var FS = window.FS || {};
    
    FS.PhotoAlbum = FS.PhotoAlbum || {};
    
    undefined;
    
    //YUI shortcuts
    var Connect = YAHOO.util.Connect;
    
    FS.PhotoAlbum.Request = function(args) {
        
        //arguments that can be overriden
        if (args !== undefined) {
            
            if (args.getUrl !== undefined) {
                this.getUrl = args.getUrl;
            }
            if (args.getMethod !== undefined) {
                this.getMethod = args.getMethod;
            }
            if (args.getTimeout !== undefined) {
                this.getTimeout = args.getTimeout;
            }
            if (args.saveUrl !== undefined) {
                this.saveUrl = args.saveUrl;
            }
            if (args.saveMethod !== undefined) {
                this.saveMethod = args.saveMethod;
            }
            if (args.saveTimeout !== undefined) {
                this.saveTimeout = args.saveTimeout;
            }
            if (args.sendUrl !== undefined) {
                this.sendUrl = args.sendUrl;
            }
            if (args.sendMethod !== undefined) {
                this.sendMethod = args.sendMethod;
            }
            if (args.sendTimeout !== undefined) {
                this.sendTimeout = args.sendTimeout;
            }            
        }
        
        this.onGetSuccess = new YAHOO.util.CustomEvent('onGetSuccess');
        this.onGetStart = new YAHOO.util.CustomEvent('onGetStart');
        this.onGetFailure = new YAHOO.util.CustomEvent('onGetFailure');
        this.onSaveSuccess = new YAHOO.util.CustomEvent('onSaveSuccess');
        this.onSaveStart = new YAHOO.util.CustomEvent('onSaveStart');
        this.onSaveFailure = new YAHOO.util.CustomEvent('onSaveFailure');
        this.onSendSuccess = new YAHOO.util.CustomEvent('onSendSuccess');
        this.onSendStart = new YAHOO.util.CustomEvent('onSendStart');
        this.onSendFailure = new YAHOO.util.CustomEvent('onSendFailure');
    };

    FS.PhotoAlbum.Request.prototype = {
            getUrl:   '/editalbum.php',
            saveUrl:   '/editalbum.php',
            sendUrl:   '/editalbum.php',
            getMethod: 'POST',
            saveMethod: 'POST',
            sendMethod: 'POST',
            getTimeout: 30000, //30 seconds
            saveTimeout: 30000, //30 seconds
            sendTimeout: 30000, //30 seconds
            onGetStart: '',
            onGetSuccess: '',
            onGetFailure: '',
            onSaveStart: '',
            onSaveSuccess: '',
            onSaveFailure: '',
            onSendStart: '',
            onSendSuccess: '',
            onSendFailure: '',
            
            send: function(postObj) {
                var callback = {
                        customevents: {
                            onSuccess: this.asyncHandler.success,
                            onFailure: this.asyncHandler.failure,
                            onStart: this.asyncHandler.start              
                        },
                        timeout: this.sendTimeout,
                        argument: 'send',
                        scope: this           
                };        
                var postData = '';
                for (var key in postObj) {
                    //prevent looking at inherited properties from other objects
                    if (YAHOO.lang.hasOwnProperty(postObj, key)) {
                        postData += '&' + key + '=' + postObj[key] + ''; 
                    }
                }
                
                //return the object reference of the ajax call 
                return Connect.asyncRequest(this.sendMethod, this.sendUrl, callback, ((postObj !== undefined) ? postData : ''));        
            },
            get: function(postObj) {
                var callback = {
                        customevents: {
                            onSuccess: this.asyncHandler.success,
                            onFailure: this.asyncHandler.failure,
                            onStart: this.asyncHandler.start              
                        },
                        timeout: this.getTimeout,
                        argument: 'get',
                        scope: this           
                };        
                var postData = '';
                for (var key in postObj) {
                    //prevent looking at inherited properties from other objects
                    if (YAHOO.lang.hasOwnProperty(postObj, key)) {
                        postData += '&' + key + '=' + postObj[key] + ''; 
                    }
                }
                
                //return the object reference of the ajax call 
                return Connect.asyncRequest(this.getMethod, this.getUrl, callback, ((postObj !== undefined) ? postData : ''));        
            },
            
            save: function(postObj) {
                var callback = {
                        customevents: {
                            onSuccess: this.asyncHandler.success,
                            onFailure: this.asyncHandler.failure,
                            onStart: this.asyncHandler.start              
                        },
                        timeout: this.saveTimeout,
                        argument: 'save',
                        scope: this           
                };        
                var postData = '';
                for (var key in postObj) {
                    //prevent looking at inherited properties from other objects
                    if (YAHOO.lang.hasOwnProperty(postObj, key)) {
                        postData += '&' + key + '=' + postObj[key] + ''; 
                    }
                }
                
                //return the object reference of the ajax call 
                return Connect.asyncRequest(this.saveMethod, this.saveUrl, callback, ((postObj !== undefined) ? postData : ''));        
            },
            
            asyncHandler: {
                success: function(eventType, args) {
                    
                    var callType = args[0].argument;
                    
                    if (callType === 'get') {
                        this.onGetSuccess.fire({
                            eventType: eventType, 
                            args: args
                        }); 
                    } else if (callType === 'save') {
                        this.onSaveSuccess.fire({
                            eventType: eventType, 
                            args: args
                        }); 
                    } else if (callType === 'send') {
                        this.onSendSuccess.fire({
                            eventType: eventType, 
                            args: args
                        }); 
                    }
                },
                start: function(eventType, args) {
                    
                    var callType = args[1];
                    
                    if (callType === 'get') {
                        this.onGetStart.fire({
                            eventType: eventType, 
                            args: args
                        }); 
                    } else if (callType === 'save') {
                        this.onSaveStart.fire({
                            eventType: eventType, 
                            args: args
                        }); 
                    } else if (callType === 'send') {
                        this.onSendStart.fire({
                            eventType: eventType, 
                            args: args
                        }); 
                    }
                },
                failure: function(eventType, args) {
                    
                    var callType = args[0].argument;
                    
                    if (callType === 'get') {
                        this.onGetFailure.fire({
                            eventType: eventType, 
                            args: args
                        }); 
                    } else if (callType === 'save') {
                        this.onSaveFailure.fire({
                            eventType: eventType, 
                            args: args
                        }); 
                    } else if (callType === 'send') {
                        this.onSendFailure.fire({
                            eventType: eventType, 
                            args: args
                        }); 
                    }
                }//end of failure method
            }//end of handler object
    };//end of prototype definition
})();