var PostNewItem = new Object();
//=====================================================//
//** extending the master Util class
var Util = $.extend(Util, {
    setUpForm: function(formName){
        $(function() {
            var extraPar = {
                callback: function(json) {
                    if (json.returnText != ''){
                        Util.showSimpleMessageInDialog(json.returnText);
                    }
                }
            }

            var options = {
                success: function(json, statusText, jqFormObj, extraPar) {
                    Validate.validateFormData(json, statusText, jqFormObj, extraPar);
                    Util.hideProgressInd();
                },
                beforeSubmit: function(frmData) {
                    Util.showProgressInd();
                },
                dataType: 'json'

            };
            $('#' + formName).ajaxForm(options);
        });
    },
    
    showSimpleMessageInDialog: function(msg) {

        Util.initDialog();
        $('#dialog').html(msg);
        
        var xButtons = {};
        
        xButtons.close = function() {
            $(this).dialog('close');
            $(this).dialog('destroy');
        };
        
        var x_dialog = $('#dialog').dialog(
            $.extend(Util.dialogDefaults, {
                buttons: xButtons
            })
        );
    },
    
    showNoFlashMessage: function() {
        var playerVersion = swfobject.getFlashPlayerVersion();
        var version = playerVersion.major;
        if (version == 0) {
            $('#noFlashInfo').show();
        }

    }

});

//=====================================================//
var Content = {
    getDescription: function(e) {
        e.preventDefault();

        var url       = $(this).attr('href');
        var record_id = $(this).attr('rec_id');

        $.get(url, function(data){
            $('#fatList').html(data);
        });
        
    }

};

//=====================================================//
var Comments = {
    showCommentsForm: function() {
        var url       = $(this).attr('href');
        var record_id = $(this).attr('rec_id');

        $.get(url, function(data){
            Util.initDialog();
            $('#dialog').html(data);

            var xButtons = {};

            xButtons['Submit'] = function() {
                Util.showProgressInd();
                $('#frmComment').submit();
            };

            xButtons['Cancel'] = function() {
                $(this).dialog('close');
                $(this).dialog('destroy');
            };

            var x_dialog = $('#dialog').dialog(
                $.extend(Util.dialogDefaults, {
                    title: 'Please add your comments...',
                    height: 550,
                    width: 650,
                    buttons: xButtons
                })
            );

            var extraPar = {
                callback: function(json){
                    $('#dialog').dialog('close');
                    $('#dialog').dialog('destroy');
                    Comments.reloadComments(record_id);
                }
            }

            var options = {
                success: function(json, statusText, jqFormObj) {
                    Validate.validateFormData(json, statusText, jqFormObj, extraPar);
                    Util.hideProgressInd();
                },
                beforeSubmit: function() {
                },
                dataType: 'json'
            };
            $('#frmComment').ajaxForm(options);
            Util.hideProgressInd();
        });
    },

    reloadComments: function(rec_id) {
        Util.showProgressInd();
        var url    = '/index.php?_room=comment&_spAction=list&record_id=' + rec_id + '&showHTML=0';
        var link   = $('#' + rec_id);

        $.get(url, function(data){
            var parent = $(link).closest('.commentHltBox');
            var commentsBox = $('.commentsList', parent);
            var countSpan = $('span.count', parent);
            var count = parseInt(countSpan.text());
            $(countSpan).html(count + 1);
            $(commentsBox).html(data);
            $(commentsBox).hide();
            $(commentsBox).slideDown();
            $(link).addClass('ic-arr-down').removeClass('ic-arr-up');
            Util.hideProgressInd();
        });
    }
    

}

