/**
 * Created with JetBrains PhpStorm.
 * User: kjh
 * Date: 14. 10. 20
 * Time: 오후 9:30
 * To change this template use File | Settings | File Templates.
 */


$(function(){
    topVisualDefaultSetting();
    rightQuickDefaultSetting();
    leftQuickDefaultSetting();
    globalNavigationDefaultSetting();
    mainVisualImageDefaultSetting();
    mobileGnbDefaultSetting();

    subInteriorControl();
})

//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// 모바일 gnb 컨트롤
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
var mobileIndex = -1;
var mobileGNBStatus = "CLOSE";
function mobileGnbDefaultSetting(){

    $('.mobile_menu_btn').on( 'click', function(){
        if( mobileGNBStatus == "CLOSE"){
            mobileGNBON();
        }else{
            mobileGNBOFF();
        }
    });

    $('.mgnb_1depth').on('click', function(){
        reset2Depth();

        if( mobileIndex == $('.mgnb_1depth').index( this ) ){
            mobileIndex = -1;
            return;
        }

        mobileIndex = $('.mgnb_1depth').index( this );
        $(this).find('ul').addClass('on');
    });



}

function reset2Depth(){
    $('.mgnb_1depth ul').removeClass('on');
}

function mobileGNBOFF(){
    mobileGNBStatus = "CLOSE";
    $('.mobile_gnb').removeClass( 'on' );
    reset2Depth();
}

function mobileGNBON(){
    mobileGNBStatus = "OPEN";
    $('.mobile_gnb').addClass( 'on' );
}


//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// 메인 비주얼컨트롤
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
var prevVisualIndex = 0;
var visualIndex = 0;
var direction = "LEFT";


function mainVisualImageDefaultSetting(){

    var visualStatus = 0;
    var visualList = $('.visual_list li');
    var maxIndex = visualList.length;
    if( maxIndex == 0 ) return;

    var timer = setInterval( nextIndex, 5000);
    function nextIndex(){
        if( direction == "LEFT"){
            prevVisualIndex = visualIndex;
            visualIndex--;
            if( visualIndex <= -1 ) visualIndex = maxIndex-1;
        }else{
            prevVisualIndex = visualIndex;
            visualIndex++;
            if( visualIndex >= maxIndex ) visualIndex = 0;
        }
        visualIndexControl();
    }

    $('.visual_con').hover(function(){
        clearInterval( timer );
    },function(){
        clearInterval( timer );
        timer = setInterval( nextIndex, 5000);
    })

    visualList.each( function(){

        if( $('.visual_list li').index( this ) != visualIndex ){
            $(this).css( 'marginLeft', 1400 );
        }
    });

    $('.left_arrow').on( 'click' , function(){

        if( visualStatus == 1 ) return;
        direction = "LEFT";
        prevVisualIndex = visualIndex;
        visualIndex--;
        if( visualIndex <= -1 ) visualIndex = maxIndex-1;

        visualIndexControl();
    });



    $('.right_arrow').on( 'click' , function(){

        if( visualStatus == 1 ) return;
        direction = "RIGHT";
        prevVisualIndex = visualIndex;
        visualIndex++;
        if( visualIndex >= maxIndex ) visualIndex = 0;

        visualIndexControl();
    });


    function visualIndexControl(){
        visualStatus = 1;
        if( direction == "LEFT"){

            // 모션.
//            TweenMax.to( visualList[prevVisualIndex], 0.7, {scaleX:0.5, scaleY : 0.5, opacity:0, ease:Cubic.easeOut } );
//            TweenMax.fromTo( visualList[visualIndex], 0.7, {scaleX:1, scaleY : 1, opacity:1, marginLeft:1400 }, {marginLeft:0, ease:Cubic.easeOut, onComplete:function (){
//                visualStatus = 0;
//            } } );
            TweenMax.to( visualList[prevVisualIndex], 1, { opacity:0, ease:Cubic.easeOut } );
            TweenMax.fromTo( visualList[visualIndex], 2.5, {marginLeft:0, opacity:0}, {opacity:1, ease:Cubic.easeOut, onComplete:function (){
                visualStatus = 0;
            } } );
        }else if( direction == "RIGHT" ){

            // 모션
//            TweenMax.to( visualList[prevVisualIndex], 0.7, {scaleX:0.5, scaleY : 0.5, opacity:0, ease:Cubic.easeOut } );
//            TweenMax.fromTo( visualList[visualIndex], 0.7, {scaleX:1, scaleY : 1, opacity:1, marginLeft:-1400 }, {marginLeft:0, ease:Cubic.easeOut, onComplete:function (){
//                visualStatus = 0;
//            } } );
            TweenMax.to( visualList[prevVisualIndex], 1, { opacity:0, ease:Cubic.easeOut } );
            TweenMax.fromTo( visualList[visualIndex], 2.5, {marginLeft:0, opacity:0}, { opacity:1, ease:Cubic.easeOut, onComplete:function (){
                visualStatus = 0;
            } } );
        }
    }

}


