﻿var baseAdres = "";
if( document.getElementsByTagName ) {
	var elems = document.getElementsByTagName('base');
	if( elems.length ) {
		baseAdres = elems[0].href;
	}
}

var page = 1;
var totalPages = 1;
var totalProjects = 0;

$('document').ready(function(){
	$('.mainMenu .mainMenuBg').css({'top':'0','display':'block','opacity':'0'});
	$('.mainMenu a').each(function(){
		$(this).hover(function(){
			$(this).animate({
				'margin-top':'-5px',
				'height':20,
				'padding-top':5,
				'color':'#003366'
			},{'queue':false,'duration':300});
			$(this).parent().find('.mainMenuBg').animate({
				'opacity':1,
				'top':-5
			},{'queue': false, 'duration':300});
		},function(){
			$(this).animate({
				'margin-top':'-5px',
				'height':17,
				'padding-top':8,
				'color':'#ffffff'
			},{'queue':false,'duration':300});
			$(this).parent().find('.mainMenuBg').animate({
				'opacity':0,
				'top':0
			},{'queue': false, 'duration':300});					 									 
		});							   
	});
	if($('#content-right').length > 0){
		var uid='revealis';
		var output = '';
		var url = "get_tweets.asp";
		  $.get(url,
		   function(data){
			c = 0;
			$(data).find('entry').each(function(){
					c ++;
					var r = '';
					r += format($(this));
					output += r;
				});
			
			$('#content-right').html('<div onclick="window.open(\'http://twitter.com/#search?q=%23' + uid + '\');" class="twitter-link"></div><div class="twitter-top"></div><div id="tweetsContainer"><div id="tweets">' + output + '</div></div><div id="twitter-footer"><div id="twitter-up" onclick="twitterNext();"></div><div id="twitter-down" onclick="twitterPrev();"></div></div>');
	
			$('#twitter-up, #twitter-down').hover(function(){
				$(this).animate({'opacity':'0.7'},{'queue':false,'duration':250});											
			 },function(){
				$(this).animate({'opacity':'1'},{'queue':false,'duration':250});														
			});
			externeLinks();
		});
	}
	if($('#etalageTop').length > 0){
			$('.etalageDiv').hover(function(){
				if(projectMove == false){
					$(this).animate({'opacity':'0.6'},{'queue':false,'duration':250});			
				}
			 },function(){
				if(projectMove == false){
					$(this).animate({'opacity':'1'},{'queue':false,'duration':250});		
				}
			});
			$('#project-up, #project-down').hover(function(){
				$(this).animate({'opacity':'0.7'},{'queue':false,'duration':250});											
			 },function(){
				$(this).animate({'opacity':'1'},{'queue':false,'duration':250});														
			});
			count = 0;
			$('.etalageDiv').each(function(){
				totalProjects ++;				   
			});
			totalPages = Math.ceil(totalProjects / 9);
			$('#project-pages').html('1 / ' + totalPages);
	}
});

var projectMove = false;
function projectNext(){
	if(projectMove == false && page < totalPages){
		projectMove = true;	
		var count = $('.page' + page).length - 1;
		$('.page' + page).each(function(nr){
			setTimeout (
				function(){
					$('.page' + page + '.pos' + (nr + 1)).animate({'opacity':'0'},{'queue':false,'duration':250,'complete':function(){$(this).css('display','none')}});
				}
			,Math.floor(nr / 3) * 150);					   
		});
		$('.page' + (page + 1)).css({'display':'block','opacity':0});
		var countNew = $('.page' + (page + 1)).length - 1;
		$('.page' + page).each(function(nr){
			setTimeout (
				function(){
					$('.page' + (page + 1) + '.pos' + (nr + 1)).animate({'opacity':'1'},{'queue':false,'duration':250});
					if(nr == countNew){
						page ++;
						projectMove = false;
						$('#project-pages').html(page + ' / ' + totalPages);						
					}
				}
			,(Math.floor(count / 3) * 150) + (Math.floor(nr / 3) * 150)) + 150;
		});
	}
}
function projectPrev(){
	if(projectMove == false && page > 1){
		projectMove = true;	
		var count = $('.page' + page).length - 1;
		$('.page' + page).each(function(nr){
			setTimeout (
				function(){
					$('.page' + page + '.pos' + (nr + 1)).animate({'opacity':'0'},{'queue':false,'duration':250,'complete':function(){$(this).css('display','none')}});
				}
			,Math.floor(nr / 3) * 150);					   
		});
		$('.page' + (page - 1)).css({'display':'block','opacity':0});
		var countNew = $('.page' + (page - 1)).length - 2;
		$('.page' + (page - 1)).each(function(nr){
			setTimeout (
				function(){
					$('.page' + (page - 1) + '.pos' + (nr + 1)).animate({'opacity':'1'},{'queue':false,'duration':250});
					if(nr > countNew){
						page --;
						projectMove = false;
						$('#project-pages').html(page + ' / ' + totalPages);						
					}
				}
			,(Math.floor(count / 3) * 150) + (Math.floor(nr / 3) * 150)) + 150;
		});
	}	
}

