FlippingBook = {};
MainSubnav = {};
$(document).ready(function () {	
	/* --- CUFON --- */
	if( $(".cufon_replace").length > 0){
		Cufon.now();
		Cufon.replace(".cufon_replace");
	}
	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($("#startslider").length > 0) { Startslider.init(); }

	//Datepicker
	if($(".datepicker").length > 0) {		
		$(".datepicker").datepicker($.datepicker.regional['de']);
	}	
	if($("#dynamicform .accordion").length > 0){
		Form.akkinit();
	}
	
});

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 = {
	max_width: 443,
	min_width: 65,
	init: function() {
		$("#topical .sbtn").click(function(){	
			Startslider.animateTopical();			
		});
		$("#becomeclient .sbtn").click(function(){	
			Startslider.animateBecomeCliente();			
		});
		$("#specialoffers .sbtn").click(function(){	
			Startslider.animateSpecialoffers();			
		});		
	}, 
	animateTopical: function(){
			var self = "#topical";
			var other = "#specialoffers";	
			if($(self).hasClass("active")){						
				$(self+" .scontent").hide();					
				$(self+"").animate({
					paddingLeft: Startslider.min_width+"px"
					},
					800,
					'linear',
					function(){
						$(this).removeClass("active").addClass("inactive");
					}
				);				
				$(this).removeClass("active").addClass("inactive");		
				$(other+"").animate({
					paddingLeft: Startslider.max_width+"px"
					},
					800,
					'linear',
					function(){
						$(other+" .scontent").fadeIn(800);
						$(this).removeClass("inactive").addClass("active");
					}
				);				
			}else{		
				$("#startslider .active .scontent").hide();				
				$("#startslider .active").animate({
					paddingLeft: Startslider.min_width+"px"
					},
					800,
					'linear',
					function(){
						$(this).removeClass("active").addClass("inactive");
					}
				);
				$(self+"").animate({
					paddingLeft: Startslider.max_width+"px"
					},
					800,
					'linear',
					function(){
						$(self+" .scontent").fadeIn(800);
						$(this).removeClass("inactive").addClass("active");
					}
				);
				
			}	
	}, 
	animateSpecialoffers: function(){
			var self = "#specialoffers";
			var other = "#becomeclient";	
			if($(self).hasClass("active")){						
				$(self+" .scontent").hide();					
				$(self+"").animate({
					paddingLeft: Startslider.min_width+"px"
					},
					800,
					'linear',
					function(){
						$(this).removeClass("active").addClass("inactive");
					}
				);				
				$(this).removeClass("active").addClass("inactive");		
				$(other+"").animate({
					paddingLeft: Startslider.max_width+"px"
					},
					800,
					'linear',
					function(){
						$(other+" .scontent").fadeIn(800);
						$(this).removeClass("inactive").addClass("active");
					}
				);				
			}else{		
				$("#startslider .active .scontent").hide();				
				$("#startslider .active").animate({
					paddingLeft: Startslider.min_width+"px"
					},
					800,
					'linear',
					function(){
						$(this).removeClass("active").addClass("inactive");
					}
				);
				$(self+"").animate({
					paddingLeft: Startslider.max_width+"px"
					},
					800,
					'linear',
					function(){
						$(self+" .scontent").fadeIn(800);
						$(this).removeClass("inactive").addClass("active");
					}
				);
				
			}	
	}, 
	animateBecomeCliente: function(){
			var other = "#specialoffers";
			var self = "#becomeclient";	
			if($(self).hasClass("active")){						
				$(self+" .scontent").hide();					
				$(self+"").animate({
					paddingLeft: Startslider.min_width+"px"
					},
					800,
					'linear',
					function(){
						$(this).removeClass("active").addClass("inactive");
					}
				);				
				$(this).removeClass("active").addClass("inactive");		
				$(other+"").animate({
					paddingLeft: Startslider.max_width+"px"
					},
					800,
					'linear',
					function(){
						$(other+" .scontent").fadeIn(800);
						$(this).removeClass("inactive").addClass("active");
					}
				);				
			}else{		
				$("#startslider .active .scontent").hide();				
				$("#startslider .active").animate({
					paddingLeft: Startslider.min_width+"px"
					},
					800,
					'linear',
					function(){
						$(this).removeClass("active").addClass("inactive");
					}
				);
				$(self+"").animate({
					paddingLeft: Startslider.max_width+"px"
					},
					800,
					'linear',
					function(){
						$(self+" .scontent").fadeIn(800);
						$(this).removeClass("inactive").addClass("active");
					}
				);
				
			}	
	}
}

