//var video_to_playlist_index = [];

var VideoPlayer = Class.create
({
	initialize: function(options)
	{
		this.options = {};
		Object.extend(this.options, options || {});

		this.playlist		= [];
		this.link			= null;
		this.old_link		= null;
		this.anchor			= 0;
		this.old_anchor		= 0;
		this.interval		= null;
		this.playlist_index	= 0;
		this.video_to_playlist_index = new Array();
		this.playlist_to_video_index = new Array();
		this.first_load = true;

		this.loader = new Element('img', {
			src: public_url + 'images/site_images/loading_big.gif',
			id: 'loading'
		});

		this.video_info_container	= $('video_info');
		this.video_title			= $('video_title');
		this.video_summary			= $('video_summary');
		this.video_views_counter	= $('views_counter').down();

		this.create_playlist();
		$$('a.play_video').invoke('observe', 'click', this.play_video.bind(this));

	},

	create_playlist: function()
	{
		$$('a.play_video').each(function(elem){
			this.add_playlist_item(elem);
		}.bind(this));

		this.load_playlist();
	},

	add_playlist_item: function(elem)
	{
		this.playlist_to_video_index[this.playlist_index] = elem.id;
		this.video_to_playlist_index[elem.id] = this.playlist_index;

		//var href = elem.readAttribute('href');
		//var sharp_offset = href.indexOf('#');
		//var video_file = sharp_offset != -1 ? href.substr(0, sharp_offset) : href;

		//DA SE NAPRAVI DA SMENQ ANCHOR-A V URL_TO PRI SMQNA NA KLIPCHETO PREZ PLAYER-A

		this.playlist[this.playlist_index] = {
			file: elem.readAttribute('rel'),
			image: elem.select('img').first().readAttribute('src').replace('_thumb', '')
		}

		this.playlist_index++;
	},

	load_playlist: function()
	{
		$('video_container').sendEvent('LOAD', this.playlist);

		//get item change from the PREV/NEXT butonns in the flash player
		$('video_container').addControllerListener('ITEM', "vp.item_change");
		this.set_video();
	},

	item_change: function(item)
	{
		var link = this.link = $(this.playlist_to_video_index[item.index]);

		window.location.hash = link.readAttribute('id').replace('play_video_','');

		this.update_views_count();
		Effect.ScrollTo('wrapper');

		if(this.old_link != this.link || this.first_load) {
			this.old_link = this.link;
			this.mark_current_thumb();
		}
	},

	play_video: function(evt)
	{
		this.link = Event.element(evt);

		if(this.link.tagName == 'IMG') {
			this.link = this.link.up();
		}

		if(this.old_link == this.link) {
			$('video_container').sendEvent('PLAY');
			console.log('playing '+this.link.id);
		}
		else {
			$('video_container').sendEvent('STOP');
			$('video_container').sendEvent('ITEM', this.video_to_playlist_index[this.link.id]);
		}
	},

	set_video: function(param)
	{
		//loading a video based on the URL anchor
		var url = window.location.toString();
		var sharp_offset = url.indexOf('#');

		if(sharp_offset != -1) {
			this.anchor = url.substr(sharp_offset + 1, url.length);
		}

		if(this.anchor != this.old_anchor && $('play_video_'+this.anchor)) {
			this.link = $('play_video_'+this.anchor);
			this.old_anchor = this.anchor;
		}
		else if(this.first_load) {
			this.link = $(this.playlist_to_video_index[0]);
		}

		if(this.old_link != this.link || this.first_load)
		{
			$('video_container').sendEvent('ITEM', this.video_to_playlist_index[this.link.id]);
			$('video_container').sendEvent('STOP');
			this.mark_current_thumb();
			this.video_views_counter.update(this.link.next().next().next().innerHTML);
		}
	},

	mark_current_thumb: function()
	{
		if($$('.gallery ul li.current_video').length) {
			$$('.gallery ul li.current_video').first().removeClassName('current_video');
		}

		this.link.up().addClassName('current_video');
		this.first_load = false;
	},

	update_views_count: function()
	{
		var video_id = this.link.id.replace(/[^\d]/g, '');

		var self = this;
		new Ajax.Request(this.options.url, {
			method: 'get',
			parameters: 'method=update_views_counter&video_id=' + video_id,
			onCreate: function()
			{
				if(self.first_load == false)
				{
					new Effect.DropOut(self.video_info_container, {
						afterFinish: function() {
							self.video_summary.update('');
							self.video_title.update('');
							self.video_title.insert(self.loader);
							new Effect.Appear(self.video_info_container);
						}
					});
				}
			},
			onComplete: this.first_load == false ? this.update_info.bind(this) : null
		});
	},

	update_info: function()
	{
		var self = this;
		new Effect.Fade(self.video_info_container, {
			afterFinish: function() {
				self.video_title.update(self.link.next().innerHTML);
				self.video_summary.update(self.link.next().next().innerHTML);
				self.video_views_counter.update(self.link.next().next().next().innerHTML);

				new Effect.Appear(self.video_info_container, {duration: .5});
			}, duration: .5
		});
	}
});