var Body= {};
var Util= {};
var Navigation= {};
var Newsletter= {};
var Event= {};
var Search= {};
var Panel= {};
var Slider= {};
var Plate= {};
var Recipe= {};
var News= {};
var Contact= {};
var Imprint= {};
var Form= {};
var Time= {};
var Video= {};

/* statemanager */
var StateManager = EXANIMO.managers.StateManager;
StateManager.onstatechange = function(e) {
	if(e.id=="defaultState") {
		Event.defaultState();
	} else {
		var ids = e.id.split("/");
		switch (ids[0]) {
			case "suche":
				if(ids[1]!='alle'){
					//Search.callback(ids[1]);
					Search.get(ids[1]);
				} else {
					Search.showAll(ids[2]);
				}
		    	break;
			case "newsletter":
		    	Newsletter.show();
		    	break;
		  	case "neuigkeiten":
		    	News.show(e.id);
		    	break;
		  	case "kontakt":
				Contact.show(e.id);
		    	break;
		  	case "impressum":
		    	Imprint.show(e.id);
		    	break;
		  	default:
				Panel.load(e.id);
		    break;
		}
		if(ids[0]=="services" && ids[1]=="rezepte" && ids[2]=="suche") {
			//Recipe.callback(ids[3]);
			Recipe.get(ids[3]);
		}
	}
}

/* const */
var minimalWidth= 900;
var minimalHeight= 675;
var maximalWidth= 1200;
var maximalHeight= 768;

/* window events */
$(document).ready(function () {	
	Body.init();
	window.setTimeout(Body.set, 2000);
	Time.set(false);
});
$(window).bind('resize', function() {	
	Body.set();
});

Body= {
	height: minimalHeight,
	width: minimalWidth,
	minLogoLeft: 710,
	
	init: function() {
		StateManager.initialize();
		Body.set();
	},	
	set: function() {
		Search.hide();
		Newsletter.hide();
		
		Body.height= Util.getWindowHeight();
		Body.width= Util.getWindowWidth();
		$("#body").css("height", Body.height);
		$("#body").css("width", Body.width);
		$("#footer").css("top", Body.height-37);
		$("#footer").css("width", Body.width);
		/*if($("#flashcontainer")) {
			$("#flashcontainer").css("width", Body.width);
			$("#flashcontainer").css("height", Body.height-50);
		}*/
		Body.center();
		Body.setLogo();
		Body.setPromotion();
		Panel.center();
		Video.init();
		window.setTimeout(Video.hideExplain, 8000);
	},
	setLogo: function() {
		var left= (Body.width-30)*0.8;
		if(left<Body.minLogoLeft) {
			left= Body.minLogoLeft;
		}
		$("#header .logo").css("left", left);
	},
	center: function() {
		var width= Util.getWindowSize();
		width= width[0];
		var left= 0
		
		if(width > Body.width) {
			var left= (width-Body.width)/2;
		}
		$("#body").css("top", "0");
		$("#body").css("left", left);
	},
	setPromotion: function() {
		//$("#shortpromotion").css("top", Body.height-150);
		$("#promotionbutton").css("top", Body.height-190);
		$("#promotion").css("top", Body.height-150);
		$("#krebs").css("top", Body.height-140);
	}
}

Util= {
	getWindowHeight: function() {
		var height= Util.getWindowSize();
		height= height[1];
		if(height<minimalHeight) {
			height= minimalHeight;
		} else if(height>maximalHeight) {
			height= maximalHeight;
		}
		return height;
	},
	getWindowWidth: function() {
		var width= Util.getWindowSize();
		width= width[0];
		if(width<minimalWidth) {
			width= minimalWidth;
		} else if(width>maximalWidth) {
			width= maximalWidth;
		}
		return width;
	},
	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;
	}
}

