﻿/* jQuery staticQuery */
; (function($) { $.staticQuery = function(a, b) { if ($.staticQuery._cache[a] === undefined) { if ((b = ((b === undefined) ? $(a) : b)).length === 0) { return b } else { $.staticQuery._cache[a] = b } }; return $.staticQuery._cache[a] }; $.extend($.staticQuery, { _cache: {}, clean: function(a) { if (a === undefined) { if ($.staticQuery._cache === {}) { return false }; $.staticQuery._cache = {}; return true }; if ($.staticQuery._cache[a] !== undefined) { delete $.staticQuery._cache[a]; return true }; return false } }) })(jQuery);

// Aplication
;(function($, j) {

    var startIndex = 1;
    var order = 'published';
    var maxResults = 10;

    // setVideo
    $.setVideo = function(hash) {
        j('#content .section').ScroolTo(500, function() {
            j('#content .section').attr('id', hash);
            window.location.href = rootUrl + 'noar/#v=' + hash;
        });

        $.ajax({
            type: "POST",
            data: ({ hash: hash }),
            url: rootUrl + "noar/view.aspx",
            success: function(data) {
                j('#video-highlight')
                    .html(data)
                    .fadeIn();
                $.bindVideoClick();
            }
        });
    };
    // End setVideo

    // searchVideos
    $.searchVideo = function(start, order, clean) {
        var keyword = j('.text-search[type="text"]').val();
        
        if(keyword) {
            _gaq.push(['_trackEvent', 'no-ar', 'filter', ''+ keyword +'']);
        }
        
        j('.ajax-loader').show();
        j('ul.old-videos').fadeTo('fast', .5).css('cursor', 'progress');

        if (clean) {
            startIndex = 1;
        };

        $.ajax({
            type: "POST",
            data: ({ startIndex: startIndex, order: order, keyword: keyword }),
            url: rootUrl + "noar/search.aspx",
            success: function(data) {
                if (clean) {
                    j('ul.old-videos').html('');
                };
                j('ul.old-videos')
                    .append(data)
                    .stop(1, 1)
                    .slideDown()
                    .removeAttr('style');
                $.bindVideoClick();
                j('.ajax-loader').hide();

                var $data = $(data).filter('li');
                if ($data.size() < 10) {
                    $.seeMore('hide');
                    // alert('Todos os registros carregados');
                };
            }
        });
    };
    // end searchVideos

    // bindVideoClick
    $.bindVideoClick = function() {
        $.seeMore('show');

        $("a.showVideo").unbind('click').click(function() {
            var $this = $(this);
            var $li = $this.closest('li');
            var hash = $li.attr('id');
            $.setVideo(hash);
            return false;
        });

        $("a.searchTags").unbind('click').click(function() {
            var value = $(this).attr('title');
            $("input.text-search[type='text']").val(value);
            $.searchVideo(startIndex, 'relevance', true);

            j('#content h4:eq(2)').ScroolTo(500);
            return false;
        });
    };
    // end bindVideoClick

    // seeMore
    $.seeMore = function(type) {
        var $button = $("a.see-more");
        var type = (type === undefined) ? 'show' : type;
        if (type == 'show') {
            $button
                .removeAttr('style')
                .unbind('click')
                .click(function() {
                    startIndex += maxResults;
                    $.searchVideo(startIndex, order);
                    return false;
                });
        } else {
            $button
                .unbind('click')
                .click(function() {
                    return false;
                })
                .fadeTo('fast', .5)
                .css('cursor', 'default');
        };
    };

    $.fn.ScroolTo = function(time, fn) { var time = time || 2000, fn = fn || null, that = $(this); if (that.length) { $('html,body').animate({ scrollTop: that.offset().top }, time, fn); }; };

    $(window).ready(function() {
        var hash = window.location.hash.match(/v=.*(&|$)/);
        if (hash) {
            hash = hash[0].replace('v=', '');
            $.setVideo(hash);
        };

        $.bindVideoClick();

        $("a.order").click(function() {
            $("a.order").removeClass('selected');
            order = $(this).attr('rel');
            $(this).addClass('selected');

            $.searchVideo(startIndex, order, true);
            return false;
        });

        $("input.text-search").keypress(function(event) {
            if (event.which == '13') {
                $.searchVideo(startIndex, 'relevance', true);
                return false;
            };
        });
        $("input.text-search[type='submit']").click(function() {
            $.searchVideo(startIndex, 'relevance', true);
            return false;
        });
    });

})(jQuery, jQuery.staticQuery);
