﻿if(typeof EasySite == "undefined" || EasySite == null) EasySite = { };
if(!EasySite.Controls) EasySite.Controls = { Items : [] };

EasySite.Controls.HorizonPane = Class.create();
EasySite.Controls.HorizonPane.prototype = 
{
    initialize : function(CInfoContainer, ArticleAbstractContainer, ArticleLink)
    {
        this.CInfoContainer = $(CInfoContainer);
        this.ArticleAbstractContainer = $(ArticleAbstractContainer);
        this.ArticleLink = $(ArticleLink);
        this.SelectedItem = null;
        this.CInfoArticleTds = null;
        
        this.ArticleTdMouseEnterHandler = this.ArticleTdMouseEnterHandler.bind(this);
        this.ArticleTdClickHandler = this.ArticleTdClickHandler.bind(this);
        this.SelectFirstPane = this.SelectFirstPane.bind(this);
        
        
        this.CssClasses = { Item : "Item", SelectedItem : "SelectedItem" };
        
        this.InitCInfoContainer();
        setTimeout(this.SelectFirstPane, 500);
    },
    
    InitCInfoContainer : function()
    {
        try
        {
            var CInfoTable = this.CInfoContainer.getElementsByTagName("TABLE")[0];
            var CInfoArticleTds = CInfoTable.getElementsByTagName("TD");
            
            if(CInfoArticleTds.length > 0) 
            {
                this.CInfoArticleTds = CInfoArticleTds;
                Event.observe(this.ArticleLink, "click", this.ArticleTdClickHandler);
            }
            
            for(var index = 0; index < CInfoArticleTds.length; index++)
            {
                var ArticleTd = $(CInfoArticleTds[index]);
                Event.observe(ArticleTd, "mouseenter", this.ArticleTdMouseEnterHandler);
                Event.observe(ArticleTd, "click", this.ArticleTdClickHandler);
                ArticleTd.className = this.CssClasses.Item;
            }
        }
        catch(Exception)
        {}
    },
    
    ArticleTdMouseEnterHandler : function(e)
    {
        if(this.SelectedItem)
        {
            this.SelectedItem.className = this.CssClasses.Item;
        }
        
        var CInfoArticleTd = e.srcElement;
        CInfoArticleTd.className = this.CssClasses.SelectedItem;
        this.SelectedItem = CInfoArticleTd;
        
        try
        {
            var Spans = this.SelectedItem.getElementsByTagName("SPAN");
               
            for(var index = 0; index < Spans.length; index++)
            {
                var Span = Spans[index];
                if(Span.title == "Abstract")
                {
                    this.ArticleAbstractContainer.innerHTML = Span.innerHTML;
                }
            }
        }
        catch(Exception)
        {}
    },
    
    ArticleTdClickHandler : function(e)
    {
        if(this.SelectedItem)
        {
            try
            {
                var Links = this.SelectedItem.getElementsByTagName("A");
                for(var index = 0; index < Links.length; index++)
                {
                    if(Links[index].innerText == Links[index].innerHTML)
                    {
                        if(Links[index].getAttribute("Target") == "_blank")
                        {
                            window.open(Links[index].getAttribute("href"), "_blank", "");
                        }
                        else
                        {
                            window.location = Links[index].getAttribute("href");
                        }
                        
                        e.returnValue = false;
                        break;
                    }
                }
            }
            catch(Exception)
            {}
        }
    },
    
    SelectFirstPane : function()
    {
        if(this.CInfoArticleTds)
        {
            var e = {srcElement : this.CInfoArticleTds[0] };
            this.ArticleTdMouseEnterHandler(e);
        }
    }
};