Navigation= {
	init: function() {
		
	},
	mouseover: function(object) {
		Search.hide();
		Navigation.reset(null);
		if($(object).parent().next().html()) {
			$("#menu_bg").animate({
				width: "280px"
			}, 100);
			$(object).parent().next().show();
			if(browser.isIE6x) {
				$(object).parent().parent().next().css("margin-top", "-28px");
			}
		}
		Navigation.reset(object);
		if(browser.isIE) {
			$(object).next().show();
		} else {
			$(object).next().fadeIn("slow");
		}
		$(object).animate({ 
			paddingLeft: "20px"
		}, 200, function() {
			$(object).addClass("arrow");
		});
	},
	mouseout: function(object) {
		if($(object).parent().next().html()) {
			$(object).parent().next().hide();
			if(browser.isIE6x) {
				$(object).parent().parent().next().css("margin-top", "10px");
			}
		}
		if($(object).css("padding-left")!="5px") {
			$(object).removeClass("arrow");
			if(browser.isIE) {
				$(object).next().hide();
			} else {
				$(object).next().fadeOut("fast");
			}
			$(object).animate({ 
				paddingLeft: "5px"
			}, 100, function() {
				$(object).removeClass("arrow");
			});
		}
	},
	reset: function(object) {
		$("#menu .link").each(function() {
			if($(this).children(".text").html()!=$(object).html()) {
				Navigation.mouseout($(this).children(".text"));
			}
		});
	},
	subMouseover: function(object) {
		if(browser.isIE) {
			$(object).next().show();
		} else {
			$(object).next().fadeIn("slow");
		}
		$(object).animate({ 
			paddingLeft: "20px"
		}, 200, function() {
			Navigation.subReset(object);
			$(object).addClass("arrow");
		});
	},
	subMouseout: function(object) {
		$(object).removeClass("arrow");
		if($(object).css("padding-left")!="5px") {
			if(browser.isIE) {
				$(object).next().hide();
			} else {
				$(object).next().fadeOut("fast");
			}
			$(object).parent().next().hide();		
			$(object).animate({ 
				paddingLeft: "5px"
			}, 100, function() {
				$(object).removeClass("arrow");
			});
		}
	},
	subReset: function(object) {
		$("#menu .sublink").each(function() {
			if($(this).children(".text").html()!=$(object).html()) {
				Navigation.mouseout($(this).children(".text"));
			}
		});
	},
	hide: function() {
		if(browser.isIE) {
			$("#menu").hide();
			$("#menu_bg").hide();
			$("#links").hide();
			$("#links_bg").hide();
		} else {
			$("#menu").fadeOut("fast");
			$("#menu_bg").fadeOut("fast");
			$("#links").fadeOut("fast");
			$("#links_bg").fadeOut("fast");
		}
	},
	show: function() {
		if(browser.isIE) {
			$("#menu").show();
			$("#menu_bg").show();
			$("#links").show();
			$("#links_bg").show();
		} else {
			$("#menu").fadeIn("fast");
			$("#menu_bg").fadeIn("fast");
			$("#links").fadeIn("fast");
			$("#links_bg").fadeIn("fast");
		}
	}
}

Newsletter= {
	show: function() {
		var top= Body.height-313-37;
		$("#newsletter").css("top", top).fadeIn("fast");
	},
	save: function() {
		var gender= "";
		if($("#newsletter_gender_male").get(0).checked) {
			gender= "male";
		} else if($("#newsletter_gender_female").get(0).checked) {
			gender= "female";
		}
		var title= $("#newsletter_title").val();
		var firstname= $("#newsletter_firstname").val();
		var lastname= $("#newsletter_lastname").val();
		var email= $("#newsletter_email").val();
		var result= true;
		
		if(firstname=="") {
			$("#newsletter_firstname").css("border", "1px solid #ee0000");
			result= result && false;
		} else {
			$("#newsletter_firstname").css("border", "1px solid #c0c0c0");
		}
		if(lastname=="") {
			$("#newsletter_lastname").css("border", "1px solid #ee0000");
			result= result && false;
		} else {
			$("#newsletter_lastname").css("border", "1px solid #c0c0c0");
		}
		if(gender=="") {
			$("#newsletter_gender_frame").css("color", "#ee0000");
		} else {
			$("#newsletter_gender_frame").css("color", "");
		}
		if(email.indexOf("@")==-1 || email.indexOf(".")==-1) {
			$("#newsletter_email").css("border", "1px solid #ee0000");
			result= result && false;
		} else {
			$("#newsletter_email").css("border", "1px solid #c0c0c0");
		}
		
		if(result) {
			$.ajax({
				url: '/newsletter/index.php',
				data: 'title='+encodeURIComponent(title)+'&firstname='+encodeURIComponent(firstname)+'&lastname='+encodeURIComponent(lastname)+'&gender='+encodeURIComponent(gender)+'&email='+encodeURIComponent(email),
				type: 'POST',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					$("#newsletter_form_frame").html(html);
				}
			});	
		}
	},
	hide: function() {
		$("#newsletter").fadeOut("fast");
	}
}

