/**
 * Show More of QPLinks
 */
var qpMoreTimer = null;
jQuery("#qpKeys").show();
jQuery("#qpMore").before(jQuery("#linkMore").clone().attr('id', 'linkMoreN').mouseover(function(){
    var qpPos = $('#linkMoreN').position();
    var viewWidth = ((document.documentElement && (document.documentElement.clientWidth + document.documentElement.scrollLeft)) || (document.body && (document.body.clientWidth + document.body.scrollLeft)))
    if (qpPos.left + $('#qpMore').width() > viewWidth) {
        $('#qpKeys').css({
            'position': 'absolute',
            'right': '-53px'
        });
    }
    else {
        $('#qpKeys').removeAttr('style');
    }
    $('#qpMore').css({
        'left': qpPos.left + 'px',
        'top': qpPos.top + 'px'
    }).show()
})).hover(function(){
    clearTimeout(qpMoreTimer);
}, function(){
    clearTimeout(qpMoreTimer);
    qpMoreTimer = setTimeout(function(){
        jQuery("#qpMore").hide()
    }, 400)
});
/**
 * Extended Search for Search and List
 */
(function(){
    jQuery('.refineCategory').css('height', jQuery('.refineCategory').height());
    if (jQuery('#searchExtend1,#searchExtend3').length === 0 || jQuery('#searchExtend1,#searchExtend3').find('#searchExtend2').length === 0) {
        return;
    }
    var searchExtendTimer = null;
    var flagClearTimerSelect4IE = false;
    var searchExtendOverFunc = function(){
        clearTimeout(searchExtendTimer);
        var seOverF = function(){
            jQuery('#searchExtend2').show();
        };
        searchExtendTimer = setTimeout(seOverF, 300);
    };
    var searchExtendOutFunc = function(){
        clearTimeout(searchExtendTimer);
        var seOutF = function(){
            jQuery('#searchExtend2').hide();
        };
        if (!flagClearTimerSelect4IE) {
            searchExtendTimer = setTimeout(seOutF, 300);
        }
    };
    jQuery('#searchExtend1').hover(searchExtendOverFunc, searchExtendOutFunc);
    // 4 IE
    if (!+'\v1') {
        // < IE7
        if (!window.XMLHttpRequest) {
            var zMaskIframe = '<iframe class="zMaskIframe" style="position:absolute;z-index:-1;left:0;top:0;width:100%;height:100px;filter:mask();display:none;" frameborder="0"><\/iframe>';
            jQuery('div[id^="lessviewid_"]').append(zMaskIframe);
            var reMaskFunc = window.Show_TabADSMenu;
            if (!reMaskFunc) {
                reMaskFunc = function(){
                }
            }
            window.Show_TabADSMenu = function(){
                reMaskFunc(arguments[0], arguments[1]);
                setTimeout(function(){
                    jQuery('.zMaskIframe').each(function(){
                        var tPa = jQuery(this).parent();
                        jQuery(this).css({
                            'display': 'block',
                            'height': tPa.outerHeight() + 'px',
                            'width': tPa.outerWidth() + 'px',
                            'filter': 'mask()'
                        });
                    });
                }, 10);
            };
        }
        var clearBodyTimerOutSelect4IE = function(evt){
            flagClearTimerSelect4IE = false;
            clearTimeout(searchExtendTimer);
            if (jQuery(evt.target).parents('#searchExtend1').length === 0) {
                jQuery('#searchExtend1').trigger('mouseout');
            }
        };
        var clearTimerOutSelect4IE = function(){
            jQuery(document.body).one('click', clearBodyTimerOutSelect4IE);
        };
        var clearTimerOverSelect4IE = function(){
            if (!flagClearTimerSelect4IE) {
                flagClearTimerSelect4IE = true;
                setTimeout(clearTimerOutSelect4IE, 50);
            }
        };
        jQuery('#searchExtend1 select').click(clearTimerOverSelect4IE);
    }
})();
/**
 * Select Simulator
 */
