var J = jQuery.noConflict();

var groupElementsBy = 3; 						// Anzahl der gleichzeitig angezeigten Elemente
var speed = 8000;

var actPos = 1;
var newPos = 1;
var amountElements = 0;							
var elementWidth = 0;
var buttonCount = 0;
var next = undefined;


J(document).ready(function() {

	amountElements = J(".overview li").length;			// Anzahl der gesamtent Elemente
	elementWidth = 240;
	buttonCount = Math.ceil(amountElements/groupElementsBy);
	
	i = 1;
	
	while(i <= buttonCount) {
		J("#cont-button").append('<img src="fileadmin/templates/www.move-thalheim.at/images/slider-navigation-symbol.png" name="'+i+'" class="pageButton" border="0" />');	
		i++;
	}
	
	maskContentWidth = (elementWidth*amountElements*2) + "px";
	
	J(".overview").css("width", maskContentWidth);
	
	J(".pageButton:first-child").attr("src","fileadmin/templates/www.move-thalheim.at/images/slider-navigation-symbol-active.png");
	
	J(".pageButton").hover(function() {
		
		J(this).css("cursor","pointer");
		
	});
	
	J(".pageButton").click(function() {
		
		go(J(this).attr("name"));
		
	});
});

window.setInterval("go()",speed);

var cloned = false;	

function go(next) {
	
	if(next == undefined) {
	
		newPos = 1+Math.round(actPos);
		
		 if (actPos == buttonCount) {
			  newPos = 1;
		 }
			
	} else {
		newPos = next;
		next = undefined;
	}


    J(".pageButton").attr("src","fileadmin/templates/www.move-thalheim.at/images/slider-navigation-symbol.png");
	
	J(".pageButton").each(function() {
		
		if(J(this).attr("name") == newPos) {
			
			J(this).attr("src","fileadmin/templates/www.move-thalheim.at/images/slider-navigation-symbol-active.png");
		}
    });
	
	if(actPos - newPos > 0) {
		
		var factor = Math.round(actPos) - Math.round(newPos) - Math.round(buttonCount);	
		
	} else {
		
		var factor = Math.round(actPos) - Math.round(newPos);	
	}
	
	if(cloned == true) {
			
		J(".overview li:lt("+amountElements+")").remove();
	
		J(".overview").css("left", (actPos-1)*(-720)+"px");	
	
		cloned = false;
	}
    
	if(actPos-newPos > 0) {
		
		J(".overview li").clone().appendTo(".overview");
		
		cloned = true;
	} 
	
	actPos = newPos;
	
	if (J("overview").position()) {
		var tempLeft = (J(".overview").position().left)-(J(".viewport").width())*Math.abs(factor)+"px";
	}
	J(".overview").animate({"left": "-="+(720)*Math.abs(factor)},{"duration":500});
}


// JavaScript Document

/* append zoom button to images */
J(document).ready(function()
{
	var numImgs = J(".csc-textpic").find("img").length;
    
    if((numImgs-2) > 1)
    {
	    J('.pic').append('<div style="position: absolute; right: 10px; top: 10px; height: 28px; width: 28px;" id="zoom"><img src="fileadmin/templates/www.move-thalheim.at/images/image-zoom.jpg" width="28" height="28" alt="Bildergalerie öffnen" title="Bildergalerie öffnen" /></div>');
    }
    
    var numImgs = J(".news-single-img").find("img").length;
    
    if(numImgs >= 1)
    {
	    J('.pic').append('<div style="position: absolute; right: 10px; top: 10px; height: 28px; width: 28px;" id="zoom"><img src="fileadmin/templates/www.move-thalheim.at/images/image-zoom.jpg" width="28" height="28" alt="Bildergalerie öffnen" title="Bildergalerie öffnen" /></div>');
    }
	//console.log(numImgs);
});