var twitMove = false;
var curTweet = 1;
var pageTwit = 3;
function twitterNext(){
	if(twitMove == false && parseInt($('#tweets').css('top')) > - ($('#tweets').height() - $('#tweetsContainer').height())){
		twitMove = true;
		curTweet += pageTwit;
		var c = 1;
		var p = 0;
		$('.twitter').each(function(nr){
			if(c < curTweet){
				p -= $(this).height() + parseInt($(this).css('padding-top')) + parseInt($(this).css('padding-bottom'));
			}
			c ++;	
		});
		$('#tweets').animate({'top':p},{'queue':false,'duration':700,'complete':function(){twitMove = false}});	
	}
}
function twitterPrev(){
	if(twitMove == false && parseInt($('#tweets').css('top')) < 0){
		twitMove = true;
		curTweet -= pageTwit;
		var c = 1;
		var p = 0;
		$('.twitter').each(function(nr){
			if(c < curTweet){
				p -= $(this).height() + parseInt($(this).css('padding-top')) + parseInt($(this).css('padding-bottom'));
			}
			c ++;	
		});
		$('#tweets').animate({'top':p},{'queue':false,'duration':700,'complete':function(){twitMove = false}});	
	}
}
String.prototype.parseURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/ig, function(url) {
		return url.link(url);
	});
};
String.prototype.parseWWW = function() {
	return this.replace(/[^/]www\.[A-Za-z0-9-_:%&\?\/.=]+/ig, function(url) {
		var linkTo = "http://" + url.substr(1);
		return ' ' + url.substr(1).link(linkTo);
	});
};
String.prototype.parseUsername = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/ig, function(u) {
		var username = u.replace("@","")
		return u.link("http://twitter.com/"+username);
	});
};
String.prototype.parseHashtag = function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/ig, function(t) {
		var tag = t.replace("#","%23")
		return t.link("http://search.twitter.com/search?q="+tag);
	});
};

//twitter block
function format(data){

	t = data.find('title').text().replace(/\#revealis$/i,'').replace(/</ig,'&lt;').parseURL().parseUsername().parseHashtag().parseWWW();

	//<a class="tweet" href="http://twitter.com/' + data.find('user screen_name').text() + '/status/' + data.children('id').text() + '" target="_blank"></a>
	var output = '<div class="twitter">' + t + '<br />';
	var d = data.children('published').text().replace(/T|\-|\:/ig,',').replace(/\+0.00|Z/ig,"").split(',');
	output += "<span>" + prettyDate(new Date(d[0],d[1] - 1,d[2],d[3],d[4],d[5])) + " door ";
	output += '<a href="' + data.find('author').find('uri').text() + '">' + data.find('author').find('name').text().replace(/^.*\(/,'').replace(/\)$/,'') + '</a>';
	output += "</span></div>";
	return output;
}

function prettyDate(time){
	var date = new Date(time),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);

	if ( isNaN(day_diff) || day_diff < 0)
		return;
			
	return day_diff == 0 && (
			diff < 60 && "nu net" ||
			diff < 120 && "1 minuut geleden" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minuten geleden" ||
			diff < 7200 && "1 uur geleden" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " uur geleden") ||
			day_diff == 1 && "gisteren" ||
			day_diff < 7 && day_diff + " dagen geleden" ||
			day_diff < 12 && Math.ceil( day_diff / 7 ) + " week geleden" ||
			day_diff < 60 && Math.ceil( day_diff / 7 ) + " weken geleden" ||
			day_diff < 365 && Math.ceil( day_diff / 30 ) + " maanden geleden" ||
			Math.ceil( day_diff / 365 ) + " jaar geleden";
}



function openMenu(menuID,submenuID){}
//extra menu openklappen
function openExtraMenu(menuID,submenuID){}

//functie om externe links automatisch in nieuw venster te tonen
window.onload = externeLinks;
function externeLinks (){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	var a = document.getElementsByTagName("a");
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return (href.indexOf("http://")!=-1 && obj.target == "" && href.indexOf("javascript")==-1 && href.indexOf(hostname)==-1) ? true : false;
	};
	this.set = function(obj){
		obj.target = "_blank";
	};
	for (var i=0;i<a.length;i++){
		if(check(a[i])) set(a[i]);
	};
};



/*
 * jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */

(function(jQuery){

	// We override the animation for all of these color styles
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	// Color Conversion functions from highlightFade
	// By Blair Mitchelmore
	// http://jquery.offput.ca/highlightFade/

	// Parse strings looking for color tuples [255,255,255]
	function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
			return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Otherwise, we're most likely dealing with a named color
		return colors[jQuery.trim(color).toLowerCase()];
	}
	
	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);

			// Keep going until we find an element that has color, or we hit the body
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	

	var colors = {};
	
})(jQuery);

