var current_autofade = null;
var timing_autofade = 3000;
var in_autofade_before_extern = true;
var banner_timer = null;
function banner_setup()
{
	//setup autofade 
	var offset = $("#content").offset();
	if($(".faderblock").find(".autofade").get(0) != undefined)
	{
		$(".faderblock").attr("inautofade", "true");
		current_autofade = $(".faderblock").children(".autofade").children(".fadecontent").first();
		current_autofade.offset(offset);
		current_autofade.animate({opacity:1}, 1000);
		banner_timer = setTimeout("banner_fadenext();", timing_autofade);
	}
	//buttons
	//offset.top += 400;
	
	$(".faderblockbuttons").children("div").each(function(){
		if($(this).attr("showid") != "none")
		{
			$(this).css("cursor", "pointer");
			$(this).click(function(){
				switch($(this).attr("showid"))
				{
					/*case "startauto":
						$(".faderblock").attr("inautofade", "true");
						clearTimeout(banner_timer);
						banner_fadenext();
						break;*/
					case "externlink":
						window.open($(this).attr("link"),'Youtube','left=20,top=20,width=900,height=700,toolbar=1,resizable=1');
						break;
					default: 
						$(".faderblock").attr("inautofade", "false");
						if( $("#" + $(this).attr("showid")).get(0) != current_autofade.get(0))
						{
							current_autofade.stop().animate({opacity:0}, 300, function(){$(this).css("left", "-2000px");});
							$("#" + $(this).attr("showid")).offset($("#content").offset());
							$("#" + $(this).attr("showid")).css({"left": "0px", "top": "0px"});
							$("#" + $(this).attr("showid")).animate({opacity:1}, 300);
							//current_autofade.stop().css({"opacity": "0", "filter": "alpha(opacity=0)", "left": "-2000px"});
							//$("#" + $(this).attr("showid")).css({"opacity": "1", "filter": "alpha(opacity=100)"});
							current_autofade = $("#" + $(this).attr("showid"));
						}
						
						$(".faderblockbuttons").find('div[showid="startauto"]').css("display", "block");
						$(".faderblockbuttons").find('div[showid="stopauto"]').css("display", "none");
						break;
				}
			});
		}
	});
	$(".faderblockbuttons").children('div[id="fotoknop"]').children("div").each(function(){
		if($(this).attr("showid") != "none")
		{
			$(this).css("cursor", "pointer");
			$(this).click(function(){
				switch($(this).attr("showid"))
				{
					case "startauto":
						$(".faderblock").attr("inautofade", "true");
						clearTimeout(banner_timer);
						banner_fadenext();
						//$(this).children("img").attr("src", "/css/front/img/play_active.gif");
						//$(this).parent().children('div[showid="stopauto"]').children("img").attr("src", "/css/front/img/pauze.gif");
						$(this).css("display", "none");
						$(this).parent().children('div[showid="stopauto"]').css("display", "block");
						break;
					case "stopauto":
						clearTimeout(banner_timer);
						//$(this).children("img").attr("src", "/css/front/img/pauze_active.gif");
						//$(this).parent().children('div[showid="startauto"]').children("img").attr("src", "/css/front/img/play.gif");
						$(this).css("display", "none");
						$(this).parent().children('div[showid="startauto"]').css("display", "block");
						break;
					case "nextfoto":
						$(".faderblock").attr("inautofade", "true");
						clearTimeout(banner_timer);
						banner_fadenext();
						clearTimeout(banner_timer);
						//$(this).parent().children('div[showid="startauto"]').children("img").attr("src", "/css/front/img/play.gif");
						//$(this).parent().children('div[showid="stopauto"]').children("img").attr("src", "/css/front/img/pauze_active.gif");
						$(this).parent().children('div[showid="startauto"]').css("display", "block");
						$(this).parent().children('div[showid="stopauto"]').css("display", "none");
						break;
					case "prevfoto":
						$(".faderblock").attr("inautofade", "true");
						clearTimeout(banner_timer);
						banner_fadeprev();
						clearTimeout(banner_timer);
						//$(this).parent().children('div[showid="startauto"]').children("img").attr("src", "/css/front/img/play.gif");
						//$(this).parent().children('div[showid="stopauto"]').children("img").attr("src", "/css/front/img/pauze_active.gif");
						$(this).parent().children('div[showid="startauto"]').css("display", "block");
						$(this).parent().children('div[showid="stopauto"]').css("display", "none");
						break;
				}
			});
		}										 
	});
	
	color_buttons();
	$(window).resize(function() {
		color_buttons();
	});
}


