/*
 * ¶óÀÌ¼¾½º
 */
var LicenseList = {

    cellHeight : 45,
    listHeight : 0,
    selectIndex : 0,
    buttonHold : false,
    defalutTopSize : 83,
    animation:false,

    init:function(random){

        this.licenseBoxReflash();
        //À§ÂÊ ¹öÆ°
        $('#licenseBox').append('<a href="#" id="topFlowButton"><img src="/ossw/image/index/licenseBox/btn_up.png" alt="\uc704\ub85c"/></a>');
        //¾Æ·¡ÂÊ ¹öÆ°
        $('#licenseBox').append('<a href="#" id="downFlowButton"><img src="/ossw/image/index/licenseBox/btn_down.png" alt="\uc544\ub798\ub85c"/></a>');
        $('#licenseBox').append('<div id="selectBox"><h2></h2><p></p></div>');

        //this.selectIndex = Math.round(Math.random()* $('#licenseList ul li').length);
        this.selectIndex = random;
        if(this.selectIndex>=10){
            this.selectIndex = 9;
        }
        this.viewLicenseData(this.selectIndex);
        this.chekeLicenseListButtonStatus();

        $('#topFlowButton').bind('click keypress',function() {
            return LicenseList.actionDownFloButton();
        });
        $('#downFlowButton').bind('click keypress',function() {
            return LicenseList.actionTopFlowButton();
        });

        $('#topFlowButton').css({
            'position':'absolute',
            'top':'65px',
            'left':'465px'
        });
        $('#downFlowButton').css({
            'position':'absolute',
            'top':'255px',
            'left':'465px'
        });
        this.loadNotImportantLicenseList();
        $('#licenseBox').mouseover(function(){
            if (window.addEventListener){
                window.addEventListener('DOMMouseScroll', wheel, false);
            }
            window.onmousewheel = document.onmousewheel = wheel;
        });
        $('#licenseBox').mouseout(function(){
            if (window.removeEventListener){
                window.removeEventListener('DOMMouseScroll', wheel, false);
            }
            window.onmousewheel = document.onmousewheel = null;
        });
    },  

    animationScoll:function(pointIndex){
        $('div#licenseList ul').animate({
            top:(83 - (pointIndex * this.cellHeight))
        }, 200,function(){
            LicenseList.animation = false;
        });
    },

    actionTopFlowButton:function (){
        if(!this.animation){
            if(this.buttonHold) return false;
            if ( this.selectIndex + 1 < $('#licenseList ul li').length) {
                this.animation = true;
                this.viewLicenseData( this.selectIndex + 1 );
                this.selectIndex += 1;
            }
            this.chekeLicenseListButtonStatus();
        }
        return false;
    },

    actionDownFloButton:function(){
        if(!this.animation){
            if(this.buttonHold) return false;
            if (this.selectIndex - 1 >= 0) {
                this.animation = true;
                this.viewLicenseData( this.selectIndex - 1 );
                this.selectIndex -= 1;
            }
            this.chekeLicenseListButtonStatus();
        }
        return false;
    },

    chekeLicenseListButtonStatus:function(){
        if( this.selectIndex - 1 >= 0 ){
            $('#topFlowButton img').attr('src','/ossw/image/index/licenseBox/btn_up.png');
        } else {
            $('#topFlowButton img').attr('src','/ossw/image/index/licenseBox/btn_up_disable.png');
        }

        if( this.selectIndex + 1 < $('#licenseList ul li').length ){
            $('#downFlowButton img').attr('src','/ossw/image/index/licenseBox/btn_down.png');
        } else {
            $('#downFlowButton img').attr('src','/ossw/image/index/licenseBox/btn_down_disable.png');
        }
    },
    licenseBoxReflash:function(){
        this.listHeight = $('#licenseList ul li').length * this.cellHeight;
        $('div#licenseList ul').height(this.listHeight + 440);
    },

    viewLicenseData:function(index){

        this.animationScoll(index);

        if (this.selectIndex == index - 1) {
            var beforeLiElement = $('div#licenseList ul li').get( index - 1);
            $(beforeLiElement).height(45 - ($(beforeLiElement).outerHeight() - $(beforeLiElement).height()));
        }

        var currentLiElement = $('div#licenseList ul li').get(index);
        $(currentLiElement).height(154 - ($(currentLiElement).outerHeight() - $(currentLiElement).height()));
        this.sicenseDataSetup(currentLiElement);

        if (this.selectIndex == index + 1 && index + 1 < $('#licenseList ul li').length) {
            var afterLiElement = $('div#licenseList ul li').get( index + 1);
            $(afterLiElement).height(45 - ($(afterLiElement).outerHeight() - $(afterLiElement).height()));
        }
    },

    hiddenLicenseData:function(liElement){
        $(liElement).children('h2').each(function(){
            $(this).css('display', 'block');
        });
        $(liElement).children('div').css('display', 'none');
    },

    sicenseDataSetup:function(liElement){
        var summary = $(liElement).children('p').html();
        summary = summary.replace(/<[Aa][^>]+>/, '');
        summary = summary.replace(/<[\/][Aa]>/, '');
        
        $('div#selectBox h2').html('<a href="/ossw/license/license/detail.do?lid='+$(liElement).children('p').attr('id').replace(/lid-/, '')+'">'+$(liElement).children('h2').html()+'</a>');
        $('div#selectBox p').html('<a href="/ossw/license/license/detail.do?lid='+$(liElement).children('p').attr('id').replace(/lid-/, '')+'">'+summary+'</a>');
    },

    loadNotImportantLicenseList: function(){
        $.getJSON('/ossw/licenseList.do',function(data){
            var liElements = "";
            for(var i = 0;i<data.length;i++){
                var libData = data[i];
                liElements+='<li><h2>'+libData.licenseName+'</h2><p id="lid-'+libData.lib+'">'+libData.summaryKo+'</p></li>';
            }
            $('div#licenseList ul').append(liElements);
            LicenseList.chekeLicenseListButtonStatus();
        });
        this.licenseBoxReflash();
    }
}

