var com_rivetlogic_init = {

    initSearch : function() {
        $("#searchVisual").data('loading', $("#searchVisual").html());
        $('#search input').unbind('keydown').keypress(function(event) {
            if (event.which == 13) {
                // Give ENTER to hovered element if there is one.
                if ($('#searchVisual a.hover').size() > 0) {
                    event.preventDefault();
                } else {
                    $('#search').submit();
                }
            }
            if (event.which >= 32) {
                // Regular key presses kick off the ajax search
                clearTimeout(this.searching);
                this.searching = setTimeout(function() {
                    var input = $('#search input');
										if (input.val().length <= 1) return; // at least 2 characters must be typed
                    var popup = $('#searchVisual');
                    popup.html(popup.data('loading')).show().
                            load(encodeURI('/ajax/search-visual?q=' + input.val()));
                }, 500);
            }
        });
    },

    initVideo : function() {
        $('.videoBlock').each(function(index, dom) {
            fnVideoInline(dom);
            dom.showVideoInline = $(this).data('events')['click']['0']['handler'];
            $(dom).unbind('click');
            fnVideoOverlay(dom);
            dom.showVideoOverlay = $(this).data('events')['click']['0']['handler'];
            $(dom).unbind('click');
        });
        $('a.close,#modShade').die('click');

        $('.videoBlock').live('click', function(event) {
            var href = $(this).attr('href');
            if (href.indexOf('/inline/') != -1) {
                this.showVideo = this.showVideoInline;
            } else if (href.indexOf('/overlay/') != -1) {
                this.showVideo = this.showVideoOverlay;
            }
            this.showVideo(event);
        });

        $('a.close,#modShade').live('click', function(event) {
            event.preventDefault();
            $('#modShade').animate({opacity: '0'}, 200, function() {
                $(this).remove();
            });
            $('#videoOverlay').remove();
            $('#videoInline').remove();
            // Rest z-index for Tech Menu on Technology Detail Template
            // Need to ask Hello about this part.
            if ($('#techDetail').length) {
                $('#techMenu .expandable').css('z-index', '40');
            }
        });
    },

    initAll : function() {
        com_rivetlogic_init.initSearch();
        com_rivetlogic_init.initVideo();
    }
}
$('document').ready(function() {
    com_rivetlogic_init.initAll();
});
