var aPf = new Array();
var pfWidth = 0;
var offset = 0;
$(document).ready(function(){
	$('#nextPfItem img').attr('src', '/images/pixel.gif');
	// round them boxes!
	DD_roundies.addRule('.rounded, .bphoto', '10px', true);

	// add some margins
	if($('#leftCol .rounded').length > 2){
		$('#leftCol .rounded:last').css('marginRight', '5px');
	}
	$('#blog').css('marginRight', '5px');
	

	// Resize content if possible
	resizeContent();
	$(window).resize(resizeContent);
	
	
	$('.rounded').hover(
		function(){
			var d = $(this).attr('id');
			$('#' + d + ' .jScrollPaneTrack, #' + d + ' .jScrollArrowUp, #' + d + ' .jScrollArrowDown').fadeIn(150);
		},
		function(){
			var d = $(this).attr('id');
			$('#' + d + ' .jScrollPaneTrack, #' + d + ' .jScrollArrowUp, #' + d + ' .jScrollArrowDown').fadeOut(150);
		}
	);
	
	if($('#pf').length){
		$('#pf .content a').each(function(){
			pfWidth += $(this).width()+parseInt($(this).css('marginRight'));
			aPf.push($(this).width()+parseInt($(this).css('marginRight')));
		});
	
		if(pfWidth > $('#pf').width()){
			// tripple them up
			html = $('#pf .content').html();
			$('#pf .content').append(html).append(html);
			$('#pf .content').width(pfWidth*3);
		} else {
			// remove next button
			$('#nextPfItem').remove();
		}
	}	
	
	$('#nextPfItem').hover(
		function(){
			$('#nextPfItem img').attr('src', '/images/next.png');
		},
		function(){
			$('#nextPfItem img').attr('src', '/images/pixel.gif');
		}
	);
	
	externalLinks();
	
	// after 4sec, get new disciplines
	setTimeout(function(){scrollPortfolio()}, 2000);
	setTimeout(function(){getDisciplines()}, 8000);
	
	
});

resizeContent = function(){
	var iWHeight = $(window).height() < 580 ? 580 : $(window).height();
	
	hPlus = iWHeight - ($('#main').height()+$('#bottom').height()+$('#footer').height());

	$('#main').height($('#main').height()+hPlus);
	$('#leftCol').height($('#leftCol').height()+hPlus);
	$('#rightCol').height($('#rightCol').height()+hPlus);

	$('#leftCol div').each(function(){
		var iDiv = $('#leftCol div').length > 2 ? parseInt(hPlus/2) : $('#leftCol').height()-$(this).height()-10;
		$(this).height($(this).height()+(iDiv));
	});

	if ($('#blog').length) {
		var sDiv = 'blog';
	} else if ($('#video').length) {
		var sDiv = 'video';		
	} else if ($('#disciplines').length) {
		var sDiv = 'disciplines';
	}

	if ($('#vraag').length) {
		//																				   (hoogte van vraag div + padding vraag div) - margin - padding blog div
		$('#'+sDiv).height(($('#rightCol').height() - ($('#vraag').height()+10) -5 -10 ));
	} else {
		$('#'+sDiv).height(($('#rightCol').height() - 10));
	}

	var iDiv = $('#rightCol div.rounded:not(\'#'+sDiv+'\'):not(\'#vraag\')').length;
	$('#rightCol div.rounded:not(\'#'+sDiv+'\'):not(\'#vraag\')').each(function(){
		$(this).height(iDiv == 1 ? ($('#rightCol').height() - 10) : $(this).height()+(parseInt(hPlus/2)));
	});
	
	// set height inner (scrollbar) divs, also set scrollbar
	$('div.rounded').each(function(){
		
		$(this).find('.jScrollPaneContainer').height($(this).height()-30);
		$(this).find('.sp').height($(this).height()-30).jScrollPane({showArrows:true, enableKeyboardNavigation: false, maintainPosition: false});
	
		if ($(this).find('.jScrollPaneTrack'))
		$(this).find('.jScrollPaneContainer').width('220px');
		
		$('.sp').width(210);
	
	});
	
	$('.jScrollPaneTrack, .jScrollArrowUp, .jScrollArrowDown').hide();		
}


function scrollPortfolio(){
	// check if we have a slogan div
	if($('#pf').length){
		right = parseInt($("#pf .content").css('right'));
		$("#pf .content").animate({right: pfWidth}, {
				easing:'linear', 
				duration: aPf.length*4500, 
				complete: function(){
					$("#pf .content").css('right', 0);
					scrollPortfolio();
				}
		});
	}
}

function getBlog(bId, type){
	if(parseInt(bId)){
		$.post('/inc/getblog.ajax.php', {id: bId, t: type}, function(data){
			if(data){
				$('#blog .sp').html(data);
				resizeContent();
				externalLinks();
			}
		});
	}
}

function externalLinks() {
	if (!document.getElementsByTagName) return;

	var anchors = document.getElementsByTagName('a');
	for (var j = 0; j < anchors.length; j++) {
		var alink = anchors[j];
		if (alink.getAttribute('href') && alink.getAttribute('rel') == 'external') {
			alink.target = '_blank';
		}
	}
}

