﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery,
cc_resx_TR_Are_you_sure_mark_as_read, cc_resx_TR_Reply_Successfully_Sent, cc_resx_TR_Are_you_sure_delete_mails, 
cc_resx_TR_Are_you_sure_delete_a_mail, cc_resx_TR_Select_All, cc_resx_TR_Select_None, cc_resx_TR_deleting,
cc_resx_TR_An_error_occurred_deleting_mails, cc_resx_TR_Delete, cc_resx_TR_An_error_occurred_processing_mails
*/

//  
//  Author: Kevin Burkitt
//  Date:   2010-05-08
//  
//  Mail Page Helper Functions
//
cc.mail = function() {

    return {
        actionInProgress: false,

        init: function(container) {

            jQuery('#cc-mail-checkall', container).unbind('change').change(function() {
                if (jQuery(this).attr('checked')) {
                    jQuery('.cc-mark-mail').attr('checked', true);
                } else {
                    jQuery('.cc-mark-mail').attr('checked', false);
                }

                cc.mail.fixButtons();
            });

            jQuery('.cc-mark-mail', container).unbind('change').change(function() {
                cc.mail.fixButtons();
            });


            jQuery('#cc-mails-read', container).unbind('click').click(function() {
                var threadId = jQuery('.cc-mail-thread-id', jQuery(this).parents('li:first')).val();
                cc.ui.showModalConfirm(cc_resx_TR_Are_you_sure_mark_as_read,
                    function() {
                        cc.mail.markReadItem(threadId);
                        jQuery.fn.colorbox.close();
                    }, null);
                return false;
            });



            jQuery('.cc-send-pm-support', container).unbind('click').click(function() {

                cc.ui.colorbox('/send-support-message/', function() {
                    // Init Response Form for Ajax Submit
//                    jQuery('#mail-form').unbind('submit').submit(function() {
//                        return cc.userprofile.sendMailSubmit('#mail-form');
//                    });
                });
            });



            jQuery('#mail-read-reply-btn', container).unbind('click').click(function() {

            cc.ui.showModalOK(cc_resx_TR_Reply_Successfully_Sent, "/myprofile/mail/1/");
                return false;
            });

            jQuery('#cc-mails-delete', container).unbind('click').click(function() {
                var threadId = jQuery('.cc-mail-thread-id', jQuery(this).parents('li:first')).val();
                cc.ui.showModalConfirm(cc_resx_TR_Are_you_sure_delete_mails,
                    function() {
                        cc.mail.deleteCheckedItems();
                        jQuery.fn.colorbox.close();
                    }, null);

                return false;
            });


            jQuery('.cc-delete-mail', container).each(function() {
                jQuery(this).unbind('click').click(function() {

                    var threadId = jQuery('.cc-mail-thread-id', jQuery(this).parents('li:first')).val();
                    cc.ui.showModalConfirm(cc_resx_TR_Are_you_sure_delete_a_mail,
                        function() {
                            cc.mail.deleteItem(threadId);
                            jQuery.fn.colorbox.close();
                        }, null);


                    return false;
                });
            });





            jQuery('#cc-mail-results', container).unbind('change').change(function() {
                var value = jQuery(this).val();
                var filter = jQuery('#sFilter').val();
                
                window.location = '/myprofile/mail/' + filter + '/?limit=' + value;
            });

            return false;
        },

        fixButtons: function() {
            if (jQuery(".cc-mark-mail:checked").size() < jQuery(".cc-mark-mail").size()) {
                jQuery('#cc-mail-checkall').attr('checked', false);
                jQuery('#cc-mail-checkall-lbl').html(cc_resx_TR_Select_All);
            }
            else {
                jQuery('#cc-mail-checkall').attr('checked', true);
                jQuery('#cc-mail-checkall-lbl').html(cc_resx_TR_Select_None);
            }

            if (jQuery(".cc-mark-mail:checked").size() > 0) {
                jQuery('#cc-mails-read').addClass('active');
                jQuery('#cc-mails-delete').addClass('active');
            } else {
                jQuery('#cc-mails-read').removeClass('active');
                jQuery('#cc-mails-delete').removeClass('active');
            }
        },


        deleteItem: function(threadId) {

            if (typeof cc.mail.actionInProgress !== 'undefined' && cc.mail.actionInProgress === true) {
                return false;
            }
            cc.mail.actionInProgress = true;
            jQuery('.cc-delete-mail', jQuery('#' + threadId).parents('li:first')).html(cc_resx_TR_deleting);
            
            cc.ajax.formSubmit(
                jQuery('#form-mail-options'),
                {
                    url: '/myprofile/mail/thread/delete/' + threadId,
                    dataType: 'json'
                },
                function(respText) {
                    if (respText.success === 'undefined' || respText.success === false) {
                        cc.ui.showModalErrorMessage(cc_resx_TR_An_error_occurred_deleting_mails);
                    }
                    else {
                        jQuery('#' + threadId).parents('li:first').slideUp(300, function() {
                            jQuery('#' + threadId).remove();
                            cc.mail.fixButtons();
                        });
                    }
                },
                function() {
                    jQuery('.cc-delete-mail', jQuery('#' + threadId).parents('li:first')).html(cc_resx_TR_Delete);
                    cc.mail.actionInProgress = false;
                    cc.mail.fixButtons();
                }
            );

        },




        markReadItem: function(threadId) {

            if (typeof cc.mail.actionInProgress !== 'undefined' && cc.mail.actionInProgress === true) {
                return false;
            }
            cc.mail.actionInProgress = true;

            var threadIds = [];
            jQuery('.cc-mark-mail:checked').each(function() {
                threadIds.push(jQuery(this).attr('id'));
            });


            cc.ajax.formSubmit(
                jQuery('#form-mail-options'),
                {
                    url: '/myprofile/mail/thread/mark-read/',
                    dataType: 'json'
                },
                function(respText) {

                    if (respText.success === 'undefined' || respText.success === false) {
                        cc.ui.showModalErrorMessage(cc_resx_TR_An_error_occurred_processing_mails);
                    }
                    else {
                        for (var i = 0; i < threadIds.length; i++) {
                            jQuery('#li-' + threadIds[i]).removeClass('unread');
                        }

                        cc.mail.fixButtons();
                    }
                },
                function() {
                    cc.mail.actionInProgress = false;
                    cc.mail.fixButtons();
                }
            );

        },

        deleteCheckedItems: function() {

            if (typeof cc.mail.actionInProgress !== 'undefined' && cc.mail.actionInProgress === true) {
                return false;
            }
            cc.mail.actionInProgress = true;

            var threadIds = [];
            jQuery('.cc-mark-mail:checked').each(function() {
                threadIds.push(jQuery(this).attr('id'));
            });

            jQuery('#cc-mails-delete').html(cc_resx_TR_deleting);
            cc.ajax.formSubmit(
                jQuery('#form-mail-options'),
                {
                    url: '/myprofile/mail/thread/delete/',
                    dataType: 'json'
                },
                function(respText) {

                    if (respText.success === 'undefined' || respText.success === false) {
                        cc.ui.showModalErrorMessage(cc_resx_TR_An_error_occurred_deleting_mails);
                    }
                    else {


                        for (var i = 0; i < threadIds.length; i++) {

                            jQuery('#' + threadIds[i]).parents('li:first').slideUp(300, function() {
                                jQuery(this).remove();
                                cc.mail.fixButtons();
                            });

                        }
                        /*
                        cc.mail.fixButtons();
                        jQuery('.cc-mark-mail:checked').each(function() {
                        jQuery(this).parents('li:first').slideUp(300, function() {
                        jQuery(this).remove();
                        cc.mail.fixButtons();
                        });
                        });
                        */
                    }
                },
                function() {
                    jQuery('#cc-mails-delete').html('Delete');
                    cc.mail.actionInProgress = false;
                    cc.mail.fixButtons();
                }
            );
        }

    };

} ();
