function replaceFormat(source, format) {
      var protocol='';
      if(source.indexOf('http://')>-1) {
            protocol='http://';
            source=source.replace('http://','');
      }else if(source.indexOf('https://')>-1) {
            protocol='https://';
            source=source.replace('https://','');
      }
      source=source.split('/');
      source[3]=format;
      return protocol+source.join('/');  
};

// Set up cycle
$('#slideshow .slide').cycle({
	interval : 5000,
	keep : true,
	navigation : true,
	previous : false,
	next : false
});


 $('.caption p span').shorten({

 length : 130

  });
 
  $('.activity p span').shorten({

 length : 70

  });

 $('.directkader p span').shorten({

 length : 100

  });
 
$('.newskader p').shorten({

 length : 80

  });

$('.highlighted-description p span').shorten({

 length : 100

  });

// Set up image gallery
var gallery = $('#photo-main #center img'),
	thumbs = $('#photo-main #right-column .foto-thumb');
	
if(gallery.length){
	
	gallery.each(function(){
		
		var thumb = $(this).clone(),
			src = thumb.attr('src'),
			newSrc = replaceFormat(src, 'fotothumb');
		
		thumb.attr('src', newSrc);
		var link = $('<a href="#" class="el" />');
		link.append(thumb);
		thumbs.append(link);
		
	});
	
}

var buttons = $('#photo-main #right-column .el');

if(gallery.length){
	
	if(gallery.length > 1){
	
		gallery.cycle({
			interval : 10000,
			speed : 300,
			navigation : false,
			onCycle : function() {
				buttons.removeClass('active');
				buttons.eq(this.getCurrentIndex()).addClass('active');
			}
		});

		buttons.each(function(index) {
			var el = $(this);
			$.data(el, 'index', index);
			el.bind('click', function(e) {
				gallery.showIndex($.data(el, 'index'));
				e.preventDefault();
			});
		});
		
	} else {
		
		buttons.addClass('active');
		
		buttons.bind('click', function(e) {
			e.preventDefault();
		});
		
	}
	
	if($('#photo-main .page-button').length){
		var clear = $('<div class="clear" />');
		$('#photo-main #right-column').append(clear, $('#photo-main .page-button'));
	}
	
}
