_Community = function () {	
	
	var self = this;
	this._loc = {cat : undefined, item : undefined};
	
	$(document).ready(function(){
		$('.community-pager').hide();	
	});
	
	this.initScrollLayer = function () {
		var wndo = new dw_scrollObj('frame2', 'community-main-frame');
		wndo.setUpScrollbar('community-drag', 'community-track', 'v', 1, 1);
	}			
	
	this.loadNewsCat = function (catId) {
		var req = new JsHttpRequest();
		req.onreadystatechange = function() {
	    if (req.readyState == 4) {
				self.showList(req.responseJS.list, req.responseJS.cat);
			}
		}
		
		num = (catId == 0) ? 6 : 1024;
		
		req.open('get', '/backend/news.php', true);
		req.send( {'cat' : catId, 'num' : num, 'textLength' : 250} );
	
		return false;
	}
	
	this.loadNewsItem = function (itemId) {
		var req = new JsHttpRequest();
		req.onreadystatechange = function() {
	    if (req.readyState == 4) {
				self.showItem(req.responseJS);
			}
		}
		req.open('get', '/backend/news.php', true);
		req.send( {id : itemId} );
		
		return false;
	}
	
	this.showList = function (data, cat) {
		var container = document.getElementById('community-main-frame');
		var content = '';
		
		for (var i = 0; i < data.length; i++) {					
			content += 
				"<div class='item'><div class='image'><a href='/index.php?p=community&amp;item=" + data[i].id +
				"' onclick='return community.loadNewsItem("+ data[i].id +");'><img src='/backend/thumbnail.php?image=" + data[i].image + "&amp;type=preview' alt='' width='62' height='62' /></a></div><div class='text'>" + 
				data[i].text + 
				"</div><div class='clear'></div></div>";
		}
		
		this.hideNav();
		this.hidePager();
		
		if (cat.id != 0) {
			this.showCatNav(cat.id, cat.title);
			this.setLocation({cat : cat.id, item : undefined});
		}
		
		container.innerHTML = content;				
		this.initScrollLayer();
		//tb_init('div#community-main-frame a.thickbox');
	}
	
	this.showItem = function (data) {
		var container = document.getElementById('community-main-frame');
		var content = '';
		
		content += "<div class='item full-item'><div class='title'>" + data.title + "</div><div class='text'>" + data.text + "</div><div class='clear'></div></div>";
		
		this.hideNav();
		this.showPager(data._adjacent.next, data._adjacent.prev); // switch prev and next
		this.showCatNav(data.cat, data.cat_title);
		this.showItemNav(data.id, data.title_short);
		this.setLocation({cat : undefined, item : data.id});
		
		container.innerHTML = content;				
		this.initScrollLayer();
	}
	
	this.hideNav = function () {
		$('h2 > a').removeClass('active');
		$('#community-pager-cat').empty();
		$('#community-pager-item').empty();
	}
	
	this.showCatNav = function (id, title) {
		$('a.community-cat_' + id).addClass('active');
		$('#community-pager-cat').empty().append('&gt <a onclick="return community.loadNewsCat(' + id + ')" href="/index.php?p=community&amp;cat=' + id + '">' + title + '</a>');
	}
		
	this.showItemNav = function (id, title) {
		$('#community-pager-item').empty().append('&gt ' + title);
	}
	
	this.hidePager = function () {
		$('.community-pager').hide();	
	}
	
	this.showPager = function (prev, next) {
		var n;
		if (prev) {
			$('.community-pager a.prev').show().attr({
				href : '/index.php?p=community&item=' + prev
			});
			document.getElementById('pager-prev').onclick = function(e) {return self.loadNewsItem(prev)};
		} else {
			$('.community-pager .prev').hide();
		}
	
		if (next) {
			$('.community-pager a.next').show().attr({
				href : '/index.php?p=community&item=' + next
			});
			document.getElementById('pager-next').onclick = function(e) {return self.loadNewsItem(next)};
		} else {
			$('.community-pager .next').hide();
		}
		
		$('.community-pager').show();
	}
	
	this.setLocation = function (l) {
		this._loc = l;
		if (l.cat != undefined) {
			window.location.hash = 'cat=' + l.cat;
		}
		else if (l.item != undefined) {
			window.location.hash = 'item=' + l.item;
		}
	
		var href = $('div.community-panel a.tell').attr('href');
		$('div.community-panel a.tell').attr('href', href.replace(/&url=.*?&/, '&url=' + escape(this.getUrl()) + '&'));
	}
	
	this.getUrl = function () {
		var url = window.location.protocol + '//' + window.location.hostname + window.location.pathname + '?p=community';
		if (this._loc.cat) {
			url += '&cat=' + this._loc.cat;
		}
		else if (this._loc.item) {
			url += '&item=' + this._loc.item;
		}	
		
		return url;
	}
	
	this.showLocation = function () {
		prompt('Адрес этой страницы:',  this.getUrl());
		return false;
	}
}

community = new _Community();
