/* ------------------------------------------------------------------------
 *	Class: rbpNews
 *	Author: Michal Skrzypek
------------------------------------------------------------------------- */

(function ($) {
    $.rbpNews = {
        version: '0.1'
    };
    $.fn.rbpNews = function (news_settings) {
    	//console.log("rbpNews");
		 news_settings = jQuery.extend({
		 	dev_url  : 'http://localhost:8888/redbranch/ci/rbp_c/public/rss2html.php?XMLFILE=http://test.redbranch.com.pl/blog/feed/&TEMPLATE=http://localhost:8888/redbranch/ci/rbp_c/public/rss_temp.html&MAXITEMS=3',
		 	dist_url : 'http://www.redbranch.eu/rss2html.php?XMLFILE=http://www.redbranch.eu/blog/feed/&TEMPLATE=http://www.redbranch.eu/rss_temp.html&MAXITEMS=3',
			pauseTime : 6000,
			animationSpeed: 400,
			animationEasing: "circEaseOut"
	}, news_settings);
		 
		 var sliderWidth, itemsLength, currentPosition;
		 var timer = 0;
		 
		$.rbpNews.initialize = function () {
            //console.log('rbpNews.initialize');
            settingsNews = news_settings;
            
            $.rbpNews.open();
            return false;
        }
        
        $.rbpNews.open = function (event) {
        	//console.log('rbpNews.open');
        	settingsNews = news_settings;
        	
        	$.get(settingsNews.dist_url, function(data) {
				
				$('body').find('.wordpress_rss')[0].innerHTML = data;
			  	 //console.log( $('.wordpress_news').size() );
			  	 
			  	 //$('.wordpress_news').find('.wp_news_desc')[0]innerHTML.trim(20);
			  	 
			  	 for(var i = 0; i < $('.wp_news_desc').size() ; i++) {
			  	 	var txt = $('.siabadaba')[i].innerHTML.substring(0, 150) + '... ';
			  	 	//console.log(txt);
			  	 	//var txt = $('.wordpress_news')[i].innerHTML.html().substring(0, 250) + '...';
					$('.siabadaba')[i].innerHTML = txt;
					//console.log('*************');
					//console.log($('.siabadaba')[i].innerHTML);
			  	 }
			  	 
			  	 
			  	 /*
			  	 $('.wp_news_desc').each(function () {
					//abridge($('.wp_news_desc'));
					//linkage($('.wp_news_desc'));
					console.log($('.wp_news_desc'));
					var txt = $('.wp_news_desc').html().substring(0, 250) + '...';
					$('.wp_news_desc').html(txt);
					
				});
				*/

			  	 if($('.wordpress_news').size() > 1) {
			  	 	itemsLength = $('.wordpress_news').size();
			  	 	$.rbpNews.bulidShow();
			  	 	$.rbpNews.buildKropki();
			  	 }
			  	
			});
        	
            return false;
        };
        
        $.rbpNews.bulidShow = function () {
        	//console.log('rbpNews.bulidShow');
        	
        	currentPosition = 0;
        	sliderWidth = itemsLength * 400;
        	//console.log(sliderWidth);
        	
        	$('body').find('ul.newslist').css('width',sliderWidth+'px');
        	
        	$.rbpNews.play();
        };
        
		$.rbpNews.play = function () {
			//console.log('rbpNews.play');
			
            timer = setInterval( function(){ newsRun(); }, settingsNews.pauseTime );
        };
        
        $.rbpNews.buildKropki = function () {
        
        	toInject = "";
        	kropkiWidth = 0;
        	
        	for(var i = 0; i < $('.wp_news_desc').size() ; i++)
        	{
        		toInject = toInject+'<div class="kropeczka"><a rel="'+i+'">'+i+'</a></div>';
        		kropkiWidth = (10+10)*i;
        	}
        
        	$('#redblog').find('#kropki')[0].innerHTML = toInject;
        	
        	$('#redblog').find('#kropki').css('bottom' , 10+'px' );
        	$('#redblog').find('#kropki').css('left' , (400/2 - kropkiWidth/2)+'px' );
        	
        	$('.kropeczka a:eq('+currentPosition+')').addClass('kropkaActive');
        	
        	 $('.kropeczka a').live('click', function(){
                //if(vars.running) return false;
                if($(this).parent().hasClass('active')) return false;
                clearInterval(timer);
                timer = '';
                //slider.css('background','url("'+ vars.currentImage.attr('src') +'") no-repeat');
                currentPosition = $(this).attr('rel') - 1;
                newsRun();
            });
        }
        
        function newsRun() {
        	//console.log('rbpNews.newsRun');
        	
        	clearInterval(timer);
            timer = '';
        	
        	if(currentPosition < itemsLength-1)
        	{
        		currentPosition++;
        		newLeft = -currentPosition*400;
        		
        		$('body').find('ul.newslist')
        		.animate({
					left : newLeft
				},settingsNews.animationSpeed , settingsNews.animationEasing );
        		
        	}
        	else if(currentPosition == itemsLength-1)
        	{
        		currentPosition=0;
        		
        		$('body').find('ul.newslist')
        		.animate({
					left : 0
				},settingsNews.animationSpeed , settingsNews.animationEasing );
        	}
        	
        	$('.kropeczka a').removeClass('kropkaActive');
        	$('.kropeczka a:eq('+currentPosition+')').addClass('kropkaActive');
			
			$.rbpNews.play();
		};
		
		 String.prototype.trim = function( len )
		{
			return this.substring( 0, len ) + "...";
		}
        
        
    };
        
})(jQuery);
