/**
 * @author alltouch
 */

function Chapters(){
 	
	this . options = new Object();
	this . options . url = piro . options . path + 'chapters.php';
	
	this . cacheData = null;
	var TabsBlock = null;
	var BottomBlock = null;
	var countErrors = 0;
	
	this . load = function(){
		
		piro . loading . hidePage('chapters');
		
		$.ajax({
			type : 'POST',
			async : false,
			url : this . options . url,
			data : 	{ data: $.toJSON({}) },
			dataType : 'json',
			success : this . set,
			error : function(obj){
				countErrors++;
				if(countErrors < 5){
					setTimeout(piro . chapters . load, 500);
				} else {
					piro . alert . loadCustom('noConnectInternet');
				}
			}
		});

	}
	
	this . set = function(obj){
		countErrors = 0;
		piro . chapters . cacheData = obj;
		
		piro . chapters . setTopLinks();
		piro . chapters . setBottomLinks();
		
		piro . loading . showPage('chapters');
	}
	
	this . setTopLinks = function(){
		
		var text = '';
		var cacheData = piro . chapters . cacheData;
		var l = cacheData . main .length;
		
		for( var i = 0 ; i < l ; i++ ){
			var item = cacheData . all[ cacheData . main[i] ];
			
			if(typeof item == 'undefined'){
				continue;
			}
			
			var link = 'pageid=' + item . id;
			if(item . codeName){
				link = item . codeName; 
			}
			if (item.showTo) {
				text += '<li id="tab' + item.id + '"><span><a href="#' + link + '">' + item.title + '</a></span></li>';
				if(item.id == 82){
					text += '<li id="tab' + item.id + 'short"><span><a href="#' + link + '"></a></span></li>';
				}
			}
		}
		
		TabsBlock . html(text);
		piro . page . correctTabs();
	}
	
	this . setBottomLinks = function(){
		
		var text = '';
		var cacheData = piro . chapters . cacheData;
		var l = cacheData . main .length;
		
		for( var i = 0 ; i < l ; i++ ){
			var item = cacheData . all[ cacheData . main[i] ];
			if(typeof item == 'undefined'){
				continue;
			}
			
			if(item . title == 'Моя страница'){
				continue;
			}
						
			var link = 'pageid=' + item . id;
			if(item . codeName){
				link = item . codeName; 
			}
				
			if(item . showTo)
				text += '<a href="#' + link + '">' + item . title + '</a> | ';
		}
		
		BottomBlock . html( text . substr(0, text.length - 2));
		
	}
	
	this . setActiveTab = function(id){
		TabsBlock . children('.selected') . removeClass('selected');
		TabsBlock . children('#tab' + id) . addClass('selected');
		if(id == 82){
			TabsBlock . children('#tab' + id + 'short') . addClass('selected');
		}
	}
	
	this . init = function(){
		
		TabsBlock = $('#tabs'); 
		BottomBlock = $('#bottomMenu'); 
		
		this . load();
		
		//try use mouseover instead of live
		
		TabsBlock . find('li') . mouseover(function(){
			var t = $(this);
			if( !t . hasClass('selected')){
				t.addClass('hover');
			}
		})
		TabsBlock . find('li') . mouseout(function(){
			var t = $(this);
			if( t . hasClass('hover')){
				t.removeClass('hover');
			}
		})
		
	}
	
	this . getTopItem = function(id){
		var tempData = piro . chapters . cacheData . all;
		var temp = tempData[id];
		if(!temp){
			return null;
		}
		
		while(temp.parent != null){
			temp = tempData[temp.parent];
		}
		return temp . id;
	}
	
	this . getParentsList = function(id){
		var result = {};
		var tempData = piro . chapters . cacheData . all;
		var temp = tempData[id];
		while(temp.parent != null){
			temp = tempData[temp.parent];
			result[temp . id] = true;
		}
		return result;
	}
	
}

piro . chapters = new Chapters();

