if (!Finam) { var Finam = {}; }

Finam.Forex = {
	calendar: null,
	tabs: null,
	initialize: function() {
		if ($('forex-calendar') != null) {
			this.calendar = new Finam.Forex.Calendar($('forex-calendar'));
		}
		if ($('forex-tabs') != null) {
			this.tabs = new Finam.Forex.Tabs($('forex-tabs'));
		}
	}
};

Finam.Forex.Calendar = Class.create({
	initialize: function(elm) {
		this.influence = null;
		var d = this.dom = {};
		var e = d.elm = elm;
		d.table = e.select('TABLE.light')[0];
		d.tbody = d.table.select('TBODY')[1];
		d.preloader = d.table.select('.preloader')[0];
		d.form = e.select('FORM')[0];
		var c = this.controls = {};
		c.date = new Finam.Forex.Calendar.DropDown(d.table.select('TH.date')[0].select('.dropdown')[0], {
				defaultValue: 'day',
				defaultOption: 'Дата',
				select: function(calendar) {
					if (calendar != this.calendar) {
						this.calendar = calendar;
						this.load();
					}
				}.bind(this)
			})
				.create();
		c.country = new Finam.Forex.Calendar.DropDown($('forex-calendar-country-dropdown'), {
				defaultValue: 0,
				defaultOption: '(все страны)',
				select: function(country) {
					country = parseInt(country);
					if (country != this.country) {
						this.country = country;
						this.load();
					}
				}.bind(this)
			})
				.create();
		c.lang = new Finam.Forex.Calendar.DropDown($('forex-calendar-lang-dropdown'), {
				defaultValue: 'ru',
				defaultOption: '(язык)',
				select: function(lang) {
					if (lang != this.lang) {
						this.lang = lang;
						this.load();
					}
				}.bind(this)
			})
				.create();
		// data
		this.calendar = d.form.elements['calendar'].value;
		this.country = parseInt(d.form.elements['country'].value);
		this.lang = d.form.elements['lang'].value;
		this.data = {};
		if (document.location.hash.length > 0) {
			var hash = document.location.hash.replace(/\#/, '').split('&');
			var calendar = null;
			var country = null;
			for (var i = 0; i < hash.length; i++) {
				var hashData = hash[i].split('=');
				if (hashData.length == 2) {
					switch(hashData[0]) {
						case 'calendar':
							calendar = hashData[1];
							break;
						case 'country':
							country = parseInt(hashData[1]);
							break;
						case 'lang':
							lang = hashData[1];
							break;
					}
				}
			}
			country = (country == null) ? this.country : country;
			calendar = (calendar == null) ? this.calendar : calendar;
			if ((this.country != country) || (this.calendar != calendar)) {
				this.calendar = calendar;
				this.country = country;
				this.load();
			}
		}
		c.date.setValue(this.calendar);
		c.country.setValue(this.country);
		// influence
		this.redraw();
		return this;
	},
	load: function() {
		var state = 'calendar=' + this.calendar + '&country=' + this.country + '&lang=' + this.lang;
		var onSuccess = function(tbody) {
			this.data[state] = tbody;
			this.dom.tbody.insert({ before: tbody });
			this.dom.tbody.remove();
			this.dom.tbody = this.dom.table.select('TBODY')[1];
			document.location.hash = state;
		}.bind(this);
		var url = '/service.asp?name=forex&action=calendar&' + state.replace('|', '&');
		this.dom.preloader.show();
		new Ajax.Request(url, {
			method: 'GET',
			encoding: 'windows-1251',
			onSuccess: function(r) {
				onSuccess(r.responseText);
				this.dom.preloader.hide();
				this.redraw();
			}.bind(this)
		});
	},
	redraw: function() {
		this.influence = null;
		$A(this.controls.country.dom.items).each(function(item) {
			if (item.hasClassName(this.calendar)) {
				item.show();
			} else {
				item.hide();
			}
		}.bind(this));
		$A(this.dom.table.select('A.influence')).invoke('observe', 'click', function(event) {
			if (this.influence != null) {
				this.influence.hide();
			}
			var link = event.findElement('A.influence');
			if (link != undefined) {
				var influence = $(link.id + '-text');
				if (influence != null) {
					influence.show();
				}
				this.influence = influence;
			}
			event.stop();
			return false;
		}.bind(this));
		$A(this.dom.table.select('DIV.influence')).invoke('observe', 'click', function(event) {
			var text = event.findElement('DIV.influence');
			if (text != undefined) {
				text.hide();
			}
			event.stop();
			return false;
		}.bind(this));
		return this;
	}
});

Finam.Forex.Calendar.DropDown = Class.create({
	initialize: function(elm, opts) {
		var d = this.dom = {};
		var e = d.elm = elm;
		d.title = e.select('.title')[0];
		d.list = e.select('.list')[0];
		d.items = d.list.select('A');
		opts = opts || {};
		this.select = opts.select || function() {};
		this.value = null;
		this.defaultValue = opts.defaultValue || '';
		this.defaultOption = opts.defaultOption || '';
		return this;
	},
	create: function() {
		$(document.body).observe('click', function(event) {
			this.hide();
		}.bind(this));
		this.dom.title.observe('click', function(event) {
			this.show();
			event.stop();
		}.bind(this));
		this.dom.list.observe('click', function(event) {
			var elm = event.findElement('A');
			if (elm != undefined) {
				this.select(elm.readAttribute('href').replace(/\#/, ''));
				this.update(elm.innerHTML);
			}
			this.hide()
			event.stop();
			return false;
		}.bind(this));
		return this;
	},
	destroy: function() {
		//
		return this;
	},
	show: function() {
		if (Finam.Forex.Calendar.DropDown._active != null) {
			Finam.Forex.Calendar.DropDown._active.hide();
		}
		this.dom.list.setStyle({
			visibility: 'visible'
		});
		Finam.Forex.Calendar.DropDown._active = this;
		return this;
	},
	hide: function() {
		this.dom.list.setStyle({
			visibility: 'hidden'
		});
		Finam.Forex.Calendar.DropDown._active = null;
	},
	redraw: function() {
		return this;
	},
	update: function(title) {
		this.dom.title.innerHTML = title;
		return this;
	},
	setValue: function(value) {
		var exists = false;
		$A(this.dom.items).each(function(item) {
			if (item.readAttribute('href') == '#' + value) {
				this.value = value;
				this.update(item.innerHTML);
				exists = true;
				throw $break;
			}
		}.bind(this));
		if (!exists) {
			this.value = this.defaultValue;
			this.update(this.defaultOption);
		}
		return this;
	},
	getValue: function() {
		return this.value;
	}
});
Finam.Forex.Calendar.DropDown._active = null;


Finam.Forex.Tabs = Class.create({
	initialize: function(elm) {
		this.selected = null;
		var d = this.dom = {};
		var e = d.elm = elm;
		var sups = d.sups = e.select('SUP');
		var tabs = d.tabs = e.select('.forex-tabs-tab');
		$A(tabs).each(function(tab, index) {
			tab.writeAttribute('index', index);
		}.bind(this));
		$A(tabs).invoke('observe', 'click', function(event) {
			this.select(event.findElement('.forex-tabs-tab'));
		}.bind(this));
		var pages = d.pages = e.select('.forex-tabs-page');
		var maxHeight = 0;
		$A(pages).each(function(page, index) {
			maxHeight = Math.max(maxHeight, page.getHeight());
			page.writeAttribute('index', index)
				.hide()
					.setStyle({ visibility: 'hidden' });
		}.bind(this));
		$A(pages).each(function(page, index) {
			page.setStyle({
				height: maxHeight + 'px'
			});
		}.bind(this));
		var selected = 1;
		if (document.location.hash.length > 0) {
			var selected = document.location.hash;
			selected = (selected == '#mt4' ? '#tab2' : selected);
			selected = parseInt(selected.replace('#tab', ''));
			selected = isNaN(selected) ? 0 : (selected > tabs.length ? tabs.length : selected) - 1 ;
		}
		this.select(tabs[selected]);
		return this;
	},
	select: function(tab) {
		if (this.selected != null) {
			this.dom.tabs[this.selected].removeClassName('selected-tab');
			this.dom.pages[this.selected].hide().setStyle({ visibility: 'hidden' });
			if (this.dom.sups.length > 0 && this.dom.sups[0].descendantOf(this.dom.pages[this.selected])) {
				$A(this.dom.sups).invoke('hide');
			}
		}
		this.selected = parseInt(tab.readAttribute('index'));
		this.dom.tabs[this.selected].addClassName('selected-tab');
		this.dom.pages[this.selected].show().setStyle({ visibility: 'visible' });
		if (this.dom.sups.length > 0 && this.dom.sups[0].descendantOf(this.dom.pages[this.selected])) {
			$A(this.dom.sups).invoke('show');
		}
		if (document.location.hash.length > 0 || this.selected > 0) {
			//document.location.hash = '#tab' + (this.selected + 1);
		}
		return this;
	}
});
Finam.Forex.Tabs._colors = ['#f2f2f2', '#f7f7f7', '#fcfcfc'];

