$(function(){ 
	
	
    // gallery
	
    $("body").onImagesLoad({ 
        selectorCallback: selectorImagesLoaded 
    }); 
    
    //the selectorCallback function, invoked once when all images contained within $('body') have loaded 
    function selectorImagesLoaded($selector){ 
        $("div.galleryContainer div.box img").each(function (){
    		w = $(this).width();
    		$(this).parent().find("div.innerDiv").css("width", w+"px");
    	});
    } 
    
    
	// language
	
	var requestURI = window.location.href;
	var exp = /(\/[a-z]{2}\/)/; 
	
		
	
	if (exp.test(requestURI)) {
		var language = requestURI.match(exp)[0];
		language = language.substring(1,3);
	} else {
		var pathName = window.location.pathname;
				
		var pathPattern = /(\w+)\/(\S*)/;
		var path = pathName.match(pathPattern);
		
		if (path[1]=="author") {
			pathName='/'+path[1]+'/de/'+path[2];
		} else {
			pathName = "/de"+pathName;
		}

		window.location = pathName;
	}

});



$(document).ready(function(){
	
	// paragraph Dynamic
	
	$("div.paragraphDynamic").click(function(event){
		
		var exp = /admin/;
		
		if (!exp.test(this.className)) {
		
			exp = /open/;
					
			if (exp.test(this.className)) {
				$("div.innerDiv", this).slideUp('slow', function() {});
				$(this).removeClass('open');
			} else {
				$("div.innerDiv", this).slideDown('slow', function() {});
				$(this).addClass('open');
			}
		}
		
	});
	
	
	$("li.hoverImg").mouseover(function(event){
		$('img', this).attr('src', function() {
			  return this.src.replace(/link/, 'hover' );
		});
	});
	
	$("li.hoverImg").mouseout(function(event){
		$('img', this).attr('src', function() {
			  return this.src.replace(/hover/, 'link' );
		});
	});
	
	
	
	// Gallery
		
		
	$("div.gallery.public div.moveLeft").mouseenter(function(){
		$(this).parent().attr("hover", "left");
		moveStage($(this).parent());		
	});
	
	$("div.gallery.public div.moveLeft").mouseleave(function(){
		$(this).parent().attr("hover", "false");
	});
	
	$("div.gallery.public div.moveRight").mouseenter(function(){
		$(this).parent().attr("hover", "right");
		moveStage($(this).parent());		
	});
	
	$("div.gallery.public div.moveRight").mouseleave(function(){
		$(this).parent().attr("hover", "false");
	});
	
	// newsCarousel
	$("div.newsCarousel.public").first().fadeIn("slow", function(){
		setTimeout(function(){nextNewsCarousel(0)}, 12000);
	});
});

function nextNewsCarousel(currNewsCarousel) {
	if ($("div.newsCarousel.public").length>1) {
		$("div.newsCarousel.public:eq("+currNewsCarousel+")").fadeOut("slow");
		if (currNewsCarousel == ($("div.newsCarousel").length-1)) {
			$("div.newsCarousel.public").first().fadeIn("slow", function(){
				setTimeout(function(){nextNewsCarousel(0)}, 8000);
			});
		} else {
			$("div.newsCarousel.public:eq("+(currNewsCarousel+1)+")").fadeIn("slow", function(){
				setTimeout(function(){nextNewsCarousel(currNewsCarousel+1)}, 8000);
			});
		}
	}
	
}


function moveStage(gallery) {
		
	var galleryContainer = gallery.children("div.galleryContainer:first");
	
	if (gallery.attr("hover")=="left") {
		if (!gallery.attr("stage")) {stage = 2;} else {stage = parseInt(gallery.attr("stage"))+1;}
	} else {
		if (gallery.attr("hover")=="right") {
			if (!gallery.attr("stage")) {stage = 1;} else {stage = parseInt(gallery.attr("stage"))-1;}
		}
	}
	
	
	if ((stage>0) && (stage < galleryContainer.children("div.box").length+1) && (gallery.attr("hover")!="false")) {
		gallery.attr("stage", stage);
		
		currStage=galleryContainer.children("div.box:nth-child("+(stage)+")");
		currPos=currStage.position();
		
		corr = 30;
		
		if (stage==1) {
			movePos = -corr;
		} else {
			if (stage < galleryContainer.children("div.box").length) {
				movePos = currPos.left - ((gallery.width()-currStage.width())/2);
			} else {
				movePos = currPos.left - (gallery.width()-currStage.width()) + (2*corr);
			}
		}
		
		galleryContainer.animate({left: -movePos}, 800, function(){
			$(this).clearQueue();
			setTimeout(function() {moveStage(gallery)}, 1200);
		});
	}
}


function changeLanguage(language) {
	
	var requestURI = window.location.href;
	
	requestURI = requestURI.replace(/(\/[a-z]{2}\/)/g, '/'+language+'/');
	
	window.location = requestURI;
}
