$(document).ready( function(){
	$("body").addClass("jsready");
	window.isIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	if(document.getElementById("q")) prepFormField("#q");
	if(document.getElementById("contact-pathfinder")) prepFormField("#from");
	if(document.getElementById("international-links")) initInternationalLinks();
	
	window.collapsers = new Collapsers();
	window.navigation = new Navigation();
	
	initTabs();
	initEqualBoxes();
	initProjectNavigation();
	initProjectGallery();
	initRecentProjectsList();
	initSplittedLists();
	initDisplayBoxes();

	$('a.popup-image').click(function() { 
		img = $('<img>').attr('src', $(this).attr('href'));
    img.appendTo('#canvas').displayBox();
		img.before('<a href="#">[sluiten]</a>');
		
		$('.displayBox a').click(function() { 
			$('.displayBox').remove();
		});
		return false; 
  	}); 
	
	// IE only functions 
	/*@cc_on
	fixQuotes();
	@*/
	
	if(typeof sIFR == "function") initsIFR();
	
	//dp.SyntaxHighlighter.ClipboardSwf = '/static/swf/clipboard.swf';
	//dp.SyntaxHighlighter.HighlightAll('code');)
	
	
	$('input:checkbox', 'form.pb-filter').click(function() {
		this.form.submit();
	});

});

// split lists into multiple ones
initSplittedLists = function() {
	lists = $(".splittedlist");
	lists.each(function(i) {
		var targetList = $(this);
		var columns = 2;
		var colIDs = new Array("col-one", "col-two", "col-three", "col-four", "col-five");
		for(var k = 0; k < colIDs.length; k++)
			if(targetList.attr("class").indexOf(colIDs[k]) > -1) columns = k+1;
		var targetListItems = targetList.children();
		var newListItemSize = (targetListItems.size()/columns);
		targetList.wrap('<div class="columns"></div>');
		for(var i = 0; i < columns; i++) {
			var newList = targetList.clone()
			newList.empty();
			newList.removeAttr("id");
			newList.removeClass("splittedlist");
			newList.addClass("column");
			if(newList.get(0).nodeName == "OL") newList.attr("start", Math.ceil(i*newListItemSize));
			for(var j = Math.ceil(i*newListItemSize); j <= Math.ceil((i+1)*newListItemSize-1); j++) {
				$(targetListItems[j]).clone().appendTo(newList);
			}
			newList.appendTo(targetList.parent());
		}
		targetList.parent().addClass(colIDs[columns-1]);
		targetList.parent().attr("id", targetList.attr("id"));
		targetList.remove();
	});
}

// display dialogs
initDisplayBoxes = function() {
	$("a.display").click(function(e){
		$('<img width="500" height="500">').attr("src", $(this).attr("href")).appendTo("#main-content").displayBox(); 
		e.preventDefault();
	});
}

// prefill form field
prepFormField = function(idString) {
	if($('body').hasClass('prikbord')) return;
	var from = $(idString);
	if(from.get(0).value == "") from.get(0).value = from.get(0).title;
	from.focus(
		function(event) {
			$(this).addClass("active");
			if(this.value == this.title) this.value = "";
		}
	).blur(
		function(event) {
			$(this).removeClass("active");
			if(this.value == "") this.value = this.title;
		}
	);
}

// initialize international links (contact)
initInternationalLinks = function() {
	var u = document.getElementById("international-links");
	$("h3", u).each(function() {
		var head = $(this);
		var h = head.text();
		head.text("");
		head.append('<a href="#">' + h + '</a>');
		head.click(function(event) {
			var l = $(this);
			$("li", l.parent.parent).each(function() {
				$(this).removeClass("current");
			});
			l.parent().addClass("current");	
			event.preventDefault();			
		});
	});
}

