var swich2 = true;
var swich3 = false;
var reg, slideErr, tmpheight, tmpmode;

function doGo(swichi) {
	if(swich2) {
		swich2 = false;
		if(!swichi) {
			$(".toprbg1").animate(
				{"height": "13px"}, "fast", function() {
					if(swich3) $(".toprbg1").animate(
						{"height": "5px"}, "fast", function() {
							swich2 = true;
						}
					); else swich2 = true; 
				}
			);
		} else $(".toprbg1").animate({"height": "5px"}, "fast", function() { swich2 = true; });
		swich3 = false;
	} else if(swichi) swich3 = true; else swich3 = false;
}

function doHover(active, hover, idd) {
	if(active==false) {
		var b = document.getElementById(idd);
		var imgname = (idd.split("img"))[0];
		
		if(hover) b.src = "images/"+imgname+"hov.png";
		else b.src = "images/"+imgname+".png";
	}
}

function doReg(height, mode) {
	if(!slideErr) {
		var newreg = reg;
		
		$('#regkastenn').slideUp(500);
		
		if($('#regkasten').css('display')=="none") {
			$('#'+mode).css('display', 'block');
			$('#regkasten').css('height', height+'px');
			$('#regkasten').slideToggle(1000);
		} else {
			if(typeof reg!="undefined" && reg!=mode) {
				$('#regkasten').slideUp(500, function() {
					$('#'+newreg).css('display', 'none');
					$('#regkasten').css('height', height+'px');
					$('#'+mode).css('display', 'block');
					$('#regkasten').slideDown(1000);
				});
			} else {
				$('#regkasten').slideToggle(500, function() {
					$('#'+newreg).css('display', 'none');
				});
			}
		}
		reg = mode;
	} else {
		tmpheight = height;
		tmpmode = mode;
	}
}

function doError(height, mode) {
	if(!slideErr) {
		slideErr = true;
		
		$('#'+mode).css('display', 'block');
		$('#regkastenn').css('top', height+'px');
		$('#regkastenn').slideDown(1000).delay(4000).slideUp(500, function() {
			$('#'+mode).css('display', 'none');
			slideErr = false;
			if(tmpheight && tmpmode) {
				doReg(tmpheight, tmpmode);
				tmpheight = 0;
				tmpmode = 0;
			}
		});
	}
}

var pause = true;
var active = false;
var pause2 = true;
var active2 = false;
var pause3 = true;
var active3 = false;

function doPause() {
	pause = true;
	$('#playpause').attr('src', 'images/play.png');
}

function doNext() {
	$tmp = $('#ref_slide').children();
	if($tmp.length >= 4 && active==false) {
		active = true;
		
		$($tmp[3]).css({ 'display': 'block' });
		
		$($tmp[0]).animate({
			marginLeft: '-230px'
		}, 1500, 'swing', function() {
			$($tmp[0]).appendTo('#ref_slide');
			$($tmp[0]).css({ 'display': 'none', 'marginLeft': '0px' });
		});
		$($tmp[1]).children('a').children('img').animate({
			width: '230px',
			height: '177px'
		}, 1500, 'swing', function() {
			$($tmp[1]).removeClass('ref_web_big');
		});
		$($tmp[1]).animate({ 'marginTop': '25px' }, 1500, 'swing');
		$($tmp[2]).children('a').children('img').animate({
			width: '306px',
			height: '236px'
		}, 1500, 'swing', function() {
			$($tmp[2]).addClass('ref_web_big');
		});
		$($tmp[2]).animate({ 'marginTop': '0px' }, 1500, 'swing');
		setTimeout("active = false", 1500);
	}
}

