var PostNewItem   = new Object();
//=====================================================//
//** extending the master Util class
var Util = $.extend(Util, {

});

//=====================================================//
var Validate = $.extend(Validate, {

});
//=====================================================//
var Member = {
    
    setupContactForm: function(e) {
        var extraPar = {
            callback: function() {
                var msg = "<div class='sysMessage'>Your profile has been updated.</div>";
                $('#contactForm').html(msg);
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#contactForm').ajaxForm(options);
    }

}

var Dialog = {
    setUpForm: function(formName) {
        $('#' + formName).livequery(function() {
            var extraPar = {
                callback: function(json) {
                    if (json.returnText != ''){
                        $('#dialog').dialog('close');
                        $('#dialog').dialog('destroy');
                        Util.showSimpleMessageInDialog(json.returnText);
                    }
                }
            }

            var options = {
                success: function(json, statusText, jqFormObj) {
                    Validate.validateFormData(json, statusText, jqFormObj, extraPar);
                    Util.hideProgressInd();
                },
                beforeSubmit: function(frmData) {
                    Util.showProgressInd();
                },
                dataType: 'json'
            };

            $('#' + formName).ajaxForm(options);

        });
    },

    openDialog: function(formName, dialogTitle) {
        url = $(this).attr('href');

        Util.showProgressInd();

        $.get(url, function(data){
            Util.initDialog();
            $('#dialog').html(data);

            var xButtons = {};

            xButtons.Submit = function() {
                $('#' + formName).submit();
            };

            xButtons.Cancel = function() {
                $(this).dialog('close');
                $(this).dialog('destroy');
            };

            var x_dialog = $('#dialog').dialog(
                $.extend(Util.dialogDefaults, {
                    width: 600,
                    height: 400,
                    title: dialogTitle,
                    buttons: xButtons
                })
            );
            Util.hideProgressInd();
        });
    }

}
//=====================================================//
var Media = {
    reloadMediaFileNamesDisplay: function(container) {
        var url = '/index.php';
        var room = $(container).attr('room');
        var record_type = $(container).attr('record_type');
        var key_field = $(container).attr('key_field');
        var id = $(container).attr('record_id');
        $.get(url, {
                    '_room'      :'media'
                    ,'_spAction' :'mediaFilesNamesWithDelete'
                    ,'room'      :room
                    ,'recordType':record_type
                    ,'keyField'  :key_field
                    ,'id'        :id
                    ,'showHTML'  :'0'
            }, function(data){
                $(container).slideUp();
                $(container).html(data);
                $(container).slideDown();
                Util.hideProgressInd();
            }
        );
    },

    setUploadify: function(mediaType, room, record_type, key_field, id, sessionID, extraParamObj){
        if (mediaType == 'picture'){
            queueID = 'fileQueuePic';
            mainDiv = $('#pictures');
            uploadifyID = 'uploadifyPic';
        } else if (mediaType == 'video') {
            queueID = 'fileQueueVid';
            mainDiv = $('#video');
            uploadifyID = 'uploadifyVid';
        } else if (mediaType == 'relatedPicture') {
            queueID = 'fileQueueRelatedPic';
            mainDiv = $('#relatedPictures');
            uploadifyID = 'uploadifyRelatedPic';
        }

        var onAllCompleteFunction = null;
        var allowMultiUpload = true;

        if (extraParamObj) {
            if (extraParamObj.onAllCompleteFunction) {
                onAllCompleteFunction = extraParamObj.onAllCompleteFunction;
            }

            if (extraParamObj.allowMultiUpload) {
                allowMultiUpload = extraParamObj.allowMultiUpload;
            }
        }
        $('#' + uploadifyID).uploadify({
              'uploader'       : '/jss/jquery/uploadify/uploadify.swf'
            , 'buttonImg'      : '/images/icons/btn_browse.png'
            , 'script'         : '/index.php'
            , 'fileDataName'   : 'fileName'
            , 'cancelImg'      : '/jss/jquery/uploadify/cancel.png'
            , 'queueID'        : queueID
            , 'multi'          : allowMultiUpload
            , 'height'         : 20
            , 'width'          : 50
            , 'auto'           : true
            , 'scriptData'     : {
                                   '_room'       :'media'
                                  ,'_spAction'   :'addMedia'
                                  ,'room'        :room
                                  ,'recordType'  :record_type
                                  ,'keyField'    :key_field
                                  ,'id'          :id
                                  ,'showHTML'    :'0'
                                  ,'sessionIDCP' :sessionID
                                  ,'successText' :'success'
                                 }
            , 'onSelect'         : function(e, queueID, fileObj){
            }
            , 'onOpen'          : function(){
                Util.showProgressInd();
            }
            , 'onComplete'  : function(e, queueID, fileObj, response){
            }
            , 'onAllComplete'   : function(e, data){
                if(onAllCompleteFunction) {
                    onAllCompleteFunction.call();
                } else if ($('.mediaFilesDisplayWrap', mainDiv).length != 0){
                    Media.reloadMediaFileNamesDisplay($('.mediaFilesDisplayWrap', mainDiv));
                }
            }

        });

        $(function() {
            $('td.del a').livequery('click', function(e){
                e.preventDefault();
                var media_id = $(this).attr('id');
                var link = $(this);
                var url = '/index.php?_room=media&_spAction=deleteMedia&media_id=' + media_id + '&showHTML=0';
        
                Util.showProgressInd();
        
                $.get(url, function(){
                    var container = $(link).closest('.mediaFilesDisplayWrap');
                    Media.reloadMediaFileNamesDisplay(container);
                });
            });

            $('a.uploadQueue').livequery('click', function(e){
                e.preventDefault();
                var fileID = $('input[type=file]', $(this).closest('.uploadWrap')).attr('id');

                $('#' + fileID).uploadifyUpload();
            });
        
            $('a.clearQueue').livequery('click', function(e){
                e.preventDefault();
                var fileID = $('input[type=file]', $(this).parent()).attr('id');
                $('#' + fileID).uploadifyClearQueue();
            });        
        });
    }
}

