/* plugin */
jQuery.fn.dwFadingLinks = function(settings) {
	settings = jQuery.extend({
		color: '#ff8c00',
		duration: 500
	}, settings);
	return this.each(function() {
		var original = $(this).css('color');
		$(this).mouseover(function() { $(this).animate({ color: settings.color },settings.duration); });
		$(this).mouseout(function() { $(this).animate({ color: original },settings.duration); });
	});
};

$(document).ready(function(){
	var screenwidth 		= 	$(window).width();
	var screenheight 		= 	$(window).height();
	var intspeed 			= 	1650;
	var intzap				=	0;
	
	$('#CONTAINER').dwFadingLinks();

	$(".project_c").stop().animate({opacity: "0.2"}, 100);
	
	fadeInProj(0);
	
	// go
	$(".topani").each(function(){
		aniTop(this, screenwidth);
	});
	
	$(".social .content, .linkpijl").fadeTo(100, '0.8');
	
	$(".linkpijl#linkspijl").addClass("dis").fadeTo(100, '0.1');
		
	$(".project:not(.nc)").fadeTo(100, '0.8');
	
	$(".social .content").hover(function(){
		$(this).fadeTo(300, 1);
		$(this).parent().addClass("nb");
	}, function(){
		$(this).fadeTo(100, '0.8');
		$(this).parent().removeClass("nb");
	});
	
	$("#linkso, #knop_bekijkpro, #opt_vor, #opt_vol, #opt_sluit, #sluitsiteroll, #topuitklap, .iconwrap .icon").hover(function(){
		$(this).addClass("mo");
	}, function(){
		$(this).removeClass("mo");
	});

	$(".project:not(.nc)").livequery(function(){
		$(this).hover(function() { 
			$(this).fadeTo(100, '1').find('.project_c_c').fadeIn(100);
		}, function() { 
			$(this).fadeTo(100, '0.8').find('.project_c_c').fadeOut(100);
		});
	});
	
	$("#twitter p").tweet({
		username: "ewinnn",
		join_text: "auto",
		avatar_size: 0,
		count: 1,
		loading_text: ".."
	});
	
	$(".footerkol ul").each(function(){
		$(this).find('a[href^="http://"]').attr("target", "_blank");
	});
	
	// overig
	
	$('a[href*="[at]"][href*="[.]"]').each(function() {
	  var email = $(this).attr('href').split('[at]').join('@').split('[.]').join('.');
	  $(this).attr('href', 'mailto:' + email.toLowerCase());
	  if ($(this).text().length == 0) $(this).text(email);
	});
	
	// links
	
	$('#topuitklapwrap').click(function(){
		$(this).hide();
		$("#siterollwrap").slideDown(600, 'easeOutElastic');
		$("#sluitsiteroll").fadeIn(1800);
	});
	
	$('#sluitsiteroll').click(function(){
		$(this).hide();
		$("#siterollwrap").slideUp(400, 'easeOutCirc');
		$("#topuitklapwrap").fadeIn(500);
	});
	
	$('.linkpijl:not(.dis)').livequery('click', function() {
		var openlinks = parseInt($(this).attr('name'));
		if(openlinks == 0){
			$('.linkpijl#linkspijl').addClass("dis").fadeTo(100, '0.1');
		} else {
			$('.linkpijl#linkspijl').removeClass("dis").fadeTo(100, '0.5');
		}
		$("#laadlinks").fadeOut(20).load("_laadlinks.php?p="+openlinks, function(response, status, xhr) {
			if(response != '0'){
				$("#laadlinks").fadeIn(400);
				var nieuwrechts = parseInt(openlinks+20);
				$('.linkpijl#rechtspijl').attr('name', nieuwrechts);
				if(openlinks != 0){
					var nieuwlinks = parseInt(openlinks-20);
				} else {
					var nieuwlinks = 0;
				}
				$('.linkpijl#linkspijl').attr('name', nieuwlinks);
			} else {
				$('.linkpijl#linkspijl').attr('name', 0);
				$('.linkpijl#rechtspijl').attr('name', 20);
				$('.linkpijl#linkspijl').addClass("dis").fadeTo(100, '0.1');
			}
		});
	});
	
});

function aniTop(item, screenwidth){
	var randn = Math.ceil(1024*Math.random());
	var randw = Math.ceil(300*Math.random());
	var rands = Math.ceil(12000*Math.random()+9000);
	$(item).animate({
		left: randn,
		width: randw
	}, rands, function() {
		aniTop(item);
	});
}

function fadeInProj(item){
	if(item < 13){
		$(".project_c").eq(item).stop().animate({
			opacity: '0.8',
			width: '200px',
			height: '121px',
			marginTop: '0px',
			marginLeft: '0px'
		  }, {duration: 200, queue: 0, complete: function(){
			var newitem = item+1;
			fadeInProj(newitem);
		}});
	};
}