//=====================================================//
var Job = {
    emailToFriendForm: function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        if (url == '' || url == 'javascript:void(0)'){
            url = $(this).attr('link');
        }

        Util.showProgressInd();

        var xButtons = {};

        xButtons['Send'] = function() {
            $('#emailToFriendForm').submit();
        };

        xButtons['Cancel'] = function() {
            $(this).dialog('close').dialog('destroy');
        };

        $.get(url, function(data){
            Util.initDialog();
            $('#dialog').dialog(
                $.extend(Util.dialogDefaults, {
                    width: 520,
                    height: 550,
                    title: 'Email A friend',
                    buttons: xButtons
                })
            );
            $('#dialog').html(data);
            Job.setupEmailToFriendForm();
            Util.hideProgressInd();
        });
    },

    setupEmailToFriendForm: function(e) {
        var extraPar = {
            callback: function() {
                Util.hideProgressInd();
                var msg = "We have forwarded this page to your friend.";
                Util.alert(msg, function() {
                    $('#dialog').dialog('destroy');
                    $('#dialog').dialog('close');
                })
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
                Util.hideProgressInd();
            },
            beforeSubmit: function() {
                Util.showProgressInd();
            },
            dataType: 'json'
        };
        $('#emailToFriendForm').ajaxForm(options);
    },



    applyJob: function(id) {
        Util.showProgressInd('Applying for job. Please wait...');
        var verifyUrl = '/index.php?_room=job&_spAction=applyJobEligibility&showHTML=0&job_id=' + id;
        $.getJSON(verifyUrl, function(json) {
            Util.alertWindowHeight = 250;
            if (json.status == 'error') {
                Util.hideProgressInd();
                Util.alert(json.errorMsg);
                return;
            } else {
                Util.hideProgressInd();
                Util.alert(json.html);
                return;
            }
        });
    }
}

//=====================================================//
var Member = {
    loginForm: function(e) {
        e.preventDefault();
        var url = "/index.php?_room=member&_spAction=loginForm&showHTML=0";

        Util.showProgressInd();

        var xButtons = {};

        xButtons['Submit'] = function() {
            $('#loginForm').submit();
        };

        xButtons['Forgot Password'] = function() {
            Member.forgotPasswordForm();
        };

        xButtons['Cancel'] = function() {
            $(this).dialog('close').dialog('destroy');
        };

        $.get(url, function(data){
            Util.initDialog();
            $('#dialog').dialog(
                $.extend(Util.dialogDefaults, {
                    width: 350,
                    height: 300,
                    title: 'Candidate Sign In',
                    buttons: xButtons
                })
            );
            $('#dialog').html(data);
            Util.hideProgressInd();
        });
    },
    
    forgotPasswordForm: function() {
        var url = "/index.php?_room=member&_spAction=forgotPasswordForm&showHTML=0";
        
        Util.showProgressInd();
        var xButtons = {};

        xButtons['Submit'] = function() {
            $('#frmForgotPassword').submit();
        };

        xButtons['Cancel'] = function() {
            $(this).dialog('close').dialog('destroy');
        };

        $.get(url, function(data){
            Util.initDialog();
            $('#dialog').dialog(
                $.extend(Util.dialogDefaults, {
                    title:"Password Retrival Form",
                    width: 400,
                    height: 230,
                    buttons: xButtons
                })
            );
            $('#dialog').html(data);
            Util.hideProgressInd();
        });
    },

    requestPassword: function(e) {
        e.preventDefault();

        if (!confirm('Are you sure to request your password?')) {
            return;
        }
        
        var url = "/index.php?_room=member&_spAction=requestPassword&showHTML=0";

        Util.showProgressInd();

        var values = $("#registerForm").serialize();
        $.post(url, values, function(json){
            if (json.status == 'error') {
                alert(json.message);
                Util.hideProgressInd();
                return;
            }
            if (json.status == 'success') {
                document.location = json.returnUrl;
            }
        }, 'json');
    }

}

var Dialog = {

    setUpForm: function(formName) {
        $('#' + formName).livequery(function() {

            /****************************************************/
            var extraPar = {
                callback: function(json) {
                    if (json.returnText != ''){
                        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: 450,
                    height: 210,
                    title: dialogTitle,
                    buttons: xButtons
                })
            );
            Util.hideProgressInd();
        });
    }

}


