function showMyTab(event){
  $$('li.btn_watki').first().addClassName('active');
  $$('li.btn_dyskusje').first().removeClassName('active');
  $('hot').hide();
  $('mine').show();
  event.stop();
}

function showHotTab(event){
  if($$('li.btn_dyskusje').size() > 0) $$('li.btn_dyskusje').first().addClassName('active');
  $$('li.btn_watki').first().removeClassName('active');
  $('mine').hide();
  $('hot').show();
  event.stop();
}

function setCommTabsHandlers(){
  if(hot_comms) hot_comms.observe('click',showHotTab);
  if(my_comms) my_comms.observe('click',showMyTab);
}

function setCommTabsVariables(){
  hot_comms = $('hot_comms');
  my_comms = $('my_comms');
}


function getMoreComments(event){
    var type = event.findElement().id.split("_")[1];
    new Ajax.Request(event.findElement().href,
		     {
                         method: 'get',
			 params: {type: type},
			 onLoading: showLoadingContent,			 
			 onComplete: function(res){
			     var link = $('more_' + type + '_comments');     
			     if(link) link.remove();
			     var bottom = $('bottom_' + type);     
			     if(bottom) bottom.remove();
			     new Insertion.Bottom('comment_' + type, res.responseText);
			     initializeMoreLinks();
			     hideRatedUsers();
			     fetchWhoRated();
			     hideLoadingContent();
			     $('bottom_' + type).scrollTo();
			 }
		     }
		    );
    event.stop();
}


function initializeMoreLink(type){
    var link = $('more_' + type + '_comments');
    if(link)
	link.observe('click', getMoreComments);
}

function initializeMoreLinks(){
    ['text', 'question', 'event', 'notice'].each(function(type){
						     initializeMoreLink(type);
						 });
    
}

function slideToComment(id){
    $('comment_' + id).scrollTo();
}

function goToThread(event){
    var tabs = {
	'1': 'text',
	'2': 'question',
	'3': 'event',
	'4': 'notice'
    };

    var url = event.findElement().href;
    new Ajax.Updater("comment_" + tabs[url.parseQuery().category], url, 
		     {
			 method: 'get', 
			 onLoading: showLoadingContent, 
			 onComplete: function(){
			     slideToComment(url.parseQuery().comment_id);
			     hideLoadingContent();
			     initializeMoreLinks();
			     initAnswerOptions();
			     hideRatedUsers();
			     fetchWhoRated();
			 }
		     });
    $('tab_' + tabs[url.parseQuery().category]).simulate('click');

    event.stop();
}

function initializeGoToThreadLinks(){
    $$('a.thread_link').each(function(link){
				 link.observe('click', goToThread);
			     });
}


function prepareCommentTabs(){
    if($$('li.btn_dyskusje').size() > 0) $$('li.btn_dyskusje').first().addClassName('active');
    //$$('li.btn_dyskusje').first().addClassName('active');
    setCommTabsVariables();
    setCommTabsHandlers();
    initializeMoreLinks();  
    initializeGoToThreadLinks();
    
}


document.observe('dom:loaded',prepareCommentTabs);