//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// 왼쪽퀵메뉴컨트롤
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

var leftQuickIndex = -1;

function leftQuickDefaultSetting(){


    ( function setCurrentPage(){

        $('.quick_con li').each( function(){
            if( $(this).attr( 'currentPage' ) !== undefined ){
                leftQuickIndex = $('.quick_con li').index( this );
                $(this).addClass('quick_over');
            }
        });

    })();


    $('.quick_con li').hover(function(){
        $(this).addClass('quick_over');
    },function(){
        if( leftQuickIndex == $('.quick_con li').index( this ) ) return;
        $(this).removeClass('quick_over');
    });

}



//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// gnb 컨트롤
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

function globalNavigationDefaultSetting(){

    $('.gnb_con').hover( function(){
        TweenMax.to( this,  0.5, {height:320, ease:Cubic.easeInOut});
    }, function(){
        TweenMax.to( this,  0.5, {height:38, ease:Cubic.easeInOut});
    });
}


//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// 오른쪽 퀵메뉴 컨트롤
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

var rightQuickStatus = 0;
var rightQuickIndex = 0;
function rightQuickDefaultSetting(){

    // 상단버튼 셋팅
    $('#rightQuickOpenCloseBtn').on('click', function(){
        rightQuickStatus = !rightQuickStatus;

        if( rightQuickStatus == 0 ){
            rightQuickClose();
        }else{
            rightQuickOpen();
        }
    });


    // 상단버튼 셋팅
    $('.right_over_out_control').hover( function(){
        var thisIndex = $('.right_over_out_control').index( this );
        bm.image.srcChange( $('.right_over_out_control img')[thisIndex], "_off", "_on" );
    }, function(){
        var thisIndex = $('.right_over_out_control').index( this );
        if( rightQuickStatus == 0 ){
            bm.image.srcChange( $('.right_over_out_control img')[thisIndex], "_on", "_off" );
        }else{
            if( rightQuickIndex != thisIndex ){
                bm.image.srcChange( $('.right_over_out_control img')[thisIndex], "_on", "_off" );
            }
        }
    });

    // 기타 버튼들 클릭시
    $( '.right_quick_btn').on( 'click', function(){
        if( rightQuickStatus == 0 ){
            rightQuickStatus = 1;
            rightQuickOpen();
        }

        rightQuickIndex = $('.right_quick_btn').index( this );
        rightContentIndexControl();
    });


    // 기초 셋팅
    rightContentIndexControl();
}

function rightQuickOpen(){
    bm.image.srcChange( $('#rightQuickOpenCloseBtn img'), "_off", "_on" );
    TweenMax.to( '#right_wrap', 0.4, {marginRight:0, ease:Cubic.easeInOut } );

    rightContentIndexControl();
}

function rightQuickClose(){
    bm.image.srcChange( $('#rightQuickOpenCloseBtn img'), "_on", "_off" );
    TweenMax.to( '#right_wrap', 0.4, {marginRight:-316, ease:Cubic.easeInOut } );

    rightContentIndexControl();
}

function rightContentIndexControl(){
    if( $('.right_quick_btn img').length == 0 ) return;

    $('.right_quick_content').hide();
    $( $('.right_quick_content')[rightQuickIndex]).show();

    bm.image.srcChange( $('.right_quick_btn img'), "_on", "_off" );
    if( rightQuickStatus == 1 ){
        bm.image.srcChange( $('.right_quick_btn img')[rightQuickIndex], "_off", "_on" );
    }
}





//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// 상단 비주얼 컨트롤
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

function topVisualDefaultSetting(){

    if( window.document.body.clientWidth > 800 ){
        setTimeout(topVisualResizeControl, 0 );
    }else{
        $('#center_wrap').css('margin', 'auto' );
    }

    bm.onEnterRegist( topVisualSizeControl );
    bm.onEnterRegist( interiorSizeControl );

    $(window).on('resize', function(){
        if( window.document.body.clientWidth > 800 ){
            setTimeout(topVisualResizeControl, 0 );
            mobileGNBOFF();
        }else{
            $('#center_wrap').css('margin', 'auto' );
        }
    });
}


