/*
  La Bellone
  Copyright (C) 2009 by Systemantics, Bureau for Informatics

  Lutz Issler
  Mauerstr. 10-12
  52064 Aachen
  GERMANY

  Web:    www.systemantics.net
  Email:  mail@systemantics.net

  Permission granted to use the files associated with this
  website only on your webserver.

  Changes to these files are PROHIBITED due to license restrictions.
*/



var flashvars = {
	autostart: "false",
	repeat: "true"
};
var params = {
	wmode: "opaque"
};

$(function() {
	var ua = navigator.userAgent.toLowerCase();
	var isTouchDevice = ua.search("iphone")>=0 || ua.search("ipod")>=0 || ua.search("ipad")>=0 || ua.search("android")>=0;

	// Convert lists to menus
	$(".menu ul").each(function() {
		$(this)
			.addClass("expanded")
			.after(
				$('<ul class="collapsed"></ul>')
					.append(
						$("li.selected", this).length>0
							? (
								$("#search", $("li.selected", this)).length>0
									? $('<li></li>').append($("input", $("li.selected", this)).val())
									: $('<li></li>').append($("li.selected a", this).html())
							)
							: $('<li></li>').append($(this).siblings("h1").text()
					)
				)
			);
	});

	// Make menus work
	$("#head,.menu div")
		.hover(
			function () {
				$(this).addClass("hover");
			},
			function () {
				$(this).removeClass("hover");
			}
		)
		.bind(isTouchDevice ? "touchstart" : "click", function (e) {
			$(this).toggleClass("open");
			var a = $(e.target).closest("a");
			if (a.length == 1) {
				location.href = a.attr("href");
			}
		});

	// Layout text in columns
	$("#content .text, #content .details")
		.columnize({
			columns: Math.min(Math.ceil($("#content .text, #content .details").height()/200), 3),
			dontsplit: "li,p,script,form",
			balance: true
		});

	// Home button hovers
	$(".homebutton").hover(
		function() {
			this.src = this.src.replace("_off", "_on");
		},
		function() {
			this.src = this.src.replace("_on", "_off");
		}
	);

	// Make agenda window functional
	$("#agenda .page:first").addClass("visible");
	if ($("#agenda .page").length>1) {
		$("#agenda h1").css("float", "left");
		$("#agenda .change").bind(isTouchDevice ? "touchstart" : "click", function() {
			$("#agenda .page").toggleClass("visible");
			$("#agenda .-shadow-shadow").css(
				"height",
				$("#agenda .-shadow-content").outerHeight()
			);
		});
	}

	// Add agenda eye candy
	$("#agenda")
		.prepend('<div class="handle"></div>')
		.wrapInner('<div class="-shadow-content"></div>')
		.prepend('<div class="-shadow-shadow"></div>')
		.children(".-shadow-shadow")
			.css({
				height: $("#agenda .-shadow-content").outerHeight(),
				width: $("#agenda .-shadow-content").outerWidth()
			});

	// Make agenda window draggable
	$("#agenda").draggable({
		handle: ".handle",
		stop: function(e, ui) {
			var expires = new Date();
			expires.setTime(expires.getTime() + 365 * 24 * 60 * 60 * 1000);
			document.cookie = "agenda="+ui.absolutePosition.top+","+ui.absolutePosition.left+","+($("#agenda .visible").is(":first-child") ? 0 : 1) +"; path=/; expires="+expires.toGMTString();
		}
	});
	cookies = document.cookie.split(/;/);
	for (var i=0; i<cookies.length; i++) {
		var cookie = $.trim(cookies[i]).split(/=/);
		if (cookie.length>0 && cookie[0]=="agenda") {
			var pos = cookie[1].split(/,/);
			if (pos.length==3) {
				$("#agenda").css({
					left: pos[1]+"px",
					top: pos[0]+"px"
				});
				if (pos[2]==1) {
					$("#agenda .change").triggerHandler("click");
				}
			}
		}
	}

	// Create hover effect on images
	$("img.gray_image")
		.each(function() {
			var preloadedImg = new Image();
			preloadedImg.src = this.src.replace('/sg_', '/s_');
		})
		.hover(
			function() {
				this.src = this.src.replace('/sg_', '/s_');
			},
			function() {
				this.src = this.src.replace('/s_', '/sg_');
			}
		);

	// Add rules to headlines
	//$(".text h2").after('<div class="rule delimiter"><hr /></div>');

	// Make search menu functional
	$("#search-q")
		.focus(function() {
			this.select();
			$(this).parents("ul")
				.css("display", "block")
				.siblings("ul").css("display", "none");
			$(this).siblings("label").hide();
		})
		.blur(function() {
			$(this).parents("ul")
				.css("display", "")
				.siblings("ul").css("display", "");
			$(this).change();
		})
		.change(function() {
			if ($(this).val()) {
				$(this).siblings("label").hide();
			} else {
				$(this).siblings("label").show();
			}
		});
	$("#search-q").change();
	$("#search").parents("ul").andSelf().css("float", "left");

	// Enhance multi-selectors
	$("select[multiple]").asmSelect({
		removeLabel: 'supprimer'
	});

	// Open images on click
	$("#content .image img:not(.cv)").bind(isTouchDevice ? "touchstart" : "click", function() {
		var img = new Image();
		var offset = $(this).offset();
		if (offset.left==466) {
			offset.left = 245;
		} else if (offset.left==687) {
			offset.left = 245;
		}
		offset.left += 6;
		offset.top += 6;
		$('<div class="enlarged"></div>')
			.append($(img))
			.css({
				left: offset.left,
				top: offset.top
			})
			.bind(isTouchDevice ? "touchstart" : "click", function() {
				$(this).remove();
			})
			.appendTo("body");
		img.src = this.src.replace("/s_", "/");
	});

	$("#newsletter-form input[type=text],#cse-search-box input[type=text]")
		.each(function() {
			$(this).data("initial-value", $(this).attr("value"));
		})
		.focus(function() {
			if ($(this).val()==$(this).data("initial-value")) {
				$(this).val("");
			}
		})
		.blur(function() {
			if ($.trim($(this).val())=="") {
				$(this).val($(this).data("initial-value"));
			}
		});
	$("#newsletter-form").validate();
	$("#cse-search-box").attr("target", "_blank");

	// Make external links open in a new window
	$("a[href^=http://], .downloads a").bind(isTouchDevice ? "touchstart" : "click", function() {
		window.open(this.href);
		this.blur();
		return false;
	});
});