Page= {
	primaryheight: 0,
	addheight: 0,	
	addkind:'plus',
	set: function() {
		//reset ul.subnav height
		if( $('#subnav .download_item').length == 3){
			$("#subnav ul.subnav").css('min-height', 0);
		}		
		
		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[href]').each(function(){
		 	if($(this).attr("href") != "" &&	 			 	   
		 	   $(this).attr("href").indexOf(window.location.hostname) < 0 &&
			   $(this).attr("href").indexOf("http://") >= 0 	
			   ||  $(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() {	
			var itemresult = true;
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			    $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
				itemresult= Util.validate($(this).attr("id"));
				result= result && itemresult;
			}			
		});	
		if(!result){
			$("#error .message").css("visibility", "visible");
			$("#form_message").addClass("error");
			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,	
	item_width:0,
	width:690,
	init: function() {
		Slide.setBackground(0);
		$("#slidecontainer .sledge .item").each(function () {
			Slide.maximum++;			
		});		
		if( Slide.maximum < 2 ){$(".next").hide();}		
		/*set sledge width*/
		$('#slidecontainer .sledge').css('width', Slide.maximum*Slide.width);
		$(".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,function(){
			Slide.setBackground(curpos);
		});
	},
	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,function(){
			Slide.setBackground(curpos);
		});
	},
	getCurrentPosition: function() {
		return parseInt(parseInt($("#slidecontainer .sledge").css("left"))/Slide.width*-1);
	},
	setBackground: function(curpos){
		//alert(curpos);
		if($(".sledge :nth-child("+(curpos+1)+")").hasClass('abholaktion')){
			$("#header_pic").removeClass('aktion').addClass('abholaktion');
		}else{
			if($("#header_pic").hasClass('abholaktion')){
				$("#header_pic").removeClass('abholaktion');
			}
			$("#header_pic").addClass('aktion');
		}
	}
}

//--------------------------
//----------    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($("#sheets_prepage").length > 0){
			SheetsPrepage.close();
		}
		if($("#flippingbook").length > 0){
			FlippingBook.close();
		}
	}
}


//--------------------------
//----------         Prepage
//--------------------------
Prepage= {
	show: function(url) {							
		Prepage.setShadow();
		$("#prepage").css('visibility', 'visible');		
	},
	close: function() {
		$("#prepage").hide();
		window.location = '/';
	},
	setShadow: function(){
		$("#shadow").show();	
		$("#shadow").css('display', 'block');	
		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);
	}
}


//--------------------------
//----------       Akkordeon
//--------------------------
Form = {
	akkinit: function(){
		$("#dynamicform fieldset.accordion").css('height', $("fieldset.accordion .form_legend").height()+'px');
		Page.set();
		$("#dynamicform fieldset.accordion .form_legend").click(function(){	
			height=0;
			$(this).parent().children('.form_row').each(function(){
				height += parseInt($(this).height())+parseInt($(this).css('margin-bottom'));
			});
			if( $(this).parent().hasClass('active') ){				
				$(this).parent().css('height',  $(this).height()+'px');
				$("#content_container").height($("#content_container").height()-height);
				$(this).parent().removeClass('active');		
			}else{
				$(this).parent().css('height',height+$(this).height()+'px');
				$("#content_container").height($("#content_container").height()+height);
				$(this).parent().addClass('active');
			}
			Page.set();			
		});				
	}
};


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');						
				
						 });						
						});							
						
				 }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');
									
						 });						
					
			 	 }
		});
	
	}
};

