function initPortfoliolist(){
	$('body').append('<div id="pitem-info"></div>');
	
	
	$('#portfoliolist .item').each( function(){
		var title = $(this).find('a img').attr('alt');
		var url = $(this).find('a').attr('href');
		$(this).append('<div class="overlay"></div>');
		$(this).find('.overlay').click( function(){
			window.location = url;
		}).hover(
			function(e){
				$('#pitem-info').html(title).show();
				$(this).stop();
				$(this).fadeTo("100",0);
			},function(){
				$('#pitem-info').html('').hide();
				$(this).stop();
				$(this).fadeTo("100",0.7);
			}
		 );
	});
	$().mousemove(function(e){
		$('#pitem-info').css({
			'left':e.pageX+10,
			'top':e.pageY+20
		})
	});
	
	$('#portfoliolist .item .overlay').fadeTo("0",0.7);	
}

function initPortfoliolist2(){
	$('#portfoliolist .item').each( function(){
		var title = $(this).find('a img').attr('alt');
		var url = $(this).find('a').attr('href');
		$(this).append('<div class="info"></div>');	
		$(this).find('.info').append('<h3>'+title+'</h3>')
			.click( function(){
				window.location = url;
			});
	});
	

	$('#portfoliolist .item').hover(
		function(){
			$(this).find('.info').stop();
			$(this).find('.info').fadeTo("300",0.9);
		},function(){
			$(this).find('.info').stop();
			$(this).find('.info').fadeTo("300",0.01);
		}
	 );
}


var toggleTimeout  = setTimeout("HomeCustomerMoveAuto()",2000);
var way = 'left';

function HomeCustomerMoveAuto(){
	if( $('#customers ul li').length <1 ){
		return false;
	}
	toggleTimeout = clearTimeout(toggleTimeout);
	var movepx = 145;
	var leng = $('#customers ul li').length;
	leng = (leng*movepx)-movepx;
	var left = $('#customers ul').css('left').replace('px','');
	//alert( left + " > " + (-1-leng) );
	if( way == 'left' ){
		if( left > (1-leng)  ){
			HomeCustomerMoveNext(leng);
		} else {
			HomeCustomerMovePrev(leng);
			way = 'right';
		}	
	} else {
		if( left >= 0 ){
			way = 'left';
			HomeCustomerMoveNext(leng);
		} else {
			HomeCustomerMovePrev(leng);
		}	
	}
}

function HomeCustomerMoveNext(leng){
	var movepx = 145;
	var left = $('#customers ul').css('left').replace('px','');
	if( left > (0-leng) ){
		$('#customers ul').animate({
			left: left-movepx+"px"	
		},500);
	}
	toggleTimeout  = setTimeout("HomeCustomerMoveAuto()",2000);
	//HomeCustomerMoveAuto();
}

function HomeCustomerMovePrev(leng){
	var movepx = 145;
	var left = $('#customers ul').css('left').replace('px','');
	left = Math.ceil(left); 
	if( left < -movepx ){
		left = left + movepx;
	} else {
		left = 0;
	}
	$('#customers ul').animate({
		left: left+"px"	
	},500);
	toggleTimeout  = setTimeout("HomeCustomerMoveAuto()",2000);
	//HomeCustomerMoveAuto();
}


function initSearchfield(){
	$('.frmSearch').val('...Zoek');	
	$('.frmSearch').focus( function(){
		if( $('.frmSearch').val() == '...Zoek' ){
			$('.frmSearch').val('');	
		}							
	});
	$('.frmSearch').blur( function(){
		if( $('.frmSearch').val() == '' ){
			$('.frmSearch').val('...Zoek');	
		}							
	});
};

function initFontsize(){
    $('#fontsize-change').click(function() {
        if (!$(this).hasClass('medium')) {
            $('body').css('font-size', '110%');
            $('#navigation li a').css('padding-bottom', '3px');
            var blockHeight = $('#read-mores .block:nth-child(1)').height();

            if ($('#read-mores .block:nth-child(2)').height() > blockHeight) {
                blockHeight = $('#read-mores .block:nth-child(2)').height();
            }
            $('#read-mores .block').css('height', blockHeight + 'px');
            $('#customer-next').css('bottom', '-21px');
            $('#customer-prev').css('bottom', '-21px');
            $('#more-news').css('right', '36px');
            $(this).addClass('medium').html('Tekst verkleinen');
        }
        else {
            $('body').css('font-size', '100%');
            $('#navigation li a').css('padding-bottom', '5px');
            $('#read-mores .block').removeAttr('style');
            $('#customer-next').removeAttr('style');
            $('#customer-prev').removeAttr('style');
            $('#more-news').removeAttr('style');
            $(this).removeClass('medium').html('Tekst vergroten');
        }
        return false;
    });
}

function initLinks(){
	$('a:not(.nonext)').each( function(){
		var href = $(this).attr('href');
		if(href){
			if( href.substring(0,7) == 'http://' ){
				$(this).addClass('external');
				$(this).after('<img src="/media/a-external.gif" alt="Pagina opent in nieuw venster"/>');
				$(this).click( function(){
					window.open(href);	
					return false;
				});
			}
		}
	});
}

function initTextSize() {
    if (hasCSS()) {
        $('#usability ul li:nth-child(1)').after('<li><a href="#" class="plus" id="fontsize-change"><span>Tekst vergroten</span></a></li>');
        initFontsize();
    }
}

function hasCSS() {
    $('body').append('<div id="cssTest"></div>')
    $('#cssTest').css({width:'1px'})
    var cssBoolean = ($('#cssTest').width() != 1) ? false : true
    $('#cssTest').remove()
    return cssBoolean;
}

$().ready( function(){	
	initPortfoliolist();
	initSearchfield();
	initLinks();
	initTextSize();
});