// JavaScript Document

//COOKIE SCRIPTS
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for	(var i=0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

//SCRIPTS VOOR SCHERMGROOTTE EN RESIZING
window.onresize = function () {
	getscreensize();
}

function getscreensize() {
	
	if (window.innerWidth) {
		document.getElementById("availWidth").innerHTML = window.innerWidth;
		myWidth = window.innerWidth - 64;
	} else if (document.documentElement.clientWidth) {
		document.getElementById("availWidth").innerHTML = document.documentElement.clientWidth;
		myWidth = document.documentElement.clientWidth - 64;
	} else {
		myWidth = 800;
	}
	
	if (window.innerHeight) {
		document.getElementById("availHeight").innerHTML = window.innerHeight;
		myHeight = window.innerHeight - 64;
	} else if (document.documentElement.clientHeight) {
		document.getElementById("availHeight").innerHTML = document.documentElement.clientHeight;
		myHeight = document.documentElement.clientHeight - 64;
	} else {
		myHeight = 528;
	}
	
	if (myWidth > 960) {
		myWidth = 960; //de breedte hoeft echt niet meer te zijn dan 960 pix
	}
	
	if ($('#canvas').length) {
		//instellingen voor alle vervolgpagina's
		$("#wrapper").height("" + (myHeight - 24) + "px");
		$("#canvas").height("" + (myHeight - 152) + "px");
		$("#submenu").height("" + (myHeight - 152) + "px");
		$("#top").height("" + (myHeight - 120) + "px");
		$("#wrapper_background").height("" + (myHeight - 24) + "px");
		
		$('#canvas').css('opacity', 0);
		
		$('#canvas').animate({
			opacity: 1.00
		}, 2000, function() {
			// Animation complete.
				
			$("#content").height("" + (myHeight - 168) + "px");
			$('#content').jScrollPane();//scrollpane eraan hangen
			
			//de hoogte van de afbeelding in de linker balk
			$("#image").height("" + ($("#canvas").height() - $("#submenu ul").height() - $("#paging").height() - 144) + "px");
			
			//de afbeeldingh wordt getoond
			$("#image").show();
			
			//tweets inladen
			loadTweeds();
			
			//youtube iframe inladen
			loadYouTube();
		});
	} else if ($('#canvas_iqasso').length) {
		//instellingen voor de iqasso pagina
		$("#wrapper").height("680px");
		$("#submenu").height("528px");
		//$("#top").height("" + (myHeight - 120) + "px");
		$("#wrapper_background").height("680px");
		
	} else {
		//instellingen voor alleen de homepage
		$("#wrapper").css("min-height", "656px");
		$("#wrapper_background").css("height", "656px");
		
		//tweets inladen
		loadTweeds();
		
		//youtube iframe inladen
		loadYouTube();
	}
	
	createCookie('screenWidth', myWidth, 1);
	createCookie('screenHeight', myHeight, 1);
}

function slideSwitch() {
    var active = $('#wrapper_background div.active');

    if (active.length == 0 ) {
		active = $('#wrapper_background div:last');
	}

    var next =  active.next().length ? active.next() : $('#wrapper_background div:first');

    active.addClass('last-active');

    next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
		active.removeClass('active last-active');
	});
}

function slideBanner() {
	
	var x_max = -1 * ($("#banner ul").width() - 210); // de maximale verschuiving naar links
	var position = $("ul.slides").position();
	
	if (position.left <= x_max) {
		//de strip is in zijn uitrst linkse positie
		$("ul.slides").animate({
			left: '0'
		}, 200, function() {
			// Animation complete.
		});
	
	} else {
		//de strip schuidt één positie op
		$("ul.slides").animate({
			left: '-=210'
		}, 200, function() {
			// Animation complete.
		});
	}
}

//validatie van het nieuwsbrief formulier
function check_contactformulier() {
	
	$("#naam").css("border", "1px solid #ccc");
	$("#telefoonnummer").css("border", "1px solid #ccc");
	$("#email").css("border", "1px solid #ccc");
	$("#bericht").css("border", "1px solid #ccc");
	
	$("#naam_alert").html('');
	$("#telefoonnummer_alert").html('');
	$("#email_alert").html('');
	$("#bericht_alert").html('');
	
	var i = 0;
	
	if (($("#naam").val() == '') || ($("#naam").val().length < 2)) {
		//naam checken
		$("#naam_alert").html('Vult u a.u.b. een correcte naam in van tenminste twee karakters.');
		$("#naam").css("border", "1px solid #900");
		
		i++;
	}
	if (($("#dossiernummer").val() == '') || ($("#dossiernummer").val().length < 2)) {
		//dossiernummer checken
		$("#dossiernummer_alert").html('Vult u a.u.b. een correct dossiernummer in van tenminste twee karakters.');
		$("#dossiernummer").css("border", "1px solid #900");
		
		i++;
	}
	if (($("#telefoonnummer").val() == '') || ($("#telefoonnummer").val().length < 10)) {
		//telefoonnummer checken
		$("#telefoonnummer_alert").html('Vult u a.u.b. een correct telefoonnummer in van tenminste tien cijfers.');
		$("#telefoonnummer").css("border", "1px solid #900");
		
		i++;
	}
	if (($("#email").val() == '') || ($("#email").val().indexOf ('@', 0) == -1) || ($("#email").val().indexOf ('.', 0) == -1) || ($("#email").val().indexOf ( '@', 0 ) < 1))  {
		//email adres checken
		$("#email_alert").html('<strong>Vult u a.u.b. een correct email adres in.</strong>');
		$("#email").css("border", "1px solid #900");
		
		i++;
	}
	if (($("#bericht").val() == '') || ($("#bericht").val().length < 5)) {
		//bericht checken
		$("#bericht_alert").html('Laat a.u.b. een bericht achter.');
		$("#bericht").css("border", "1px solid #900");
		
		i++;
	}
	if (i < 1) {
		document.contactformulier.submit();
	}//alle benodige velden zij correct ingevuld; het formulier wordt verzonden
}

