/**
 * 옵션미리보기 스크립트, 상품리스트 관련 스크립트 추가함(munhui)
 * 기존 html/js/product_list.js를 개별디자인3에서 사용하기 위해 jquery화 시킴.
 * @auther duellist
 * @date 2011-06-16
 **/
var __option_preview_obj = {}; // 옵션 미리보기용 전역변수
function mk_prd_option_preview(uid, e, device) {
    var link_path = '/shop/';
    if (typeof device != "undefined") {
        link_path = device == 'MOBILE' ? '/m/' : '/shop/';
    }
    jQuery.ajax({
        type: 'POST',
        url: link_path + 'product_data.ajax.html',
        dataType: 'html',
        data: {
            'branduid': uid,
            'type': 'option'
        },
        success: function(req) {
            jQuery('#MK_opt_preview').html(req);
            return false;
        },
        errer: function() {
            alert('옵션정보 가져오기 실패');
        }
    });

    var _evt = e.currentTarget ? e.currentTarget : window.event.srcElement;
    var offset = ObjectPosition(_evt);
    __option_preview_obj = _evt;

    jQuery('#MK_opt_preview').css({
        'visibility': 'visible',
        'left': offset[0] + 'px',
        'top': offset[1] + 'px'
    });

    // window resize 이벤트 발생시 옵션 미리보기 위치를 다시 잡음
    jQuery(window).resize(function() {
        var _offset = ObjectPosition(__option_preview_obj);
        jQuery('#MK_opt_preview').css({
            'left': _offset[0] + 'px',
            'top': _offset[1] + 'px'
        });
    });
}
function ObjectPosition(obj) {
    var curleft = 0;
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    // 가운데 정렬때문에 추가함
    if (jQuery('#wrap').css('marginLeft') == 'auto') {
        curleft -= jQuery('#wrap').offset().left;
    }

    return [curleft,curtop];
}

function mk_prd_benefit(page_type, uid, target, is_mobile) {
    var url = is_mobile == true ? '/m/product_benefit.html' : '/shop/product_benefit.html';
    jQuery.ajax({
        type: 'GET',
        url: url,
        dataType: 'html',
        data: {
            'uid': uid,
            'page_type': page_type,
            'target': target,
            'rand':Math.random()
        },
        success: function(req) {
            jQuery('#MS_popup_product_benefit_'+page_type).html(req);
            jQuery('#MS_popup_product_benefit_'+page_type).show();
            return false;
        },
        errer: function() {
        }
    });
}

var mk_coupon_down_ing = false;
function mk_prd_benefit_down(page_type, uid, couponnum, is_mobile) {
    var url = is_mobile == true ? '/m/smart_coupon.action.html' : '/shop/smart_coupon.action.html';

    if (mk_coupon_down_ing === true) {
        return false;
    }
    jQuery.ajax({
        type: 'POST',
        dataType: 'json',
        url: url,
        data: {
            'action_type' : 'down_coupon',
            'down_page' : 'BENEFIT',
            'couponnum': couponnum,
            'rand':Math.random()
        },
        success: function(req) {
            if (req.success === true) {
               mk_prd_benefit(page_type, uid, couponnum, is_mobile);
               alert("쿠폰이 다운로드 되었습니다.");
            } else {
                if (req.msg && req.msg.length > 0) {
                    alert(req.msg);
                }
            }
            mk_coupon_down_ing = false;
        }
    });
}
function mk_prd_benefit_login(is_mobile) {
    var login_url = is_mobile == true ? '/m/login.html' :'/shop/member.html?type=login';
    if (confirm("쿠폰 다운은 로그인 후 가능합니다.\n로그인 하시겠습니까?")) {
        document.location.href = login_url;
    }
}
function mk_prd_benefit_down_fail() {
    alert('이미 발급받았거나, (선착순) 마감된 쿠폰입니다.');
}

var mk_prd_wish_action_lodding = false;
function mk_prd_wish_toggle(uid, is_mobile) {
        if (mk_prd_wish_action_lodding === true) {
            return;
        }
        mk_prd_wish_action_lodding = true;
        var action_type = 'toggle';
        var is_block = false;

        // block 있을 경우 재확인 함
        if ($('.my-wish-list[data-uid='+uid+']').length > 0) {
            is_block = true;
            if ($('.my-wish-list[data-uid='+uid+']').hasClass('wish-on') === true) {
                action_type = 'delete';
            } else {
                action_type = 'insert';
            }
        }

        var get_prd_count = 'N';
        if ($('.my-wish-count[data-uid='+uid+']').length > 0) {
            get_prd_count = 'Y';
        }
        
        var _url = is_mobile == true ? '/m/wish.action.html' :'/shop/wish.action.html';
        jQuery.ajax({
            url  : _url,
            type : 'POST',
            data: {
                action_type: action_type,
                page_type: 'product_list',
                data_type: 'ones',
                uid : uid,
                wish_get_prd_count : get_prd_count
            },
            dataType: 'json',
            success: function(response) {
                if (response.is_not_login == true) {
                    mk_prd_wish_login(is_mobile);
                    mk_prd_wish_action_lodding = false;
                    return;
                }

                if (response.success == true) {
                    if (is_block === true) {
                        if (response.data.action_type == 'insert') {
                            $('.my-wish-list[data-uid='+uid+']').addClass('wish-on');
                            if (response.data.pixel_event_id && response.data.pixel_event_id.length > 0) {
                                mk_call_fbq('AddToWishlist', response.data.pixel_event_id)
                            }
                        } else {
                            $('.my-wish-list[data-uid='+uid+']').removeClass('wish-on');
                        }
                    }
                    if (get_prd_count == 'Y') {
                        $('.my-wish-count[data-uid='+uid+']').html(response.data.prd_count);
                    }
                } else {
                    if (response.message != '') {
                        alert(response.message);
                    }
                }
                mk_prd_wish_action_lodding = false;
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                mk_prd_wish_action_lodding = false;
            }
        });
        return;
}
function mk_prd_wish_login(is_mobile) {
    var login_url = is_mobile == true ? '/m/login.html' :'/shop/member.html';
    if (confirm("회원에게만 제공이 되는 서비스입니다.\n우선 로그인을 하여 주시기 바랍니다.")) {
        document.location.href = login_url;
    }
}
function mk_call_fbq(type, id) {
    if (typeof fbq === 'function') {
        fbq('track', type, {}, {eventID: id});
    }
}
