(function($) {
    $.eway = {
        popup: function(title, message, button) {
            // Show popup nếu chưa show
            if ($("#popup-window").css('display') == 'none'){
                $("div.bg_opacity").show();
                $('#show_title_popup').html('Đang tải ...');
                $('#show_content_popup').html('');
                $("#popup-window").show();
            }
            
            // Set title cho popup
            $('#show_title_popup').html(title);
            // Set nội dung
            $('#show_content_popup').html(message);
            // Set button
            $('#show_button_popup').html('');
            if (button.request){
                $('#show_button_popup').append('<input type="button" value="' + button.request.value + '" class="popup-btn" onclick="' + button.request.callback + '"/>');
            }
            if (button.accept){
                $('#show_button_popup').append('<input type="button" value="' + button.accept.value + '" class="popup-btn" onclick="' + button.accept.callback + '"/>');
            }
            if (button.reject){
                $('#show_button_popup').append('<input type="button" value="' + button.reject.value + '" class="popup-btn" onclick="' + button.reject.callback + '"/>');
            }
            if (button.cancel || button == 1){
                //if (message) $('#show_content_popup').html('<p>' + message + '</p>');
                if (message) $('#show_content_popup').html(message);
                $('#show_button_popup').append('<input type="button" value="Đóng" class="popup-btn-close" onclick="closePopup();"/>');
            }
        }
    }

    ePopup = function(message, title, button) {
        $.eway.popup(message, title, button);
    }

})(jQuery);
$(function() {
    var offset = $("#popup-window").offset();
    var topPadding = 1;
    //    $(window).scroll(function() {
    //        if ($(window).scrollTop() > offset.top) {
    //            $("#popup-window").css('marginTop', $(window).scrollTop() - offset.top + topPadding);
    //        } else {
    //            $("#popup-window").css('marginTop', 0);
    //        };
    //    });
    $(window).scroll(function () {
        $("#popup-window").css("top", $(window).scrollTop()+100 + "px");
    });

});
function openPopup(){
    $(window).scroll(function () {
        $("#popup-window").css("top", $(window).scrollTop()+100 + "px");
    });
    $("div.bg_opacity").show();
    $('#show_title_popup').html('Đang tải ...');
    $('#show_content_popup').html('');
    $('#show_button_popup').html('');
    $("#popup-window").show();
}
function closePopup(){
    $(".user-popup-c").attr("style", "width:467px");
    $("div.bg_opacity").hide();
    $('#btn_close').hide();
    $("#popup-window").hide();
}
function showObj(objId){
    $("#"+objId).show();
}
function hideObj(objId){
    $("#"+objId).hide();
}
function requireLogin(){
    var title = 'Thông báo lỗi !';
    var content = '<br/><center><b>Có lẽ bạn cần đăng nhập để thực hiện !</b><br/><br/><img src="http://ca7.upanh.com/17.729.22119455.gHp0/291615412256033.gif"></center>';
    ePopup(title, content, 1);
}

