/**
 * @deprecated
 */

var Tag = Class.create({

	id: 0,
	name: '',
	selected_blogs_count: 0,
	blogs_count: 0,
	
	initialize: function(id, name)
	{
		this.id = id;
		this.name = name;
	}
});

var Blog = Class.create({

	id: 0,
	name: '',
	url: '',
	has_created_checkbox: false,
	
	initialize: function(id, name, url)
	{
		this.id = id;
		this.name = name;
		this.url = url;
	}
});

var BlogTag = Class.create({

	tag_id: 0,
	blog_id: 0,
	
	initialize: function(blog_id, tag_id)
	{
		this.tag_id = tag_id;
		this.blog_id = blog_id;
	}
});


var BlogSelector = Class.create({

	blog_tags: null,
	blogs:     null,
	tags:      null,
	update_el:   null,
	update_id:   null,
	selector_id:   null,
	leftColumn: null,
	rightColumn: null,
	input_name_id: null,
	checkboxes_observed: false,
	selected_blogs: new Hash(),
	show_save_favorite: null, 
	show_save_blacklist: null,
	after_save: null,
	prefix: null,
	
	/**
	 * @param {String} input_name_id jak mają się nazywać ukryte inputy przechowujące wybrane wartości (input_name_id[blog_id] = blog_id) 
	 * @param {Hash} blogs tablica Blog
	 * @param {Array} tags tablica Tag
	 * @param {Array} blog_tags tablica BlogTag
	 * @param {String} update_id nazwa diva w którym pokazują się inputy
	 * @param {String} selector_id nazwa diva gdzie wygeneruje się wybierałka
	 * @param {Boolean} show_save_favorite Pokaż przycisk zapisz jako ulubione (nie zaimplementowane)
	 * @param {Boolean} show_save_blacklist Pokaż przycisk zapisz jako czarna lista (nie zaimplementowane)
	 * @param {function} afterSave dodatkowa funkcja która ma być wywołana po zapisaniu blogów, jako parametr przekazywany jest ten obiekt
	 * 					 (function nazwa_funkcji(BlogSelector)) 
	 */
	initialize: function(input_name_id, blogs, tags, blog_tags, update_id, selector_id, prefix, afterSave)
	{
		this.blogs = blogs;
		this.tags = tags;
		this.blog_tags = blog_tags;
		this.update_id = update_id;
		this.update_el = $(update_id);
		this.selector_id = selector_id;
		this.input_name_id = input_name_id;
		this.show_save_favorite = null;
		this.show_save_blacklist = null;
		this.after_save = afterSave;
		this.prefix = prefix;

		if (!(blogs instanceof Hash &&  tags instanceof Hash && blog_tags instanceof Array))
		{
			alert("BlogSelector: Parametry nie poprawne");
		}

		var icon = Builder.build(
			'<a href="#" id="' + this.prefix + 'builder_starter"><img src="/images/icons/silk/add.jpg" border="0"></a>'
		);
		$(this.selector_id).insert(icon, {position: 'bottom'}); 
		Event.observe(this.prefix + 'builder_starter', 'click', this.btnStarter.bindAsEventListener(this));

		var container = Builder.build(
			'<table id="' + this.prefix + 'blog_selector_table" class="blog_selector_table" style="display: none">' +
			'<tr><td>' +
			'<div id="' + this.prefix + this.selector_id +'_left_column" class="blog_selector_left_column"></div>' +
			'</td><td>' +
			'<div id="' + this.prefix + this.selector_id +'_right_column" class="blog_selector_right_column"></div>' +
			'</td></tr>' +
			'<tr><td colspan="2">' + 
			'<a href="#" id="' + this.prefix + this.selector_id +'_save_button"> [Zapisz] </a>' +
			'<a href="#" id="' + this.prefix + this.selector_id +'_select_all_button"> [Zaznacz wszystko] </a>' +
			'<a href="#" id="' + this.prefix + this.selector_id +'_cancel_button"> [Anuluj] </a>' +
			'</td></tr>' +
			'</table>'
		);
		$(this.selector_id).insert(container, {position: 'bottom'}); 
		this.leftColumn = $(this.prefix + this.selector_id + '_left_column');
		this.rightColumn = $(this.prefix + this.selector_id + '_right_column');

		Event.observe(this.prefix + this.selector_id + '_save_button', 'click', this.btnSaveClick.bindAsEventListener(this));
		Event.observe(this.prefix + this.selector_id + '_select_all_button', 'click', this.btnSelectAllClick.bindAsEventListener(this));
		Event.observe(this.prefix + this.selector_id + '_cancel_button', 'click', this.btnCancelClick.bindAsEventListener(this));
		this.updateBlogsCount();

		// metatag [wszystkie]
		var link = Builder.build('<div class="blog_selector_div">'+
			'<a href="#" rel="all" id="' + this.prefix + 'tag_link_all" class="blog_selector"> [Wszystkie] </a> &nbsp;&nbsp;&nbsp;' +
			'</div>'
		);
		this.leftColumn.insert(link, {position: 'bottom'});
		Event.observe(this.prefix + 'tag_link_all', 'click', this.handleTagClick.bindAsEventListener(this));
		
		tags.values().each(function(t) 
		{
			var link = Builder.build('<div class="blog_selector_div">'+
				'<a href="#" rel="'+ t.id +'" id="' + this.prefix + 'tag_link_'+ t.id +'" class="blog_selector">' + this.formatLink(t) +'</a> &nbsp;&nbsp;&nbsp;' +
				'<a href="#" rel="'+ t.id +'" id="' + this.prefix + 'tag_link_select_all_'+ t.id +'" class="mega_action">[wszystko]</a> '+
				'<a href="#" rel="'+ t.id +'" id="' + this.prefix + 'tag_link_select_none_'+ t.id +'" class="mega_action">[nic]</a>'+
				'</div>'
			);
			this.leftColumn.insert(link, {position: 'bottom'});
			Event.observe(this.prefix + 'tag_link_' + t.id, 'click', this.handleTagClick.bindAsEventListener(this));
			Event.observe(this.prefix + 'tag_link_select_all_' + t.id, 'click', this.handleSelectAllFromTagClick.bindAsEventListener(this));
			Event.observe(this.prefix+ 'tag_link_select_none_' + t.id, 'click', this.handleSelectNoneFromTagClick.bindAsEventListener(this));
		}, this);



		this.createCheckboxes();

		this.loadDataFromUpdateId();
	},

	formatLink: function(tag)
	{
		return '<span style="float: right">' + tag.selected_blogs_count + '/' + tag.blogs_count + '</span>' + tag.name;
	},
	
	createCheckboxes: function()
	{
		this.blogs.values().each(function(b)
		{		
			if (b.has_created_checkbox == false)
			{									
				var blog_input = Builder.build(
					'<div class="blog_selector_blog '+ this.prefix +'blog_selector_blog" id="' + this.prefix + 'blog_selector_blog_div_'+ b.id +'" style="display: none">' +
						'<input id="' + this.prefix + 'blog_selector_blog_'+ b.id +'" type="checkbox" name="blogs[]" value="'+ b.id +'" />' +
						'<label for="' + this.prefix + 'blog_selector_blog_'+ b.id +'">' + b.url + '</label>' + 
					' </div>'
				);
				
				this.rightColumn.insert(blog_input, {position: 'bottom'});
			
				Event.observe(this.prefix + 'blog_selector_blog_' + b.id, 'change', this.handleBlogClick.bindAsEventListener(this));
				b.has_created_checkbox = true;
			}
			
		}, this);
	},
	
	loadDataFromUpdateId: function()
	{
		this.update_el.select('input').each(function(el)
		{
			blog_id = $(el).readAttribute('value');
			
			if ($(this.prefix + "blog_selector_blog_" + blog_id)) 
			{
				this.selected_blogs.set(blog_id, blog_id);
				$(this.prefix + "blog_selector_blog_" + blog_id).checked = true;
				
				this.updateSelectedBlogsCount(blog_id, true);
			}
		}, this);
	},

	updateBlogsCount: function()
	{
		this.blog_tags.each(function(bt){
			if (tag = this.tags.get(bt.tag_id))
			{
				tag.blogs_count++;
			}
		}, this);
	},

	/**
	 * @param {Number || Hash} blog_id
	 * @param {Boolean} status
	 */
	updateSelectedBlogsCount: function(blog_id, status)
	{
		if (blog_id instanceof Hash)
		{
			ids = blog_id.clone();
			this.blog_tags.each(function(bt)
			{
				if (ids.get(bt.blog_id)) 
				{
					tag_id = bt.tag_id;
					if (t = this.tags.get(tag_id)) {
						if (status) {
							t.selected_blogs_count = t.selected_blogs_count + 1;
						}
						else {
							t.selected_blogs_count = t.selected_blogs_count - 1;
						}
						$(this.prefix + "tag_link_" + t.id).update(this.formatLink(t));
					}
				}
			}, this);			
		} 
		else
		{
			this.blog_tags.each(function(bt){
				if (bt.blog_id == blog_id) {
					tag_id = bt.tag_id;
					if (t = this.tags.get(tag_id)) 
					{
						if (status) {
							t.selected_blogs_count = t.selected_blogs_count + 1;
						}
						else {
							t.selected_blogs_count = t.selected_blogs_count - 1;
						}
						$(this.prefix + "tag_link_" + t.id).update(this.formatLink(t));
					}
					
					return true;
				}
			}, this);
		}
		
	},

	updateBlogList: function(tag_id)
	{
		var b = 0;
		
		$$('.' + this.prefix + 'blog_selector_blog').invoke('hide');
		
		this.blog_tags.each(function(bt)
		{
			if (bt.tag_id == tag_id || tag_id == 'all')
			{
				if (blogs.get(bt.blog_id))
				{
					var b = blogs.get(bt.blog_id);
					
					if (b.has_created_checkbox == true)
					{
						$(this.prefix + 'blog_selector_blog_div_' + b.id).show();
					}
				}
			}
			
		}, this);
	},
	
	handleTagClick: function(e)
	{
		e.stop();
		tag_id = e.element().readAttribute('rel');
		this.updateBlogList(tag_id);
	},
	
	handleBlogClick: function(e)
	{	
		blog_id = e.element().readAttribute('value');
		if (e.element().checked)
		{
			this.selected_blogs.set(blog_id, blog_id);
		}
		else
		{
			this.selected_blogs.unset(blog_id);
		}

		this.updateSelectedBlogsCount(blog_id, e.element().checked);
		
	},
	
	btnSelectAllClick: function(e)
	{
		tmp = new Hash();
		this.blogs.each(function(pair)
		{
			if (!this.selected_blogs.get(pair.key)) 
			{
				this.selected_blogs.set(pair.key, pair.key);
				tmp.set(pair.key, pair.key);
				$(this.prefix + "blog_selector_blog_" + pair.key).checked = true;		
			}
		}, this);
				
		this.updateSelectedBlogsCount(tmp, true);
		
		e.stop();
	},
			
	btnSaveClick: function(e)
	{
		e.stop();
		
		this.update_el.update();
		this.selected_blogs.each(function(pair)
		{
			var blog_input = Builder.build(
					'<input type="hidden" name="blogs['+ pair.value +']" value="'+ pair.value +'" />'
			);
			this.update_el.insert(blog_input);
		}, this);
		$(this.prefix + 'blog_selector_table').fade({duration: 0.5});
		
		
		
		if (this.after_save)
		{
			this.after_save(this);
		}
		
		document.fire('blogselector:saved', {blogSelector: this});
	},
	
	btnCancelClick: function(e)
	{
		$(this.prefix + 'blog_selector_table').fade({duration: 0.5});
		e.stop();
	},

	btnStarter: function(e)
	{
		e.stop();
		
		offset = $(this.prefix + 'builder_starter').viewportOffset();
		y = offset['top'];
		x = offset['left']
			
		if (y + $(this.prefix + 'blog_selector_table').getHeight() > document.viewport.getHeight())
		{
			y = document.viewport.getHeight() - $(this.prefix + 'blog_selector_table').getHeight() - 15;
		}

		if (x + $(this.prefix + 'blog_selector_table').getWidth() > document.viewport.getWidth())
		{
			x = document.viewport.getWidth() - $(this.prefix + 'blog_selector_table').getWidth() - 15 ;
		}
		
		base = document.viewport.getScrollOffsets();
		
		$(this.prefix + 'blog_selector_table').setStyle(
		{
			position: "absolute",
			top: base['top'] + y + "px",
			left: base['left'] + x + "px"	
		});
		
		$(this.prefix + 'blog_selector_table').appear({
			duration: 0.5
		});		
	},
	
	handleSelectAllFromTagClick: function(e)
	{
		tag_id = e.element().readAttribute("rel");
		
		tmp = new Hash();
		this.blog_tags.each(function(bt)
		{
			if (bt.tag_id == tag_id)
			{
				if (!this.selected_blogs.get(bt.blog_id)) 
				{
					tmp.set(bt.blog_id, bt.blog_id);
					$(this.prefix + "blog_selector_blog_" + bt.blog_id).checked = true;		
				}
			}
		}, this);
		
		tmp.each(function(pair){
			this.selected_blogs.set(pair.value, pair.value);
		}, this);
		
		this.updateSelectedBlogsCount(tmp, true);
		
		e.stop();	
	},
	
	handleSelectNoneFromTagClick: function(e)
	{
		tag_id = e.element().readAttribute("rel");
		
		tmp = new Hash();
		this.blog_tags.each(function(bt)
		{
			if (bt.tag_id == tag_id)
			{
				if (this.selected_blogs.get(bt.blog_id)) 
				{
					tmp.set(bt.blog_id, bt.blog_id);
					$(this.prefix + "blog_selector_blog_" + bt.blog_id).checked = false;				
				}
			}
		}, this);
		
		tmp.each(function(pair){
			this.selected_blogs.unset(pair.value);
		}, this);

		this.updateSelectedBlogsCount(tmp, false);
		
		e.stop();
	}
});