$(document).ready( function(){
	window.jumpstations = new JumpStations();
});

JumpStations = function() {
	var self = this;
	this.root = $(".Groepen");
	this.duration = 100;
	this.easing = "";
	this.defaultOpened = $("#vak");
	this.waiting = true;
	
	this.open = function(item) {
		if(item != this.defaultOpened && this.waiting == true) {
			this.close(this.defaultOpened);
			this.waiting = false;
		}
		item.addClass("opened");
		var h = $(".head", item);
		var b = $(".body", item);
		b.stop();
		b.animate({
			marginTop: 0
		}, self.duration, self.easing);
		item.stop();
		item.animate({
			marginTop: -(h.height() + b.height() - 45) / 2 + "px"
		}, self.duration, self.easing);	
	}
	this.close = function(item) {
		var b = $(".body", item);
		b.stop();
		b.animate({
			marginTop: -b.height() - 20 + "px"
		}, self.duration, self.easing, function() {
			$(this).parents(".item").removeClass("opened");
		});	
		item.stop();
		item.animate({
			marginTop: 0
		}, self.duration, self.easing);	
	}
	
	$(".item", this.root).each(function() {
		var b = $(".body", $(this));
		b.css({ marginTop: -b.height() - 20 + "px" })
		$(this).hover(
			function() { self.open($(this)); },	
			function() { self.close($(this)); }
		);
		$(".head", $(this)).click(function(e) {
			if(e.target.tagName != "A") document.location = $("a", $(this)).eq(0).attr("href");
		});
	});
	
	window.setTimeout(function() { self.open(self.defaultOpened); }, 1000);
}

$(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');)
	
	
	
	/* ### CMS functions ### */	
	
	// CMS toolbar switch
	if ($('a.cms-toolbar').length == 1) {
		switch ($.cookie("toolbars")) {
			case "on" : $('body').removeClass('hide-cms'); break;
			case "off" : $('body').addClass('hide-cms'); break;
		}

		$('a.cms-toolbar').click(function() {
			$('body').toggleClass('hide-cms');
			if ($('body').attr("class").indexOf("hide-cms") != -1) {
				$.cookie("toolbars", "off");
			} else {
				$.cookie("toolbars", "on");
			}
			return false;
		});
	}
	
	// attach HTTP DELETE behaviour to anchors
	$("a.http-delete").click(function(){
		var f = document.createElement('form'); 
		f.style.display = 'none'; 
		this.parentNode.appendChild(f); 
		f.method = 'post'; 
		f.action = this.href;
		var m = document.createElement('input'); 
		m.setAttribute('type', 'hidden'); 
		m.setAttribute('name', '_method'); 
		m.setAttribute('value', 'delete'); 
		f.appendChild(m);
		f.submit();
		return false;
	});

	// attach HTTP PUT behaviour to anchors
	$("a.http-put").click(function(){
		var f = document.createElement('form'); 
		f.style.display = 'none'; 
		this.parentNode.appendChild(f); 
		f.method = 'post'; 
		f.action = this.href;
		var m = document.createElement('input'); 
		m.setAttribute('type', 'hidden'); 
		m.setAttribute('name', '_method'); 
		m.setAttribute('value', 'put'); 
		f.appendChild(m);
		f.submit();
		return false;
	});


});

// 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();
	});
}

// insert some string at the cursor position of a textarea
function insertAtcursor(textarea, myValue) {
	//IE support
	if (document.selection) {
		textarea.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		textarea.focus();
	}
	//
	//MOZILLA/NETSCAPE support
	else if (textarea.selectionStart || textarea.selectionStart == '0') {
		var startPos = textarea.selectionStart;
		var endPos = textarea.selectionEnd;
		var cursorPos = startPos + myValue.length;
		//
		textarea.value = textarea.value.substring(0, startPos)
		+ myValue
		+ textarea.value.substring(endPos, textarea.value.length);
		//
		textarea.selectionStart = cursorPos;
		textarea.selectionEnd = cursorPos;
	} else {
		textarea.value += myValue;
	}
	textarea.focus();
	return false;
}

// insert an IMG tag in a textarea
function insertImage(url, align) {
	imgtag = '<img src="'+url+'" class="'+align+'" alt="" />' + "\n\n";
	currentEditor.pasteHTML(imgtag);
	return false;
}

// insert a video tag in a textare
function insertVideo(screenshot_url, video_url, width, height) {
	html = '<div class="play"><img src="'+screenshot_url+'" class="center" alt="" />';
	html += '<a class="thickbox" title="Video" href="/videoplayer?video='+ video_url +'&width='+width+'&height='+height+'">..</a></div>' + "\n\n";
	currentEditor.pasteHTML(html);
	return false;
}

// prefill form field
prepFormField = function(idString) {
	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 thumb = $(this);
		var fullImage = $("#full-image");
		fullImage.attr("src", thumb.attr("href"));
		fullImage.parent().attr("href", thumb.attr("lb:alternatesrc"));
		$(".project-gallery li").removeClass("current");
		$(this).parent().addClass("current");
		event.preventDefault();
	});	
}

// fix Q element quotes in IE
fixQuotes = function() {
   	var innerqClass = "innerq";
   	$("q q").append("â€™").prepend("â€˜").addClass(innerqClass);
   	$("q:not(." + innerqClass + ")").append("â€").prepend("â€œ");
}