// Like shop
var check_likeshop = new Array;
function likeShop(id, like, block){
    if (check_likeshop[id] != 1){
        check_likeshop[id] = 1;
        if (!block) block = 0;
        // Check block
        if (block != 0) {
            var block_id = block + "_" + id;
            block = "'" + block + "'";
        } else {
            var block_id = id;
        }
        $.ajax({
            type: "POST",
            url: "/ajax.php?mod=shop&act=likeshop",
            data: "shopId=" + id + "&likeId=" + like,
            success: function(data){
                data = eval( "("+data+")");
                $('#likeshoptotal_' + block_id).html(data.total);
                if (like == 0){
                    $('#likeshop_' + block_id).html("<a href=\"javascript:void(0)\" onclick=\"likeShop('" + id + "', 1, " + block + ");\" >Bỏ thích</a>");
                } else {
                    $('#likeshop_' + block_id).html("<a href=\"javascript:void(0)\" onclick=\"likeShop('" + id + "', 0, " + block + ");\" >Thích</a>");
                }
                check_likeshop[id] = 0;
            }
        });
    }
}
// Like nhóm
var check_likegroup = new Array;
function likeGroup(id, like){
    if (check_likegroup[id] != 1){
        check_likegroup[id] = 1;
        $.ajax({
            type: "POST",
            url: "/ajax.php?mod=groups&act=likegroup",
            data: "groupId=" + id + "&likeId=" + like,
            success: function(data){
                data = eval( "("+data+")");
                $('#likegrouptotal_' + id).html(data.total);
                if (like == 0){
                    $('#likegroup_' + id).html("<a href=\"javascript:void(0)\" onclick=\"likeGroup('" + id + "', 1);\" class=\"light\" >Bỏ thích</a>");
                } else {
                    $('#likegroup_' + id).html("<a href=\"javascript:void(0)\" onclick=\"likeGroup('" + id + "', 0);\" class=\"light\" >Thích</a>");
                }
                check_likegroup[id] = 0;
            }
        });
    }
}
function joinGroup(groupId, type){
    openPopup();
    $.ajax({
        type: "GET",
        url: "/ajax.php?mod=groups&act=join-group",
        data: "groupId=" + groupId + "&type=" + type,
        dataType: "json",
        success: function(returnData){
            if (type == 0){
                popTitle = 'Tham gia nhóm ' + returnData.title + '?';
                ePopup(popTitle, returnData.data, returnData.button);
            } else {
                if (returnData == 'success'){
                    closePopup();
                    location.reload();
                } else {
                    joinGroup(id, 0);
                }
            }
        }
    });
}
// Hiển thị user like
var check_likegroupdetail = new Array;
function likeGroupDetail(id, option, sCount){
    if (option == 1){
        $("#ng-t-"+id).hide();
    } else {
        if (!check_likegroupdetail[id]){
            var returnText = "";
            $.ajax({
                type: "POST",
                url: "/ajax.php?mod=groups&act=get-like-group",
                data: "groupId=" + id + "&sCount=" + sCount,
                success: function(returnData){
                    returnData = eval( "("+returnData+")");
                    var user = returnData.data;
                    var total = returnData.total;
            
                    var last_item = user.length-1;
                    for (i=0;i<=last_item;i++){
                        if (i < last_item-1){
                            returnText = returnText + user[i] + ", ";
                        } else if (i < last_item){
                            returnText = returnText + user[i];
                        } else {
                            if (last_item == 0){
                                returnText = user[i] + ' thích nhóm này.';
                            } else {
                                if (total == 0){
                                    returnText = returnText + " và " + user[i] + ' thích nhóm này.';
                                } else if (total > 0){
                                    returnText = returnText + ", " + user[i] + ' và ' + total + ' người khác thích nhóm này.';
                                }
                            }
                        }
                    }
                    check_likegroupdetail[id] = returnText;
                    $("#ng-t-s-"+id).html(returnText);
                    $("#ng-t-"+id).show();
                }
            });
        } else {
            $("#ng-t-s-"+id).html(check_likegroupdetail[id]);
            $("#ng-t-"+id).show();
        }
    }
}
// Like com
var check_likecom = new Array;
function likeCom(id, like){
    if (check_likecom[id] != 1){
        check_likecom[id] = 1;
        $.ajax({
            type: "POST",
            url: "/ajax.php?mod=default&act=likecom",
            data: "comId=" + id + "&likeId=" + like,
            success: function(data){
                data = eval( "("+data+")");
                $('#likecomtotal_' + id).html(data.total);
                if (like == 0){
                    $('#likecom_' + id).html("<a href=\"javascript:void(0)\" onclick=\"likeCom('" + id + "', 1);\" class=\"light\" >Bỏ thích</a>");
                } else {
                    $('#likecom_' + id).html("<a href=\"javascript:void(0)\" onclick=\"likeCom('" + id + "', 0);\" class=\"light\" >Thích</a>");
                }
                check_likecom[id] = 0;
            }
        });
    }
}
$(".like_com").livequery("click", function (e){
    if (notLogin){
        requireLogin();
        return false;
    }
    var commodity = $(this);
    var comId = $(this).attr('rel');
    var like =0;
    if($(this).hasClass('unlike_com')) like = 1;
    $.ajax({
        url: '/ajax.php?mod=default&act=like-com',
        type:'GET',
        data: "comId=" + comId + "&likeId=" + like,
        success:function(data){
            if (data == 0){
                if(like == 0){
                    $(commodity).addClass('unlike_com').text("Bỏ thích");
                }else{
                    $(commodity).removeClass('unlike_com').text("Thích");   
                }
            }
        }  
    });
});
// Like ware
$(".like_ware").livequery("click", function (e){
    if (notLogin){
        requireLogin();
        return false;
    }
    var warehouse = $(this);
    var whid = $(this).attr('rel');
    var like =0;
    if($(this).hasClass('unlike_com')) like = 1;
    $.ajax({
        url: '/ajax.php?mod=default&act=like-ware',
        type:'GET',
        data: "whid=" + whid + "&likeId=" + like,
        dataType: "json",
        success:function(data){
            if (data.code == 0){
                $('#likewhtotal_' + whid).text(data.total);
                if(like == 0){
                    $(warehouse).addClass('unlike_com').text("Bỏ thích");
                }else{
                    $(warehouse).removeClass('unlike_com').text("Thích");   
                }
            }
        }  
    });
});
function ntAcFriend(id, option, index){
    $('#loading_accept_friend').show();
    $.ajax({
        type: "POST",
        url: "/ajax.php?mod=trader&act=accept-friend",
        data: "userId=" + id + "&send=1&option=" + option + "&notice_index=" + index,
        success: function(data){
            if (data == 'success'){
                $('#loading_accept_friend').hide();
                $('div.notice_accept_friend_' + id).remove();
            } else {
                $('#loading_accept_friend').hide();
            }
        }
    });
}
$('a.close_ware_teaser').livequery("click", function(){
    var whId = $(this).attr('rel');
    if ($('#wh_teaser_' + whId).hasClass("ware_teaser_close")){
        $('#wh_teaser_' + whId).removeClass("ware_teaser_close").addClass("ware_teaser");
        $(this).text("Thu nhỏ");
    } else {
        $('#wh_teaser_' + whId).removeClass("ware_teaser").addClass("ware_teaser_close");
        $(this).text("Mở rộng");
    }
});
function sell_this_product(wh_id){
    openPopup();
    $.ajax({
        url: '/ajax.php?mod=default&act=sell-warehouse',
        type:'GET',
        data: "wh_id=" + wh_id,
        dataType: "json",
        success:function(data){
            if (data.code == 1){
                closePopup();
                window.location = '/' + data.shop + '/admin/add-product?pid=' + wh_id;
            } else if (data.code == 2){
                popTitle = 'Tôi bán sản phẩm này';
                content = 'Bạn chưa có gian hàng nào cả, vui lòng tạo một gian hàng trước khi bán sản phẩm';
                var button = new Array;
                button.request = new Array;
                button.request.value = "Tạo gian hàng";
                button.request.callback = "window.location='/tao-gian-hang.html'";
                button.cancel = 1;
                ePopup(popTitle, content, button);
            } else {
                popTitle = 'Tôi bán sản phẩm này';
                var button = new Array;
                button.request = new Array;
                button.request.value = "Chọn gian hàng";
                button.request.callback = "selectShop(" + wh_id + ")";
                button.cancel = 1;
                ePopup(popTitle, data.value, button);
            }
        }  
    });
}
function selectShop(wh_id){
    var shop = $('#choose_shop').val();
    window.location = '/' + shop + '/admin/add-product?pid=' + wh_id;
    closePopup();
}

function upProduct(sp_whid, sp_shop){
    $.ajax({
        url: '/ajax.php?mod=trader&act=upsp',
        type:'POST',
        data:"sp_shop=" + sp_shop + "&sp_whid=" + sp_whid,
        success:function(data){
            alert(data);
        }
    });
}