//¹è³Ê
var Banner = {

    bannerListWidth:0,
    bannerViewAreaWidth : 0,
    bannerLinkLenght : 0,
    bannerLinkCurrentIndex : 0,
    bannerActive : false,
    interval:null,

    init : function(){
        $('div#bannerBox div ul li').each(function(){
            Banner.bannerListWidth += $(this).outerWidth();
        });
        $('div#bannerBox div ul').width(this.bannerListWidth);
        this.bannerLinkLenght = $('div#bannerBox div ul li').length;

        this.bannerViewAreaWidth = $('div#bannerBox div').outerWidth();
        //¿ÞÂÊ ¹öÆ°
        $('div#bannerBox').append('<a href="#" id="bannerLeftFlow"><img src="/ossw/image/index/bannerBox/btn_flowLeft.gif" alt="\uc67c\ucabd"/></a>');
        //¿À¸¥ÂÊ ¹öÆ°
        $('div#bannerBox').append('<a href="#" id="bannerRightFlow"><img src="/ossw/image/index/bannerBox/btn_flowRight.gif" alt="\uc624\ub978\ucabd"/></a>');

        $('#bannerLeftFlow').bind('click keypress',function(){
            return Banner.leftFlowButton();
        });
        $('#bannerRightFlow').bind('click keypress',function(){
            return Banner.rightFlowButton();
        });
        $('div#bannerBox').mouseover(function(){
            clearInterval(Banner.interval);
        });
        $('div#bannerBox').mouseout(function(){
            Banner.interval = setInterval("Banner.autoFlowAction();", 1500);
        });
        Banner.interval = setInterval("Banner.autoFlowAction();", 1500);
    },
//    ¹è³Ê ÀÚµ¿ ÀÌµ¿ 
//    autoFlowAction:function(){
//        if( Banner.bannerLinkCurrentIndex + 1 < Banner.bannerLinkLenght  && !Banner.bannerActive){
//            var changeValue = $('div#bannerBox div ul').position().left - $($('div#bannerBox div ul li').get(Banner.bannerLinkCurrentIndex+1)).outerWidth();
//            if(Banner.bannerListWidth - Banner.bannerViewAreaWidth >= -changeValue){
//                ++Banner.bannerLinkCurrentIndex;
//                Banner.bannerActive = true;
//                $('div#bannerBox div ul').animate({
//                    left:changeValue
//                },400,function(){
//                    Banner.bannerActive = false;
//                });
//
//            }else{
//                Banner.bannerLinkCurrentIndex = 0;
//                Banner.bannerActive = true;
//                $('div#bannerBox div ul').animate({
//                    left:0
//                },400,function(){
//                    Banner.bannerActive = false;
//                });
//            }
//        }
//        return false;
//    },
    leftFlowButton:function(){
        if( Banner.bannerLinkCurrentIndex + 1 < Banner.bannerLinkLenght  && !Banner.bannerActive){
            var changeValue = $('div#bannerBox div ul').position().left - $($('div#bannerBox div ul li').get(Banner.bannerLinkCurrentIndex+1)).outerWidth();
            if(Banner.bannerListWidth - Banner.bannerViewAreaWidth >= -changeValue){
                ++Banner.bannerLinkCurrentIndex;
                Banner.bannerActive = true;
                $('div#bannerBox div ul').animate({
                    left:changeValue
                },400,function(){
                    Banner.bannerActive = false;
                });

            }
        }
        return false;
    },
    rightFlowButton:function(){
        if( Banner.bannerLinkCurrentIndex - 1 >= 0 && !Banner.bannerActive){
            var changeValue = $('div#bannerBox div ul').position().left + $($('div#bannerBox div ul li').get(Banner.bannerLinkCurrentIndex-1)).outerWidth();
            if(Banner.bannerListWidth - Banner.bannerViewAreaWidth >= -changeValue){
                --Banner.bannerLinkCurrentIndex;
                Banner.bannerActive = true;
                $('div#bannerBox div ul').animate({
                    left:changeValue
                },400,function(){
                    Banner.bannerActive = false;
                });
            }
        }
        return false;
    }
}

function guideBoxMouseOverActionInit(){
    $('#guideBox ul li a').each(function(){
        $(this).bind('mouseover', function(){
            var src = $(this).children('img').attr('src');
            src = src.replace(/[.].+$/, '')+"_over."+src.replace(/^.+[.]/, '');
            $(this).children('img').attr('src' ,src);
        });
        $(this).bind('mouseout', function(){
            $(this).children('img').attr('src' ,$(this).children('img').attr('src').replace(/_over/, ''));
        });
    });
}

/** This is high-level function.
 * It must react to delta being more/less than zero.
 */
function handle(delta) {
    if (delta < 0){
        LicenseList.actionTopFlowButton();
    }else{
        LicenseList.actionDownFloButton();
    }
}

/** Event handler for mouse wheel event.
 */
function wheel(event){
    var delta = 0;
    if (!event) /* For IE. */
        event = window.event;
    if (event.wheelDelta) { /* IE/Opera. */
        delta = event.wheelDelta/120;
        /** In Opera 9, delta differs in sign as compared to IE.
                 */
        if (window.opera)
            delta = -delta;
    } else if (event.detail) { /** Mozilla case. */
        /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
        delta = -event.detail/3;
    }
    /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
    if (delta)
        handle(delta);
    /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
    if (event.preventDefault)
        event.preventDefault();
    event.returnValue = false;
}