Event= {
	keyHandler: function(event) {
		var kC= (window.event) ? event.keyCode : event.keyCode;
		var Esc = (window.event) ? 27 : event.DOM_VK_ESCAPE;
		if(kC==Esc) { 
			StateManager.setState(StateManager.defaultStateID);
		}
	},
	defaultState: function() {
		Newsletter.hide();
		Search.hide();
		Panel.hide();
		
		Navigation.show();
	}
}

Search= {
	last: 0,
	timestamp: 0,
	
	callback: function(term) {
		term= decodeURIComponent(term);
		if($("#searchfield").val()!=term) {
			$("#searchfield").val(term);
		}
	},	
	go: function() {
		var searchstring= $("#searchfield").val();
		StateManager.setState('suche/'+searchstring);
	},	
	get: function(term) {
		//do not search the same again
		if(Search.last==term) return;
		//remember the search
		Search.last= term;
		
		//get searchstring and search only with words longer than 2 chars
		if(term.length<=1) {
			$("#searchresults").hide();
			return;
		}
		
		term= term.toLowerCase();
		term= term.replace(/ä/g, "%C3%A4").replace(/ü/g, "%C3%BC").replace(/ö/g, "%C3%B6").replace(/ß/g, "%C3%9F").replace(/\'/g, "%27");
		
		$.ajax({
			url: '/_content/productsearch.php',
			data: 'type=searchasyoutype&term='+term,
			type: 'GET',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				$("#searchresults").html(html);
				$("#searchresults .results .row").each(function() {
					$(this).hover(function() {
						$(this).addClass("hover");
					}, function() {
						$(this).removeClass("hover");
					});
				});
				
				Search.show();

				$("#searchresults .close").click(function () {
					Search.hide();
				});
			}
		});
	},
	show: function() {
		if($("#searchresults").css("display")=="none") {
			$("#searchresults").css("top", Body.height);
			$("#searchresults").show();
			Search.adjust();
			$("#searchresultscloser").width(Body.width);
			$("#searchresultscloser").height(Body.height);
			$("#searchresultscloser").show();
		} else {
			Search.adjust();
		}
	},
	hide: function() {
		if($("#searchresults").css("display")=="block") {
			Search.last= "";
			$("#searchresultscloser").hide();
			var height= Body.height;
			$("#searchresults").animate({
				top: height+"px"
			}, 500, function() {
				$(this).hide();
			});
		}
	},
	adjust: function() {
		var height= Body.height;
		if($("#searchresults").html().match("<!--notall-->")) {
			height-= 338;
		} else {
			height-= 369;
		}
		$("#searchresults").animate({
			top: height+"px"
		}, 500);
	},
	showAll: function(term) {
		//get searchstring and search only with words longer than 2 chars
		if(term.length<=1) {
			return;
		}
		
		term= term.toLowerCase();
		term= term.replace(/ä/g, "%C3%A4").replace(/ü/g, "%C3%BC").replace(/ö/g, "%C3%B6").replace(/ß/g, "%C3%9F").replace(/\'/g, "%27");
		
		$.ajax({
			url: '/_content/productsearch.php',
			data: 'type=all&term='+term,
			type: 'GET',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				$("#panel").html(html);
				Panel.show();
				Panel.initSlideables();
			}
		});
	}
}

