﻿(function ($) {

    $.fn.multipleUploader = function (options) {

        var defaults = {}; // set defaults in MultipleUploader.ascx.vb
        var config = $.extend(defaults, options);
        this.each(function () {

            var self = this, $self = $(this);
            var newIds = [];
            $("div.TabPanel").watch("display,visibility", configButton);

            var folderKeyValue = config.folderKeyValue || 0;

            // PUBLIC FUNCTIONS

            self.setFolderKeyValue = function (id) {
                if (!isNaN(id)) folderKeyValue = id;
            }

            initialize();
            configButton();

            // PRIVATE FUNCTIONS

            function initialize() {
                var queueID = $("div.FileQueue", self)[0].id;
                var $error = $("label.error", self);
                var haveErrors = false;
                $("input.Uploadify", self).uploadify({
                    uploader: '/Standard/Core/Javascript/uploadify.swf',
                    script: $r.cleanHref(window.location), // POST to same page
                    scriptData: getUploadifyScriptData(),
                    cancelImg: '/Standard/Core/Images/Dingbats/x.red.15.gif',
                    queueID: queueID,
                    buttonImg: " ",
                    wmode: "transparent",
                    fileDesc: config.fileDesc,
                    fileExt: config.fileExt,
                    auto: true,
                    multi: true,
                    'onInit': function () {
                    },
                    'onSelect': function (event, queueID, fileObj) {
                        $("div.FileQueue", self).show();
                        $("input.Uploadify", self).uploadifySettings("scriptData", getUploadifyScriptData());
                        $error.html('').hide();
                        haveErrors = false;
                    },
                    'onComplete': function (event, queueID, fileObj, response, data) {
                        var newId = parseInt(response);
                        if (newId != response) {
                            // Error
                            $error.html($error.html() + response + '<br>').show();
                        } else {
                            newIds.push(newId);
                        };
                        $self.trigger('complete', [{ newId: newId}]);
                    },
                    'onAllComplete': function (event, data) {
                        $("div.FileQueue", self).hide();
                        if (haveErrors == true) {
                        } else {
                            $(config.databindSelector).trigger("dataBind"); //, [newIds]
                        };
                    },
                    'onError': function (event, queueID, fileObj, errorObj) {
                    }
                });
                configButton();
            } // initialize

            function configButton() {
                // Position and wire up hover effects on fake button 
                var $buttonContainer = $("div.CssButtonContainer", self).show();
                var $objectWrapper = $("div.UploadifyObjectWrapper", self);
                var $object = $(".object", self);
                var $fakeButton = $("a.JqButton", self);
                var width = $fakeButton.outerWidth();
                var height = $fakeButton.outerHeight();
                $object.attr("width", width).attr("height", height);
                $buttonContainer.css("width", width + "px").css("height", height + "px")
                $objectWrapper.css("width", width + "px").css("height", height + "px")
            };

            function getUploadifyScriptData() {
                return {
                    sessionID: config.sessionId,
                    authID: config.authId,
                    multipleUploaderId: self.id,
                    action: 'upload',
                    folderKeyValue: folderKeyValue,
                    folderFieldName: config.folderFieldName
                };
            }

        }); // each

        //return this; // don't break the chain

    }; // $.fn.multipleUploader 

})(jQuery);



