MyHistory= {
	hash: '',
	checkHash: function() {
		if(MyHistory.hash!=location.hash) {
			MyHistory.hash= location.hash;
			
			var multicontentframe= $('#item'+MyHistory.hash.replace(/#/, ''));
			if(multicontentframe.html()) {
				Page.clapTeaser(multicontentframe);
			}
		}
		window.setTimeout(MyHistory.checkHash, 500);
	}
}

Page= {
	sizes: null,
	itemsPerPage: 8,
	partnerCountry: '',
	
	init: function() {
		Page.sizes= Util.getWindowSize();
		
		var left= 0;
		$("#navigation .main a.mainlink").each(function() {
			$(this).hover(function() {
				Navigation.hoverMenu($(this));
			}, function() {
				$("#navigation .subnavigation").hide();
			});
			left+= $(this).width()+20;
			$(this).attr("rel", left);
		});
		// breadcrumbs
		left=0;
		$("#navigation .breadcrumb a").each(function() {
			$(this).hover(function() {
				Navigation.hoverBreadcrumb($(this));
			}, function() {
				$("#navigation .subnavigation").hide();
			});
			left+= $(this).width()+20;
			$(this).attr("rel", left);
		});
		// mainmenu
		Page.setMainMenu();
		// startpage headlines
		$("#start .boxes .box .category img").each(function() {
			if($(this).height()<30) {
				$(this).css("padding-top", 30-$(this).height());
			}
		});
		// leftbox headlines
		$("#content .left .box .category img").each(function() {
			if($(this).height()<30) {
				$(this).css("padding-top", 30-$(this).height());
			}
		});
		// keep the subnavigation in front
		$("#navigation .subnavigation").hover(function() {
			$("#navigation .subnavigation").show();
		}, function() {
			$("#navigation .subnavigation").hide();
		});
		// set glass		
		$("#glass").css("width", Page.sizes[0]).css("height", Page.sizes[1]).click(Page.closeAll);
		
		Login.init();
		Page.setHeadlines();
		Page.setTeaser();
		Page.initAppointmentCategory();
		Page.initPartnermenu();
		window.setTimeout(Page.setMainMenu, 2000);
	},
	setMainMenu: function() {
		$("#navigation .main .mainlink").each(function() {
			if($(".standard img", $(this)).height()<30) {
				/* check because of actives */
				height= $(".standard img", $(this)).height();
				if(height==0) {
					height= $(".hover img", $(this)).height();
				}
				if(height!=null) {
					$(".standard, .hover", $(this)).css("padding-top", 30-height);
				}
			}
		});
	},
	setTeaser: function() {
		$(".multicontentframe").each(function() {
			$('.teaser', $(this)).show();
			$('.details', $(this)).css("height", $('.details', $(this)).height()).hide();
		});
		
		var counter= 0;
		$(".multicontentframe").each(function() {
			//add a counter
			$(this).addClass("counter"+counter);
			
			if(!$(this).attr("class").match("inactive")) {
				// set teaser functionality
				/*$('.teaser', $(this)).click(function() {
					Page.clapTeaser($(this));
				});*/			
				// set paging functionality
				if(counter>=Page.itemsPerPage) {
					$(this).addClass('hide');
				}
			
				counter++;
			}
		});
		
		//add page buttons
		var pages= Math.ceil(counter/Page.itemsPerPage);
		if(pages>1) {
			var pagesHtml= '<ul class="pages">';
			active= ' active';
			for(var i=1;i<=pages;i++) {
				pagesHtml+= '	<li class="page'+active+'">'+i+'</li>';
				active= '';
			}
			pagesHtml+= '</ul>';
			if($("#page ul.pages").html()) {
				$("#page ul.pages").replaceWith(pagesHtml).show();
			} else {
				$("#page .content .right").append(pagesHtml);
			}
			// set page functionality
			$("#page .content .right .pages .page").hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			}).click(function() {
				//set location
				location.href= '#';
				//set button
				$("#page .content .right .pages .page").removeClass("active");
				$(this).addClass("active");
				//set teaser
				var page= $(this).html();
				$(".multicontentframe").addClass("hide");
				var first= ((page-1)*Page.itemsPerPage);
				var last= (page*Page.itemsPerPage)-1;

				for(var i=first;i<=last;i++) {
					$(".counter"+i).removeClass("hide");
				}
				//close and hide all before
				$(".multicontentframe").each(function() {
					$('.teaser', $(this)).show();
					$('.details', $(this)).css("height", $('.details', $(this)).height()).hide();
				});
			});
		} else {
			$("#page ul.pages").hide();
		}
	},
	clapTeaser: function(obj) {
		//close and hide all before
		$(".multicontentframe").each(function() {
			$('.teaser', $(this)).show();
			$('.details', $(this)).css("height", $('.details', $(this)).height()).hide();
		});
	
		//open clicked
		var details= $('.details', obj);
		var teaser= $('.teaser', obj);
	
		var height= parseInt(details.css("height"));
		details.css("height", 0);
		details.css("visibility", "hidden").show();
		details.css("visibility", "visible").animate({
			height: height+'px'
		}, 200, 'swing', function() {
			details.css("height", "auto");
			$.scrollTo('#'+obj.attr("id"), 200, {easing:'swing'});
		});
		teaser.hide();
		
		$('.details', obj).css("height", $('.details', $(this)).height()).hide();
	},
	initAppointmentCategory: function() {
		if($("#appointment_categories")) {
			$("#appointment_categories a").each(function() {
				var cssclass= $(this).attr('class').replace(/ active/, '');
				var hide= true;
				
				$(".multicontentframe").each(function() {
					if($(this).attr("class").match(cssclass)) {
						hide= false;
					}
				});
				// exception for all
				if(cssclass=="all") {
					hide= false;
				}
				if(hide) {
					$(this).hide();
				}
			});
		}
	},
	// init for references is not necessary
	setAppointmentCategory: function(type) {
		$(".submenu li a").removeClass("active");
		$(".submenu li a."+type).addClass("active");
		$(".multicontentframe").each(function() {
			$(this).removeClass("hide");
			if(!$(this).attr("class").match(type) && type!="all") {
				$(this).addClass("inactive");
			} else {
				$(this).removeClass("inactive");	
			}
		});
		
		Page.setTeaser();
	},
	setPartnerCountry: function(type) {
		Page.partnerCountry = type;
		$(".submenu li a").removeClass("active");
		$(".submenu li a."+type.toLowerCase()).addClass("active");
		$("#partnermenu li").removeClass("active");
		$(".partnerframe").removeClass("hide");
		
		if(type!="all") {
			var counter= 0;
			//var types= new Array('Screens' => false, 'HDAV' => false, 'Multidisplay' => false, 'Showcontrol' => false, 'Mediacontrol' => false, 'Digitalsignage' => false);
			$(".partnerframe").each(function() {
				var countries= $(".countries", $(this)).html();
				if(countries=='' || countries==null || !countries.match(Page.partnerCountry)) {
					$(this).addClass("hide");
				} else {
					counter++;
				}
				
			});
			if(counter<=3) {
				$("#partnermenu").hide();
			} else {
				$("#partnermenu").show();
			}
		}
	},
	initPartnermenu: function() {
		$("#partnermenu li").hover(function() {
			$(this).addClass("hover");
		}, function() {
			$(this).removeClass("hover");
		}).click(function() {
			$("#partnermenu li").removeClass("active");
			$(this).addClass("active");
			$(".partnerframe").removeClass("hide");
			//check on countries
			$(".partnerframe").each(function() {
				var countries= $(".countries", $(this)).html();
				if(Page.partnerCountry!="all" && (countries=='' || countries==null || !countries.match(Page.partnerCountry))) {
					$(this).addClass("hide");
				}
			});
			var product= $(this).html();
			$(".partnerframe").each(function() {
				var productrange= $(".partner", $(this)).html();
				if(productrange=='' || productrange==null || !productrange.match(product)) {
					$(this).addClass("hide");
				}
			});
		});
	},
	setHeadlines: function() {
		$("#content h2").each(function() {
			formerHeight= parseInt($(this).css("height"));
			$(this).css("height", "auto");
			if($(this).height()>formerHeight) {
				$(this).addClass("big");
				$(this).css("height", formerHeight);
				$(this).css("height", 36);
			} else {
				$(this).css("height", formerHeight);
			}
		});
	},
	closeAll: function() {
		$(".overlay").hide();
	}
}