Panel= {
	ids: '',
	minTop: 80,
	minLeft: 100,
	
	show: function() {
		Navigation.hide();
		$("#panel").show();
	},
	initSlideables: function() {
		$("#panel .slideable").each(function() {
			var slideable= $(this);
			var classes= slideable.attr("class").split(" ");
			var height= slideable.height();
			for(var index=0; index<classes.length; index++) {
				if(classes[index].match("height")) {
					height= classes[index].replace(/height/g, '');
				}
			}
			if(height<slideable.height()) {
				/* special for nutrition */
				$(".nutritionspacer", slideable).each(function() {
					$(this).css("width", parseInt($(this).css("width"))-20);
				});
				/* set slideable */
			    slideable.html('<div class="slideable_content" style="width: '+(slideable.width()-20)+'px;">'+slideable.html()+'</div><div class="slideable_slider" style="height: '+height+'px;"></div>');	
				var overflow= $('.slideable_content', slideable).height()-height; 
		
				$(".slideable_slider", slideable).slider({ 
					handle: '.ui-slider-handle',
					slide: function (event, ui) {
					 	var top= parseFloat(overflow*ui.value/100);
						$(".slideable_content", slideable).css('margin-top', '-' + top + 'px');
					}, 
					stop: function (event, ui) {
						var top= overflow*ui.value/100;
						$(".slideable_content", slideable).animate({ 'margin-top' : '-' + top + 'px' }, 500, 'linear');
					}
				});
				
				/* set frame */
				slideable.css("width", slideable.width()).css("height", height);
			}
		});
	},
	center: function() {
		var top= 0;
		var left= 0;
		var panelHeight= parseInt($("#panel").css("height"));
		var panelWidth= parseInt($("#panel").css("width"));
		
		top= (Body.height-panelHeight)/2;
		left= (Body.width-panelWidth)/3;

		if(top<Panel.minTop) {
			top= Panel.minTop;
		}
		/*if(left<Panel.minLeft) {
			left= Panel.minLeft;
		}*/
		
		$("#panel").css("top", top).css("left", left);
	},
	load: function(ids) {
		if(Panel.ids==ids) {
			Panel.show();
			return;
		}
		//check what must be reloaded
		var currentIds;
		var futureIds;
		//cause of the problem that in javascript an empty array after a split is an array with the length 1
		if(Panel.ids=="") {
			currentIds= new Array();
		} else {
			currentIds= Panel.ids.split("/");
		}
		futureIds= ids.split("/");
		switch(futureIds[0]) {
			case 'ueber_uns':
				Panel.loadContentPages(ids, currentIds, futureIds);
				break;
			case 'produkte':
				Panel.loadProductPages(ids, currentIds, futureIds);
				break;
			case 'milch_abc':
				Panel.loadContentPages(ids, currentIds, futureIds);
				break;
			case 'services':
				switch(futureIds[1]) {
					case 'rezepte':
						Panel.loadRecipePages(ids, currentIds, futureIds);
						break;
					case 'kaeseplatten':
						Panel.loadPlatePages(ids, currentIds, futureIds);
						break;
					default:
						Panel.loadContentPages(ids, currentIds, futureIds);
						break;
				}
				break;
			case 'ngw':
				Panel.loadNgwPages(ids, currentIds, futureIds);
				break;
		}
		
		Panel.ids= ids;
	},
	loadContentPages: function(ids, currentIds, futureIds) {
		//world is already open
		if(currentIds.length>1 && futureIds.length>1 && currentIds[0]+"/"+currentIds[1]==futureIds[0]+"/"+futureIds[1]) {
			var box= 'box0'; /* default overview */
			$("#panel .navigation li").each(function() {
				if($(this).attr("class").match(futureIds[2])) {
					cssclass= $(this).attr('class').split(" ");
					box= cssclass[1];
				}
			});
			boxCount= box.replace(/box/g, '');
			marginLeft= boxCount*-690;
			$("#panel .row").css("margin-left", marginLeft);
			
			Panel.setNavigation(box);
		//new
		} else {
			$.ajax({
				url: '/_content/get.php',
				data: 'action=panel&ids='+ids,
				type: 'GET',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					$("#panel").html(html);
					Panel.show();
					Panel.initSlideables();
				}
			});
		}
	},
	loadProductPages: function(ids, currentIds, futureIds) {
		//if the first three parameters are the same the panel is already open but one content has changed
		if(currentIds.length>2 && futureIds.length>2 && currentIds[0]+"/"+currentIds[1]+"/"+currentIds[2]==futureIds[0]+"/"+futureIds[1]+"/"+futureIds[2]) {
			var box= '';
			$("#panel .navigation li").each(function() {
				cssclass= $(this).attr('class').split(" ");
				if(cssclass[0]==futureIds[3]) {
					box= cssclass[1];
				}
			});
			boxCount= box.replace(/box/g, '');
			marginLeft= boxCount*-690;
			if(futureIds[3]=="erhaeltlichbei") {
				marginLeft= 6*-690;
			}
			$("#panel .row").css("margin-left", marginLeft);
			
			if(futureIds[3]!="produktinfo") {
				$("#wheretobuy").hide();
			} else {
				$("#wheretobuy").show();
			}
			
			Panel.setNavigation(box);
		//new
		} else {
			$.ajax({
				url: '/_content/get.php',
				data: 'action=panel&ids='+ids,
				type: 'GET',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					$("#panel").html(html);
					Panel.show();
					Slider.init();
					//set sliderPanel position
					ids= Panel.ids.split("/");
					if(ids[2]!="") {
						Slider.jumpTo(ids[2]);
					}
					Panel.initSlideables();
				}
			});
		}
	},
	loadPlatePages: function(ids, currentIds, futureIds) {
		$.ajax({
			url: '/_content/get.php',
			data: 'action=panel&ids='+ids,
			type: 'GET',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				$("#panel").html(html);
				Panel.show();
				Slider.init();
				//set sliderPanel position
				ids= Panel.ids.split("/");
				if(ids[2]!="") {
					Slider.jumpTo(ids[2]);
				}
				Panel.initSlideables();
			}
		});
	},
	loadRecipePages: function(ids, currentIds, futureIds) {
		//if the first four parameters are the same the panel is already open but one content has changed
		if((currentIds.length>3 && futureIds.length>3 && currentIds[0]+"/"+currentIds[1]+"/"+currentIds[2]+"/"+currentIds[3]==futureIds[0]+"/"+futureIds[1]+"/"+futureIds[2]+"/"+futureIds[3])
		|| (currentIds.length>3 && futureIds.length>3 && currentIds[0]=='services' && currentIds[1]=='rezepte' && currentIds[2]=='suche' && futureIds[0]=='services' && futureIds[1]=='rezepte' && futureIds[2]=='suche')) {
			//new
		} else {
			$.ajax({
				url: '/_content/get.php',
				data: 'action=panel&ids='+ids,
				type: 'GET',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					$("#panel").html(html);
					Panel.show();
					Panel.initSlideables();
					if(futureIds[2]=="suche") {
						$("#recipesearchfield").val(futureIds[3]);
					}
				}
			});
		}
	},
	hide: function() {
		Navigation.show();
		$("#panel").hide();
	},
	setNavigation: function(box) {
		if(box=="") {
			box= "ignore";
		}
		$("#panel .navigation li").each(function() {
			if($(this).attr("class").match(box)) {
				$(this).addClass("active");
			} else {
				$(this).removeClass("active");
			}
		});
	},
	loadNgwPages: function(ids, currentIds, futureIds) {
		//world is already open
		if(currentIds.length>0 && futureIds.length>0 && currentIds[0]==futureIds[0]) {
			var box= 'box0'; /* default overview */
			$("#panel .navigation li").each(function() {
				if($(this).attr("class").match(futureIds[1])) {
					cssclass= $(this).attr('class').split(" ");
					box= cssclass[1];
				}
			});
			boxCount= box.replace(/box/g, '');
			marginLeft= boxCount*-690;
			$("#panel .row").css("margin-left", marginLeft);
			
			Panel.setNavigation(box);
		//new
		} else {
			$.ajax({
				url: '/_content/get.php',
				data: 'action=panel&ids='+ids,
				type: 'GET',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					$("#panel").html(html);
					Panel.show();
					Panel.initSlideables();
				}
			});
		}
	}
}

