/********************** truncateWord ************************
 *
 * Usage: Truncate num of words in one line adding '...'
 * on the end of truncated sentence
 * 
 * */
(function($){
	$.fn.extend({
	
	truncateWord: function(options) {
		var defaults = {
			width: 'auto',
			after: ' ...'
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			
			if(options.width=='auto'){ truncateWidth=$(this).width()-1; }else{ truncateWidth = options.width}
			
			if($(this).width()>truncateWidth){		 
				
				var new_text = $(this).text();
				
				$(this).html('<span id="truncateWrapper" style="display:inline;">'+new_text+options.after+'</span>');
				
				while($('#truncateWrapper').width() > truncateWidth) {					
					
					new_text = new_text.substring(0, new_text.lastIndexOf(" "));
					$('#truncateWrapper').html(new_text + options.after);
				}
				
				$(this).html($('#truncateWrapper').html());
				}				
			});			
		}	
	});
})(jQuery);

/********************** truncateHeight ************************
 *
 * Usage: Truncate paragraph to some height adding '...'
 * on the end of truncated sentence
 * 
 * */
(function($){
	$.fn.extend({
	
	truncateHeight: function(options) {
		var defaults = {
			width: 'auto',
			after: ' ...',
			lines: 2
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			
			truncateHeight=$(this).height()-1;			
			alert(parseInt($(this).css('line-height')));
			
			if($(this).height()>truncateHeight){		 
				
				var new_text = $(this).text();
				
				$(this).html('<span id="truncateHeight" style="">'+new_text+options.after+'</span>');				
					
				while($('#truncateHeight').height() > truncateHeight) {
									
					
					new_text = new_text.substring(0, new_text.lastIndexOf(" "));
					$('#truncateHeight').html(new_text + options.after);
				}
				
				$(this).html($('#truncateHeight').html());
				}				
			});			
		}	
	});
})(jQuery);

/********************** truncateLines ************************
 *
 * Usage: Truncate to num of lines of some paragraph 
 * adding '...' on the end of truncated sentence
 * 
 * */
(function($){
	$.fn.extend({
	
	truncateLines: function(options) {
		var defaults = {
			width: 'auto',
			after: ' ...',
			lines: 2
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			
			//$(this).css('display','none');
			truncateLines = parseInt($(this).css('line-height')) * options.lines;
			
			if($(this).height()>truncateLines){		 
				
				var new_text = $(this).text();
				
				$(this).html('<span id="truncateLines" style="">'+new_text+options.after+'</span>');	
				
				
				while($('#truncateLines').height() > truncateLines) {	
					
					new_text = new_text.substring(0, new_text.lastIndexOf(" "));
					$('#truncateLines').html(new_text + options.after);
				}
				
				$(this).html($('#truncateLines').html());
				//$(this).css('display','block');
				}				
			});			
		}	
	});
})(jQuery);


/************************************* Cookies *********************************/
function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
    var cookie_string = name + "=" + escape ( value );

    if ( exp_y )  {
	    var expires = new Date ( exp_y, exp_m, exp_d );
	    cookie_string += "; expires=" + expires.toGMTString();
    }

    if ( path ){
        cookie_string += "; path=" + escape ( path );
    }
  
    if ( domain ){
        cookie_string += "; domain=" + escape ( domain );
    }
  
    if ( secure ){
        cookie_string += "; secure";  
    }
  
    document.cookie = cookie_string;
}

function get_cookie ( cookie_name )
{
    var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

    if ( results ){
        return ( unescape ( results[2] ) );
    }else{
        return null;
    }
}


/*************** verticalSlide za Live widget ****************
/*************** verticalSlide za Live widget ****************
 *
 * Usage: for vertical scroll list with different <li> heights
 * <ul> must be in some container like <div> ....
 * 
 * */
(function() {

	$.fn.extend({
		
		verticalSlide : function(options){
		
			var defaults = {			
				pause: 		4000,
				speed: 		500
			};
			
			var options = $.extend(defaults, options);
			
			this.each(function() { 
				
				var object = $('ul', this);
				object.css({'position': 'absolute'})
				
				function slideverticalSlide (obj){
					// slide one row
					
					// check for addon time (from markup) and reset it
					var addon = $(obj).attr('class')
					if(addon.indexOf("addon" != -1)){
						
						addon = parseInt(addon.substring(6));
						$(obj).attr('class', '').addClass('addon_0')
					}
					
					$(obj).clone().appendTo(object).hide().fadeIn(function(){
						
						$(object).delay(options.pause + addon).animate({
							'top': - $(obj).outerHeight()
						}, options.speed, function(){
							$(obj).remove();
							$(object).css({'top': '0'});
							slideverticalSlide ($('li:first', object));
						})										    
					})
				}		
						
				slideverticalSlide ($('li:first', object));				
			})	
		}
	})
})(jQuery)



 /*----------------------------------------------------------------------------------*/
/*************** reload live feed ****************
 *
 * Usage: for vertical scroll list with different <li> heights
 * <ul> must be in some container like <div> ....
 * 
 * */
function reload_live_feed (articleID, _reload_live_feed_url){    
	
	//_reload_live_feed_url = '/www/json/newjson.json'
	
    $.ajax({
        url: _reload_live_feed_url,
        dataType: 'JSON',
        success: function(json){
            
            var items = [];
            var jsonObj = $.parseJSON(json)
            
            $.each(jsonObj.data, function(i,item) {
            	// build li items
            	if(i == 0){var addon = 3000}else{addon = 0}
                items.push('<li class="addon_' + addon + '"><span class="gutter"><strong>' + jsonObj.data[i].time_slice + '</strong> ' + jsonObj.data[i].description + '</span></li>');
            });            
            
            $('.article' + articleID + ' ul').empty();            
            $('.article' + articleID + ' ul').html(items.join(''));          
            
            var articleTitle = jsonObj.heading;       
        	$('.article' + articleID + ' .live_title a.title').attr('title', articleTitle)
            
            // if sport results box
            if($('.article' + articleID + ' .live_title .half_1 .rez').length > 0){
            	
            	var rez1 = jsonObj.result.home;
            	var rez2 = jsonObj.result.guest;
            	
            	$('.article' + articleID + ' .live_title .half_1 .rez').text(rez1);
            	$('.article' + articleID + ' .live_title .half_2 .rez').text(rez2);
            	
            } else {
            	// live box add title
            	$('.article' + articleID + ' .live_title a.title').text(articleTitle)
            }
             
            setTimeout(function(){
                 reload_live_feed(articleID, _reload_live_feed_url);                 
            }, 60000);
        },
        error: function(){
        	
            setTimeout(function(){
                 reload_live_feed(articleID, _reload_live_feed_url);                 
            }, 10000);
        }
    }); 
}




































