﻿function RealtyOfDay(servicePath, scrollerId)
{
	this._servicePath = servicePath;
	this._scrollerId = scrollerId;
	this._items = $("#" + scrollerId + " .items");

	this.Init = function()
	{
		var self = this;
		$(".item", this._items).live('click', function() { self.ShowObject(this); });

		self.ShowObject(null);
	}

	this.ShowObject = function(item) {
		var self = this;
		var first = false;

		if (!item) {
			var items = $('.item', this._items);
			item = items.get(0);
			first = true;
		}

		if (this._item) this._items.append(this._item);
		this._item = item;

		item = $(item);
		item.remove();

		if (first) 
		{
			var objects = $('.realtyOfDay .buy-objects');
			if (objects.css('visibility') == 'hidden') objects.css('visibility', 'visible');
			return;
		}

		var data = $('table', item).metadata();

		if (isNaN(data.id)) return;

		$('.realtyOfDay .buy-object').hide();

		Sys.Net.WebServiceProxy.invoke(this._servicePath, "GetRealtyOfDay", false,
			{ id: data.id }, Function.createDelegate(this, this.ShowObjectCallBack), null, { details: data.url, price: data.price });
	}

	this.ShowObjectCallBack = function(result, context)
	{
		var parent = $('.realtyOfDay .content');

		$('a', parent).attr('href', context.details);
		$('.img', parent).attr('src', result.ImageUrl);

		$('.priceText', parent).text(context.price);
		$('.headerText', parent).html(result.Header.newLineToBR());
		$('.topText', parent).html(result.Top.newLineToBR());
		$('.middleText', parent).text(result.Middle);
		$('.bottomText', parent).text(result.Bottom);

		$('.realtyOfDay .buy-object').show();
	}
}

