// base = "/schueler/";
base = "/";

$(function(){
	
	$('.diashow_cont').each(function(){
		setDiaTimer($(this));
		$(this).data({active:1,max:$(this).find('img').size()});
		$(this).find('img:not(:first-child)').hide();
		$(this).find('.diashow_title span:not(:first-child)').hide();
		$(this).find('.diashow_tab').click(function(){
			var cont = $(this).parents('.diashow_cont');
			var max = parseInt(cont.data('max'));
			var num = parseInt($(this).children(0).html());
			var active = cont.data('active');
			if (num != active){
				cont.find('img').fadeOut();
				cont.find('img').eq(num-1).fadeIn();
				
				cont.find('.diashow_tab').eq(active-1).removeClass("diashow_tab_on");
				cont.find('.diashow_tab').eq(active-1).addClass("diashow_tab_off");
				cont.find('.diashow_tab').eq(num-1).removeClass("diashow_tab_off");
				cont.find('.diashow_tab').eq(num-1).addClass("diashow_tab_on");
				
				cont.find('.diashow_title span').hide();
				cont.find('.diashow_title span').eq(num-1).show();
				
				cont.data('active',num);
				
				setDiaTimer(cont);
			}
		});
	});
	
	// ** Projekte page diashow
	$('.projekte_high_container').each(function(){
		var length = $(this).find('.projekte_high_box').length;
		$(this).data({max:length, cur:1});
		$(this).next().find('.js_projekte_next').click(function(){
			var cont = $(this).parent().prev();
			if (cont.data("cur") < cont.data("max")-1){
				cont.data("cur", cont.data("cur")+1);
				cont.find('.projekte_high_box').each(function(){
					$(this).animate({left:'-=442'},1000);
				});
				if (cont.data("cur") == cont.data("max")-1){
					$(this).attr('src', $(this).attr('src').replace("on", "off"));
				}
				console.log($(this).next().next());
				$(this).next().next().attr('src', $(this).next().next().attr('src').toString().replace("off", "on"));
			}
		});
		$(this).next().find('.js_projekte_prev').click(function(){
			var cont = $(this).parent().prev();
			if (cont.data("cur") > 1){
				cont.data("cur", cont.data("cur")-1);
				cont.find('.projekte_high_box').each(function(){
					$(this).animate({left:'+=442'},1000);
				});
				if (cont.data("cur") == 1){
					$(this).attr('src', $(this).attr('src').replace("on", "off"));
				}
				$(this).prev().prev().attr('src', $(this).prev().prev().attr('src').toString().replace("off", "on"));
			}
		});
	});
	$('.projekte_high_box').each(function(index){
		var marginleft = index * 442;
		$(this).css('left', marginleft);
	});
	
	// ** News list
	
	$('.news_listitem').mouseover(function(){
		if (!$(this).hasClass('active')) $(this).addClass('hover');
	});
	$('.news_listitem').mouseout(function(){
		if (!$(this).hasClass('active')) $(this).removeClass('hover');
	});
	
	// ** Projekte lightbox
	var tempArray = new Array()
	var images = $('.js_projekte');
	images.each(function(){
		tempArray.push(new Array($(this).attr('src'),$(this).next().find('.projekte_box_headertext').html()));
	});
	
	images.lightBox({tempArray:tempArray});
	
});

function setDiaTimer(cont){
	
	cont.stopTime();
	
	cont.oneTime(5000, function(){
		var active = cont.data('active');
		var max = cont.data('max');
		var next = ((active) % max) + 1;
		//console.log( cont.data());
		cont.find('.diashow_tab').eq(next-1).click();
		//$(this).data({active:next});
	});
	// alert(cont);
}

function setSendForm(){
	// ** Empfehlen Form
	
	$('#empfehlenform').submit(function(){
		var email = $(this).find('input[name=email]').val();
		var name = $(this).find('input[name=name]').val();
		var url = $(this).find('input[name=url]').val();
		$.post("redirect.php", {action:"empfehlen", email:email, name:name, url:url}, function(){
			$('#empfehlenform').remove();
			$('#empfehlen_content span').html("Ihre Email wurde verschickt.");
		});
		return false;
	});
	
	$('.js_close').click(closeSend);
}

function showSend(){
	var ltURL = String(window.location);
	// ltURL = "http://www.google.de";
	if ($('#empfehlen').length == 0){
		$.post(base+"includes/send.php", {url:ltURL, base:base}, function(data){
			$('body').append(data);

			var rootview = $(window).scrollTop();
			// var rootwidth = $('#root').width();
			// var rootheight = $('#root').height();
			var rootX = $('#root').offset().left;
			$('#empfehlen').css({left:rootX + 750, bottom:-rootview + 30, opacity:0});
			
			$('#empfehlen').animate({bottom:"+=30px", opacity:1},500);
			
			setSendForm();
		});
	}
}

function closeSend(){
	$('#empfehlen').animate({bottom:"-=30px", opacity:0},500,function(){
		$(this).remove();
	});
	// $('#empfehlen').remove();
}
