/*
 * 	Content Fit to the Screen 0.1 - jQuery plugin
 *
 *	Copyright (c) 2010 Kotelnitskiy Evgeniy (http://4coder.info)
 *	Licensed under the GPL (GPL-LICENSE.txt) licenses.
 */
 (function($) {
	jQuery.fn.CFS = function(options){
		// default configuration properties
		var defaults = {	
			height: 200,
			button_next_selector: ""
		}; 
			
		var options = jQuery.extend(defaults, options);  
		var this_el = jQuery(this);
		var content_el = this_el.children();
		content_el.css('overflow', 'hidden');
		var content_height = content_el.height();

		var pcount = Math.ceil(content_height / defaults['height']);
		var pcurrent = 0;
		if (pcount > 1) {
			jQuery(defaults['button_next_selector']).addClass('active');
		}

		this_el.css('height', defaults['height']);
		this_el.css('display', 'block');
		this_el.css('overflow', 'hidden');
		this_el.css('position', 'relative');
		content_el.css('position', 'absolute');
		content_el.css('left', '0');
		content_el.css('top', '0');
		
		jQuery(defaults['button_next_selector']).click(function() {
															
			if (pcount > pcurrent + 1) {
				pcurrent++;
				if (pcurrent + 1 == pcount) content_el.animate({'top': defaults['height'] - content_height},1500);
				else content_el.animate({'top': 0 - pcurrent * defaults['height']},1500);
				jQuery(defaults['button_prev_selector']).addClass('active');
			}
			if (pcount <= pcurrent + 1){
				jQuery(defaults['button_next_selector']).removeClass('active');
			}
			return false;
		});
		
		jQuery(defaults['button_prev_selector']).click(function() {
			if (pcurrent > 0) {
				pcurrent--;
				content_el.animate({'top': 0 - pcurrent * defaults['height']},1500);
				jQuery(defaults['button_next_selector']).addClass('active');
			}
			if (pcurrent <= 0){
				jQuery(defaults['button_prev_selector']).removeClass('active');
			}
			return false;
		});
	};

})(jQuery);
