var lastind = 0;
var empty   = 0;
var row_ind = 1;
var table;

var	clicked = 0;

function runnextload(){
    if(lastind >= search_sites.length){
        if(empty){
            $("#notfoundsites").show();
        }
        $('#loading').hide("slow");
        $("span.shown_messages").text($("tr.message").length);
        $('div.pagination').show();

		$('#table thead tr th').addClass('header');
		$('#table thead tr th').click(function(){
						if (clicked == 0){
							/*var col = $(this).text();
							if (col == 'Time'){ 
							$.tablesorter.defaults.sortList = [[1,0]]; 
							}*/
							$("#table").tablesorter({
									sortList: [[1,1]] 
									});
							clicked = 1;
							}
				});
next();
        return;
    }
    var sitename = search_sites[lastind];
//	console.log(sitename+" "+lastind);
    lastind += 1;        
    if(site_cache[sitename]){
        fillMessages(sitename, site_cache[sitename]);
    }
    else {
      $("#current_board_loading").text(sitename);
	    $.getJSON('/search/site_messages_json/'+sitename+'/'+ticker+'/'+messages_number,
	      function(data){ fillMessages(sitename, data);}
	    );
    }
}

function msg_path(uri){
  return '/boards/'+ ticker + '/' + uri;
}

function tpl() {
		var s_name = site_id2name[this.site_id];
//		console.log(this.site_id+ ' '+s_name);
  var trclass = '';
  if(this['old']){
    trclass = ' old';
  }
  var td_subject_attr = {'class': 'subject'};
  var tr_content = [
    'td', td_subject_attr, [
      'div', {}, [
        'a', {'href': msg_path(this.uri), 'title': this.subject+"", 'id': this.id+''}, this.subject+""
      ]
    ],
    'td', {'class': 'date'}, ['span',{style: 'display: none;'}, this.date_time,'span', {}, this.dttm + ''],
    'td', {'class': 'site'}, [
      'img', 
      {'src': '/static/img/sitelogo/' + site_id2name[this.site_id] + '.gif', 'alt': '', 'title': site_name2display[site_id2name[this.site_id]]}
    ]
  ];
  return ['tr', {'class': 'message'+trclass}, tr_content];
}

function fillMessages(sitename, data){
	if(data.messages.length > 0){
	   for(i=0; i < data.messages.length; ++i){
	      table.tplAppend(data.messages[i], tpl);
        ++row_ind;
/*        if(row_ind == 10){
          table.after('<table class="messages messages_long" border="0" cellspacing="0" cellpadding="0"></table>');
          table = table.next('table.messages');
        }
        else if(row_ind == 19){
          table = $("#article_cont table.messages_short:eq(1)");
        }
        else if(row_ind == 28){
          table.after('<table class="messages messages_long" border="0" cellspacing="0" cellpadding="0"></table>');
          table = table.next('table.messages');
        }*/
	   }
//	   $('table.messages tr:odd').addClass('odd');
//	   $('table.messages tr.message:eq(0)').addClass('first');
	   $('table.messages tr.message').hover(function(){$(this).addClass('hover')}, function(){$(this).removeClass('hover')});
	   
	}
  runnextload();
}

function next(){
        var refresh = $('#search-settings #refresh-time option:selected').val() * 1000;
		setTimeout(function(){
		    lastind = 1;
            $('#table tr.message').remove();
            runnextload();
        }, refresh);
}

$(document).ready(function(){
    table = $("#article_cont table.messages"); 

    runnextload();

    $("#options-opener").click(
      function(e) {
        e.preventDefault();
        $("#search-settings").toggle();
      }
    );

});