function doPrev() {
	if(active==false) {
		$tmp = $('#ref_slide').children();
		if($tmp.length >= 4) {
			active = true;
			$($tmp[$tmp.length-1]).prependTo('#ref_slide');
			
			$($tmp[$tmp.length-1]).css({ 'display': 'block', 'marginLeft': '-230px' });
			
	
			$($tmp[$tmp.length-1]).animate({
				marginLeft: '0px'
			}, 1500, 'swing');
			$($tmp[0]).children('a').children('img').animate({
				width: '306px',
				height: '236px'
			}, 1500, 'swing', function() {
				$($tmp[0]).addClass('ref_web_big');
			});
			$($tmp[0]).animate({ 'marginTop': '0px' }, 1500, 'swing');
			$($tmp[1]).children('a').children('img').animate({
				width: '230px',
				height: '177px'
			}, 1500, 'swing', function() {
				$($tmp[1]).removeClass('ref_web_big');
			});
			$($tmp[1]).animate({ 'marginTop': '25px' }, 1500, 'swing');
			
			$($tmp[2]).delay(1500).fadeOut(0);
	
			setTimeout("active = false", 1500);
		}
	}
}

function doPlaySchleife() {
	if(!pause) {
		doNext();
		setTimeout("doPlaySchleife()", 5000);
	}
}

function doPlay() {
	if(pause) {
		pause = false;
		$('#playpause').attr('src', 'images/pause.png');
		setTimeout("doPlaySchleife()", 5000);
	} else doPause();
}



function doPause2() {
	pause2 = true;
	$('#playpause2').attr('src', 'images/play.png');
}

function doNext2() {
	$tmp = $('#ref_2').children();
	if($tmp.length >= 2 && active2==false) {
		active2 = true;
		
		$($tmp[0]).animate({ 'marginLeft': '-426px' }, 500, 'swing', function() {
			$($tmp[0]).css({ 'display': 'none' });
			$($tmp[0]).appendTo('#ref_2');
			$($tmp[1]).css({ 'display': 'block', 'marginLeft': '542px' });
			$($tmp[1]).delay(250).animate({ 'marginLeft': '58px' }, 500, 'swing');
		});
		
		setTimeout("active2 = false", 1250);
	}
}

function doPrev2() {
	$tmp = $('#ref_2').children();
	if($tmp.length >= 2 && active2==false) {
		active2 = true;
		
		$($tmp[0]).animate({ 'marginLeft': '542px' }, 500, 'swing', function() {
			$($tmp[0]).css({ 'display': 'none' });
			$($tmp[$tmp.length-1]).prependTo('#ref_2');
			$($tmp[$tmp.length-1]).css({ 'display': 'block', 'marginLeft': '-426px' });
			$($tmp[$tmp.length-1]).delay(250).animate({ 'marginLeft': '58px' }, 500, 'swing');
		});
		
		setTimeout("active2 = false", 1250);
	}
}

function doPlaySchleife2() {
	if(!pause2) {
		doNext2();
		setTimeout("doPlaySchleife2()", 6000);
	}
}

function doPlay2() {
	if(pause2) {
		pause2 = false;
		$('#playpause2').attr('src', 'images/pause.png');
		setTimeout("doPlaySchleife2()", 6000);
	} else doPause2();
}



function doPause3() {
	pause3 = true;
	$('#playpause3').attr('src', 'images/play.png');
}

function doNext3() {
	$tmp = $('#ref_3').children();
	if($tmp.length >= 2 && active3==false) {
		active3 = true;
		
		$($tmp[0]).animate({ 'marginLeft': '-635px' }, 500, 'swing', function() {
			$($tmp[0]).css({ 'display': 'none' });
			$($tmp[0]).appendTo('#ref_3');
			$($tmp[1]).css({ 'display': 'block', 'marginLeft': '766px' });
			$($tmp[1]).delay(250).animate({ 'marginLeft': '65px' }, 500, 'swing');
		});
		
		setTimeout("active3 = false", 1250);
	}
}

