if (! ('indexOf' in Array.prototype)) {
    if (! ($('#news').length > 0)) {
        Array.prototype.indexOf = function(find, i) {
            if (i === undefined) i = 0;
            if (i < 0) i += this.length;
            if (i < 0) i = 0;
            for (var n = this.length; i < n; i++)
            if (i in this && this[i] === find)
            return i;
            return - 1;
        };
    }
}
var fi = fi || {};
fi.common = fi.common || {};
fi.common.Header = function() {
    this.root = $('#main-header');
    this.pageContainer = $("#cse").next();
    this.init();
};
fi.common.Header.prototype = {
    init: function() {
        $(window).bind("customSearchReady", $.proxy(this.onSearchReady, this));
        if (navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
            this.root.css("position", "static");
            $('.header-push').height(0);
        }
        this.setNavState();
    },
    setNavState: function() {
        $("ul li", this.root).removeClass("selected");
        $("ul li a", this.root).each(function(i, val) {
            if (location.pathname.indexOf($(val).attr("href")) >= 0) {
                $(val).parent().addClass("selected");
                return;
            }
        });
    },
    onSearchReady: function() {
        if ($("#cse #close-btn").length <= 0) {
          $("#cse").prepend("<div id='close-btn' style='display: none;'></div>");
        }

        $(".gsc-input input", this.root).focus(function() {
            $(this).stop().animate({
                width: 211
            },
            150);
            if ($(this).val() == "Search") {
                $(this).val("")
            }
        });
        $(".gsc-input input", this.root).blur(function() {
            $(this).stop().animate({
                width: 171
            },
            150);
            if (!$(this).val().length) {
                $(this).val("Search");
            }
        });
        var self = this;
        $(".gsc-input input", this.root).keypress(function(e) {
            if (e.which == 13 && $(this).val() != "Search" && $(this).val()) {
                self.pageContainer.fadeOut(function() {
                    $(".gsc-input input", this.root).trigger("blur");
                    $("#close-btn").click(function() {
                        $("#cse").fadeOut(function() {
                          self.pageContainer.fadeIn();
                        });
                    });
                    if (!$("#close-btn").is(":visible")) $("#close-btn").show();
                    $("#cse").fadeIn();
                });
            }
        });
    }
}
google.load('search', '1', {
    language: 'en'
});
google.setOnLoadCallback(function() {
    var customSearchControl = new google.search.CustomSearchControl('001839184402242818998:uocpr0ofsti');
    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    var options = new google.search.DrawOptions();
    options.setSearchFormRoot('cse-search-form');
    customSearchControl.draw('cse', options);
    $(".gsc-input input", this.root).val("Search");
    $(window).trigger("customSearchReady");
},
true);
