$(document).ready(function(){
	initPopup();
	
	initSlider();
});


// provides functionality for the popups (colorbox)
function initPopup(){
	$('[rel=cms-inhalt-popups-colorbox]').each(function(i){ 
		$(this).colorbox({inline:true, href:'.inline:eq(' + i + ')', width:'70%', height:'70%'}); 
	}); 
	
	// trigger the first colorbox manually
	$('[rel=cms-inhalt-popups-colorbox]:first').colorbox({open:true});
}


// provides functionality for the slider (e.g. newsbar)
function initSlider(){
	$('.cms-inhalt-slider').each(function(){
		
		var availableWidth = $(this).width();								  
										  
		// wrap content
		$(this).html("<div class='cms-inhalt-wrapper'>"+ $(this).html() +"</div>")
		
		// get the wrap
		var wrapEl = $("div.cms-inhalt-wrapper", this);
		
		// get width of wrapper
		var w = 0
		$(wrapEl).children().each(function(){
			var cw = $(this).width();
			if (cw > w)w=cw;
		})
		
		// pixels to slide per iteration
		var step = 1;
		
		// init margin left variable
		var ml = 0;
		
		// this is the function which will be called recursively
		var slide = function(){
			// get margin left
			//ml = parseInt($(wrapEl).css("margin-left"));
			ml = ml - step;
			
			if ((ml < 0) && Math.abs(ml) > w){
				ml = availableWidth;
			};
			
			$(wrapEl).css("margin-left", ml-step);
			setTimeout(function() {
				slide();
			}, 25);
		}
		slide();
	})
};
