/*
 * Catch console log calls for IE
 */
function _log(msg) { if (window.console) { console.log(msg); } }

/*
 * Preload images
 */
(function() {
	var imgs = [
		"../img/layer.png",
		"../img/layer_over_bottom.png",
		"../img/layer_over_top.png",
		"../img/layer_over_center.png",
		"../img/layer-menu.png",
		"../img/bg_schatten.png",
		"../img/logobar.png",
		"../img/preloader.gif",
		"../img/bildwechsel-links.png",
		"../img/bildwechsel-links-over.png",
		"../img/bildwechsel-rechts.png",
		"../img/bildwechsel-rechts-over.png"
	];
	
	for (var i = 0, ii = imgs.length; i < ii; i++) {
		var img = new Image();
		img.src = imgs[i];
	}
})();

/*
 * Embed main flash movie
 */
var embedded = swfobject.embedSWF("../swf/main.swf", "stage", "920", "570", "8", false, {
	xmlFile: "../swf/config.xml"
}, {
	base: "../swf",
	swliveconnect: "true",
	wmode: "opaque"
}, {
	id: "myCom",
	name: "myCom"
});
if (swfobject.hasFlashPlayerVersion("6.0.0")) {
	jQuery(function($) {
		$("body").removeClass("no-flash").addClass("js");
	})
}

var printWin = null;

