$('#sidebar div.section_body').ready(function(){
  $('#sidebar div.section_body').show();
});
$('#sidebar #archives ul.year').ready(function(){
  $('#sidebar #archives ul.year').show();
});

$(document).ready(function(){
  $('textarea').css('resize', 'none');
  var active_section = false;
  var is_category = $('#sidebar input[name=is_category]').val();
  var is_archive = $('#sidebar input[name=is_archive]').val();  
  if (is_category == 1) {
    active_section = 0;
  }
  else if (is_archive == 1) {
    active_section = 1;
  }
  else {
    active_section = 0;
  }
  $("#sidebar_accordion").accordion({active:active_section, autoHeight:false, collapsible:true, header: 'h3'});  
  
	var count = 0;
	var pageSize;
	$('#sidebar .sidebar_blogroll li').each(
		function(index) {
			count++;
		} 
	);
	
	if (count > 20  &&  count % 20 < 4){
		pageSize = 16;
	}
	else{
		pageSize = 20;
		
	}
	// Number of items per page
	
	// Beginning page number
	var pageNumber = 1;		
	
	$('#sidebar .sidebar_blogroll li').each(
		function(index){
			if(index > 0 && ((index + 0) % pageSize) == 0){
				pageNumber++;
			}
			$(this).addClass('group' + pageNumber);
			if(pageNumber > 1){
				$(this).hide();	
			}
		}
	);

	$('#pagingWrapper').append('PAGE:');
	for(i = 1; i <= pageNumber; i++){
		$('#pagingWrapper').append(' <a class="pagingIndex" id="page_' + i + '" href="javascript:void(0);">' + i + '</a>');
		if(i == 1){
			$('#page_' + i).addClass('activePage');
		}
	}
	
	$('.pagingIndex').click(
		function(event){
			$('.pagingIndex').removeClass('activePage');
			$(this).addClass('activePage');
			idArray = this.id.split('_');
			$('#sidebar .sidebar_blogroll li').hide();
			$('.group' + idArray[1]).fadeIn("slow");
		}
	);

  
  var archives = new Array();
  var a;
  var m = new Object();
  var o;
  var year;
  var month;
  var years = new Array();
  var active_m = $('.sidebar_archives input[name=m]').val();
  var active_y = $('.sidebar_archives input[name=y]').val();
  var active_year = 0;
  var y_counter = 0;
  $('#archives_monthly li').each(function(){
    a = $(this).find('a');
    m = {};
    year = a.attr('title').split(' ')[1];
    month = a.attr('title').split(' ')[0];
    m.year = year;
    m.month = month;
    
    if (active_y == year && active_m == month) {
      a.addClass('active');
    }
    
    m.a = $(this).html();
    archives.push(m);
  });

  $('#archives a').each(function(){
    a = $(this);
    year = a.attr('title');
    if (active_y == year) {
      active_year = y_counter;
    }
    else {
      y_counter++;
    }
    o = '';
    o += '<h4>' + year + '</h4>';
    o += '<ul class="year">'
    for (var i in archives) {
      if (archives[i].year == year) {
        o += '<li>' + archives[i].a + '</li>';
      }
    }
    o += '</ul>'
    $(this).parent().append(o);
    a.remove();
  });
  $('#archives').show();
  $("#archives").accordion({active:active_year, autoHeight:false, collapsible:true, header: 'h4'});
  
  
  var search_default_text = 'Search Blog';
  if ( ! $('#s').val()) {
    $('#s').val(search_default_text);
  }
	$('#s').blur(function(){
		if ($(this).val() == '') {
			$(this).val(search_default_text);
		}			
	});
	$('#s').click(function(){
		if ($(this).val() == search_default_text) {
			$(this).val('');
		}
	});
});
