var Announce = 
{
    data: null,
    container: null,
	description: null,
    header: null,

    datasource: {},

    root: null,
    content: null,
    nav: null,
	ttl: null,

    desc: null,
    descContent: null,
    remember: null,

    current: null,
    prev: null,
	
	links: {},

    init: function( datasource, container, description )
	{
		this.data = document.getElementById( datasource );
		this.container = document.getElementById( container );
		this.description = document.getElementById( description );
        this.header = document.getElementById( 'header' );

        var dataItems = this.data.getElementsByTagName('UL')[0].childNodes;
        var item;         
        for( var i=0; i < dataItems.length; i++ )
        {
            if( dataItems[i].nodeName.toUpperCase() != 'LI' ) continue;
            item = dataItems[i];
            
            var params = item.getAttribute('NAME').split(";");
            
            this.datasource[ item.id ] =  
			{
				id: item.id,
				name: params[0],
				type: params[1],
				color: params[2],
				bgcolor: params[3],
				description: item.getElementsByTagName('DIV')[0].innerHTML,
				descriptionVerticalAlignment: params[4],
				content: item.getElementsByTagName('DIV')[1]
			};
			if( this.current == null ) this.current = item.id;
        }
		
		var cookie = Http.getCookieByName( 'UI.AnnounceCurrent' );
        if( cookie ) this.current = cookie.getValue();
// новая строка       
        this.current = '2945109c-0a00-001e-01f6-023ffc01380e';
    },
    
    build: function()
	{                
        this.root = document.createElement( 'DIV' );
        this.root.id = 'maintoday';        

        this.content = document.createElement( 'DIV' );
        this.content.id = 'AnnounceContent';
        this.buildContent();        

        this.nav = document.createElement( 'UL' );
        this.nav.id = 'AnnounceNav';
        this.buildNav();
        this.root.appendChild( this.nav );

        this.descContent = document.createElement('DIV');
        this.descContent.style.marginBottom = 20 + 'px';
        this.buildDescription();

        this.ttl = document.createElement( 'IMG' );
        this.ttl.id = 'AnnounceTtl';
        this.buildTtl();
        this.root.appendChild( this.ttl );
        
        this.container.appendChild( this.root );

        this.display( null );
    },

    update: function( id )
    {        
        this.current = id;
        this.buildContent();
        this.buildTtl();
        this.buildDescription();
    },

    buildDescription: function()
    {
        var data = this.datasource[ this.current ];
        
        this.description.innerHTML = '';
        this.description.style.height = 'auto';
        this.description.style.backgroundColor = '#' + data.color;
        this.description.parentNode.style.backgroundColor = '#' + data.color;

        this.descContent.innerHTML = data.description;
        
        var table = document.createElement( 'TABLE' );
        var tr = table.insertRow( table.rows.length );
        var cell = null;
        cell = tr.insertCell( 0 );
		
		var cookie = Http.getCookieByName( 'UI.AnnounceCurrent' );


//        this.remember = document.createElement('INPUT');
//        this.remember.id = 'checkoffer';
//        this.remember.type = 'checkbox';
//        this.remember.className = 'checkbox';
//        this.remember.onclick = Announce.saveChoise;
//        cell.appendChild( this.remember );
//		this.remember.checked = ( cookie && cookie.getValue() == this.current );
//
//        cell = tr.insertCell( 1 );
//        cell.style.verticalAlign = 'middle';
//
//        var label = '<LABEL FOR="checkoffer" STYLE="';
//		if( data.color == 'F79239' ) label += 'color:#FCD3B0;';
//		if( data.color == 'B47E68' ) label += 'color:#E1CBC3;';
//		label += '">Начинать работу с этой закладки</LABEL>';
//		cell.innerHTML = label;


        this.description.appendChild( this.descContent );
        this.description.appendChild( table );
        
        if(data.descriptionVerticalAlignment == 'top')
        {
            this.description.style.position = 'relative';
            this.description.style.height = 'auto';
            this.description.style.bottom = 'auto';
            this.description.style.top = '0pt';

            this.description.parentNode.style.height = 'auto';
        }else{ // bottom
            this.description.style.position = '';
            this.description.style.height = 'auto';
            this.description.style.bottom = '';
            this.description.style.top = '';

            this.description.parentNode.style.height = '110px';
        }

    },

    buildNav: function()
    {
        var li, a, data;
		for( i in this.datasource )		        
        {
            data = this.datasource[i]; 
            li = document.createElement('LI');
            a = document.createElement('A');			
            a.id = this.datasource[i].id;
            a.href = '#';
            a.innerHTML = data.name;
            a.style.borderColor = '#' + data.color;            
            a.onclick = function()
            {
                return Announce.display.call( Announce, this );                
            };
			this.links[ this.datasource[i].id ] = a;
            li.appendChild( a );
            this.nav.appendChild( li );
        }
    },
    
    buildTtl: function()
    {
        var data = this.datasource[ this.current ];
        this.header.style.backgroundColor = '#' + data.color;         
        switch( data.color )
        {
            case '686A6C':
                this.ttl.src = '/media/img/ttl-maintoday-1.gif';
                break;
            case 'F79239':
                this.ttl.src = '/media/img/ttl-maintoday-3.gif';
                break;
			case '8C6A4B':
                this.ttl.src = '/media/img/ttl-maintoday-4.gif';
                break;			
			case 'B47E68':
                this.ttl.src = '/media/img/ttl-maintoday-5.gif';
                break;							
            default: //787A97
                this.ttl.src = '/media/img/ttl-maintoday.gif';
                break;
        }        
    },
    
    buildContent: function()
    {
        var data = this.datasource[this.current];
        this.content.innerHTML = '';
        this.content.className = data.color;

        ReadFile.stop();        
        
        switch( data.type )
        {
            case 'image':
                this.content.style.backgroundImage = 'url(' + data.content.getElementsByTagName('IMG')[0].src + ')';
                this.content.innerHTML = '<br/>';

//--
                this.content.style.backgroundColor = "";
				this.content.style.height = "";
				this.content.style.overflow = "";
//--

                break;
            case 'quotations':
                var div = document.createElement('DIV');

                div.className = 'text';
                div.style.borderColor = '#' + data.color;
                div.style.backgroundColor = '#' + data.bgcolor;

//--
				div.style.overflow = "visible";
				div.style.marginTop = "auto";
				div.style.borderWidth = "0px";
				div.style.height = "284px";

                this.content.style.backgroundColor = '#' + data.bgcolor;
				this.content.style.height = "284px";
				this.content.style.overflow = "auto";
//--

                this.content.style.backgroundImage = 'none';
                var MonthNamesRus = new Array("Января","Февраля","Марта","Апреля","Мая","Июня","Июля","Августа","Сентября","Октября","Ноября","Декабря")    
                var date = new Date();
                div.innerHTML = '<h2>Внебиржевая сессия</h2><p><small id="quotation_time">Цены на '+date.getDate()+' '+MonthNamesRus[date.getMonth()].toLowerCase()+' '+date.getFullYear()+', ' + ( ( date.getHours() < 10 ) ? '0'+date.getHours() : date.getHours() ) + ':' + ( (date.getMinutes() < 10) ? '0'+date.getMinutes() : date.getMinutes() ) + '</small></p>' +                                								
                                '<TABLE class="quotation"><THEAD><TR><TD><NOBR>эмитент</NOBR></TD><TD><NOBR>покупка</NOBR></TD><TD><NOBR>продажа</NOBR></TD><TD>объём, шт</TD></TR></THEAD><TBODY id="quotes"><TR><TD><EM>loading...</EM></TD></TR></TBODY></TABLE>' +
                                '<div class="quotationDescription"><p>Все цены приведены в рублях.</p><br/><A HREF="/dic/firm_quote/">Цены в режиме он-лайн</A></div>';
                this.content.appendChild( div );
				
				ReadFile.table = div.getElementsByTagName('TABLE')[0].tBodies[0];
				ReadFile.time = div.getElementsByTagName('SMALL')[0];
                    
                links =
                {
                    'SLAVNEFT': "http://www.troika.ru/doc_eng.jsp?category_id=E4F639C1-6F74-3EF6-9130-00007F000001&document_id=E6EE3D11-6F78-10EE-8EFB-0000FFFF8332"
                };				
				
                ReadFile.start();
                ReadFile.read( data.content.getElementsByTagName('A')[0].href );
				ReadFile.stop();
                break;
            default:
                var div = document.createElement('DIV');
                div.className = 'text';
                div.style.borderColor = '#' + data.color;
                div.style.backgroundColor = '#' + data.bgcolor;
                div.innerHTML = data.content.innerHTML;
                this.content.style.backgroundImage = 'none';

//--
				div.style.overflow = "";
				div.style.marginTop = "";
				div.style.borderWidth = "";
				div.style.height = "";

                this.content.style.backgroundColor = "";
				this.content.style.height = "";
				this.content.style.overflow = "";
//--
                this.content.appendChild( div );

                break;
        }
		this.root.appendChild( this.content );
    },

    display: function( link )
    {
        if( !link ) link = this.links[ this.current ];
		if( !link ) link = this.nav.getElementsByTagName('A')[0];
        link.blur();
        this.current = link.id;

        if( this.prev )
        {
            this.prev.parentNode.style.backgroundImage = 'none';
            this.prev.style.background = 'transparent';
        }
        
        var color = this.datasource[ this.current ].color;        
        switch( color.toUpperCase() )
        {
            case '686A6C':
                link.parentNode.style.backgroundImage = 'url(/media/img/maintoday-menu-1.gif)';                
                break;
            case 'F79239':
                link.parentNode.style.backgroundImage = 'url(/media/img/maintoday-menu-3.gif)';
                break;
			case '8C6A4B':
                link.parentNode.style.backgroundImage = 'url(/media/img/maintoday-menu-4.gif)';
                break;	
			case 'B47E68':
                link.parentNode.style.backgroundImage = 'url(/media/img/maintoday-menu-5.gif)';
                break;					
            default:
                link.parentNode.style.backgroundImage = 'url(/media/img/maintoday-menu-2.gif)';
        }
        link.style.background = '#' + color;

        this.prev = link;

        this.update( this.current );

        return false;
    },
	
	saveChoise: function( e )
	{
		var expires = new Date();		
		expires.setFullYear( expires.getFullYear() + ( ( this.checked ) ? 1 : -1 ) );
		var cookie = new Http.Cookie( 'UI.AnnounceCurrent', Announce.current, expires );
		cookie.save();
		this.blur();
	}
};

