﻿var _shoutbox_cur_page = 1;
function shoutbox_show(id, show) {
    var data = {
        'action': 'show',
        'id': id,
        'is_show': show
    };

    shoutbox_send(data, function(res) {
        if (res.success == 'ok') {
            shoutbox_list();
        }
        else {
            alert(res.message);
        }
    });
}

function shoutbox_save() {
    var msg = $('#shoutMessage').val();

    if (msg == '') {
        alert('Vui lòng nhập nội dung');
        $('#shoutMessage').focus();

        return;
    }

    $('#shoutButton').unbind('click').attr('disabled', true);


    var data = {
        'action': 'save',
        'msg': msg
    };

    shoutbox_send(data, function(res) {
        if (res.success == 'ok') {
            shoutbox_list(); // refresh list

            $('#shoutMessage').val("");
        }
        else {
            alert(res.message);
        }
    });

    $('#shoutButton').click(function(e) {
        shoutbox_save();
        e.preventDefault();
    }).attr('disabled', false);
}

function shoutbox_list(cur_page) {
    if (cur_page == null)
        cur_page = 1;

    _shoutbox_cur_page = 1;

    var data = {
        'action': 'list',
        'cur_page': cur_page
    };

    shoutbox_send(data, function(res) {
        if (res.success == 'ok') {
            $('#shoutbox_list').html(res.message);

            _shoutbox_bind();
        }
        else {
            alert(res.message);
        }
    });

    return false;
}

function shoutbox_send(data, callback, error) {
    $.ajax({
        url: '/services/shoutbox.ashx',
        beforeSend: function(xhr) {
            xhr.setRequestHeader("gm-ajax-request", 'gm-ajax-request');
        },
        data: data,
        dataType: 'json',
        type: 'post',
        async: true,
        success: function(results) {
            if (callback) {
                callback(results);
            }
        },
        error: function(xhr) {
            if (error) {
                error();
            } else {
                alert('Xảy ra lỗi khi thực thi!');
            }
        }
    });
}

function shoutbox_mark(sender) {
    var id = _shoutbox_get_id(sender);
    if (id != null) {
        var data = {
            'action': 'show',
            'id': id,
            'is_show': '0'
        };

        shoutbox_send(data, function(res) {
            if (res.success == 'ok') {
                shoutbox_list(_shoutbox_cur_page); // refresh list
            }
            else {
                alert(res.message);
            }
        });
    }
}

function shoutbox_delete(sender) {
    var id = _shoutbox_get_id(sender);
    if (id != null) {
        if (confirm('Bạn muốn xóa tin nhắn này?')) {
            var data = {
                'action': 'delete',
                'id': id
            };

            shoutbox_send(data, function(res) {
                if (res.success == 'ok') {
                    shoutbox_list(_shoutbox_cur_page); // refresh list
                }
                else {
                    alert(res.message);
                }
            });
        }
    }
}

function shoutbox_answer_delete(sender) {
    var id = _shoutbox_answer_get_id(sender);
    if (id != null) {
        if (confirm('Bạn muốn xóa tin nhắn này?')) {
            var data = {
                'action': 'delete',
                'id': id
            };

            shoutbox_send(data, function(res) {
                if (res.success == 'ok') {
                    shoutbox_list(_shoutbox_cur_page); // refresh list
                }
                else {
                    alert(res.message);
                }
            });
        }
    }
}

function shoutbox_reply(sender) {
    var id = _shoutbox_get_id(sender);

    // get text message
    var div_parent = sender.closest('div.detail-message');
    var txt = div_parent.find('textarea');

    txt.toggle();
    txt.focus();
    txt.autoResize({ limit: 300, extraSpace: 0 });
}

function shoutbox_reply_save(sender) {
    var id = _shoutbox_get_id(sender);
    var msg = sender.val();

    if (msg == '') {
        alert('Vui lòng nhập nội dung');
        sender.focus();
        return;
    }

    var data = {
        'action': 'save',
        'parent': id,
        'msg': msg
    };

    shoutbox_send(data, function(res) {
        if (res.success == 'ok') {
            shoutbox_list(_shoutbox_cur_page); // refresh list

            $('#shoutMessage').val("");
        }
        else {
            alert(res.message);
        }
    });
}

