(function($){
	var newsBySections = $.newsBySections = {
			sections: [],
			controls: [],
			containerHeight: null,
			params: [],
			rolling: [false, false],
			pages: []
	};
	
	newsBySections.init= function(){
		var params = arguments[0];
		$.extend(this.params, this.defaults, params);
		
		var sectionWrappers = [$('#crimean_news_section'), $('#ukrainian_news_section')];
		var controls = [$('.control.left'), $('.control.right')];
		
		for(var i = 0; i < sectionWrappers.length; i++) {
			//this.sections[this.sections.length] = $('ul', sectionWrappers[i]).css('opacity', 0).show();
		  this.sections[this.sections.length] = $('ul', sectionWrappers[i]).css('left', '1000px').show();
			this.controls[this.controls.length] = {
					up: $('.up', controls[i]),
					down: $('.down', controls[i])
			};
		}

		// calculate params
		this.containerHeight = parseInt($('.main_news_wrapper').first().height());
		this._padToBlocks();
		this._initHandlers();
		//this._fadeInSections();
		this._revertSections();
	};
	
	newsBySections._padToBlocks = function(){
		$.each(this.sections, function(i, e){
			var items = $('li', e);
			var counter = 0;
			var pageHeight = 0;
			var i = 0;
			while(i < items.size()){
				var e = items.eq(i);
				if(e.height() + pageHeight + newsBySections.params.padded <= newsBySections.containerHeight) {
					pageHeight += e.height() + newsBySections.params.padded;
					counter++;
					i++;
				} else {
					items.eq(i-1).css('margin-bottom', parseInt(items.eq(i-1).css('margin-bottom')) + (newsBySections.containerHeight - pageHeight) + 'px');
					newsBySections.pages[newsBySections.length] = counter;
					pageHeight = counter = 0;
				}
			}
		});
	
	};
	
	newsBySections._roll = function(i, scroll) {
		var currentTop = parseInt(this.sections[i].css('top'));
		this.sections[i].animate({top: currentTop + scroll}, {duration: newsBySections.params.speed, complete: function() {newsBySections.rolling[i] = false;}});
	};
	
	newsBySections._initHandlers = function() {
		$.each(this.controls, function(i, e){
			e.up.click(function(event){
				if(newsBySections.rolling[i]) {
					return false;
				}
				newsBySections.rolling[i] = true;
				
				var scroll = newsBySections.containerHeight;
				var listTop = parseInt(newsBySections.sections[i].css('top'));
				if (listTop < 0) {
					if (Math.abs(listTop) < scroll * 2) {
						scroll = -listTop;
					}
					newsBySections._roll(i, scroll);
				} else {
					newsBySections.rolling[i] = false;
				}
				
				event.preventDefault();
			});

			e.down.click(function(event){
				if(newsBySections.rolling[i]) {
					return false;
				}
				newsBySections.rolling[i] = true;

				var scroll = newsBySections.containerHeight;
				var listTop = parseInt(newsBySections.sections[i].css('top'));
				var leftHeight = listTop + newsBySections.sections[i].height();
				if (leftHeight > newsBySections.containerHeight) {
					newsBySections._roll(i, -scroll);
				} else {
					newsBySections.rolling[i] = false;
				}

				event.preventDefault();
			});

		});
	};
	
	newsBySections._fadeInSections = function(){
		$.each(this.sections,function(i, e){
			e.animate({opacity: 1}, {duration: newsBySections.params.startupFadeInSpeed, complete: function(){
			  e.get(0).style['filter'] = '';
			}});
		});
	};
	
	newsBySections._revertSections = function(){
	  $.each(this.sections,function(i, e){
	    e.css('left', 0);
	  });
  };
		
	newsBySections.defaults = {
		speed: 1800,
		startupFadeInSpeed: 2000,
		padded: 8
	};
})(jQuery);
