FlippingBook = {};
MainSubnav = {};
$(document).ready(function () {	
	//alert('js');
	Page.set();
	MainSubnav.init();
	Util.externalLinks();
	//--- Lightbox ---// 
	if($(".lightbox").length > 0) {
		$("a.lightbox").lightbox();
	} 
	//--- Slide ---//
	if($("#slidecontainer .sledge").length > 0) {
		Slide.init();
	}

	//--- Prepage---//
	if($("#prepage").length > 0){
		Prepage.show();
	}
	
	// --- Zend_Form --- //
	// --- Validation Errors --- //
	if( $("ul.errors").length > 0 ){
		$('#error .message').css('visibility', 'visible');
		$("ul.errors").css('color', '#E70000');
		var height = 0;
		$("ul.errors").each(function(){
			height += $(this).height();
			height += 20; //margin-values
		});
		var form_height = $('#form').height();
		var container_height = $('#content_container').height();
		$("#form").css('height', parseInt(form_height)+parseInt(height));
		$("#content_container").css('height', parseInt(container_height)+parseInt(height));
		
		Page.set();
	}
	
	//Input - Vorbelegung
	$("input.preset").click(function(){
		$(this).val('');
		$(this).removeClass("preset");
	});
	
	if( $("#akkordeon_news").length > 0  ){
		Akkordeon.init();
	}
	
	if($("#header_left_frame").length>0 && $("#header_left_frame").html().length>0) {
		Startslider.init();
	}
});

MainSubnav = {
	init: function() {
		$('#mainnav a').hover(function(){
			$('#main_subnav ul').hide();
			if( !$(this).hasClass('active') ){
				var nav_class = $(this).attr('class');						
				$('#main_subnav ul.'+nav_class).show();
				
			}
		},function(){
			$('#main_subnav ul').hide();
		});
				
		$('#main_subnav ul').hover(function(){
			$(this).show();
			var nav_class = $(this).attr('class');						
			$('#mainnav a.'+nav_class).addClass('active');
		},function(){
			$(this).hide();
			var nav_class = $(this).attr('class');						
			$('#mainnav a.'+nav_class).removeClass('active');
		});		
		
	}
}

Startslider = {
	current: 1,
	max: 0,
	sleep: 4000,
	
	init: function() {
		Startslider.max= $("#header_left_frame .header_left").size();
		window.setTimeout(Startslider.next, Startslider.sleep);
	},
	next: function() {
		Startslider.current++;
		//alert(Startslider.current+" "+Startslider.max);
		if(Startslider.current>Startslider.max) {
			Startslider.current= 1;
		}
		$("#header_left_frame .header_left").hide();
		$("#header_left_slide"+Startslider.current).show();
		window.setTimeout(Startslider.next, Startslider.sleep);
	}
}

Page= {
	primaryheight: 0,
	addheight: 0,	
	addkind:'plus',
	set: function() {
		
		var dimensions= Util.getWindowSize();				
		var height= 0;
		var min_height = 375;
		var height_haeder = $("#header").height();
		var height_subnav=$("#subnav").height();
		var height_subnavBottom = height_subnav - height_haeder;
		var content_margintop=330;
		var footerheight=40;		
		height = $("#content_container").height();		
		if(height_subnavBottom > height)		
			height = height_subnavBottom;
		if( height < min_height)
			height = min_height;	
		if( (height+content_margintop+footerheight)>dimensions[1])	
			dimensions[1] = height+content_margintop+footerheight;
		
		if( parseInt(dimensions[1]) <= parseInt(dimensions[3] )){
	    	dimensions[1] = dimensions[3];
	    	dimensions[1]-=20;
	    }else if(height<=dimensions[1]) {	
				dimensions[1] = parseInt(height+content_margintop + footerheight);	
			  } else {
				dimensions[1] = dimensions[3];
		    	dimensions[1]-=40;
		    	height = parseInt(dimensions[1]-content_margintop-footerheight);
		}
		
		if( Page.addheight != 0 ){
			if( Page.addkind == 'plus' ){
				dimensions[1] =  Page.primaryheight+Page.addheight; 
			}else{
				dimensions[1] =  Page.primaryheight-Page.addheight;
			}
			
		}else{
			Page.primaryheight = dimensions[1];
		}
				
		if($("#content_container").length>0){
			$("#outer_container").css("height", dimensions[1]);
			//$("#content_container").css("height", height );
			$("#content_container").css("height", dimensions[1]-footerheight-content_margintop );		
			$("#white_left").css("height", dimensions[1]-33);
			$("#white_right3").css("height", dimensions[1]-645);	
		}else{
			$("#outer_container").css("height", dimensions[1]);			
		}
		
		
	}
}