Slider= {
	itemWidth: 116,
	visibleItems: 6,
	
	init: function() {
		if(!$("#sliderPanel")) {
			return;
		}

		if($("#sliderPanel").attr("class").match("big")) {
			Slider.itemWidth= 140;
			Slider.visibleItems= 5;
		} else {
			Slider.itemWidth= 116;
			Slider.visibleItems= 6;
		}
		
		var items= $("#sliderPanel .items li");
		var delimeter= $("#sliderPanel .items li.delimeter");
		var sliderWidth= ((items.length-delimeter.length)*Slider.itemWidth)-(690);
		
		/* set events */
		$("#sliderPanel .previous").click(function() { 
			Slider.previous(); 
		});
		$("#sliderPanel .next").click(function() { 
			Slider.next(); 
		});
		
		//set jumppoints
		Slider.positionJumppoints();
		
		//set sliderhandle
		$("#slider").slider({ 
			handle: '.ui-slider-handle',
			slide: function (event, ui) {
				var left= (sliderWidth*ui.value)/100;
				$("#sliderPanel .items").css('left', '-' + left + 'px');
			}, 
			stop: function (event, ui) {
				var left= (sliderWidth*ui.value)/100;
				$("#sliderPanel .items").animate({ 'left' : '-' + left + 'px' }, 500, 'linear');
			}
		});
	},
	previous: function() {
		Slider.shift("previous");
	},
	next: function() {
		Slider.shift("next");
	},
	shift: function(direction) {
		if(direction=="next") {
			direction= -1;
		} else {
			direction= 1;
		}
		
		var items= $("#sliderPanel .items li").length;
		items-= $("#sliderPanel .items li.delimeter").length; /* delimeter aren't items */
		
		var left= parseInt($("#sliderPanel .items").css('left'));
		if(isNaN(left)) left= 0;
		var position= Math.round(parseFloat(left)/Slider.itemWidth);
		
		position+= direction;
		if(position>0) {
			position= 0;
		}
		if(position<(items*-1)+Slider.visibleItems) { /* ignore the visible ones */
			position= (items*-1)+Slider.visibleItems;
		}
		var listPosition= position*Slider.itemWidth;
		
		var sliderPosition = 0;
		if(!isNaN(parseInt($('#sliderPanel .ui-slider-handle').css('left')))) {
			sliderPosition = parseInt($('#sliderPanel .ui-slider-handle').css('left'));
		}
		var each= 578/(items-Slider.visibleItems); /* - cause of visible items, and 578 is max */
		sliderPosition-= direction*each;
		if(sliderPosition<0) sliderPosition= 0;
		if(sliderPosition>578) sliderPosition= 578;
		
		$("#sliderPanel .items").animate({ 'left' : listPosition + 'px' }, 250, 'linear');
		$('#sliderPanel .ui-slider-handle').animate({ 'left' : sliderPosition + 'px' }, 250, 'linear');
	},
	jumpTo: function(name) {
		var item= false;
		var position= 0;
		var items= 0;
		$("#sliderPanel .items li").each(function() {
			if($(this).attr("id")==name) {
				item= $(this);
				position= items;
			}
			if($(this).attr("class")!="delimeter") {
				items++;
			}
		});
		position= position-((Slider.visibleItems-1)/2); /* to put it in the middle */
		
		var listPosition= (position)*Slider.itemWidth*-1;
		if(listPosition>0) {
			listPosition= 0;
		}
		if(listPosition<(items-Slider.visibleItems)*Slider.itemWidth*-1) {
			listPosition= (items-Slider.visibleItems)*Slider.itemWidth*-1;
		}
		
		var sliderPosition = 0;
		if(!isNaN(parseInt($('#sliderPanel .ui-slider-handle').css('left')))) {
			sliderPosition = parseInt($('#sliderPanel .ui-slider-handle').css('left'));
		}
		var each= 578/(items-Slider.visibleItems); /* - cause of visible items, 578 is max */
		sliderPosition= each*(position); /* to recalculate positioning */
		if(sliderPosition<0) sliderPosition= 0;
		if(sliderPosition>578) sliderPosition= 578;
		
		//$("#sliderPanel .items").animate({ 'left' : listPosition + 'px' }, 250, 'linear');
		//$('#sliderPanel .ui-slider-handle').animate({ 'left' : sliderPosition + 'px' }, 250, 'linear');
		$("#sliderPanel .items").css("left", listPosition);
		$('#sliderPanel .ui-slider-handle').css("left", sliderPosition);
	},
	positionJumppoints: function() {
		if($("#sliderPanel .jumppoint").length==0) {
			return;
		}
		var jumppoints= new Object();
		var items= 0;
		$("#sliderPanel .items li").each(function() {
			/* count jumppoints */
			if(!jumppoints[$(this).attr("class")]) {
				jumppoints[$(this).attr("class")]= 1;
			} else {
				jumppoints[$(this).attr("class")]++;
			}
			/* count items */
			if($(this).attr("class")!="delimeter") {
				items++;
			}
		});
		
		var each= Math.round(640/items); /* 640 cause of previous and next button */
		var cumulate= 0;
		$("#sliderPanel .jumppoint").each(function() {
			left= (cumulate-3)*each; /* left border, 3 cause of middle */
			width= jumppoints[$(this).attr("id")]*each; /* current width */
			left+= (width/2)-($(this).width()/2); /* center item */

			$(this).css("left", left);
			
			cumulate+= jumppoints[$(this).attr("id")];
		});
	}
}

