var focusIndex = 1000;

$(document).ready(
	function(){
		// Get the contents for the different windows on the page
		
		// Funkology Window
		$.get(rootUrl+'default/index/getcontent/content/funkology', buildWindow);
		
		// CV Window
		$.get(rootUrl+'default/index/getcontent/content/cv', buildWindow);
		
		// Projectenoverzicht Window
		$.get(rootUrl+'default/index/getcontent/content/projectenoverzicht', buildWindow);
		
		// Postadres Window
		$.get(rootUrl+'default/index/getcontent/content/postadres', buildWindow);
		
		// Contact Window
		$.get(rootUrl+'default/index/getcontent/content/contact', buildWindow);
		
		// Links Window
		$.get(rootUrl+'default/index/getcontent/content/links', buildWindow);
		
		// Betalingsgegevens Window
		$.get(rootUrl+'default/index/getcontent/content/betalingsgegevens', buildWindow);
	}
);

function buildWindow(data, onTop, center){
	data = eval('('+ data +')');
	$('#menu_'+data['contentId']).css('text-decoration', 'none');
	
	if(center == true){
		viewport = getViewportSize();
		data['left'] = (viewport['width'] - data['width'])/2;
		data['top'] = (viewport['height'] - data['height'])/2;
	}
	
	$('body').append('<div class="window" id="window_'+data['contentId']+'" style="position:absolute;left:'+data['left']+'px;top:'+data['top']+'px;width:'+data['width']+'px;min-height:'+data['height']+'px;z-index:'+data['z-index']+';"><div class="windowTitle"><div style="float:left; width:90%;">'+data['title']+'</div><div style="float:left; text-align:right; width:10%; cursor:pointer;" onclick="hideWindow(\''+data['contentId']+'\');">X</div></div>'+data['text']+'</div>');
	
	if(center == true){
		$('#window_'+data['contentId']).css('position', 'fixed');	
	}
	
	if(onTop == true){
		$('#window_'+data['contentId']).css('z-index', focusIndex);
		focusIndex++;
	}
	
	$('#window_'+data['contentId']).show();
	$('#window_'+data['contentId']).draggable({ opacity: '0.9', cursor: 'move', distance: '10'});
	$('#window_'+data['contentId']).mousedown(function(){ giveFocus(this.id); });
}

function hideWindow(windowId){
	$('#menu_'+windowId).css('text-decoration', 'line-through');
	$('#window_'+windowId).hide();
	$('#window_'+windowId).remove();
}

function checkWindow(windowId){
	if(document.getElementById('window_'+windowId) == null){
		$.get(rootUrl+'default/index/getcontent/content/'+windowId, function(r){ buildWindow(r, true); });
	}
}

function giveFocus(windowId){
	$('#'+windowId).css('z-index', focusIndex);
	focusIndex++;
}

function showProject(projectId){
	if(document.getElementById('window_project'+projectId) == null){
		$.get(rootUrl+'default/index/getproject/project/'+projectId, function(r){ buildWindow(r, true, true); });
	}
}

function sendContactForm(){
	name = $('#contact_name').attr('value');
	email = $('#contact_email').attr('value');
	message = $('#contact_message').attr('value');
	
	if(name.length > 0 && email.length > 0 && message.length > 0){
		$.post(rootUrl+'default/index/sendContactform', 'name='+name+'&email='+email+'&message='+message, contactFormSent);
	}else{
		alert('Alle velden van het contact formulier dienen te zijn ingevuld.');	
	}
}

function contactFormSent(res){
	if(res == 'ok'){
		$('#contact_content').hide('slow', function(){ $('#contact_content').html('Het formulier is verzonden. Bedankt voor uw interesse, we nemen zo snel mogelijk contact met u op!');  $('#contact_content').show('slow');});
	}
}

function getViewportSize(){
	var viewportwidth;
	var viewportheight;
	var ret = Array();
 
	if(typeof window.innerWidth != 'undefined'){
		viewportwidth = window.innerWidth;
		viewportheight = window.innerHeight;
	}else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
       viewportwidth = document.documentElement.clientWidth;
       viewportheight = document.documentElement.clientHeight;
	}else{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}
	ret['width'] = viewportwidth;
	ret['height'] = viewportheight;
	
	return ret;
}