Util= {
	getWindowSize: function(){
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) {
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else if(document.body) { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		if(yScroll < windowHeight) {
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		if(xScroll < windowWidth) {
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}
		var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	},
	externalLinks: function(){
		 $('a').each(function(){
		 	if( $(this).attr('rel') == "external"  ){
		 		$(this).attr('target', '_blank');
		 	}
		 
		 });
	},
  validateForm: function(id) {
		var result= true;				
		$("#"+id + " input, #"+id + " textarea, #"+id + " input.checkbox, #"+id + " .select").each(function() {
		//$("#"+id + " input.checkbox").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			    $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
				var itemresult= Util.validate($(this).attr("id"));
				result= result && itemresult;
			}
		});
		
		if(!result){
			$('#error .message').css('visibility', 'visible');
			Page.set();
		}
	
		
		return result;
		
	},
	validate: function(id) {
		var result= true;
		var obj= $("#"+id);		
		var commands= $("#"+id).attr("class").split(" ");
		for(var i=0; i<commands.length; i++) {
			var command= commands[i].replace(/[0-9]/g, "");			
			switch(command) {
				case("minlength"):
					var length= parseInt(commands[i].replace(/minlength/g, ""));
					result= result && (obj.val().length>=length);
					break;
				case("maxlength"):
					var length= parseInt(commands[i]);
					result= result && (obj.val().length<=length);
					break;
				case("email"):
					result = result && ( obj.get(0).value.match(/\S@\S.\S{2,}/)!=null );
					break;
				case("checkbox"):
					if( $("#"+id+":checked").length <= 0 ){
						result = result && false;
						$("#"+id).parent().children(".checkbox .info").addClass("error");
					}					
					break;
				case("select"):
					result = result && !( $("#"+id).val() == '---' );					
					break;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}
						
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");
			
		}
		return result;
	}
}

//--------------------------
//----------           Slide
//--------------------------
Slide = {
	maximum: 0,	
	width:690,
	init: function() {
		$("#slidecontainer .sledge .item").each(function () {
			Slide.maximum++;
		});
		
		if( Slide.maximum < 2 ){
			$(".next").hide();
		}
		
		/*set sledge width*/
		$('#slidecontainer .sledge').css('width', Slide.maximum*Slide.width);
		
		/* arrow effects 
		$(".prev, .next").hover(function () {
			$(this).addClass("hover");
		}, function() {
			$(this).removeClass("hover");
		})*/
		$(".prev, .next").click(function() {
			if($(this).attr('class')=="next") {
				Slide.next();
			} else {
				Slide.previous();
			}
			
		});
		$(".prev").hide();
		
	},
	previous: function() {
		var curpos= Slide.getCurrentPosition();
		curpos--;
		if(curpos==0) {
			$(".prev").hide();
		}
		if(curpos<Slide.maximum) {
			$(".next").show();
		}
		$("#slidecontainer .sledge").animate({ 
			left: curpos*-Slide.width
		}, 300);
	},
	next: function() {
		var curpos= Slide.getCurrentPosition();
		curpos++;
		if(curpos==Slide.maximum-1) {
			$(".next").hide();
		}
		if(curpos>0) {
			$(".prev").show();
		}
		$("#slidecontainer .sledge").animate({ 
			left: curpos*-Slide.width
		}, 300);
	},
	getCurrentPosition: function() {
		return parseInt(parseInt($("#slidecontainer .sledge").css("left"))/Slide.width*-1);
	}
}

//--------------------------
//----------    FlippingBook
//--------------------------
FlippingBook= {
	show: function(url) {	
		$("#shadow").show();
		$("body").append('<iframe id="flippingbook" frameborder="0" src="' + url + '"  ></iframe>');			
		FlippingBook.set();
		
	},
	close: function() {
		$("#closebook").hide();
		$("#flippingbook").remove();
	},
	set: function() {
		var dimension= Util.getWindowSize();
		
		var width= dimension[0];
		var height= dimension[1];
		
		if(dimension[0]<dimension[2]) {
			width= dimension[2];
		}
		if(dimension[1]<dimension[3]) {
		height= dimension[3];
		}
		
		$("#shadow").css("height", height).css("width", width);
		var top= '0';//(height-820)/2;
		var left= (width-1025)/2;		
		$("#flippingbook").css("top", top).css("left", left).css("visibility", 'visible');		
		$("#closebook").css("left", left+1025-75);
		$("#closebook").show();
		
	}
}

//----------    Lightup 
Lightup = {
	close: function(){
		$("#shadow").hide();
		
		if($("#prepage").length > 0){
			Prepage.close();
		}
		if($("#flippingbook").length > 0){
			FlippingBook.close();
		}
	}
}


//--------------------------
//----------         Prepage
//--------------------------
Prepage= {
	show: function(url) {	
		$("#shadow").show();	
		$("#shadow").css('display', 'block');						
		Prepage.set();
		
	},
	close: function() {
		$("#prepage").hide();
		window.location = '/';

	},
	set: function() {
		var dimension= Util.getWindowSize();		
		var width= dimension[0];
		var height= dimension[1];		
		if(dimension[0]<dimension[2]) {
			width= dimension[2];
		}
		if(dimension[1]<dimension[3]) {
			height= dimension[3];
		}		
		$("#shadow").css("height", height).css("width", width);
		$("#prepage").css('visibility', 'visible');
		
	}
}


//--------------------------
//----------       Akkordeon
//--------------------------
Akkordeon= {
	init: function() {		
		$("#akkordeon_news").click(function(){					 
				if( $(this).hasClass('active_akkordeon') ){							
						var textheight = $('#akkordeon_news .button').height();			
						$('#news_addtheme').fadeOut(500, function(){
							$('#akkordeon_news').animate({
								height: textheight + 'px'
								 }, 1000, 'swing', function() {
									Page.addheight = 1;						
									$('#akkordeon_news .button').fadeIn( 500, function(){ Page.set(); } );
									$('.active_akkordeon').removeClass('active_akkordeon');
									
				
						 });						
						});		
						
						 
						//$(this).removeClass('active_akkordeon');
						
				 }else{		
 						var textheight = $('#news_addtheme').height();								
						$('#akkordeon_news .button').fadeOut(500);		
						$('#akkordeon_news').animate({
								height: textheight + 'px'
								 }, 1000, 'swing', function() {
									Page.addheight = textheight;									
									$('#news_addtheme').css('height', textheight);
									$('#news_addtheme').fadeIn( 500, function(){ Page.set(); } );
									$('#akkordeon_news').addClass('active_akkordeon');
									
				
						 });
						
					
			 	 }
		});
	
	}
}