Plate= {
	shoppingCountUp: function() {
		var count= parseInt($("#shoppinglist_counter").html());
		if(count<99) {
			$("#shoppinglist_counter").html(count+1);
			$("#printlist_button").attr("href", $("#printlist_button").attr("href").replace("count="+count, "count="+(count+1)));
		}
		
	},
	shoppingCountDown: function() {
		var count= parseInt($("#shoppinglist_counter").html());
		if(count>1) {
			$("#shoppinglist_counter").html(count-1);
			$("#printlist_button").attr("href", $("#printlist_button").attr("href").replace("count="+count, "count="+(count-1)));
		}
		
	}
}

Recipe= {
	lastTerm: '',
	lastType: '',
	
	showTip: function() {
		if($("#panel .tipcontent")) {
			$("#panel .tipcontent").fadeIn("fast");
		}
	},
	hideTip: function() {
		if($("#panel .tipcontent")) {
			$("#panel .tipcontent").fadeOut("fast");
		}
	},
	callback: function(term) {
		term= decodeURIComponent(term);
		if($("#recipesearchfield").val()!=term) {
			$("#recipesearchfield").val(term);
		}
	},	
	go: function() {
		var searchstring= $("#recipesearchfield").val();
		StateManager.setState('services/rezepte/suche/'+searchstring);
	},	
	get: function(term) {
		var type= $("#searchrecipe_form input[@name=searchtype]:checked").val();
		if(!type) {
			type= 'Alle Rezepte';
		}
		//do not search the same again
		if(Recipe.lastTerm==term && Recipe.lastType==type) return;
		//remember the search
		Recipe.lastTerm= term;
		Recipe.lastType= type;
		
		term= encodeURIComponent(term.toLowerCase());
		//term= term.replace(/ä/g, "%C3%A4").replace(/ü/g, "%C3%BC").replace(/ö/g, "%C3%B6").replace(/ß/g, "%C3%9F").replace(/\'/g, "%27");

		$.ajax({
			url: '/_content/recipesearch.php',
			data: 'term='+term+'&type='+type,
			type: 'GET',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				$("#recipesearchresults").html(html);
				Panel.initSlideables();
			}
		});
	}
}