popup = function(tpl){
    if ($('#bg').length == 0)
    $("#content").before('<div id="bg"></div><a href="javascript:;" class="close pnghack"></a><div id="popup"></div>');
    $('#popup').load('/inc/tpl.ajax.php', {'tpl': tpl} ,function(){
        $('#popup').css('textAlign', 'left');
        $('#bg').css('left',0).css('top',0).click(hideDetail);

        // hide all the selects

        $('#popup').css('left', ($(document).width()/2-$('#popup').width()/2) + 'px');
        $('#popup').css('top', "100px");
        $('#popup').css('opacity', 0).show();

        $('#popup').height(450);
        $('#popup .tpl').height(400);

        $('a.close').css('left',($(document).width()/2-$('#popup').width()/2+$('#popup').width()-9)).css('top','93px').click(hideDetail);

        // re-set opacity so IE works
        $('#bg').css('opacity', 0.5);
        $('#bg, a.close').fadeIn(250);
        $('#popup').fadeTo(250, 1);
        
    });    
}
popupContent = function(html){
	if ($('#bg').length == 0)
  $("#content").before('<div id="bg"></div><a href="javascript:;" class="close pnghack"></a><div id="popupC"></div>');
 	$('#popupC').css('textAlign', 'left');
  $('#bg').css('left',0).css('top',0).click(hideDetail);

  // hide all the selects
	$('#popupC').html('<div id="container"><div id="cnt">'+html+'</div></div>');
  $('#popupC').css('left', ($(document).width()/2-$('#popupC').width()/2) + 'px');
  $('#popupC').css('top', "100px");
  $('#popupC').css('opacity', 0).show();

  $('a.close').css('left',($(document).width()/2-$('#popupC').width()/2+$('#popupC').width()-9)).css('top','93px').click(hideDetail);

  // re-set opacity so IE works
  $('#bg').css('opacity', 0.5);
  $('#bg, a.close').fadeIn(250);
  $('#popupC').fadeTo(250, 1);
}
popupPf = function(id){
    if ($('#bg').length == 0)
    $("#content").before('<div id="bg"></div><a href="javascript:;" class="close pnghack"></a><div id="popupPf"></div>');
    $('#popupPf').load('/inc/pf.ajax.php', {'id': id} ,function(){
        $('#popupPf').css('textAlign', 'left');
        $('#bg').css('left',0).css('top',0).click(hideDetail);

        // hide all the selects

        $('#popupPf').css('left', ($(document).width()/2-$('#popupPf').width()/2) + 'px');
        $('#popupPf').css('top', "100px");
        $('#popupPf').css('opacity', 0).show();

        $('#popupPf').height(305);

        $('a.close').css('left',($(document).width()/2-$('#popupPf').width()/2+$('#popupPf').width()+40-9)).css('top','93px').click(hideDetail);

        // re-set opacity so IE works
        $('#bg').css('opacity', 0.5);
        $('#bg, a.close').fadeIn(250);
        $('#popupPf').fadeTo(250, 1);
        
    });    
}

hideDetail = function() {
	$('#bg, #popup, #popupPf, #popupC, #popup_vid, #popup_vid_w, a.close').fadeOut(150, function(){$(this).remove()});
}

function showVid(url, width, height, target){
	var flashvars = {};
	var params = {wmode:  'transparent'};
	swfobject.embedSWF(url, target, width, height, '9.0.0', '/emo/flash/expressInstall.swf', flashvars, params);	
};


function getDisciplines(){
	$.get('/inc/dis.ajax.php', function(data){
		$('#disciplines div').fadeOut(function(){
			$('#disciplines div').html(data);
	      $('#disciplines div').fadeIn();
		})
	});	
	setTimeout(function(){getDisciplines()}, 8000);
}

function showVidPopup(url, width, height, aspectRatio){
	if(url.length){
		if(aspectRatio == 'normal'){
			divId = 'popup_vid';
		} else {
			divId = 'popup_vid_w';
		}
		// remove inserted divs
		$('#bg').remove();
		$('#close').remove();
		$('#popup_vid').remove();
		$('#popup_vid_w').remove();
		$('#video_popup_c').remove();
		
		if ($('#bg').length == 0) {
			$("#content").before('<div id="bg"></div><a href="javascript:;" class="close pnghack"></a><div id="'+divId+'"><div id=\"video_popup_c\"></div></div>');

			// IE6 won't scale the bg correctly, overwrite values with jquery
			$('#bg').css('height', ($('#content').height()+40)+'px');
		} else {
			$('#video_popup_c').html('');
		}
		
		showVid(url, width, height, 'video_popup_c');
		
		// center popup
		$('#'+divId).css('left', ($(document).width()/2-$('#'+divId).width()/2) + 'px');
		$('#'+divId).css('top', "80px");

    $('a.close').css('left',($(document).width()/2-$('#popupPf').width()/2+$('#popupPf').width()+40-9)).css('top','93px').click(hideDetail);		
		
		$('#bg').show().fadeTo(0, 0.5);
		$('#'+divId).show();
		$('#close').show();
		
		
		$('#'+divId).css('left', ($(document).width()/2-$('#'+divId).width()/2) + 'px');
    $('#'+divId).css('top', "100px");
    $('#'+divId).css('opacity', 0).show();

    $('a.close').css('left',($(document).width()/2-$('#'+divId).width()/2+$('#'+divId).width()+40-9)).css('top','93px').click(hideDetail);

    // re-set opacity so IE works
    $('#bg').css('opacity', 0.5);
    $('#bg, a.close').fadeIn(250);
    $('#'+divId).fadeTo(250, 1);
	} 
}