﻿Type.registerNamespace("cc");

cc.PageSearch = function(element)
{
    cc.PageSearch.initializeBase(this, [element]);
    
    this._searchElement = null;
    this._highlightedElements = null;
    this._currentSelectedIndex = null;
    this._visible = false;
};

cc.PageSearch.prototype =
{
    set_text: function(value)
    {
        $(this.get_element()).find("#page-search-text").val(value);
    },

    show: function()
    {
        $(this.get_element()).show();
        $(this.get_element()).find("#page-search-text").focus();
        this._visible = true;
    },

    hide: function()
    {
        $(this.get_element()).hide();
        $(this._searchElement).removeHighlight();
        this._highlightedElements = null;
        this._currentSelectedIndex = null;
        this._visible = false;
    },
    
    isVisible: function()
    {
        return this._visible;
    },

    select: function(index)
    {
        var element = this._highlightedElements[index];
        $(this._searchElement).scrollTop(element.offsetTop);

        if (index > 0)
        {
            $(this.get_element()).find("#page-search-prev").removeAttr("disabled");
        }
        else
        {
            $(this.get_element()).find("#page-search-prev").attr("disabled", "disabled");
        }
        
        if (index < (this._highlightedElements.length - 1))
        {
            $(this.get_element()).find("#page-search-next").removeAttr("disabled");
        }
        else
        {
            $(this.get_element()).find("#page-search-next").attr("disabled", "disabled");
        }

        if (this._currentSelectedIndex != null)
        {
            $(this._highlightedElements[this._currentSelectedIndex]).removeClass("highlight-selected");
        }
        $(element).addClass("highlight-selected");

        this._currentSelectedIndex = index;
    },
    
    highlight: function()
    {
        var text = $(this.get_element()).find("#page-search-text").val();

        $(this._searchElement).removeHighlight();

        if (text.indexOf(","))
        {
            text = text.replace(", ", ",");
            text = text.replace(" ,", ",");
            
            var a = text.split(",");
            
            for (var i = 0; i < a.length; i++)
            {
                $(this._searchElement).highlight(a[i]);
            }
        }
        else
        {
            $(this._searchElement).highlight(text);
        }

        this._highlightedElements = $(this._searchElement).find(".highlight");

        if (this._highlightedElements.length > 0)
        {
            this.select(0);
        }
    },

    _pageLoad: function()
    {
        this._searchElement = $get("content");
        this._highlightedElements = null;
        this._currentSelectedIndex = null;

        $(this.get_element()).find("#page-search-prev").attr("disabled", "disabled");
        $(this.get_element()).find("#page-search-next").attr("disabled", "disabled");
    },

    initialize: function()
    {
        cc.PageSearch.callBaseMethod(this, "initialize");

        Sys.Application.add_load(Function.createDelegate(this, this._pageLoad));

        var self = this;

        $(this.get_element()).find("#page-search-button").click(function(e)
        {
            self.highlight();
        });

        $(this.get_element()).find("#page-search-prev").click(function(e)
        {
            self.select(self._currentSelectedIndex - 1);
        });

        $(this.get_element()).find("#page-search-next").click(function(e)
        {
            self.select(self._currentSelectedIndex + 1);
        });

        $(this.get_element()).find("#page-search-close").click(function(e)
        {
            self.hide();
        });

        $(this.get_element()).draggable();
    },

    dispose: function()
    {
        cc.PageSearch.callBaseMethod(this, "dispose");
    }
};

cc.PageSearch.registerClass("cc.PageSearch", Sys.UI.Control);
Sys.Application.notifyScriptLoaded();