News= {
	show: function(ids) {
		$.ajax({
			url: '/_content/get.php',
			data: 'action=panel&ids='+ids,
			type: 'GET',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				$("#panel").html(html);
				Panel.show();
				Panel.initSlideables();
			}
		});
	}
}

Contact= {
	show: function(ids) {
		$.ajax({
			url: '/_content/get.php',
			data: 'action=panel&ids='+ids,
			type: 'GET',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				$("#panel").html(html);
				Panel.show();
			}
		});
	}
}

Imprint= {
	show: function(ids) {
		$.ajax({
			url: '/_content/get.php',
			data: 'action=panel&ids='+ids,
			type: 'GET',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				$("#panel").html(html);
				Panel.show();
			}
		});
	}
}

Form= {
	send: function(id) {
		var filledOut= true;
		$("#"+id+" .text").each(function() {
			if($(this).val()=="") {
				filledOut= filledOut && false;
				$(this).css("border", "1px solid #ee0000");
			} else {
				$(this).css("border", "1px solid #c0c0c0");
			}
		});
		$("#"+id+" .email").each(function() {
			if($(this).val().indexOf("@")>0 && $(this).val().indexOf(".")>0) {
				$(this).css("border", "1px solid #c0c0c0");
			} else {
				filledOut= filledOut && false;
				$(this).css("border", "1px solid #ee0000");
			}
		});
		
		if(filledOut) {
			var data= "";
			var first= "";
			$("#"+id+" .text, #"+id+" .email").each(function() {
				data+= first+$(this).attr("id")+"="+encodeURIComponent($(this).val());
				first= "&";
			});
			$.ajax({
				url: '/formular/index.php',
				data: data,
				type: 'GET',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					if(html=="ok") {
						$("#panel .box .left").html($("#panel .box .left").html() + "<p><strong>Vielen Dank für Ihre Nachricht</strong></p>");
						$("#contactform_div").hide();
					} else {
						$("#panel .box .left").html($("#panel .box .left").html() + "<p><strong>Leider trat ein Fehler auf. Versuchen Sie es bitte später erneut oder schicken Sie direkt eine E-Mail an office@schaerdinger.at</strong></p>");
						$("#contactform_div").hide();
					}
				}
			});
		}
	}
}

