﻿/* 
 * 빙그레 메인 페이지 자바스크립트 함수
 * 
 */
$(function() {

    // 이미지 소스 변경
    replaceImage = function(img_obj, old_src, new_src) { img_obj.get(0).src = img_obj.get(0).src.replace(old_src, new_src); }

    // 메인 탑메뉴 롤오버 #1
    $("#mainbady #main_header .nav li a img").hover(
        function(e) {
            this.src = this.src.replace('.gif', '_on.gif');
        }, function(e) {
            this.src = this.src.replace('_on.gif', '.gif');
        }
    );
    // 메인 탑메뉴 롤오버 #2
    $("#gnb_wrap #header .nav li a img").hover(
        function(e) {
            this.src = this.src.replace('.gif', '_on.gif');
        }, function(e) {
            this.src = this.src.replace('_on.gif', '.gif');
        }
    );

    // 신제품 배너 슬라이드 구현
    var play_brand;
    
    changeBrand = function(active_brand, active_brand_con) {

        $("div.brand ul li").each(function(index, element) {
            replaceImage($(element).find("img"), "_on.gif", "_off.gif");
        });
        replaceImage($(active_brand).find("img"), "_off.gif", "_on.gif");
        
        $("div.brand ul li").removeClass("active");
        active_brand.addClass("active");

        $("div.brand_con ul li").removeClass("active").hide();
        active_brand_con.addClass("active").show();
    };

    rotateBrand = function() {
        var active_brand, active_brand_con;

        play_brand = setInterval(function() {
            active_brand = $("div.brand ul li[class='active']").next();
            if (active_brand.length == 0) {
                active_brand = $("div.brand ul li:first");
            }

            active_brand_con = $("div.brand_con ul li[class='active']").next();
            if (active_brand_con.length == 0) {
                active_brand_con = $("div.brand_con ul li:first")
            }
            changeBrand(active_brand, active_brand_con);
        }, 5000);
    };

    // 신제품 배너 슬라이드 시작
    if ($("div.brand_con ul li").length > 1) {
        $("div.brand ul li:first").addClass("active");
        $("div.brand_con ul li:first").addClass("active");
        rotateBrand($("div.brand_con ul li"));
    }

    // 신제품 배너 단추
    $("div.brand ul li img").hoverIntent({
        over: function() {
            clearInterval(play_brand);
            var idx = $("div.brand ul li img").index(this);
            changeBrand($("div.brand ul li:eq(" + idx + ")"), $("div.brand_con ul li:eq(" + idx + ")"));
        },
        out: function() {
            rotateBrand();
        }
    });

    // 신제품 배너
    $("div.brand_con ul li a").hover(
        function() {
            clearInterval(play_brand);
        }, function() {
            rotateBrand();
        }
    );

    // 이벤트 배너 슬라이드 구현
    var play_event;

    changeEvent = function(active_event, active_event_con) {

        $("div.event ul li").each(function(index, element) {
            replaceImage($(element).find("img"), "_on.gif", "_off.gif");
        });
        replaceImage($(active_event).find("img"), "_off.gif", "_on.gif");
        
        $("div.event ul li").removeClass("active");
        active_event.addClass("active");

        $("div.event_con ul li").removeClass("active").hide();
        active_event_con.addClass("active").show();
    };

    rotateEvent = function() {
        var active_event, active_event_con;

        play_event = setInterval(function() {
            active_event = $("div.event ul li[class='active']").next();
            if (active_event.length == 0) {
                active_event = $("div.event ul li:first");
            }

            active_event_con = $("div.event_con ul li[class='active']").next();
            if (active_event_con.length == 0) {
                active_event_con = $("div.event_con ul li:first")
            }
            changeEvent(active_event, active_event_con);
        }, 5000);
    };

    // 이벤트 배너 슬라이드 시작
    if ($("div.event_con ul li").length > 1) {
        $("div.event ul li:first").addClass("active");
        $("div.event_con ul li:first").addClass("active");
        rotateEvent($("div.event_con ul li"));
    }

    // 이벤트 배너 단추
    $("div.event ul li img").hoverIntent({
        over: function() {
            clearInterval(play_event);
            var idx = $("div.event ul li img").index(this);
            changeEvent($("div.event ul li:eq(" + idx + ")"), $("div.event_con ul li:eq(" + idx + ")"));
        },
        out: function() {
            rotateEvent();
        }
    });

    // 이벤트 배너
    $("div.event_con ul li a").hover(
        function() {
            clearInterval(play_event);
        }, function() {
            rotateEvent();
        }
    );

    // NOTICE
    $("div.notice > ul > li:eq(0)").hoverIntent({
        over: function() {
            $("div.notice > h2 > a").attr("href", "javascript:GoBingMenu('MN010401')");
            replaceImage($("div.notice > ul > li:eq(0) > a > img"), "_off.gif", "_on.gif");
            replaceImage($("div.notice > ul > li:eq(1) > a > img"), "_on.gif", "_off.gif");
            $("div.notice div.notice_con ul:eq(0)").show();
            $("div.notice div.notice_con ul:eq(1)").hide();
        },
        out: function() {}
    });

    // 전자공고
    $("div.notice > ul > li:eq(1)").hoverIntent({
        over: function() {
            $("div.notice > h2 > a").attr("href", "javascript:GoBingMenu('MN010301')");
            replaceImage($("div.notice > ul > li:eq(0) > a > img"), "_on.gif", "_off.gif");
            replaceImage($("div.notice > ul > li:eq(1) > a > img"), "_off.gif", "_on.gif");
            $("div.notice div.notice_con ul:eq(0)").hide();
            $("div.notice div.notice_con ul:eq(1)").show();
        },
        out: function() {}
    });

    // 패밀리 사이트 바로가기
    $("#select_family").hoverIntent({
        interval: 100,
        over: function() { $("#select_family .abs").fadeIn("normal"); },
        out: function() { $("#select_family .abs").fadeOut("normal"); }
    });

    //
});

// 메인 플래시 연동
function mainLike(val) {
    if (val == 0) {
        $("#main_footer .main_ifm").hide();
    } else {
        if (val == 1) {
            //$("#main_footer .main_ifm .btn_face").html("<fb:like href='http://www.bing.co.kr/brand/brand_view.aspx?pidx=24' send='false' layout='button_count' width='90' show_faces='true'></fb:like>");
        } else if (val == 2) {
            //$("#main_footer .main_ifm .btn_face").html("<fb:like href='http://www.bing.co.kr/brand/brand_view.aspx?pidx=26' send='false' layout='button_count' width='90' show_faces='true'></fb:like>");
        } else if (val == 3) {
            //$("#main_footer .main_ifm .btn_face").html("<fb:like href='http://www.bing.co.kr/brand/brand_view.aspx?pidx=28' send='false' layout='button_count' width='90' show_faces='true'></fb:like>");
        } else if (val == 4) {
            //$("#main_footer .main_ifm .btn_face").html("<fb:like href='http://www.bing.co.kr/brand/brand_view.aspx?pidx=25' send='false' layout='button_count' width='90' show_faces='true'></fb:like>");
        }
        $("#main_footer .main_ifm .btn_face").html("<fb:like href='http://www.facebook.com/bingsmile' send='false' layout='button_count' width='100' show_faces='false'></fb:like>");
        if (typeof FB != "undefined") {
            FB.XFBML.parse($("#main_footer .main_ifm .btn_face").get(0));
            $("#main_footer .main_ifm").show();
        }
    }
}
