if(!GOOM.Components.promoTunes) {
    GOOM.Components.promoTunes = {
        imgInt : null,
        arrowTO : null,
        leaveFn : function() {
            var me = GOOM.Components.promoTunes;
            clearInterval(me.imgInt);
            GOOM.Navigation.Events.onPageLeave.unsubscribe(me.leaveFn);
        },
        request: function() {
            var menuDiv = byId('promoTunesRightMenu'),
                leftImgDiv = byId('promoTunesLeftImageContainer'),
                promoTunesDiv = byId('promoTunes'),
                promoTunesArrowDiv = byId('promoTunesArrowDown'),
                promoTunesArrowImg = $('#promoTunesArrowDownImg'),

                parseRequest = function(data) {
                    var r = data.results,
                        cntImg = 0,
                        menuTpl = '',
                        leftTpl = '',
                        menuImgTpl = '<a href="%{href}" %{onclick}>' +
                                       '<img onerror="defaultImg(this, \'/img/promoTunesDefaultMenuElement.jpg\');" src="%{src}" class="promoTunesRightMenuElement" style="top: %{top};" id="%{id}">' +
                                     '</a>',
                        leftImgTpl = '<a href="%{href}" %{onclick}>' +
                                       '<img onerror="defaultImg(this, \'/img/promoTunesDefaultLeftImage.jpg\');" src="%{src}" class="promoTunesLeftImage" style="z-index: %{zIndex};" id="%{id}">' +
                                     '</a>',
                        today = new Date(),
                        now = Math.round(today.getTime() / 1000),
                        tzOffset = today.getTimezoneOffset() * 60;

                    if(r.length === 1) { r[2] = r[1] = r[0]; }
                    if(r.length === 2) { r[2] = r[0]; r[3] = r[1]; }

                    for(var i = 0, len = r.length; i < len; ++i) {
                        if(r[i].active) {
                            var start = r[i].startTimestamp + tzOffset,
                                end = r[i].endTimestamp + tzOffset;

                            if((!start || now > start) && (now < end || !end)) {
                                var onclickFn = ((r[i].urlType == 'internal') ? 'onclick="GOOM.Navigation.load(this.href); return false;"' : 'target="_blank"');
                                menuTpl += replace(menuImgTpl, {
                                    href : r[i].url,
                                    onclick : onclickFn,
                                    src : window.getASSET() + '/' + r[i].imageSmall,
                                    top : ((cntImg * 67) - 67) + 'px',
                                    id : 'promoTunesRightMenuElement' + cntImg});

                                leftTpl += replace(leftImgTpl, {
                                    href : r[i].url,
                                    onclick : onclickFn,
                                    src : window.getASSET() + '/' + r[i].image,
                                    zIndex : 0,
                                    id : 'promoTunesLeftImage' + cntImg});

                                ++cntImg;
                            }
                        }
                    }

                    menuDiv.innerHTML = menuTpl + replace(menuImgTpl, {
                        href : '',
                        onclick : 'onclick="return false;"',
                        src : '',
                        top : ((cntImg * 67) - 67) + 'px',
                        id : 'promoTunesRightMenuElement' + cntImg
                    });
                    leftImgDiv.innerHTML = leftTpl;
                    byId('promoTunesLeftImage' + ((cntImg > 4) ? 4 : 0)).style.display = 'block';
                };

            GOOM.util.Request('GET', '/default/list/promo-tunes', {
                success : function(data) {
                    parseRequest(data);

                    var menuEls = $('img.promoTunesRightMenuElement'),
                        elNum = menuEls.length;

                    HTMLImageElement.prototype.setLastMenuEl = function(href, onclick, src, top) {
                        this.parentNode.href = href;
                        this.parentNode.onclick = onclick;
                        this.src = src;
                        if(top) { this.style.top = top; }
                    }

                    var lastElIndex = elNum - 1,
                        lastEl = menuEls[lastElIndex],
                        firstEl = menuEls[0],
                        prevLastEl = firstEl,
                        duration = 300,
                        leftImgs = byId('promoTunesLeftImageContainer').getElementsByTagName('img'),
                        leftImgsNum = leftImgs.length,
                        curImgIndex = ((leftImgsNum > 4) ? 4 : 0),
                        callNum = 0,
                        animationMutex = false,
                        nextPict = function() {
                            menuEls.animate({
                                top: '+=67'
                            }, duration, function() {
                                if(++callNum == elNum) {
                                    var prevLastElIndex = lastElIndex;
                                    if(--lastElIndex < 0) {
                                        lastElIndex = elNum - 1;
                                    }
                                    prevLastEl = menuEls[prevLastElIndex];
                                    lastEl = menuEls[lastElIndex];
                                    prevLastEl.setLastMenuEl(lastEl.parentNode.href, lastEl.parentNode.onclick, lastEl.src, '-67px');
                                    callNum = 0;
                                }
                            });
                            var prevImgIndex = curImgIndex;
                            if(--curImgIndex < 0) {
                                curImgIndex = leftImgsNum - 1;
                            }
                            leftImgs[prevImgIndex].style.zIndex = 0;
                            leftImgs[curImgIndex].style.zIndex = 1;
                            $('#' + leftImgs[curImgIndex].id).fadeIn(duration, function() {leftImgs[prevImgIndex].style.display = 'none';} );
                        },
                        imgIntFn = function() {
                            return setInterval(nextPict, 5000);
                        };

                    lastEl.setLastMenuEl(firstEl.parentNode.href, firstEl.parentNode.onclick, firstEl.src);

                    GOOM.Components.promoTunes.imgInt = imgIntFn();

                    promoTunesDiv.onmouseover = function(e) {
                        clearInterval(GOOM.Components.promoTunes.imgInt);
                    }
                    promoTunesDiv.onmouseout = function(e) {
                        GOOM.Components.promoTunes.imgInt = imgIntFn();
                    }

                    menuDiv.onmouseover = function() {
                        clearTimeout(GOOM.Components.promoTunes.arrowTO);
                        promoTunesArrowImg.fadeIn('slow');
                    }
                    menuDiv.onmouseout = function() {
                        GOOM.Components.promoTunes.arrowTO = setTimeout(function() {
                            promoTunesArrowImg.fadeOut('slow');
                        }, 500);
                    }

                    promoTunesArrowDiv.onmouseover = function() {
                        clearTimeout(GOOM.Components.promoTunes.arrowTO);
                    }
                    promoTunesArrowDiv.onclick = function() {
                        if (!animationMutex) {
                            animationMutex = true;
                            nextPict();
                            setTimeout(function() {
                                animationMutex = false;
                            }, duration * 1.5);
                        }
                    }
                }
            });
        }
    }
}
