// JavaScript Document
Page = {
	init: function(){
		Page.setLayout();			
		$('a').each(function(){
		 	if( $(this).attr('rel') == "external"  ){
		 		$(this).attr('target', '_blank');
		 	}
		});					
		//Akkordeon
		if( $('.akkordeon').length > 0){
			Akkordeon.init();
		}
		
	},
	setLayout: function(){
		var dimensions= Util.getWindowSize();
		var footer_h = $('#footer').height();
		if( !$('body').hasClass('start')  ){
			$('#page').css('height', 'auto');	
		}			
		var page_h = $('#page').height();
		var page_w = $('#page').width();		
		
		//set content height
		if($('body').hasClass('start')){
			var header_h = $('#header').height();
			if((header_h + footer_h + page_h) < dimensions[3]){
				$('#bg_start').css('top',parseInt( (dimensions[3]-page_h) / 2 ) + 'px');
				$('#page').css('margin-top',parseInt( (dimensions[3]-page_h) / 2 ) +'px');
				$('#header').css('height', parseInt( (dimensions[3]-page_h) / 2 ) +'px');
				$('#footer').css('height', parseInt( (dimensions[3]-page_h) / 2 ) +'px');
			}			
		}else if( (footer_h + page_h) < dimensions[3] ){
			page_h = dimensions[3] - footer_h;
			$('#page').css('height', page_h+'px' )
		}		
		if(dimensions[2] > page_w ){
			$('#bg_logo').css('width', parseInt( (dimensions[2]-page_w)/2 ) +'px' );
			$('#bg_logo').css('right', -(parseInt((dimensions[2]-page_w)/2 )) +'px');
			$('#bg_nav_main').css('width', parseInt((dimensions[2]-page_w)/2 ) +'px' );
			$('#bg_nav_main').css('right', -(parseInt((dimensions[2]-page_w)/2 )) +'px' );
			$('#bg_groundbar').css('width', parseInt(dimensions[2]/2) +'px' );
		}else{
			$('body').width(page_w);
			$('#bg_logo').css('display', 'none' );
			$('#bg_nav_main').css('display', 'none'  );
			$('#bg_groundbar').css('width', parseInt(page_w/2)+'px' );
		}
		$('#bg_page').css('height', page_h+'px');
		$('#bg_circle').css('height', page_h+'px');
	}	
};


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){
			$('.forminfo').addClass('error');
		}
		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;
				case("number"):
					result = result && !( isNaN( $("#"+id).val())  );					
					break;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}
						
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");
			
		}
		return result;
	}
}

Scroll = {
	left: 0,
	width: 0,
	itemWidth: 0,
	interrupt: false,
	id: 0,
	init: function(p_id) {		
		Scroll.left = 0;
		Scroll.width = 0;		
		Scroll.id = p_id;
		$(Scroll.id + " li").each(function() {
			Scroll.width+= parseInt($(this).width())+parseInt($(this).css("margin-left"))+parseInt($(this).css("margin-right"));
		});
		if( Scroll.width > $(Scroll.id).width() ){
			$(Scroll.id + " .sledge").css("width", (Scroll.width*2));
			$(Scroll.id + " .sledge li").each(function() {
				$(this).clone().insertAfter(Scroll.id + " .sledge li:last");
			});
			Scroll.run();
			$(Scroll.id).hover(function() {
				Scroll.interrupt= true;
			}, function() {
				Scroll.interrupt= false;
			});
	
		}			
	},
	run: function() {
		var lightbox = $('#overlay').css('display');
		if(!Scroll.interrupt && lightbox == 'none') {
			if((Scroll.left*-1)>=Scroll.width) {
				Scroll.left = 0;
				$(Scroll.id + " .sledge").animate({
					marginLeft: Scroll.left
				}, 0);
			}
			Scroll.left-=10;
		}
		$(Scroll.id + " .sledge").animate({
			marginLeft: Scroll.left
		}, 200, "linear", function() {
			Scroll.run();
		});  
	}
};

//--------------------------
//----------       Akkordeon
//--------------------------
Akkordeon= {
	init: function() {	
		$(".akkordeon li").click(function(){	
			 //$(".sledge").css('margin-left','0px');
			 var open = true;
			 if( $(this).hasClass('active_akkordeon') ){
			 	open = false;
			 }			 
			 if( $('.active_akkordeon').length > -1 ){
			 	Scroll.interrupt = true;
				//aktive Akkordeon-Box schließen					
				$('.active_akkordeon').children('.akk_content_container').animate({
						height: 0 + 'px'
					},500, 'swing', function() {							
					$(this).hide();	
					$(this).css('height', '0px'); 
					
					Page.setLayout();				
				 });	
				 $('.active_akkordeon').removeClass('active_akkordeon');						
			  }
			 
			 if(open){
			 	//neue Akkordeon-Box öffnen		
				 $(this).children('.akk_content_container').css('height', 'auto');			 
				 var textheight = $(this).children('.akk_content_container').height();
				 $(this).children('.akk_content_container').css('display', 'none');
				 $(this).children('.akk_content_container').css('height', '0px');
				 $(this).addClass('active_akkordeon');			
				 
				 $(this).children('.akk_content_container').animate({
					 height: textheight+'px'
				 },500, 'swing', function(){
				 	$(this).css('display', 'block');
				 	//$(this).children('.akk_content').css('display', 'block');				 	
					//$(this).css('height', 'auto');
				 	$('#page').css('height', 'auto');
				 	Page.setLayout();
			 		
				 });	
			 	
			 } 
			 
		});
	
	}
};

HousetypeSelection = {
	showCat: function(cat){
		var selected = cat;
		if( selected != '' ){
			$('.housetype').hide();
			$('.'+selected).show();	 
		}else{
			$('.housetype').show();
		}	
		Page.setLayout();		
	}
};