// collapsable content (profile page)
Collapsers = function() {
	$("div.collapsable").each(function() {
		var t = $(this);
		t.addClass("minimal");
		var a = document.createElement("a");
		a.href="#"; a.className = "toggle"; a.title = "Toon/verberg";
		a.innerHTML = "<span>Toon/verberg</span>"
		t.append(a);
		$(a).toggle(
			function(event) {
				$("body").addClass("tickleIE");
				event.preventDefault();	
				$(".body", t).slideDown("normal",function() {
					$("body").removeClass("tickle");
				});
				t.toggleClass("minimal");
			}
			,function(event) {	
				$("body").addClass("tickleIE");
				event.preventDefault();	
				$(".body", t).slideUp("normal", function() {
					$("body").removeClass("tickle");
					t.toggleClass("minimal");
				});			
			}
		);
	});
}

// tabs
initTabs = function() {
	$(".tabs").each(function() {
		$(".tabs-navigation a", $(this)).each(function(){
			$(this).click(function(event) {
				$("li", $(this).parent().parent()).each(function() { $(this).removeClass("current"); });
				$(".tab", $(this).parent().parent().parent()).each(function() { $(this).removeClass("current"); });
				$(this).parent().addClass("current");
				var id = $(this).attr("href");
				$(id, $(this).parent().parent().parent()).addClass("current");
				event.preventDefault();
			});
		});
	});
}

// equalize boxes in a row
initEqualBoxes = function(root) {
	var self = this;
	if(!root) root = $(document);
	this.setHeight = function(obj) {
		var h = 0;
		obj.each(function() { h = Math.max(h, $(this).height()); });
		obj.css({ minHeight: h + "px" });
		// compensate for stubborn IE6
		if(window.isIE6) {
			obj.css({ height: h + "px" });
		}
	}
	$(".equal", root).each(function() {
		self.setHeight($(this).children());
	});	
}

// main navigation
Navigation = function() {
	var self = this;
	this.duration = 300;
	
	this.open = function(item) {
		$(this).addClass("hover");
		$(".sub-navigation", item).stop().css({ display: "block" }).fadeTo(self.duration, 1);
		if(!item.hasClass("current")) {
			$("li.current").removeClass("opened");
			$("li.current .sub-navigation", item.parent()).stop().fadeTo(self.duration * 2.5, 0, function() {
				$(this).css({ display: "none" });
			});
		}
		if($(".sub-navigation", item).length > 0) item.addClass("opened");		
	}
	this.close = function(item) {
			item.removeClass("hover").removeClass("opened");
		if(!item.hasClass("current")) {
			$(".sub-navigation", item).stop().fadeTo(self.duration * 2.5, 0, function() {
				$(this).css({ display: "none" });
			});
		}
		$("li.current", item.parent()).addClass("opened");
		$("li.current .sub-navigation", item.parent()).stop().css({ display: "block" }).fadeTo(self.duration, 1);
	}
	
	$("#main-navigation>ol>li").hover(
		function() {
			self.open($(this));
		},
		function() {
			self.close($(this));
		}
	).focus(function(){
		self.open($(this));
	}).blur(function(){
		self.close($(this));
	});
}

// project navigation
initProjectNavigation = function() {
	$(".client-projects li").each(function(){
		$(this).hover(
			function() {
				$(this).not(".current").addClass("hover");
			},
			function() {
				$(this).removeClass("hover");
			}
		);
		$(this).click(function(event) {
			if(event.target.tagName != "A" && $("a",$(this)).length > 0) document.location = $("a",$(this)).attr("href");
		});
	});
}

// project gallery
initProjectGallery = function() {
	$(".project-gallery li a").click(function(event) {
		var src = null;
		var thumb = $(this);
		var fullImage = $("#full-image");
		src = thumb.attr("href");
		fullImage.attr("src", src);
		fullImage.parent().attr("href", src);
		$(".project-gallery li").removeClass("current");
		$(this).parent().addClass("current");
		event.preventDefault();
	});	
}