var selectMenuTimer = null;
jQuery('.select').hover(function(){
    clearTimeout(selectMenuTimer);
    var qpPos = $(this).position();
    var selectMenu = $(this).siblings('.selectMenu');
    var maxWidth = $(this).innerWidth();
    if (maxWidth < selectMenu.width()) {
        $(this).css('padding-right', parseInt($(this).css('padding-right')) + selectMenu.width() - maxWidth);
        maxWidth = selectMenu.width();
    }
    selectMenu.css({
        'width': maxWidth + 'px',
        'left': qpPos.left + 'px',
        'top': (qpPos.top + $(this).outerHeight(true)) + 'px'
    }).show();
}, function(){
    clearTimeout(selectMenuTimer);
    selectMenuTimer = setTimeout(function(){
        jQuery('.selectMenu').hide();
    }, 200);
});
jQuery('.selectMenu').hover(function(){
    clearTimeout(selectMenuTimer);
}, function(){
    clearTimeout(selectMenuTimer);
    selectMenuTimer = setTimeout(function(){
        jQuery('.selectMenu').hide();
    }, 200);
});
/**
 * TextSuggest and CorrelateWords
 */
(function(){
    jQuery('head').append('<link rel="stylesheet" type="text/css" href="/style/suggest_text.css" />');
    jQuery('.textsuggest').each(function(){
        window.TextSuggest.initialize(this, (window.MIC_SRVRNM || '') + '/suggest');
    });
    var TS = window.TextSuggest;
    if (window.noCorrelateWords || !TS || !TS.textInput) {
        return;
    }
    var cwClass = 'correlateWords';
    var correlateWords = jQuery('<div class="titleborder ' + cwClass + '"><h2 class="title">Related Searches</h2><div class="relpadding"></div></div>');
    var qKind = TS.getRequestType();
    var qWord = jQuery.trim(TS.textInput.val());
    switch (qKind) {
        case 1:
            qKind = 4;
            break;
        case 0:
        default:
            qKind = 3;
    }
    if (qWord != '' && !!jQuery('.' + cwClass)) {
        jQuery.getJSON(TS.url, {
            param: qWord,
            kind: qKind,
            id: cwClass,
            count: TS.options.count,
            ignoreCase: TS.options.ignoreCase,
            matchAnywhere: TS.options.matchAnywhere,
            time: new Date().getTime()
        }, function(suggestions){
            if (!suggestions || suggestions.length == 0) {
                return false;
            }
            correlateWords.insertBefore('#pager');
            var TI = TS.textInput;
            var iUrl = TI.parent('form').attr('action');
            iUrl += (iUrl.match(/\?/) ? '&' : '?') + TI.parent('form').serialize();
            jQuery.each(suggestions, function(i, n){
                correlateWords.find('div').append(jQuery('<a href="' + iUrl.replace(/(word=).+?(&)/i, '$1' + encodeURIComponent(n).replace(/%20/g, '+') + '$2') + '">' + n + '</a>').click(function(){
                    var nn = TI.val();
                    TI.val(n).parent('form').submit();
                    TI.val(nn);
                    return false;
                }));
            });
            return true;
        });
    }
})();
/**
 * ScrollLayer Player
 */