// 메인 상단 비쥬얼 컨테이너 크기 조정
var prevTopVisualHeight = 0;
function topVisualSizeControl(){

    if( $('.visual_0').length == 0 ) bm.onEnterRemove( topVisualSizeControl );

    var currHeight = $('.visual_0').height();

    if( prevTopVisualHeight != currHeight ){
        $('.visual_con').css( 'height', currHeight );
        prevTopVisualHeight = currHeight;
    }
}


// 서브 - 내부 둘러보기 비쥬얼 컨테이너 크기 조정
var prevInteriorHeight = 0;
function interiorSizeControl(){

    if( $('.view_rolling_con').length == 0 ) bm.onEnterRemove( interiorSizeControl );

    var currHeight = $( $('.view_rolling_con li img')[0] ).height();

    if( prevInteriorHeight != currHeight ){
        $('.view_rolling_con').css( 'height', currHeight );
        prevInteriorHeight = currHeight;
    }
}



function topVisualResizeControl(){
    var leftQuickSize = $('.quick_con').width();
    var rightQuickSize = $('.quick_tab_btn_con').width();
    var windowWidth = parseInt( window.document.body.clientWidth );

    var availableWidth = windowWidth - leftQuickSize - rightQuickSize;
    var maxWidth = 1400;

    if( availableWidth < maxWidth ){
//        TweenMax.to( '.visual_con, .center_con, .gnb_con, #sub_content_con', 0.5, {paddingLeft: 0, ease:Cubic.easeInOut } );
        TweenMax.to( '.visual_con, .center_con, .gnb_con, #sub_content_con', 0, {paddingLeft: 0, ease:Cubic.easeInOut } );
        $('.visual_con, .visual_list, .center_con, .gnb_con, #sub_content_con').css( 'maxWidth', availableWidth );

        $('#center_wrap').css('marginLeft', 180 );
        $('#center_wrap').css('marginRight', 0 );
    }else{
        var paddingLeftValue = leftQuickSize - rightQuickSize >> 1;
//        TweenMax.to( '.visual_con, .center_con, .gnb_con, #sub_content_con', 0.5, {paddingLeft: paddingLeftValue, ease:Cubic.easeInOut } );
        TweenMax.to( '.visual_con, .center_con, .gnb_con, #sub_content_con', 0, {paddingLeft: paddingLeftValue, ease:Cubic.easeInOut } );

        $('.visual_con, .visual_list, .center_con, .gnb_con, #sub_content_con').css( 'maxWidth', 1400 );
        $('#center_wrap').css('margin', 'auto' );
    }


}



function subInteriorControl(){
    if( $('.view_rolling_con li img').length == 0 ) return;

    var thumbList = $('.thumb_con li p');
    var thumbImgList = $('.thumb_con li img');
    var imgMaxIndex = 15;
    var prevIndex = 0;
    var imgIndex = 0;

    // 최초 한번 실행
    indexControl();

    $('#sub_content_con .roll_btn .left_btn').on( 'click', function(){
        prevImg();
    });
    $('#sub_content_con .roll_btn .right_btn').on( 'click', function(){
        nextImg();
    });

    $('.thumb_con li').on( 'click' , function(){
        var currIndex = $('.thumb_con li').index( this );
        if( currIndex != imgIndex ) prevIndex = imgIndex;
        imgIndex = currIndex;
        indexControl();
    });

    function prevImg(){
        prevIndex = imgIndex;

        imgIndex--;
        if( imgIndex == -1 ) imgIndex = imgMaxIndex-1;
        indexControl();
    }

    function nextImg(){
        prevIndex = imgIndex;

        imgIndex++;
        if( imgIndex == imgMaxIndex ) imgIndex = 0;
        indexControl();
    }

    function indexControl(){
        $('.view_rolling_con li img').each( function(){
            var currIdx = $('.view_rolling_con li img').index( this );
            if( currIdx == imgIndex ) {
                $(this).fadeIn(1000);
                $( thumbList[currIdx]).addClass( 'thumb_on');
            }else{
                $(this).hide();
                $( thumbList[currIdx]).removeClass( 'thumb_on');
            }
        });

        if( prevIndex != imgIndex ){
            bm.image.srcChange( thumbImgList[prevIndex] , "on", "off" );
        }

        bm.image.srcChange( thumbImgList[imgIndex] , "off", "on" );
    }
}