function trace(o) {
	if(window.console) console.log(o);
}
function init() {
	var cnt = 0;
	pictureStacks = 0;
	$(".gallery").each(function() {
		cnt++;
		var n = window["block"+cnt] = initSlides(this);
	});
	slides = initSlides(".masked_pictures");
	$(".imagelink").each(function() {
		this.img = $(this).find("img").get(0);
		this.up = this.img.src;
		this.over = this.rel;
	}).hover(function() {
		this.img.src = this.over;
	},function() {
		this.img.src = this.up;
	});
	
	//img load
	//var boxes = $(".set img").length;
	var boxes = 0;
	var loaded = 0;
	$(".set img").each(function() {
		boxes++;
		if(!this.complete) {
			$(this).one("load",function() {
				loaded++;
				if(boxes-loaded==0) initBoxes();
			});
		} else {
			loaded++;
		}
	});
	if(boxes > 1) {
		if(boxes-loaded == 0) initBoxes();
	}
}
function initBoxes() {
	var sidx = 0;
	$(".sets").each(function() {
		sidx++;
		var idx = 0;
		var mx = 0;
		var me = this;
		this.iv = 0;
		$(this).find(".set").each(function() {
			var h = $(this).height();
			h += 10;
			mx = Math.max(h,mx);
			$(this).height(mx).css({position:"absolute",left:0,top:0});
			idx++;
			this.idx = idx;
			this.id = "b_"+sidx+"_"+idx;
		});
		$(this).height(mx);
		this.num = sidx;
		this.idx = 1;
		this.length = idx;
		this.iv = setTimeout(function() {nextBox(me)},2000+sidx*500);
	});
}
function nextBox(st) {
	if(st.iv > 0) clearTimeout(st.iv);
	st.iv = 0;
	var nxt = st.idx+1;
	if(nxt > st.length) nxt = 1;
	st.nxt = nxt;
	if(jQuery.browser.msie) {
		$(st).find(".set:nth-child("+st.idx+")").hide();
		$(st).find(".set:nth-child("+st.nxt+")").show();
		st.idx = st.nxt;
		st.iv = setTimeout(function() {nextBox(st)},3000);
	} else {
		$(st).find(".set:nth-child("+st.idx+")").fadeOut(2000);
		$(st).find(".set:nth-child("+nxt+")").hide().fadeIn(2000,function() {
			st.idx = st.nxt;
			st.iv = setTimeout(function() {nextBox(st)},3000);
		});
	}
}
function initSlides(ident) {
	pictureStacks++;
	var o = {idx:0,iv:0,cnt:pictureStacks};
	o.picts = $(ident).find("img");
	var c = o.picts.length;
	o.loaded = 0;
	if(c > 1) {
		o.picts.each(function() {
			if(this.complete) {
				o.loaded++;
			} else {
				$(this).one("load",function() {
					o.loaded++;
					if(c-o.loaded==0) {
						if(o.iv > 0) clearTimeout(o.iv);
						o.iv = setTimeout(function() { nextSlide(o); },1000);
					}
				});
			}
		});
		if(c-o.loaded==0) {
			o.iv = setTimeout(function() { nextSlide(o); },1900);
		}
	}
}
function nextSlide(d) {
	if(d.iv > 0) clearTimeout(d.iv);
	d.iv = 0;
	var last = d.picts.get(d.idx);
	d.idx++;
	if(d.idx >= d.picts.length) d.idx = 0;
	var cur = d.picts.get(d.idx);
	$(last).css({zIndex:990});
	$(cur).css({zIndex:991,display:"block",opacity:0}).animate({opacity:1.0},2000,function() {
		$(last).hide();
		d.iv = setTimeout(function() {nextSlide(d)},5000);
	});
}

function onVideoPlayPause(ident,f) {
}
function onVideoInitialized(ident) {
	flTell(ident,"showVideo");
}
function onVideoDone(ident) {
}
function getVideoData(ident) {
	var res = videos[ident];
	return res;
}
function flTell(ident,fn,p) {
	var fl = document.getElementById(ident);
	if(!fl) return;
	fl.flCall(fn,p);
}

function checkForm(ev) {
	//this == form
	var req = this.required;
	var ok = true;
	if(req && req.value != "") {
		var rf = req.value.split(",");
		for(var i=0;i<rf.length;i++) {
			var n = rf[i];
			var ai = n.indexOf("[");
			if(ai > 0) {
				var ain = n.substr(0,ai);
				var enn = ".error.field-"+ain;
				var aic = $("input[name^="+ain+"]:checked");
				if(aic.length == 0) {
					ok = false;
					$(enn).show();
					$("input[name^="+ain+"]").one("click",function() {
						$(enn).hide();
					});
				}
			}
			var elm = this[n];
			if(!elm && typeof(elm.value)!="undefined") continue;
			if(elm.type=="text" || elm.type=="textarea") {
				var v = jQuery.trim(elm.value);
				var oneok = true;
				if(v=="" || v=="Bitte ausfüllen") {
					oneok = false;
					elm.value="Bitte ausfüllen";
				}
				if(oneok && n=="email") {
					if(v.indexOf("@") < 2 || v.indexOf(" ") != -1) {
						elm.value="Bitte E-Mail eintragen";
						oneok = false;
					}
				}
				if(!oneok) {
				 	ok = false;
					$(elm).focus(function() {
						this.value="";
						$(this).removeClass("error");
						$(this).unbind("focus");
					}).addClass("error");
				}
			}
		}
	}
	if(ok) this.ctrl.value=1;
	return ok;
}


$(document).ready(init);