//=====================================================//
var Media = {
    reloadMediaFileNamesDisplay: function(container) {
        var url  = '/index.php';
        var record_type  = $(container).attr('record_type');
        var contact_id   = $('#contact_id').attr('value');
        $.get(url, {
                    '_room'     :'media'
                    ,'_spAction' :'mediaFilesNamesWithDelete'
                    ,'room'      :'contact'
                    ,'recordType': record_type
                    ,'keyField'  :'contact_id'
                    ,'id'        : contact_id
                    ,'showHTML'  :'0'
            }, function(data){
                $(container).slideUp();
                $(container).html(data);
                $(container).slideDown();
                Util.hideProgressInd();
            }
        );
    },

    setUploadify: function(contact_id, sessionID) {
    	$('#uploadifyAtt').uploadify({
    		  'uploader'       : '/jss/jquery/uploadify/uploadify.swf'
    		, 'script'         : '/index.php'
    		, 'fileDataName'   : 'fileName'
    		, 'cancelImg'      : '/jss/jquery/uploadify/cancel.png'
    		, 'queueID'        : 'fileQueueAtt'
    		, 'multi'          : true
    		, 'buttonImg'      : '/images/btn_browse.png'
            , 'width'          : '47' 
            , 'height'         : '18'
            , 'auto'           : true
    		, 'scriptData'     : {
    		                       '_room'       :'media'
    		                      ,'_spAction'   :'addMedia'
    		                      ,'room'        :'contact'
    		                      ,'recordType'  : 'cv'
    		                      ,'keyField'    :'contact_id'
    		                      ,'id'          : contact_id
    		                      ,'showHTML'    :'0'
    		                      ,'sessionIDCP' :sessionID
    		                      ,'successText' :'success'
    		                     }
    		, 'onOpen'          : function(){
                Util.showProgressInd();
    		}
    		, 'onComplete': function(e, queueID, fileObj, response){
    		}
    		, 'onAllComplete': function(e, data){
                Media.reloadMediaFileNamesDisplay($('#attachments .mediaFilesDisplayWrap'));
    		}
    	});
    },

    setUploadifyForRegister: function(sessionID) {
    	$('#cvAttachment').uploadify({
    		  'uploader'       : '/jss/jquery/uploadify/uploadify.swf'
    		, 'script'         : '/index.php'
    		, 'fileDataName'   : 'cvAttachment'
    		, 'cancelImg'      : '/jss/jquery/uploadify/cancel.png'
    		, 'queueID'        : 'fileUploadProgress'
    		, 'multi'          : false
    		, 'buttonImg'      : '/images/btn_browse.png'
            , 'width'          : '47'
            , 'height'         : '18'
            , 'auto'           : true
    		, 'scriptData'     : {
    		                       '_room'       :'media'
    		                      ,'_spAction'   :'uploadCVRegistrationTemp'
    		                      ,'recordType'  : 'cv'
    		                      ,'showHTML'    :'0'
    		                      ,'sessionIDCP' :sessionID
    		                      ,'successText' :'success'
    		                     }
    		, 'onSelect': function() {
                Util.showProgressInd();
                $('#registerForm #fileUploadProgress').show();
    		}
    		, 'onComplete': function(e, queueID, fileObj, response){
                $('#registerForm #fileUploadProgress').hide();
                $('#registerForm #fileNameDiv')
                .html(response)
                .show();
                Util.hideProgressInd();
    		}
    	});
    },

    setUploadifyForQuickSubmit: function(sessionID) {
    	$('#fld_fileName').uploadify({
    		  'uploader'       : '/jss/jquery/uploadify/uploadify.swf'
    		, 'script'         : '/index.php'
    		, 'fileDataName'   : 'fld_fileName'
    		, 'cancelImg'      : '/jss/jquery/uploadify/cancel.png'
    		, 'queueID'        : 'fileUploadProgress'
    		, 'multi'          : false
    		, 'buttonImg'      : '/images/btn_browse.png'
            , 'width'          : '47'
            , 'height'         : '18'
            , 'auto'           : true
    		, 'scriptData'     : {
    		                       '_room'       :'media'
    		                      ,'_spAction'   :'uploadCVQuickSubmitTemp'
    		                      ,'recordType'  : 'cv'
    		                      ,'showHTML'    :'0'
    		                      ,'sessionIDCP' :sessionID
    		                      ,'successText' :'success'
    		                     }
    		, 'onSelect': function() {
                Util.showProgressInd();
                $('#quickSubmitForm #fileUploadProgress').show();
    		}
    		, 'onComplete': function(e, queueID, fileObj, response) {
                $('#quickSubmitForm #fileUploadProgress').hide();
                $('#quickSubmitForm #fileNameDiv')
                .html(response)
                .show();
                Util.hideProgressInd();
    		}
    	});
    }

}


