var FilmRelated = Class.create({

	initialize: function(film_id, container) {
		this.film_id = film_id;
		this.container = $(container);
		this.waitRes = $('relwait');
	},

	bindTabs: function(tabs) {

		if (tabs.isJSON()) {
			this.tabs = tabs.evalJSON();
		} else {
			alert (tabs);
		}
		$H(this.tabs).each(function(tab) {

			var li = $('li_'+tab.key);
			if (Object.isElement(li)) {
				if (tab.value.current) {
					li.addClassName("current");
					$A(li.childElements()).each(function(el) {
						if (el.tagName == 'A') {
							//el.replace(el.text);
							el.onclick = function(){return false;};
						}
					}, this);
				} else {
					li.removeClassName("current");
					$A(li.childElements()).each(function(el) {
						if (el.tagName == 'A') {
							el.onclick = this.get.bind(this, tab.value.a, this.page);
						}
					}, this);
				}
			}
		}, this);

	},

	tabMouseOver: function(el) {
		el.style.background = "#ddd";
	},
	tabMouseOut: function(el) {
		el.style.background = "";
	},

	refreshTabs: function() {
		$H(this.tabs).each(function(pair) {
			el = $('li_'+pair.key);
			if (Object.isElement(el)) {
				if (pair.value.current) {
					el.addClassName("current");
					el.onclick = function(){return false;};
				} else {
					el.removeClassName("current");
				}
			}
		});
	},

	get: function(action, page) {
		new Ajax.Request('/view/', {
	    	method:'post',
	    	parameters: $H({
				"a":action,
				"film_id":this.film_id,
				"page":page
			}).toQueryString(),
			onLoading: this.showWait.bind(this),
	    	onComplete: this.afterGet.bind(this),
	    	onFailure: this.error.bind(this)
		});
		return false;
	},

	getRelated: function(page) {
		this.showWait();
		new Ajax.Request('/view/', {
	    	method:'post',
	    	parameters:'a=getRelated&page='+page+'&film_id='+film_id,
	    		onComplete: this.afterGet.bind(this),
	    		onFailure: this.error.bind(this)
		});
		return false;
	},

	getUserVideo: function(page) {
		this.showWait();
		new Ajax.Request('/view/', {
	    	method:'post',
	    	parameters:'a=getUserVideo&page='+page+'&film_id='+film_id,
	    		onComplete: this.afterGet.bind(this),
	    		onFailure: this.error.bind(this)
		});
		return false;
	},

	afterGet: function(request) {
		this.hideWait();
		this.container.update(request.responseText);
	},
	error: function() {
		this.container.update("Извините, произошла ошибка");
	},
	showWait: function() {
		this.waitRes.show();
	},
	hideWait: function() {
		this.waitRes.hide();
	}

});

