﻿/**
 * Контрол для показа списка новостей на главной странице
 * (Использует паттерн MS Ajax Component. Будет доступен через $find)
 */
function NewsControl()
{
	//Стандартный интерфейс для компонента
	NewsControl.initializeBase(this);
	//ID элемента, в котором находится данный контрол
	this.containerElementID = null;
	this.newsList = null;
};
NewsControl.prototype =
{
    get_containerElementID: function()
    {
        return this.containerElementID;
    },
    set_containerElementID: function(value)
    {
        this.containerElementID = value;
    },
    /**
    * Устанавливает список новостей
    * @param {Object} value Значение цены
    */
    set_newsList: function(value)
    {
        var maxRegionHeight = 270;
        this.newsList = value;
        var containerID = this.containerElementID;
        var container = $("#" + this.containerElementID);
        container.empty();
        var addHeight = 0;
        if (container.height() > 0)
        {
            addHeight = container.height();
        }
        for (var i = 0; i < this.newsList.length; i++)
        {
            var item = this.newsList[i];
            var spanDate = $("<span></span>").addClass("date").text(item.Date);
            var newsHref = $("<a></a>").attr("title", "Перейти к новости").attr("href", item.Url).text(item.Title);
            var p = $("<p></p>");
            p.append(newsHref);
            container.append(spanDate).append(p);
            if ($("#newsContainer").height() - addHeight > maxRegionHeight)
            {
                $('p:last-child', container).remove();
                $('span:last-child', container).remove();
                break;
            }
        }
    },
    get_newsList: function()
    {
        return this.newsList;
    }
};

// JSON object that describes all properties, events, and methods of this component that should
// be addressable through the Sys.TypeDescriptor methods, and addressable via xml-script.
NewsControl.descriptor = {
properties: [
	{ name: 'containerElementID', type: String }, 
	{ name: 'newsList', type: Object}]
};
NewsControl.registerClass('NewsControl', Sys.Component);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();