﻿$(function() {
    var objbody = {};
    var objCnt = 0;
    var timer = {}
    var lng_now = 1;

    $.f_footBanner = {
        fBanner: {
            actype: "next",
            ftimer: 2000,
            fspeed: 1000
        }
    };

    $.fn.jQBannerfoot = function(fBanner) {
        var config = $.extend({}, $.f_footBanner.fBanner, fBanner);

        objbody = $(this);
        objCnt = $(this).find("img").length;
        $(objbody).find("img").each(function(i) { $(this).css("left", (i * 64) + "px"); });
        $("#logonext").click(function() { config.fspeed = 100; config.actype = "next"; moveLogo(config); });
        $("#logonext").mouseover(function() { $(this).attr("src", $(this).attr("oversrc")); });
        $("#logonext").mouseout(function() { $(this).attr("src", $(this).attr("outsrc")); });

        $("#logoback").click(function() { config.fspeed = 100; config.actype = "back"; moveLogo(config); });
        $("#logoback").mouseover(function() { $(this).attr("src", $(this).attr("oversrc")); });
        $("#logoback").mouseout(function() { $(this).attr("src", $(this).attr("outsrc")); });

        $("#banner_foot").mouseover(function() { clearTimeout(timer); });
        $("#banner_foot").mouseout(function() { config.actype = "next"; config.fspeed = 1000; timer = setTimeout(function() { automove(config) }, config.ftimer); });
        timer = setTimeout(function() { automove(config) }, config.ftimer);
    };

    function moveLogo(config) {
        //이동거리 이미지넓이(54), margin(10)
        var minSel = lng_now;
        var maxSel = lng_now + 7;
        if (objCnt < maxSel) maxSel = objCnt;
        var maxSel2 = 7 - (maxSel - minSel);
        var next = 0;
        var lngleft;
        var viewindex = -1;

        if (config.actype === "next") {
            viewindex = -1;
            if (maxSel == objCnt && maxSel2 == 0)
                next = 1;
            else if (maxSel == objCnt && maxSel2 > 0)
                next = maxSel2 + 1;
            else
                next = maxSel + 1;

            $(objbody).find("img:nth-child(" + next + ")").css("left", "512px");
            $(objbody).find("img:nth-child(" + next + ")").animate({ 'left': '448px' }, config.fspeed);

            for (i = minSel; i <= maxSel; i++) {
                $(objbody).find("img:nth-child(" + i + ")").animate({ 'left': (viewindex * 64) + 'px' }, config.fspeed);
                viewindex++;
            }
            if (maxSel2 > 0) {
                for (i = 1; i <= maxSel2; i++) {
                    $(objbody).find("img:nth-child(" + i + ")").animate({ 'left': (viewindex * 64) + 'px' }, config.fspeed);
                    viewindex++;
                }
            }

            lng_now++;
            if (lng_now > objCnt) lng_now = 1;
        }
        else if (config.actype === "back") {
            viewindex = 1;
            if (minSel > 1)
                next = minSel - 1;
            else
                next = objCnt;

            $(objbody).find("img:nth-child(" + next + ")").css("left", "-64px");
            $(objbody).find("img:nth-child(" + next + ")").animate({ 'left': '0px' }, config.fspeed);

            for (i = minSel; i <= maxSel; i++) {
                $(objbody).find("img:nth-child(" + i + ")").animate({ 'left': (viewindex * 64) + 'px' }, config.fspeed);
                viewindex++;
            }
            if (maxSel2 > 0) {
                for (i = 1; i <= maxSel2; i++) {
                    $(objbody).find("img:nth-child(" + i + ")").animate({ 'left': (viewindex * 64) + 'px' }, config.fspeed);
                    viewindex++;
                }
            }

            lng_now--;
            if (lng_now < 1) lng_now = objCnt;
        }
    }

    function automove(config) {
        moveLogo(config);
        timer = setTimeout(function() { automove(config) }, config.ftimer);
    }
});
