var ShortView = new Class({
	container : null,
	content : null,
	contentType : 'article',
	links : {},
	current : false,
	tabContainer : null,
	
	initialize : function(containerID, contentType)
	{
		this.contentType = contentType;
		
		var mark = $(containerID);
		
		var info = this.getTabs();
		this.tabContainer = new Element('div');
		if (info.header_class)
			this.tabContainer.addClass(info.header_class);
		mark.grab(this.tabContainer, 'before');
		mark.destroy();
		
		
		var tabs = info.tabs;
		var first = false;
		tabs.each(function(item)
		{
			var lnk = new Element('a', {
				'href' : '#',
				'text' : item.title,
				'class' : item.classname
			});
			if (first && info.delimiter)
				this.tabContainer.grab(info.delimiter.clone());
			this.tabContainer.grab(lnk);
			
			lnk.addEvent('click', function() {this.refreshContent(item.code); return false; }.bind(this) );
			this.links[item.code] = lnk;
			//alert(item.code + ' ' + item.title);
			if (!first)
				first = item.code;
		}, this);
		var clearer = new Element('div', {'class' : 'clear'});
		this.tabContainer.grab(clearer);
		
		this.content = new Element('div');
		if (info.wrapper_class)
			this.content.addClass(info.wrapper_class);
		this.tabContainer.grab(this.content, 'after');
		if (first)
			this.refreshContent(first);
	},
	
	refreshContent : function(mode)
	{
		//alert(mode);
		if (mode == this.current)
			return;
		this.links[mode].addClass('active');
		//alert(l);
		if (this.current)
			this.links[this.current].removeClass('active');
		var url = this.getAjaxUrl();
		if (!url)
			return;
		var req_data = new Hash({
			'q' : url,
			'mode' : mode
		});
		this.req = new Request.HTML({
			method : 'post',
			url : '/index-ajax.php',
			data : req_data.toQueryString(),
			update : this.content,
			onSuccess : this.onRefreshComplete.bind(this)
		}).send();
		this.current = mode;
	},
	
	getAjaxUrl : function()
	{
		var url = "";
		switch (this.contentType)
		{
		case 'article':
			url = "assets/snippets/article/ajax.php";
			break;
		case 'comment':
			url = "assets/snippets/comments/ajax.php";
			break;
		case 'media':
			url = "assets/snippets/MyGallery/right_column.php";
			break;
		}
		return url;
	},
	
	getTabs : function()
	{
		var tabs;
		var result;
		switch (this.contentType)
		{
		case 'article':
			result = {
				'header_class' : 'distribution',
				'delimiter' : null,
				'wrapper_class' : '',
				'tabs' : [
							{code : 'latest_rank', title : "по популярности", classname : "pop"},
							{code : 'latest_date', title : "по дате", classname : "date"}
						]
			};
			break;
		case 'comment':
			result = {
					'header_class' : 'distribution',
					'delimiter' : null,
					'wrapper_class' : 'last-comments-inner',
					'tabs' : [
								{code : 'shortview_rank', title : "по популярности", classname : "pop"},
								{code : 'shortview_date', title : "по дате", classname : "date"}
							]
				};
			break;
		case 'media':
			result = {
					'header_class' : 'fotovideo-title',
					'delimiter' : new Element('span', {'text' : '|'}),
					'wrapper_class' : '',
					'tabs' : [
								{code : 'video', title : "Видео", classname : "video"},
								{code : 'image', title : "Фото", classname : "foto"}
							]
				};
			break;
		default:
			result = {};
		}
		return result;
	},
	
	onRefreshComplete : function()
	{
		if (this.contentType == 'comment')
		{
			this.content.getElements('*[class=inner]').each(function(el)
			{
				el.addEvent('mouseover', function()
				{
					this.getElements('*[class=popap]').each(function(child) { child.setStyle('display', 'block'); });
				}.bind(el));
				el.addEvent('mouseout', function()
				{
					this.getElements('*[class=popap]').each(function(child) { child.setStyle('display', 'none'); });
				}.bind(el));
			});
		}
	}
});
