Body= {};
Util= {};
//Effect= {};

$(document).ready(function () {	
	//Effect.set();
	Body.set();
	window.setTimeout(Body.set, 2000);
	//window.setTimeout(Body.set, 100);
	
	/* init lightbox */
	if($(".lightbox")) {
		$(".lightbox").lightbox();
	}
	/* start */
	if($("#video")) {
		var lang= $("#language").html();
		$('#video .flash').flash({
		    src: '/_flash/humer_11_01_2012.swf',
		    width: 960,
		    height: 540,
			id: 'humer',
			name: 'humer',
			flashvars: { language: lang, xml: 'http://www.humer.com/_flash/config.xml' }
			//flashvars: { language: lang, xml: 'config.xml' }
		}).show();
	}
	
	/* teamflash */
	if($("#team_flash")) {
		$('#team_flash .flash').flash({
		    src: '/_flash/humer_team_11_11_2010.swf',
		    width: 640,
		    height: 500,
			id: 'humer',
			name: 'humer',
			flashvars: { assetUrl: 'http://www.humer.com/_flash/assets/', xmlUrl: 'http://www.humer.com/team_flash.php' }
		}).show();
	}
	Locationmap.init();
	Testimonial.init();
	ShopMenu.init();
	
	//hide side links if there is too less space
	var dimensions= Util.getWindowSize();
	if(dimensions[0]<1100) {
		$("#school_balloon, #feedback, #oeffnungszeiten, #standorte, #prospekte").hide();
	}
});
$(window).bind('resize', function() {	
	//Body.set();
});

Locationmap= {
	init: function() {
		$("#locationmap a, #locationmap span").hover(function() {
			detail= $(this).attr("class").replace("bigdot ", "").replace("smalldot ", "");
			$("#locationmap ."+detail+"_detail").fadeIn("fast");
		}, function() {
			detail= $(this).attr("class").replace("bigdot ", "").replace("smalldot ", "");
			$("#locationmap ."+detail+"_detail").fadeOut("slow");
		});
	}
}

Body= {
	set: function() {
		var dimension= Util.getWindowSize();
		var pageHeight= dimension[1];
		var windowHeight= dimension[3];
		
		var height= pageHeight;
		
		if(windowHeight>pageHeight) {
			height= windowHeight;
		}
		
		var leftHeight= height-270-32-150; /*  header-margin-footer */
		var rightHeight= height-270-20-83; /* header-margin-footer */

		if(BrowserDetect.browser=="Explorer") {
			leftHeight+=5;
		} else {
			rightHeight-=5;
		}
		$("#page .left .content").css("height", leftHeight);
		$("#page .right .content").css("height", rightHeight);
		
		Menu.init();
	} 
};

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;
	},
	submitForm: function(id) {
		if(Util.validateForm(id)) {
			 $("#form_shop").get(0).submit();
		}
	},
	validateForm: function(id) {
		var result= true;
		$("#"+id + " input, #"+id + " textarea").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
				var itemresult= Util.validate($(this).attr("id"));
				result= result && itemresult;
			}
		});
		if(result) {
			$("#form_error").removeClass("error");
		} else {
			$("#form_error").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;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");
		}
		return result;
	},
	validatePlaceSearch: function() {
		if ($('#form_searchplace').val() == "") {
			return false;
		}
	}
}

Effect= {
	shopHover: function(type, image, height, position) {
		var top= 24; //for list
		if(type=="search") {
			top= -26;
		}
		$("#shophover .hovercontent").html('<img src="'+image+'" />');
		if(height<198) {
			$("#shophover .hovercontent img").css("padding-top", (198-height)/2);
		}
		$("#shophover").css("top", top+(38*position));
		$("#shophover").show();
	},
	shopHoverOff: function() {
		$("#shophover").hide();
	}
}