// recent project list
initRecentProjectsList = function() {
	$("#recent-projects li").each(function(){
		$(this).hover(
			function() {
				$(this).addClass("hover");
			},
			function() {
				$(this).removeClass("hover");
			}
		);
		$(this).click(function(event) {
			if(event.target.tagName != "A") document.location = $("a",$(this)).attr("href");
		});
	});
}

// lostbox, media viewer
LostBox = function() {
	this.init();
}
LostBox.prototype.init = function() {
	$("a.lostbox",document).each(function() {
		$(this).click(function(event) {
			window.lostbox.open(
				$(this).get(),
				$(this).attr("type"),
				$(this).attr("title")
			);
			event.preventDefault();
		});
	});
}
LostBox.prototype.open = function(url,mimetype,description) {
	this.url = url;
	this.mimetype = mimetype;
	this.description = description;
	//alert(url + " / " + mimetype);
	var body = $("body");
	body.append("<div id=\"lostbox-sheet\"></div");
	body.addClass("lostbox-active");
	var sheet = $("#lostbox-sheet");
	sheet.height($("#canvas").height());
	sheet.click(function(event) { window.lostbox.close(event); });
	body.append("<div id=\"lostbox-throbber\"></div");
	$("#lostbox-throbber").css("top",(window.scrollY+(body.height()/2)) + "px");
	this.cache;
	if(this.mimetype == "" || this.mimetype.match("image/")) {
		this.cache = new Image;
		this.cache.alt = "[afbeelding]";
		this.cache.src = this.url;
		this.cache.onload = function() { window.lostbox.render(); }
	}
}
LostBox.prototype.close = function(event) {
	$("#lostbox-sheet").remove();
	$("#lostbox-canvas").remove();
	$("body").removeClass("lostbox-active");
	event.preventDefault();
}
LostBox.prototype.render = function() {
	var body = $("body");
	body.append("<div id=\"lostbox-canvas\"></div>");
	var canvas = $("#lostbox-canvas");
	canvas.append("<h1>Afbeelding</h1>" + this.cache + "<p>" + this.description + "</p><a href=\"#\" class=\"close\" title=\"Sluit deze afbeelding\">sluiten</a>");
	$("a.close", canvas).click(function(event) { window.lostbox.close(event); });
	canvas.css({ width: this.cache.width + "px", marginLeft: - canvas.width() / 2 + "px", top: (window.scrollY + (body.height() / 2) - (canvas.height() / 2)) + "px" });
	$("#lostbox-throbber").remove();
}

// fix Q element quotes in IE
fixQuotes = function() {
   	var innerqClass = "innerq";
   	$("q q").append("’").prepend("‘").addClass(innerqClass);
   	$("q:not(." + innerqClass + ")").append("”").prepend("“");
}

// flash thickbox connection
function FlashLightBox (url, title) {
	tb_show(title, url);
}

// sifr execution
initsIFR = function() {		
	if($('body').hasClass('prikbord')) return;
	sIFR.bHideBrowserText = true;
	sIFR.replaceElement(named({
		sSelector: "h1 .sifr, h2 .sifr, h3 .sifr", 
		sFlashSrc: "/static/swf/arial.swf", 
		sColor: "#ED1C24", 
		sLinkColor: "#ED1C24", 
		sHoverColor: "#0073FF",
		sWmode: "transparent",
		nPaddingBottom:-5
	}));
	sIFR.replaceElement(named({
		sSelector: "#last-words", 
		sFlashSrc: "/static/swf/arial.swf", 
		sColor: "#768791", 
		sLinkColor: "#ED1C24", 
		sHoverColor: "#0073FF",
		sWmode: "transparent",
		sFlashVars: "textalign=center",
		nPaddingBottom:10
	}));
	sIFR.replaceElement(named({
		sSelector: ".sifr", 
		sFlashSrc: "/static/swf/arial.swf", 
		sColor: "#000000", 
		sLinkColor: "#ED1C24", 
		sHoverColor: "#0073FF",
		sWmode: "transparent"
	}));
	$("body").addClass("tickleIE");
}