Navigation= {
	hoverMenu: function(object) {
		// load submenu
		$.ajax({
			url: '/helper/submenu',
			data: 'url='+object.attr("href"),
			type: 'GET',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				$("#navigation .subnavigation .navigation").html(html);
				// calculate position
				if(html.length>0) {
					var positionLeft= parseInt(object.attr("rel"))+parseInt($("#navigation .main").css("left"))-object.width()-12-20;
					if(positionLeft+222>934) {
						$("#navigation .subnavigation .mainbackground").css("margin-left", ((positionLeft+222)-934));
						positionLeft=712;
					} else {
						$("#navigation .subnavigation .mainbackground").css("margin-left", 0);
					}
					// because the sub is not outside wie have to reposition the highlight
					if(parseInt($("#navigation .subnavigation .mainbackground").css("margin-left"))==0) {
						$("#navigation .subnavigation .navigation_begin").css("background-image", "url(/_img/background/subnavigation_borders_left.png)");
					} else if(parseInt($("#navigation .subnavigation .mainbackground").css("margin-left"))+parseInt(object.width()+22)==222) {
						$("#navigation .subnavigation .navigation_begin").css("background-image", "url(/_img/background/subnavigation_borders_right.png)");
					} else {
						$("#navigation .subnavigation .navigation_begin").css("background-image", "url(/_img/background/subnavigation_borders.png)");
					}
					$("#navigation .subnavigation").css("left", positionLeft).css("top", 53);
					$("#navigation .breadcrumb").css("z-index", 5);
					$("#navigation .subnavigation .mainbackground .middle").css("width", object.width()+12);
					
					$("#navigation .subnavigation").show();
					
					// check height of submenu items
					$("#navigation .subnavigation .navigation li").each(function() {
						$(this).css("height", $(this).height());
					});
					$("#navigation .subnavigation .navigation li").each(function() {
						if(parseInt($(this).css("height"))>23) {
							$("a", $(this)).addClass("big");
						}
					});
				}
			}
		});
	},
	hoverBreadcrumb: function(object) {
		$.ajax({
			url: '/helper/submenu',
			data: 'url='+object.attr("href").substr(0, object.attr("href").lastIndexOf('/'))+'&original='+object.attr("href"),
			type: 'GET',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				$("#navigation .subnavigation .navigation").html(html);
				// calculate position
				if(html.length>0) {
					var positionLeft= parseInt(object.attr("rel"))+parseInt($("#navigation .breadcrumb").css("left"))-object.width()-12-20;
					if(positionLeft+222>934) {
						$("#navigation .subnavigation .mainbackground").css("margin-left", ((positionLeft+222)-934));
						positionLeft=712;
					} else {
						$("#navigation .subnavigation .mainbackground").css("margin-left", 0);
					}
					// because the sub is not outside wie have to reposition the highlight
					if(parseInt($("#navigation .subnavigation .mainbackground").css("margin-left"))==0) {
						$("#navigation .subnavigation .navigation_begin").css("background-image", "url(/_img/background/subnavigation_borders_left.png)");
					} else if(parseInt($("#navigation .subnavigation .mainbackground").css("margin-left"))+parseInt(object.width()+22)==222) {
						$("#navigation .subnavigation .navigation_begin").css("background-image", "url(/_img/background/subnavigation_borders_right.png)");
					} else {
						$("#navigation .subnavigation .navigation_begin").css("background-image", "url(/_img/background/subnavigation_borders.png)");
					}
					$("#navigation .subnavigation").css("left", positionLeft+8).css("top", 100);
					$("#navigation .subnavigation .mainbackground .middle").css("width", object.width()-2);
					$("#navigation .breadcrumb").css("z-index", 100);
					
					$("#navigation .subnavigation").show();
					
					// check height of submenu items
					$("#navigation .subnavigation .navigation li").each(function() {
						$(this).css("height", $(this).height());
					});
					$("#navigation .subnavigation .navigation li").each(function() {
						if(parseInt($(this).css("height"))>23) {
							$("a", $(this)).addClass("big");
						}
					});
				}
			}
		});
	}
}

