Page= {
	height: 0,
	header: 0,
	footer: 0,
	
	init: function() {	
		Scroll.init();
		if( $('h2.cheadline').length > 2  ){Indexlist.init();}		
		Page.setDimensions();
		Util.externalLinks();
		$(".lightbox").lightBox();
		
		Newsletter.init();
	},
	setDimensions: function(){
		var dimensions= Util.getWindowSize();
		Page.height= dimensions[1];
		
		if($("body").attr("class").match("page")) {
			Page.header= parseInt($("#page .navigation").height()) +
							  parseInt($("#page .navigation").css("padding-top")) + 
							  parseInt($("#page .navigation").css("padding-bottom")) +
							  parseInt($("#page .navigation").css("margin-top")) + 
							  parseInt($("#page .navigation").css("margin-bottom"));
			Page.footer= parseInt($("#footer").height())
							  parseInt($("#footer").css("padding-top")) + 
							  parseInt($("#footer").css("padding-bottom")) +
							  parseInt($("#footer").css("margin-top")) + 
							  parseInt($("#footer").css("margin-bottom"));
							
			Page.contentHeight= Page.height-Page.header-Page.footer;
			if($("#page .content").height()<Page.contentHeight) {
				$("#page .content").css("height", Page.contentHeight);
			}
			if($("#shop").length>0) {
				$("#shop").css("height", Page.contentHeight);
			}
		}
	}
}

Indexlist={
	init: function(){
		var idxlst='';
		$('h2.cheadline').each(function(){
			if($(this).html().length>0) {
				idxlst += '<li><a href="#'+ $(this).attr('id') +'">'+ $(this).html() +'</a></li>\n';
			}
		});
		$('#idxholder').append('<ul id="indexlist">'+idxlst+'</ul>').show();
	}
};


Scroll = {
	left: 0,
	width: 0,
	itemWidth: 0,
	interrupt: false,

	init: function() {
		$("#slider li").each(function() {
			Scroll.width+= parseInt($(this).width())+parseInt($(this).css("margin-left"))+parseInt($(this).css("margin-right"));
		});
		$("#slider .sledge").css("width", (Scroll.width*2));
		$("#slider .sledge li").each(function() {
			$(this).clone().insertAfter("#slider .sledge li:last");
		});
		Scroll.run();
		$("#slider").hover(function() {
			Scroll.interrupt= true;
		}, function() {
			Scroll.interrupt= false;
		});
	},
	run: function() {
		if(!Scroll.interrupt) {
			if((Scroll.left*-1)>=Scroll.width) {
				Scroll.left = 0;
				$("#slider .sledge").animate({
					marginLeft: Scroll.left
				}, 0);
			}
			Scroll.left-=10;
		}
		$("#slider .sledge").animate({
			marginLeft: Scroll.left
		}, 150, "linear", function() {
			Scroll.run();
		});  
	}              
};

Start = {
	current: 1,
	max: 0,
	
	init: function() {
		Start.max = $("#start .content .image img").size();
		$("#start .content .image img").css("z-index", 10);
		$("#intro"+Start.current).css("z-index", 20);
		
		//styles for startpage
		var dimensions= Util.getWindowSize();
		height= dimensions[1]-$("#start").height()+30;
		$("#fillup").css("height", height+10);
		
		
		if(Start.max>1) {
			window.setTimeout(Start.run, 1000);
		}
		
		Newsletter.init();
	},
	run: function() {
		next= Start.current+1;
		if(next>Start.max) {
			next= 1;
		}

		$("#start .content .image img").css("z-index", 10);
		$("#intro"+Start.current).css("z-index", 20);
		$("#intro"+next).css("z-index", 15);
		
		$("#intro"+Start.current).fadeOut("slow", function() {
			$("#start .content .image img").css("z-index", 10).show();
			$("#intro"+next).css("z-index", 20).show();
			Start.current= next;
			window.setTimeout(Start.run, 1000);
		});
	}
}

Newsletter= {
	init: function() {
		$("#.services .newsletter").click(function() {
			Newsletter.show();
		});
		$("#newsletter_subscribe .close").click(function() {
			Newsletter.hide();
		});
	},
	show: function() {
		$("#newsletter_subscribe").show();
	},
	hide: function() {
		$("#newsletter_subscribe").hide();
	},
	subscribe: function() {
		email= $("#newsletter_email").val();
		if(email.length>0 && email.match(/\S@\S.\S{2,}/)!=null) {
			$.ajax({
				url: '/helper/newsletter',
				data: 'newsletter_email='+email,
				type: 'POST',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					$("#newsletter_email").replaceWith('<div class="newsletter_result">Danke für Ihre Anmeldung</div>');
					$("#newsletter_submit").hide();
				}
			});
		} else {
			$("#newsletter_email").addClass("error");
		}
		return false;
	}
}

Util = {
	externalLinks: function() {
		//Source: http://www.sitepoint.com/print/standards-compliant-world/
		if (!document.getElementsByTagName) {
			return;
		}
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			
			//it's an external if the following 4 condition are true
			//1. has an attribute "href"			
			//2. the rel attribute = "external"
			//3. the refering domain is no the same as the current websit-domain
			//4. it's an "http"-Url									
			if (anchor.getAttribute("href") &&				
				anchor.getAttribute("rel") == "external" &&
				anchor.getAttribute("href").indexOf(window.location.hostname) < 0 &&
				anchor.getAttribute("href").indexOf("http://") >= 0 ) {
				anchor.target = "_blank";
			}
		}
	},
	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;
	}
}