function doPrev3() {
	$tmp = $('#ref_3').children();
	if($tmp.length >= 2 && active3==false) {
		active3 = true;
		
		$($tmp[0]).animate({ 'marginLeft': '766px' }, 500, 'swing', function() {
			$($tmp[0]).css({ 'display': 'none' });
			$($tmp[$tmp.length-1]).prependTo('#ref_3');
			$($tmp[$tmp.length-1]).css({ 'display': 'block', 'marginLeft': '-635px' });
			$($tmp[$tmp.length-1]).delay(250).animate({ 'marginLeft': '65px' }, 500, 'swing');
		});
		
		setTimeout("active3 = false", 1250);
	}
}

function doPlaySchleife3() {
	if(!pause3) {
		doNext3();
		setTimeout("doPlaySchleife3()", 6000);
	}
}

function doPlay3() {
	if(pause3) {
		pause3 = false;
		$('#playpause3').attr('src', 'images/pause.png');
		setTimeout("doPlaySchleife3()", 6000);
	} else doPause3();
}

//YouTube
var ytplayer;
function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("myytplayer");
	ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

var lastState;
var QualitySet;
function onytplayerStateChange(newState) {
	$('#schatten').clearQueue();
	if(newState==1) {
		doSchatten(true);
		if(lastState==3 && QualitySet!=true) {
			ytplayer.setPlaybackQuality("hd1080");
			QualitySet = true;
		}
	}
	if(newState==2) doSchatten(false);
	lastState = newState;
}

function doSchatten(kind) {
	if(kind==true) $('#schatten').css({ 'display': 'block' }).delay(1000).animate({ 'opacity': '0.85' }, 'slow');
	if(kind==false) $('#schatten').delay(1000).animate({ 'opacity': '0.0' }, 'slow', function() { $(this).css({ 'display': 'none' }); });
}

//Sonstiges
function doToggleSonst(idd) {
	$('#togglesonst_'+idd).slideToggle();
}