Search= {
	active: false,
	
	init: function() {
		$("#search").hover(function() {
			$(this).get(0).focus();
		});
		$("#search").keyup(function() {
			Search.go();
		}).click(function() {
			Search.go();
		});
		$("#searchresults .close").click(function() {
			Search.hideResults();
		});
	},
	go: function() {
		if($("#search").val().length>=3) {
			$.ajax({
				url: '/helper/search',
				data: 'term='+$("#search").val(),
				type: 'GET',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					$("#searchresults .list").html(html);
					Search.showResults();
				}
			});
		} else {
			Search.hideResults();
		}
	},
	showResults: function() {
		if(!Search.active) {
			$("#searchresults").css("top", '-'+$("#searchresults").height()+35);
			$("#searchresults").animate({
				top: "35px"
			}, 500, 'swing', function() {
				Search.active= true;
			});
		}
	},
	hideResults: function() {
		if(Search.active) {
			postop= ($("#searchresults").height()+35)*-1;
			$("#searchresults").animate({
				top: postop
			}, 500, 'swing', function() {
				Search.active= false;
			});
		}
	}
}

Bigbox= {
	current: 0,
	max: 0,
	autorun: true,
	eternity: null,
	
	init: function() {
		Bigbox.max= $(".bigbox").size()-1;
		
		if(Bigbox.max==0) {
			return;
		}
		
		$("#start .bigboxcounter li").click(function() {
			Bigbox.stop(parseInt($(this).html())-1);
		});
		
		if(Bigbox.autorun) {
			Bigbox.eternity= window.setTimeout(Bigbox.toggle, 5000);
		}
	},
	toggle: function() {
		Bigbox.current++;
		if(Bigbox.current>Bigbox.max) {
			Bigbox.current= 0;
		}

		Bigbox.change();
		
		if(Bigbox.autorun) {
			window.setTimeout(Bigbox.toggle, 5000);
		}
	},
	change: function() {
		$(".bigbox").hide();
		if($.browser.name=='msie') {
			$("#bigbox"+Bigbox.current).show();
		} else {
			$("#bigbox"+Bigbox.current).fadeIn("slow");
		}
		
		$("#start .bigboxcounter li").removeClass("active");
		$("#start .bigboxcounter li").each(function() {
			if($(this).html()==(Bigbox.current+1)) {
				$(this).addClass("active");
			}
		});
	},
	stop: function(id) {
		window.clearTimeout(Bigbox.eternity);
		Bigbox.current= id;
		Bigbox.autorun= false;
		
		Bigbox.change();
	}
}

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;
	},
	validateForm: function(id) {
		var result= true;
		$("#"+id + " input, #"+id + " textarea").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			  // trim input field
			  $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
				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;
	}
}

Login= {
	init: function() {
		$("#partnerlogin").css("top", ((Page.sizes[3]-$("#partnerlogin").height())/2)).css("left", ((Page.sizes[2]-$("#partnerlogin").width())/2));
		$("#partnerlogin .close").click(function() {
			Page.closeAll();
		});
	},
	show: function() {
		$("#glass").show();
		$("#partnerlogin").fadeIn("fast");
	}
}