jQuery(function($) {
	/*
	 * Content layers
	 */
	$.layers = $.layers || {
		$layers: $(".layer"),
		current: null,
		
		init: function() {
			this.$layers.each(function(idx, elem) {
				var $elem = $(elem);
				var $title = $(".title", elem);
				
				if (!$elem.is("#recommend")) {
					$("<div/>")
						.attr("class", "print")
						.appendTo($title)
						.click(function() {
							var url = "./" + $elem.attr("id") + ".htm";
							var printWin = window.open(url, "printWin", "left=40, top=20, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, toolbar=no, width=620, height=480");
							var $body = $elem.clone();
							var $object = $("object object", $body);
							if (!$object.length) {
								$object = $("object", $body);
							}
							$object.each(function(idx, obj) {
								var $obj = $(obj);
								var $img = $("img", $obj);
								$obj.replaceWith($img);
							});
							
							var page = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
									+ '<html xmlns="http://www.w3.org/1999/xhtml">'
									+ '<head>'
									+ '<title>' + $title.text() + '</title>'
									+ '<link rel="stylesheet" type="text/css" href="../css/print.css" media="all" />'
									+ '</head>'
									+ '<body id="' + elem.id + '">' + $body.html() + '</body>'
									+ '</html>';
							printWin.document.write(page);
							printWin.document.close();
							printWin.print();
						});
				}
				
				$("<div/>")
					.attr("class", "close")
					.appendTo($title)
					.click(function() {
						$elem.removeClass("show");
						$.layers.current = null;
						$.menu.clear();
					});
			});
		},
		
		show: function(id) {
			if (id != "menu") {
				this.hideAll();
				
				if (this.current) {
					this.current.removeClass("show");
					this.current = null;
				}
				
				var $body = $("#" + id + " .body");
				if (!$body.children().length) {
					$('<div class="content"><p class="preloader">' + msgPreload + '</p></div>').appendTo($body);
					
					$body.load("./" + id + ".htm .content", null, function() {
						$(".panels", $body).panel();
						$(".steps", $body).stepper();
						
						if (id == "rasenmaeher") {
							$("#rasenmaeher").consultant();
						}
						
						if (id == "recommend") {
							$("#recommend").recommend();
						}
					});
				}
				this.current = $("#" + id).addClass("show");
			} else {
				$("#menu").addClass("show");
			}
		},
		
		hideAll: function() {
			this.$layers.removeClass("show");
			this.current = null;
		}
	};
	
	$.layers.init();
	
	/*
	 * Content popups
	 */
	$.popups = $.popups || {
		$popups: $(".popup"),
		current: null,
		
		init: function() {
			this.$popups.each(function(idx, elem) {
				var $elem = $(elem);
				var $items = $(".body li", elem);
				
				$items.click(function() {
					var cnm = this.className.split(" ")[0];
					$(this).focus();
					$.layers.show(cnm);
					$.popups.hideAll();
					$.menu.hilite(elem.id, cnm);
				}).hover(function() {
					$(this).addClass("over");
				}, function() {
					$(this).removeClass("over");
				});
			});
		},
		
		show: function(id, xmouse, ymouse) {
			this.hideAll();
			
			if ($.layers.current) return;
			
			if (this.current) {
				this.current.removeClass("show");
				this.current = null;
			}
			
			var $popup = $("#" + id);
			var _left = Math.round(xmouse);
			var _top = Math.round(ymouse - $popup.height() - 11);
			
			if (_left < 36) {
				_left = 36;
			} else if ($("#main").width() < _left + $popup.width() + 25) {
				_left -= (_left + $popup.width() - $("#main").width()) + 50;
			}
			
			$popup.css({
				left: _left + "px",
				top: _top + "px"
			});
			
			this.current = $("#" + id).addClass("show");
		},
		
		hideAll: function() {
			this.$popups.removeClass("show");
			this.current = null;
		}
	};
	
	$.popups.init();

	/*
	 * Menu layer
	 */
	$.menu = $.menu || {
		$menu: $("#menu"),
		$mainItems: null,
		$subItems: null,
		
		init: function() {
			var $menu = this.$menu;
			var $mainItems = $(".body > ul > li", $menu);
			this.$mainItems = $mainItems;
			var $subItems = $(".body li li", $menu);
			this.$subItems = $subItems;
			
			$("<div/>")
				.attr("class", "close")
				.appendTo($(".title", $menu))
				.click(function() {
					$menu.removeClass("show");
					//$.menu.clear();
				});
			
			$(".body li", $menu).mouseover(function() {
				$(this).addClass("over");
			}).mouseout(function() {
				$(this).removeClass("over");
			});
			
			$mainItems.click(function() {
				$("ul", $mainItems).removeClass("open");
				$("ul", this).toggleClass("open");

				$mainItems.removeClass("active");
				$(this).toggleClass("active");
			});
			
			$("a", $subItems).click(function(evt) {
				$subItems.removeClass("active");
				$(this).parent("li").addClass("active");
				
				$.layers.show($(this).parent().attr("class").split(" ")[0]);
				
				return $("body").hasClass("no-flash");
			});
		},
		
		hilite: function(mainItem, subItem) {
			this.clear();
			
			var $mainItem = this.$mainItems.filter("." + mainItem);
			$("ul", $mainItem).addClass("open");
			$mainItem.addClass("active");
			this.$subItems.filter("." + subItem).addClass("active");
		},
		
		clear: function() {
			$("ul", this.$mainItems).removeClass("open");
			this.$mainItems.removeClass("active")
			this.$subItems.removeClass("active");
		},
		
		goto: function(mainItem, subItem) {
			this.hilite(mainItem, subItem);
			$("a", this.$subItems.filter("." + subItem)).click();
		}
	};
	
	$.menu.init();

	/*
	 * Enter button
	 */
	$("#welcome .enter").click(function() {
		$.layers.hideAll();
	});
	
	/*
	 * Flash external interface
	 */
	$.flashExternalInterface = $.flashExternalInterface || {
		
		onMatrixLoaded: function() {
			var hash = window.location.hash;
			if (hash && hash != "") {
				var id = hash.split('#')[1];
				$.layers.hideAll();
				$.layers.show(id);
			} else {
				$.layers.show("welcome");
			}
		},
		
		onMatrixScrollButtonPress: function() {
			$.layers.hideAll();
			$.popups.hideAll();
		},
		
		onMatrixScrollButtonRelease: function() {
			$("#myCom").blur();
		},
		
		onScrollContentRollOver: function() {
			$.popups.hideAll();
		},
		
		onScrollContentPress: function() {
			$.layers.hideAll();
		},
		
		onScrollContentRelease: function() {
			//
		},
		
		onContentNaviButtonRollOver: function(id, xmouse, ymouse) {
			$.popups.show(id, xmouse, ymouse);
		},
		
		onContentNaviButtonRollOut: function(id) {
			//
		},
		
		onContentNaviButtonPress: function(id) {
			$.layers.hideAll();
		},
		
		onContentNaviButtonRelease: function(id, xmouse, ymouse) {
			$.popups.show(id, xmouse, ymouse);
		},
		
		onNaviButtonRelease: function(id) {
			$.layers.show(id);
		},
		
		setDisplay: function() {
			//
		}
	}
	
	/*
	 * Legals
	 */
	$(".impressum a").click(function() {
		$.layers.show("impressum");
		return $("body").hasClass("no-flash");
	});
});
