function current_page(p) {
    var mypage = '.page_' + String(p);
    $( '.pagination a' ).removeClass( 'page-current' ).addClass( 'page-other' );
    $( mypage ).removeClass( 'page-other' ).addClass( 'page-current' );
};

function topic_page() {
    var s_array = new Array();
    var e_array = new Array();
    var s = '';
    var e = '';
    var destination = AJAX_LINK;
    var pageNum = 1;

    $( 'a.turned-on' ).each(function(n) {
	s_array.push(this.id.replace(/ss/, ''));
    });
    if ( s_array )
    {
	s = '&s=' + s_array.join(',');
    };

    $( 'a.turned-off' ).each(function(n) {
	e_array.push(this.id.replace(/ss/, ''));
    });
    if (e_array )
    {
	e = '&e=' + e_array.join(',');
    };

    try {
	pageNum = $( this ).text();
	if ( ! /^[1-9][0-9]*$/.test(pageNum) ) {
	    pageNum = '1';
	};
    }
    catch(err) {
	pageNum = '1';
    };

    if ( destination )
    {
	$( '#articles' ).html( '&nbsp;' ).load( destination + '?p=' + pageNum + s + e );
	current_page(pageNum);
    };
};

function add_dynamic() {
    $( '#spinner' ).hide();
    $( '.more' ).fadeTo( 'normal', 0.01 );

    $( '.item' ).hover(
        function() { $( '.more', this ).fadeTo( 'normal', 1 );
                     $( this ).addClass( 'itemborder' );},
        function() { $( '.more', this ).fadeTo( 'normal', 0.01 );
                     $( this ).removeClass( 'itemborder' );}
    );

    var max_page = Number($( '.paginator-max' ).text());
    for ( var p = 1 ; p <= 10 ; p++) {
      if (p <= max_page) {
	$('.page_' + String(p)).show();
      }
      else {
	$('.page_' + String(p)).hide();
      };
    };
};

function turn_on() {
  $( this ).parent('a').removeClass( 'turned-off' );
  if ( $( this ).parent('a').hasClass('turned-on') ) {
    $( this ).parent('a').removeClass( 'turned-on' );
    $( this ).parent('a').children('.tick').fadeTo( 'fast', 0.3);
    $( this ).parent('a').children('.cross').fadeTo( 'fast', 0.3);
  }
  else {
    $( this ).parent('a').addClass( 'turned-on' );
    $( this ).parent('a').children('.tick').fadeTo( 'fast', 1);
    $( this ).parent('a').children('.cross').fadeTo( 'fast', 0.3);
  };
  topic_page();
};

function turn_off() {
  $( this ).parent('a').removeClass( 'turned-on' );
  if ( $( this ).parent('a').hasClass('turned-off') ) {
    $( this ).parent('a').removeClass( 'turned-off' );
    $( this ).parent('a').children('.tick').fadeTo( 'fast', 0.3);
    $( this ).parent('a').children('.cross').fadeTo( 'fast', 0.3);
  }
  else {
    $( this ).parent('a').addClass( 'turned-off' );
    $( this ).parent('a').children('.tick').fadeTo( 'fast', 0.3);
    $( this ).parent('a').children('.cross').fadeTo( 'fast', 1);
  };
  topic_page();
};

function turn_over() {
  if ( $( this ).parent('a').hasClass('turned-on') ) {
    $( this ).parent('a').removeClass( 'turned-on' ).addClass( 'turned-off' );
    $( this ).parent('a').children('.tick').fadeTo( 'fast', 0.3);
    $( this ).parent('a').children('.cross').fadeTo( 'fast', 1);
  }
  else if ( $( this ).parent('a').hasClass('turned-off') ) {
    $( this ).parent('a').removeClass( 'turned-on' ).removeClass( 'turned-off' );
    $( this ).parent('a').children('.tick').fadeTo( 'fast', 0.3);
    $( this ).parent('a').children('.cross').fadeTo( 'fast', 0.3);
  }
  else {
    $( this ).parent('a').removeClass( 'turned-off' ).addClass( 'turned-on' );
    $( this ).parent('a').children('.tick').fadeTo( 'fast', 1);
    $( this ).parent('a').children('.cross').fadeTo( 'fast', 0.3);
  };
  topic_page();
};

var j = 1;
$.fn.columnize = function columnize(options) {
    var defaults = {
	maxColumns : 2,
	minItems : 4
    };

    $(this).each(function() {
	var settings = $.extend(defaults, options);
	var totalListElements = $(this).children('li').size();
	var listClass = $(this).attr('class');
	var myColumns = settings.maxColumns;
	var baseColItems = Math.ceil(totalListElements / myColumns);

	// Make sure each column has at least settings.minItems items
	while (myColumns > 1 && baseColItems < settings.minItems) {
	    myColumns--;
	    baseColItems = Math.ceil(totalListElements / myColumns);
	};

	// Create numbered list containers
	for (i = 1; i <= myColumns; i++) {
	    if (i == 1) {
		$(this).addClass('listcol1').removeClass('last').wrap('<div class="multicol listcontainer' + j + '"></div>');
	    } else if ($(this).is('ul')) { // Check whether the list is ordered or unordered
		$(this).parents('.listcontainer' + j).append('<ul class="listcol' + i + '"></ul>');
	    } else if ($(this).is('ol')) { // Check whether the list is ordered or unordered
		$(this).parents('.listcontainer' + j).append('<ol class="listcol' + i + '"></ul>');
	    } else {
		// Should not reach here
	    }
	    $('.listcontainer' + j + ' > ul,.listcontainer' + j + ' > ol').addClass(listClass);
	};

	// Append the list items
	var listItem = 0;
	$(this).children('li').each(function() {
	    thisColumn = Math.floor(listItem++ / baseColItems) + 1;
	    if (thisColumn > 1) {
		$(this).parents('.listcontainer'+j).find('.listcol'+thisColumn).append(this);
	    };
	});

	// Set class last on the last UL or OL
	$('.listcontainer'+j).find('ol:last,ul:last').addClass('last');

	// Use a new .listContainer for the next usage
	j++;
	});
};

function imageResize(img, imgwidth) {
    var oldwidth = img.width();
    var oldheight = img.height();
    var imgheight;

    if(oldwidth > imgwidth) {
	imgheight = Math.ceil(oldheight / oldwidth * imgwidth);
	img.css("height", imgheight);
	img.css("width", imgwidth);
    };
};

$( document ).ajaxStart( function() { $( '#spinner' ).show();
           }).ajaxStop(  function() { add_dynamic();
});

$(document).ready(function() {
    add_dynamic();
    current_page(1);

    $('.optional').hide();
    $('.expose').click(function(){
	$(this).children('.optional').show();
    });

    $('.tick').fadeTo( 'fast', 0.3 ).click(turn_on);
    $('.cross').fadeTo( 'fast', 0.3 ).click(turn_off);
    $('.columns2').columnize({maxColumns:2});
    $('.columns3').columnize({maxColumns:3});
    $('.label').click(turn_on);

    $('img.resize300').each(function () {
	if ($(this).width() > 300 || $(this).complete) {
	    imageResize($(this), 300);
	}
	else {
	    $(this).load(function() {
		imageResize($(this), 300);
	    });
	}});

    $('.pagination a').click(topic_page);
});

var RecaptchaOptions = {
   theme : 'white'
};