$(function() {
	
	if($('#playpausenext').length) doPlay();
	if($('#playpausenext2').length) doPlay2();
	if($('#playpausenext3').length) doPlay3();
	
	$('.normtext').jScrollPane({verticalDragMinHeight: 26, verticalDragMaxHeight: 26});
	$('.normtextfull').jScrollPane({verticalDragMinHeight: 26, verticalDragMaxHeight: 26});
	$('#twocolssecond2').jScrollPane({verticalDragMinHeight: 26, verticalDragMaxHeight: 26});
	
	//$('.fancybox').fancybox({centerOnScroll: false, overlayOpacity: 0.8, titlePosition: 'inside', overlayColor: '#000', showNavArrows: false, onStart: function() { doPause(); doPause2(); }});
	$("a[rel^='prettyPhoto']").prettyPhoto({ theme: 'light_square', changepicturecallback: function() { doPause(); doPause2(); } });
	
	$('#schatten').bind({
		click: function() { doSchatten(false); }
	});
	
	$('.input input, .textarea textarea').bind({
		focusout: function() {
			var getid = $(this).attr('id');
			if(getid=="name") {
				if($(this).val()=="") {
					$(this).val('Name');
					$(this).parent().children('.x_haken').removeClass('haken_x');
				} else $(this).parent().children('.x_haken').addClass('haken_x');
			}
			if(getid=="email") {
				if($(this).val()=="") {
					$(this).val('eMail');
					$(this).parent().children('.x_haken').removeClass('haken_x');
				} else {
					$this = $(this);
					$.ajax({
						url: 'email.php',
						type: 'post',
						data: ({ email: $(this).val() }),
						success: function(dataa) {
							if(dataa==1) $this.parent().children('.x_haken').addClass('haken_x');
							else $this.parent().children('.x_haken').removeClass('haken_x');
						}
					});
				}
			}
			if(getid=="betreff") {
				if($(this).val()=="") {
					$(this).val('Betreff');
					$(this).parent().children('.x_haken').removeClass('haken_x');
				} else $(this).parent().children('.x_haken').addClass('haken_x');
			}
			if(getid=="nachricht") {
				if($(this).val()=="") $(this).val('Nachricht');
			}
		}, focus: function() {
			var getid = $(this).attr('id');
			if(getid=="name" && $(this).val()=="Name") $(this).val('');
			if(getid=="email" && $(this).val()=="eMail") $(this).val('');
			if(getid=="betreff" && $(this).val()=="Betreff") $(this).val('');
			if(getid=="nachricht" && $(this).val()=="Nachricht") $(this).val('');
		}
	});
	
	$('#kontaktform').bind('submit', function() {
		$children = $(this).children().children('.x_haken');
		var send = true;
		for(var i = 0; i < $children.length; i++) {
			if(!$($children[i]).hasClass('haken_x')) send = false;
		}
		if(!send) $('#showmsg').text(unescape('Bitte alle Felder korrekt ausf%FCllen'));
		return send;
	});
	
	$('#loginkasten input').bind({
		focusout: function() {
			var getid = $(this).attr('id');
			if(getid=="nick" && $(this).val()=="") $(this).val('Name');
			if(getid=="pwd" && $(this).val()=="") $(this).val('Passwort');
		}, focus: function() {
			var getid = $(this).attr('id');
			if(getid=="nick" && $(this).val()=="Name") $(this).val('');
			if(getid=="pwd" && $(this).val()=="Passwort") $(this).val('');
		}
	});
	
	$('#reg input').bind({
		focusout: function() {
			var getid = $(this).attr('id');
			if(getid=="nickreg") {
				if($(this).val()=="") {
					$(this).val('Name');
					$('#reghaken').removeClass('haken_x');
				} else {
					$.ajax({
						url: 'nick.php',
						type: 'post',
						data: ({ nick: $(this).val() }),
						success: function(dataa) {
							if(dataa==1) $('#reghaken').addClass('haken_x');
							else $('#reghaken').removeClass('haken_x');
						}
					});
				}
			}
			if(getid=="pwdreg" && $(this).val()=="") $(this).val('Passwort');
			if(getid=="pwdreg2" && $(this).val()=="") $(this).val('Passwort');
		}, focus: function() {
			var getid = $(this).attr('id');
			if(getid=="nickreg" && $(this).val()=="Name") $(this).val('');
			if(getid=="pwdreg" && $(this).val()=="Passwort") $(this).val('');
			if(getid=="pwdreg2" && $(this).val()=="Passwort") $(this).val('');
		}
	});
	
	$('#loginform').bind({
		'submit': function() {
			var error = false;
			if($('#nick').val().length<6 || $('#nick').val().length>20) var error = true;
			
			if(!error) {
				$.ajax({
					url: 'login.php',
					type: 'post',
					data: ({ nick: $('#nick').val(), pwd: $('#pwd').val() }),
					success: function(dataa) {
						if(dataa==1) location.reload();
						else doError(121, 'login2error');
					}
				});
				return false;
			} else {
				doError(121, 'loginerror');
			}
			return false;
		}
	});
	
	$('#regform').bind({
		'submit': function() {
			var error = false;
			if(!$('#reghaken').hasClass('haken_x')) var error = true;
			if($('#pwdreg').val().length<6 || $('#nickreg').val().length>20) var error = true;
			if($('#pwdreg').val()!=$('#pwdreg2').val()) var error = true;
			
			if(!error) {
				$.ajax({
					url: 'reg.php',
					type: 'post',
					data: ({ nick: $('#nickreg').val(), pwd: $('#pwdreg').val(), pwd2: $('#pwdreg2').val() }),
					success: function(dataa) {
						alert(dataa);
					}
				});
				return false;
			} else {
				doError(227, 'regerror');
			}
			
			return false;
		}
	});
	
	//Downloads2 PopUp
	$('.popup_download').bind({
		'click': function() {
			$('#popup').css({'visibility': 'visible'});
		}
	});
	$('.popup .close').bind({
		'click': function() {
			$(this).parent().css({'visibility': 'hidden'});
		}
	});
});
