/**
 * @author garaspet
 */

var cThumb = Class.create({
	initialize: function(_container) {
		this.id = universe.id('thumb');
		this.container = _container;
		this.element = null;
		this.data = {};
		this.active = "";
		this.init();
	},
	init: function() {
		this.element = document.createElement('div');
		this.element.id = this.id;
		this.container.appendChild(this.element);
	},
	loadData: function(data) {
		this.data = data;
		this.active = "default";
		this.data.activeItem = this.active;
		this.render(this.active);
	},
	render: function(sub) {
		if (!sub) {
			sub = this.active;
		}
		this.active = sub;
		this.data.activeItem = this.active;
		if (this.data.movie && !userer.isGuest()) {
			this.data.listak = userer.getListsWithMovie(this.data.movie.azon);
		}
		templater.render(this.element, 
			'template_thumb_' + this.data.type + '_' + sub, 
			this.data, 
			this.afterRender.bind(this));
		return false;
	},
	afterRender: function() {
		if (this.data.assessor) {
			universe.bindDatasheets($$('#' + this.id + ' .assessorDatasheetLink'), 'ppl', this.data.assessor.azon);
		}
		if (this.data.assess) {
			var opinion = $$('#' + this.id + ' .opinion1');
			if (opinion.length > 0) opinion[0].observe('click', userer.makeOpinionExt.bind(userer, 1, this.data.assess.azon, function(){
				this.data.assess.opinion1++;
				this.render();
			}, this));
			opinion = $$('#' + this.id + ' .opinion0');
			if (opinion.length > 0) opinion[0].observe('click', userer.makeOpinionExt.bind(userer, 0, this.data.assess.azon, function(){
				this.data.assess.opinion0++;
				this.render();
			}, this));
		}
		if (this.data.movie) {
			universe.bindDatasheets($$('#' + this.id + ' .movieDatasheetLink'), 'movie', this.data.movie.azon);
		}
		if (this.data.movie) {
			universe.bindEntities($$('#' + this.id + ' .creativeDatasheetLink'));
			var trailer = $$('#' + this.id + ' .trailerLink');
			if (trailer.length > 0) trailer[0].observe('click', userer.showTrailerExt.bind(userer, this.data.movie));
			
			var lists = $$('#' + this.id + ' .movielist');
			if (lists.length > 0) {
				for (var i=0; i<lists.length; i++) {
					var id = universe.getFilmxId(lists[i]);
					if (lists[i].hasClassName('add')) {
						lists[i].observe('click', this.putMovieToList.bind(this, this.data.movie.azon, id, true));
					} else {
						lists[i].observe('click', this.putMovieToList.bind(this, this.data.movie.azon, id, false));
					}
				}
			}
		}
		this.renderMenu();
		return false;
	},
	renderMenu: function() {
		var menu = $$('#' + this.id + ' .thumb-menu');
		templater.render(menu[0], 
			'template_thumb_' + this.data.type + '_menu', 
			this.data, 
			this.afterRenderMenu.bind(this));
		return false;
	},
	afterRenderMenu: function() {
		var items = [
			{name: 'default', link: this.render.bind(this, 'default')}
		];
		if (this.data.type == 'ppl_small') {
			items = items.concat([
				{name: 'assess', link: this.render.bind(this, 'assess')}, 
				{name: 'know', link: this.render.bind(this, 'know')}, 
				{name: 'im', link: this.callAuthRender.bind(this, 'im')}, 
				{name: 'attend', link: this.attendAssessor.bind(this, 1)}, 
				{name: 'notattend', link: this.attendAssessor.bind(this, 0)}
			]);
		}
		else if (this.data.type == 'movie_big') {
			items = items.concat([
				{name: 'assess', link: this.showAssess.bind(this)}, 
				{name: 'description', link: this.render.bind(this, 'description')}, 
				{name: 'creatives', link: this.render.bind(this, 'creatives')}, 
				{name: 'list', link: this.render.bind(this, 'list')}, 
				{name: 'need', link: this.render.bind(this, 'need')}, 
				{name: 'recom', link: this.doRecom.bind(this)}
			]);
		}
		for (var i=0; i<items.length; i++) {
			var menuItem = $$('#' + this.id + ' .menu-' + items[i].name);
			if (menuItem.length > 0) menuItem[0].observe('click', items[i].link);
		}
		return false;
	},
	callAuthRender: function(sub) {
		if (userer.isGuest()) {
			userer.showLogin();
		} else {
			this.render(sub);
		}
		return false;
	},
	attendAssessor: function(attend) {
		userer.makeAttend(this.data.assessor.azon, attend);
		this.render();
		return false;
	},
	putMovieToList: function(movieId, listId, isAdd) {
		if (isAdd) {
			userer.addItemToList(listId, movieId, '');
		} else {
			userer.removeItemFromList(listId, movieId);
		}
		this.render();
		return false;
	},
	doRecom: function() {
		userer.showRecom(this.data.movie.azon);
		return false;
	},
	showAssess: function() {
		return false;
	}
});