(function(){
    //
    var Speed = 10;
    var Space = 7;
    var PageWidth = 955;
    var ScrollWidth = 191;
    var fill = 0;
    var MoveLock = false;
    var MoveTimer;
    var Comp = 0;
    var AutoPlaySpeed = 5000;
    var AutoPlayTimer = null;
    var AutoPlayEnabled = false;
    //
    var AutoPlay = function(){
        clearInterval(AutoPlayTimer);
        AutoPlayTimer = setInterval(function(){
            scrollLayerGoDown();
            scrollLayerStopDown();
        }, AutoPlaySpeed);
    };
    var scrollLayerGoUp = function(){
        if (MoveLock) {
            return;
        }
        clearInterval(AutoPlayTimer);
        MoveLock = true;
        MoveTimer = setInterval(scrollLayerScrUp, Speed);
    };
    var scrollLayerStopUp = function(){
        clearInterval(MoveTimer);
        if (fullLayer.scrollLeft() % ScrollWidth - fill != 0) {
            Comp = fill - (fullLayer.scrollLeft() % ScrollWidth);
            CompScr();
        }
        else {
            MoveLock = false;
        }
        if (AutoPlayEnabled) {
            AutoPlay();
        }
    };
    var scrollLayerScrUp = function(){
        if (originLayer.outerWidth() <= PageWidth) {
            return;
        }
        if (fullLayer.scrollLeft() <= 0) {
            fullLayer.scrollLeft(fullLayer.scrollLeft() + originLayer.outerWidth());
        }
        fullLayer.scrollLeft(fullLayer.scrollLeft() - Space);
    };
    var scrollLayerGoDown = function(){
        clearInterval(MoveTimer);
        if (MoveLock) {
            return;
        }
        clearInterval(AutoPlayTimer);
        MoveLock = true;
        scrollLayerScrDown();
        MoveTimer = setInterval(scrollLayerScrDown, Speed);
    };
    var scrollLayerStopDown = function(){
        clearInterval(MoveTimer);
        if (fullLayer.scrollLeft() % ScrollWidth - fill != 0) {
            Comp = ScrollWidth - fullLayer.scrollLeft() % ScrollWidth + fill;
            CompScr();
        }
        else {
            MoveLock = false;
        }
        if (AutoPlayEnabled) {
            AutoPlay();
        }
    };
    var scrollLayerScrDown = function(){
        if (originLayer.outerWidth() <= PageWidth) {
            return;
        }
        if (fullLayer.scrollLeft() >= originLayer.outerWidth()) {
            fullLayer.scrollLeft(fullLayer.scrollLeft() - originLayer.outerWidth());
        }
        fullLayer.scrollLeft(fullLayer.scrollLeft() + Space);
    };
    var CompScr = function(){
        var num;
        if (Comp == 0) {
            MoveLock = false;
            return;
        }
        if (Comp < 0) {
            if (Comp < -Space) {
                Comp += Space;
                num = Space;
            }
            else {
                num = -Comp;
                Comp = 0;
            }
            fullLayer.scrollLeft(fullLayer.scrollLeft() - num);
            setTimeout(CompScr, Speed);
        }
        else {
            if (Comp > Space) {
                Comp -= Space;
                num = Space;
            }
            else {
                num = Comp;
                Comp = 0;
            }
            fullLayer.scrollLeft(fullLayer.scrollLeft() + num);
            setTimeout(CompScr, Speed);
        }
    };
    //
    var fullLayer = jQuery('#prod-slbottom-f').find('.layers').wrap('<div class="outerWrap"></div>').parent().css({
        'width': PageWidth + 'px',
        'overflow': 'hidden'
    });
    var originLayer = jQuery('#prod-slbottom-f').find('.item');
    var controlButtons = jQuery('#prod-slbottom-f').find('.scroll').find('img');
    if (originLayer.outerWidth() >= PageWidth) {
        originLayer.parent().append(originLayer.clone());
        fullLayer.scrollLeft(fill);
        if (AutoPlayEnabled) {
            fullLayer.hover(function(){
                clearInterval(AutoPlayTimer);
            }, AutoPlay);
            AutoPlay();
        }
        controlButtons.addClass('handHover');
        controlButtons.eq(0).mousedown(scrollLayerGoUp).mouseup(scrollLayerStopUp).mouseout(scrollLayerStopUp);
        controlButtons.eq(1).mousedown(scrollLayerGoDown).mouseup(scrollLayerStopDown).mouseout(scrollLayerStopDown);
    }
})();
