$.widget('ui.articles', {

	_init: function() {
		var $items = this.element.find('.content > ul');
		var data = [
			$items.eq(0).html(),
			$items.eq(1).html(),
			$items.eq(2).html()
		];

		$items.eq(1).remove();
		$items.eq(2).remove();

		this.element.find('.menu').show();
		this.element.find('.menu ul li a').bind('click', this, function(event) {
			event.data.type($(this).closest('ul').find('a').index(this) + 1);
		});
		this.element.find('.pagination input').bind('keyup', this, function(event) {
			event.data.change(parseInt($(this).val(), 10));																			 
		});
		this.element.find('.pagination .previous, .pagination .next').bind('click', this, function(event) {
			event.data.change($(this).hasClass('previous') ? '-1' : '1');
		});

		this._setData('data', data);
		this.element.find('.menu ul li a').eq((String(document.location).split('/').pop() == 'faq' || data[2] == null ? 0 : 1)).trigger('click');
   },


	change: function(offset) {
		var min, max;
		var limit = 10;
		var $items = this.element.find('.content > ul li');
		var pageCount = Math.ceil($items.length / limit);

		switch (offset) {
      	case '-1':
         	index = (index == 1 ? pageCount : index - 1);
            break;
         case '1': 
         	index = (index == pageCount ? 1 : index + 1);
         	break;
			default:
         	index = offset;
      }
		
		if (isNaN(index)) return;
		
		$items.hide().removeClass();

		index = Math.max(1, Math.min(index, pageCount));
		min = (index - 1) * limit;
		max = min + limit;

		for (var i = min; i < max; i++) {
			$items.eq(i).show();
		}
		
		$items.filter(':visible:first').addClass('first');
		$items.filter(':visible:last').addClass('last');

		this.element.find('.pagination')[pageCount == 1 ? 'hide' : 'show']();
		this.element.find('.pagination .previous').toggleClass('disabled', (pageCount == 1));
		this.element.find('.pagination .next').toggleClass('disabled', (pageCount == 1));
		this.element.find('.pagination input').val(index);
		this.element.find('.pagination .page .total').html('of ' + pageCount);
		this.element.find('.pagination .buttons').toggleClass('hidden', (pageCount == 1));
		
		$(window).trigger('resize');
	},


	type: function(index) {
		var $buttons = this.element.find('.menu ul li a');
		var $button = $buttons.eq(index - 1);

		$buttons.removeClass('active');
		$button.addClass('active');

		this.element.find('.content > ul').html(this._getData('data')[index - 1]);
		this.change(1);
	}

});


$().ready(function() {
   $('#articles .search').corner({ tl: { radius: 2 }, tr: { radius: 2 }, bl: { radius: 2 }, br: { radius: 2 } });
	$('#articles .search form').validate();
	$('#articles .results').results();
	$('#articles .items').articles();
	$('#articles .article').article();

	$(window).resize(function() {
		var $parent = $('#article-category .items ul');
		var $items;

		$parent.find('li h3').equal('height');

		for (var i = 0; i < $parent.length; i++) {
			$items = $parent.eq(i).find('> li');
			
			$items.find('.wysiwyg').equal('height');
			$items.find('.links-first').equal('height');
			$items.find('.content').equal('height', true);
			$items.find('.content').equal('height');
		}
		
		$('#articles .column1, #articles .column2, #articles .column3').equal('height');
		$('#article-category .column1, #article-category .column2').equal('height');
	});
	
	$(window).trigger('resize');
});