Menu= {
	init: function() {
		$(".startmenu a, .special_startmenu a").hover(function() {
			var startmenu= $(this);
			$(".navigation li a").each(function() {
				var navigation= $(this);
				if(startmenu.attr("href")==navigation.attr("href")) {
					navigation.addClass("hover");
				}
			});
		}, function() {
			$(".navigation li a").removeClass("hover");
		});
		
		$(".navigation li a").hover(function() {
			var navigation= $(this);
			$(".startmenu a, .special_startmenu a").each(function() {
				var startmenu= $(this);
				if(startmenu.attr("href")==navigation.attr("href")) {
					startmenu.addClass("hover");
				}
			});
		}, function() {
			$(".startmenu a, .special_startmenu a").removeClass("hover");
		});
	}
}

Testimonial= {
	current: 1,
	left: 220,
	max: -1,
	init: function() {
		Testimonial.max= $("#testimonials .testimonial").size();
		counter= 1;
		$("#testimonials .testimonial").each(function() {
			$(this).attr("id", "testimonial"+counter);
			counter++;
		});
		
		$("#testimonials .testimonial_left").click(function() {
			Testimonial.move(-1);
		}).hide();
		
		$("#testimonials .testimonial_right").click(function() {
			Testimonial.move(1);
		});
		//check if arrows are necessary
		if(Testimonial.max<=1) {
			$("#testimonials .testimonial_right").hide();
		}
		
		$("#testimonials .testimonial .imageswitch li").click(function() {
			$("li", $(this).parent()).removeClass("active");
			$(this).addClass("active");
			$("a", $(this).parent().parent().parent()).hide();
			image= $(this).html();
			$(".gallery"+image, $(this).parent().parent().parent()).show();
		});
	},
	move: function(direction) {
		Testimonial.current+= direction;
		if(Testimonial.current==1) {
			$("#testimonials .testimonial_left").hide();
		} else {
			$("#testimonials .testimonial_left").show();
		}
		if(Testimonial.current==Testimonial.max) {
			$("#testimonials .testimonial_right").hide();
		} else {
			$("#testimonials .testimonial_right").show();
		}
		
		var left= parseInt(Testimonial.left-(Testimonial.current-1)*200);
		
		$("#testimonials .testimonial").each(function() {
			$(this).attr("class", $(this).attr("class").replace(/ active/, ' inactive'));
		});
		$("#testimonials .sledge").animate({
		    left: left
		  }, 300, function() {
		    $("#testimonial"+Testimonial.current).attr("class", $("#testimonial"+Testimonial.current).attr("class").replace(/ inactive/, ' active'));
		});
		
	}
}       

ShopMenu= {
	init: function() {

		/* mouseover small image */
		$(".entry").hover(function() {
			$(".exit").hide();
			var id= $(this).attr("id");
			$("#page .startmenu_shop #entry"+id.toString()+"_link").show();
		}, function() {
		});		
	
		/* mouseover big image */
		$(".exit").hover(function() {
			var imageLink= $(this);
			$(".navigation li a").each(function() {
				var navigation= $(this);
				if(imageLink.attr("href")==navigation.attr("href")) {
					navigation.addClass("hover");
				}
			});
		}, /* mouseout big image */ 
		function() {
			var helper= $(this).attr("id");
			
			var id= helper.substring(5,6);
			$("#page .startmenu_shop #entry"+id.toString()+"_link").hide();
			
			$(".navigation li a").removeClass("hover");
			
			var id= helper.substring(5,7);
			
			$("#page .startmenu_shop #entry"+id.toString()+"_link").hide();
			
			$(".navigation li a").removeClass("hover");
		});
	
		/* mouseover menu */
		$(".navigation li a").hover(function() {
			var navigation= $(this);
			$(".shopmenu img").each(function() {
				var imageSmall= $(this);
				if(imageSmall.attr("rel")==navigation.attr("href")) {
					var id= imageSmall.attr("id");
					$("#page .startmenu_shop #entry"+id.toString()+"_link").show();
				}
			});
		}, 
		/* mouseout menu */
		function() {
			var navigation= $(this);
			$(".shopmenu img").each(function() {
				var imageSmall= $(this);
				if(imageSmall.attr("rel")==navigation.attr("href")) {
					var id= imageSmall.attr("id");
					$("#page .startmenu_shop #entry"+id.toString()+"_link").hide();
				}
			});
		});
	}
}