//validatie van het niewusbrief aanmeld formulier
function check_aanmelding_nieuwsbrief() {
	
	$("#bedrijfsnaam").css("border", "1px solid #ccc");
	$("#email").css("border", "1px solid #ccc");
	
	$("#bedrijfsnaam_alert").html('');
	$("#email_alert").html('');
	
	var i = 0;
	
	if (($("#bedrijfsnaam").val() == '') || ($("#bedrijfsnaam").val().length < 2)) {
		//naam checken
		$("#bedrijfsnaam_alert").html('Vult u a.u.b. een correcte bedrijfsnaam in van tenminste twee karakters.');
		$("#bedrijfsnaam").css("border", "1px solid #900");
		
		i++;
	}
	
	if (($("#email").val() == '') || ($("#email").val().indexOf ('@', 0) == -1) || ($("#email").val().indexOf ('.', 0) == -1) || ($("#email").val().indexOf ( '@', 0 ) < 1))  {
		//email adres checken
		$("#email_alert").html('<strong>Vult u a.u.b. een correct email adres in.</strong>');
		$("#email").css("border", "1px solid #900");
		
		i++;
	}
	
	if (i < 1) {
		document.aanmelding_nieuwsbrief.submit();
	}//alle benodige velden zij correct ingevuld; het formulier wordt verzonden
}

//LinkedIn discussion
function displayNetworkUpdates() {
	
	var thisHTML = "";
	var i = 0;
	
	IN.API.Raw("/groups/3605985/posts:(site-group-post-url,title,summary,creator:(first-name,last-name,picture-url),attachment:(content-url))?order=popularity") // Read (GET) the status
	.result( function(result) {
		for (var update in result.values) {
			var thisupdate = result.values[update];
			
			if (i < 4) {
				thisHTML += "<li>";
				thisHTML += thisupdate.title;
				//thisHTML += "<br />Gestart door: " + creator[last-name] + "";
				//thisHTML += "<p>" + thisupdate.creation-timestamp + "</p>";
				thisHTML += "</li>";
			}
			
			i++;
		}
		
		if (i > 0) {
			$("#linkedin ul").html(thisHTML);
		} else {
			$("#linkedin").append("<p>Naar de <a href='http://www.linkedin.com/groups?home=&gid=3841023&trk=anet_ug_hm&goback=.gdr_1314696497413_1' target='_blank'>Alles over Incasso</a> discussion group</p>");
		}
		
	})
	.error(  function(error)  { 
		$("#linkedin").append("<p>Error - no connection to LinkedIn server</p>");
	});
	
}

function loadTweeds () {
	$("#twitter").append('<ul id="twitter_update_list"></ul>');
	$("#twitter").append('<script type="text/javascript" src="http://bosveld.domini.nl/js/twitter.js"></script>');
	$("#twitter").append('<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/Bosveld_Incasso.json?callback=twitterCallback2&count=10"></script>');
}

function loadLinkedIn () {
	$("#linkedin").append('<div style="position: relative; float: left; width: 184px; height: 100%; display: block; padding: 0; background-color: #ddd;">');
	$("#linkedin").append('<ul>');
	$("#linkedin").append('</ul>');
	$("#linkedin").append('<script type="IN/Login" data-onAuth="displayNetworkUpdates"></script>');
	$("#linkedin").append('</div>');
}

function loadYouTube () {
	$("#youtube_movie").html('<iframe width="853" height="480" src="http://www.youtube.com/embed/-59_COr4JAc?rel=0" frameborder="0" allowfullscreen></iframe>');
}

$(document).ready(function() {
	
	//paginering voor het submenu
	$('#paging ul li').click(function() {
		
		var i = $(this).text();
		
		$("#submenu ul").hide();//alle uls worden eerst verborgen
		$("#paging li").css('background-color', '');//alle vakjes worden grijs
		
		$("#column_" + i + "").show();//corresponderende ul wordt zichtbaar
		
		$("#paging li:contains('" + i + "')").css('background-color', '#009ee0'); //gekozen vakje in paginering wordt blauw
		$("#paging ul").show();//paginering wordt zichtbaar
		
	});
	
	//slideshow op de homepage
	$(function() {
		if ($("#wrapper_background img").size() > 1) {
			//alleen als er meer dan één afbeelding in de slideshow zijn wordt deze daadwerkelijk opgestart
			setInterval( "slideSwitch()", 6000 );
		}
	});
	
	$("#banner ul").css("width", "" + ($("#banner li").length * 210) + ""); //de lengte van de ul instellen
	
	//banner op de homepage
	$(function() {
		if ($("#banner li").length > 1) {
			//alleen als er meer dan één afbeelding in de slideshow zijn wordt deze daadwerkelijk opgestart
			setInterval( "slideBanner()", 4000 );
		}
	});
	
	getscreensize();
	
});