Time= {	
	morningstart: 5,
	daystart: 8,
	nightstart: 18,
	
	set: function(force) {
		var type= 0; // 0=morning, 1=day, 2=night
		var date= new Date();
		var hour= date.getHours();
		
		if(hour>=Time.nightstart || hour<Time.morningstart) {
			type= 2;
		}
		if(hour>=Time.daystart && hour<Time.nightstart) {
			type= 1;
		}
		if(hour>=Time.morningstart && hour<Time.daystart) {
			type= 0;
		}
		//reset
		$("#background").removeClass("morning").removeClass("day").removeClass("night").removeClass("dark");
		if(!swfobject.hasFlashPlayerVersion('9.0.28') || force) {
			if(type==0) $("#background").addClass("morning");
			if(type==1) $("#background").addClass("day");
			if(type==2) $("#background").addClass("night");
			$("#background").html('<img src="/_img/bg/blank.gif" width="1200" height="675" alt="" />');
		}
		if(!swfobject.hasFlashPlayerVersion('9.0.28') && !force) {
			Video.hideExplain();
			$("#header .novideo").hide();
		}
		if(type==2) $("#body").addClass("dark");
	},
	setHtml: function() {
		
	}
}

Video= {
	init: function() {
		$("#header .novideo").click(function() {
			$("#flashcontainer").html('<div id="background"></div>');
			$("#background").css("visibility", "visible");
			Time.set(true);
			$(this).hide();
		});
		$("#header .novideo_explain").hover(function() {
			Video.hideExplain();
		}, function() {
			Video.hideExplain();
		});
	},
	hideExplain: function() {
		$("#header .novideo_explain").hide();
	}
}