function _shoutbox_get_id(sender) {
    var div_parent = sender.closest('div.detail-message');
    var id_obj = div_parent.find('input[type=hidden]');

    return id_obj.val();
}

function _shoutbox_answer_get_id(sender) {
    var id = sender.attr('id');
    id = id.replace(/answer-/ig, '');

    return id;
}

function _shoutbox_bind() {
    $('#shoutbox_list').find('div.detail-message').each(function() {
        var div_parent = $(this);

        div_parent.find('span.usr-msg-action').click(function() {
            if (isLoggedin == true) {
                shoutbox_reply($(this));
            }
            else {
                show_login();
            }
        });

        div_parent.find('span.usr-warning').click(function() {
            shoutbox_mark($(this));
        });

        div_parent.find('span.usr-delete').click(function() {
            shoutbox_delete($(this));
        });

        div_parent.find('textarea').keypress(function(e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
                if (isLoggedin == true) {
                    shoutbox_reply_save($(this));
                }
                else {
                    show_login();
                }
                e.preventDefault();
            }
        });

        div_parent.find('span.usr-delete-answer').each(function() {
            $(this).click(function() {
                shoutbox_answer_delete($(this));
            });
        });
    });
}

// Common funtions
function show_login() {
    loginDialog.show();

    $('#simple_dialog .close').click(function(e) {
        loginDialog.close();
        e.preventDefault();
    });
}

function header_menu(obj_selector) {
    obj_selector.hover(function() {
        $(this).addClass("hover");
        $('ul:first', this).css('visibility', 'visible');

    }, function() {
        $(this).removeClass("hover");
        $('ul:first', this).css('visibility', 'hidden');
    });
}

function set_filter_mode(mode) {
    var date = new Date();
    date.setTime(date.getTime() + (24 * 60 * 60 * 1000)); // a month
    $.cookie('gm_filter_mode', mode, { expires: date, path: '/' });

    if (mode == 2)
        window.location.href = '/';
    else
        window.location.href = '/deals';

    return false;
}

function do_search() {
    var keyword = $('#query');
    if (keyword.length > 0) {
        if (keyword.val().length > 2) {
            window.location.href = '/search.aspx?query=' + encodeURIComponent(keyword.val());
        }
        else {
            alert('Từ khóa tìm kiếm quá ngắn');
            keyword.focus();
        }
    }
}

var mailDialog;
var couponLinkDialog;

jQuery(document).ready(function() {

    header_menu($('#menu_settings'));
    header_menu($('#menu_location'));
    header_menu($('#add-menu'));

    mailDialog = new Giamua.RegisterMailDialog();
    $('#btnShowMailForm').click(function() {
        mailDialog.show();

        $('#simple_dialog .close').click(function(e) {
            mailDialog.close();
            e.preventDefault();
        });
    });

    // bind event for shoutbox login
    loginDialog = new Giamua.LoginDialog();
    $('#shoutbox_login').click(function(e) {
        show_login();
        e.preventDefault();
    });

    // Search events
    $('#query').keypress(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            do_search();
            e.preventDefault();
        }
    });
    $('#search_btn').click(function(e) {
        do_search();
        e.preventDefault();
    });
    $('#show_search_form').click(function(e) {
        $('#gm_search').toggle();
        $('#query').focus();
        e.preventDefault();
    });

    // Shoutbox events
    $('#shoutButton').click(function(e) {
        if (isLoggedin == true) {
            shoutbox_save();
        }
        else {
            show_login();
        }

        e.preventDefault();
    });
    $('#shoutMessage').maxlength();
    $('#shoutMessage').bind('focusin', function() {
        if (!isLoggedin) {
            show_login();
        }
    });
    _shoutbox_bind();           
});      