function banner_fadenext()
{
	if($(".faderblock").attr("inautofade") == "true")
	{
		var next_autofade = null;
		if(current_autofade.next().parent().hasClass("autofade"))
			next_autofade = current_autofade.next();
		else
			next_autofade = $(".faderblock").children(".autofade").children(".fadecontent").first();
		offset = $("#content").offset();
		next_autofade.offset(offset);
		if(next_autofade.get(0) != current_autofade.get(0))
		{
			next_autofade.animate({opacity:1}, 1000);
			current_autofade.animate({opacity:0}, 1000, function(){$(this).css("left", "-2000px");});
		}
		current_autofade = next_autofade;
	}
	banner_timer = setTimeout("banner_fadenext();", timing_autofade);
}

function banner_fadeprev()
{
	if($(".faderblock").attr("inautofade") == "true")
	{
		var next_autofade = null;
		if(current_autofade.prev().parent().hasClass("autofade"))
			next_autofade = current_autofade.prev();
		else
			next_autofade = $(".faderblock").children(".autofade").children(".fadecontent").last();
		offset = $("#content").offset();
		next_autofade.offset(offset);
		if(next_autofade.get(0) != current_autofade.get(0))
		{
			next_autofade.animate({opacity:1}, 1000);
			current_autofade.animate({opacity:0}, 1000, function(){$(this).css("left", "-2000px");});
		}
		current_autofade = next_autofade;
	}
	banner_timer = setTimeout("banner_fadenext();", timing_autofade);
}

function banner_show_extern_img(src)
{
	in_autofade_before_extern = ($(".faderblock").attr("inautofade") == "true");
	$(".faderblock").attr("inautofade", "false");
	$(".faderblockbuttons").css({"display":"none"});
	clearTimeout(banner_timer);
	//creëren van de img
	if($("#banner_extern_img").get(0) == undefined)
	{
		$(".faderblock").append('<img src="" id="banner_extern_img" style="opacity:0; filter: alpha(opacity=0); position:absolute; left:-2000px;"/>');
		$("#banner_extern_img").load(function(){
			$(this).offset($("#content").offset());
			/*current_autofade.animate({opacity:0}, 150);
			$(this).animate({opacity:1}, 150);*/
			current_autofade.css({"opacity":"0", "filter": "alpha(opacity=0)"});
			$(this).css({"opacity":"1", "filter": "alpha(opacity=100)"});
		});
	}
	$("#banner_extern_img").attr("src", src);
	
}

function banner_hide_extern()
{
	if($("#banner_extern_img").get(0) != undefined)
	{
		$(".faderblockbuttons").css({"display":"block"});
		/*$("#banner_extern_img").animate({opacity:0}, 150);
		current_autofade.animate({opacity:1}, 150);*/
		$("#banner_extern_img").css({"opacity":"0", "filter": "alpha(opacity=0)"});
		current_autofade.css({"opacity":"1", "filter": "alpha(opacity=100)"});
		if(in_autofade_before_extern)
			$(".faderblock").attr("inautofade", "true");
		banner_timer = setTimeout("banner_fadenext();", timing_autofade);
	}
}

function color_buttons()
{
	$(".faderblockbuttons").children("div").each(function(){
		if($(this).find(".background").length <= 0)
		{
			$(this).append( $("<div>").addClass('background').css({position: 'absolute',
																//top:$(this).offset().top,
																//left:$(this).offset().left,
																top:0,
																left:0,
																zIndex:-1,
																width:($(this).width() + parseInt($(this).css("padding-left").replace("px", "")) + parseInt($(this).css("padding-right").replace("px", ""))) ,
																height:($(this).height() + parseInt($(this).css("padding-top").replace("px", "")) + parseInt($(this).css("padding-bottom").replace("px", ""))),
																//width:$(this).width(),
																//height:$(this).height(),
																opacity: 0.5,
																filter: 'alpha(opacity=50)'
															  }));
			
		}
		else
		{
			//$(this).find(".background").css({top:$(this).offset().top, left:$(this).offset().left});
		}
	});
}
