var curActiveSection = 'blog';
var curActiveSubSection = '';
var curActiveProfile = 0;
var curProject = 0;
var sectionArray = new Array('about','programme','profiles','blog','sponsors');


function scrollWin(scrollTo){

var speed = 0;
var wheight = $(window).height();
var dheight = $(document).height(); 
var speed = Math.floor(100000/(dheight / wheight * 200));

var projectImageWidth = 470;
var projectInfoWidth = 190;
var projectSmallArrowWidth = 20;


//alert( $("#"+scrollTo).offset().top);
	$('html,body').animate({
	scrollTop: $("#"+scrollTo).offset().top - 10
	}, 600);
}


function loadBlogs_init(type_id,type)
{	
	loadStop = true;
	$.post("/site/ajax/loadblogs.php?type_id="+type_id+"&type="+type+"&lan="+lan,
	
    function(data){
        if (data != "")
        {
			$("#blog_container").append(data);
			updateBlogs();
			//$("a.thickbox").removeClass("thickbox");
			tb_init('a.thickbox, area.thickbox, input.thickbox');
        }
        loadStop = false;
    });
};

function loadBlogs(type_id,type)
{	
	$("#blog_container").children().fadeOut(200,function(){

		$("#blog_container").empty();
		loadStop = true;
		
		$("#blog_container").append('<div class="blog_loader"><img src="/site/gfx/loadingAnimation.gif" alt="loading" /></div>');
		$.post("/site/ajax/loadblogs.php?type_id="+type_id+"&type="+type+"&lan="+lan,
		
	    function(data){
	        if (data != "")
	        {
	        	$("#blog_container .blog_loader").remove();
				$("#blog_container").append(data);
				$("#blog_container").children().fadeOut(0);
				updateBlogs();
				$("#blog_container").children().children().fadeIn(400);
				tb_init('a.thickbox, area.thickbox, input.thickbox');
	        }
	        loadStop = false;
	    });
   });
};



function updateBlogs()
{
	if($('#blog_content').length > 0) {

		/* add navigation and place item HTML in <div class='inside'></div> */
		 
		html = '<a href="#" class="scrollleft fl"></a>\n' + '<a href="#" class="scrollright fl"></a>';
		$("#blog .nav_arrows").empty();
		$("#blog .nav_arrows").append(html);
		html_item = $('#blog .container').html();
		item_inner_width = ($('#blog .blog_item').length * 225) - 20;
		$('#blog .container').html('<div class="inside" style="width:' + item_inner_width + 'px;overflow:hidden;">' + html_item + '</div>');

		/* deactivate navigation based on current margin */

		currentMargin = parseInt($("#blog .inside").css('margin-left'));
		boundaryLeft = 0;
		boundaryRight = 900 - (225 * ( $("#blog .blog_item").length));
		
		if( currentMargin == boundaryLeft ) {
			$("#blog .scrollleft").addClass('inact');
		} else if( currentMargin == boundaryRight ) {
			$("#blog .scrollright").addClass('inact');
		}

		/* scroll left or right ( default-direction : right ) */
		
		function innerScrollBlogs(direction) {
		
			// stop animations and define margins
			$("#blog .inside").stop(1,1);
			currentMargin = parseInt($("#blog .inside").css('margin-left'));
			newMargin = (direction == 'left') ? currentMargin + 225 : currentMargin - 225;
			// define boundaries
			boundaryLeft = 0;
			//boundaryRight = -225 * ( $("#profiles .profile_item").length - 1 );
			boundaryRight = 900 - (225 * ( $("#blog .blog_item").length));
			if( newMargin > boundaryLeft || newMargin < boundaryRight ) {
				return false;
			} else {
				// remove inact class
				$("#blog .scrollleft").removeClass('inact');
				$("#blog .scrollright").removeClass('inact');

				// add inact class if necessary
				if( newMargin == boundaryLeft ) {
					$("#blog .scrollleft").addClass('inact');
				} else if( newMargin == boundaryRight ) {
					$("#blog .scrollright").addClass('inact');
				}
				$("#blog .inside").animate({ marginLeft: newMargin }, 300 );
			}
		}		
		$("#blog .scrollleft").click(function() {
			innerScrollBlogs('left');
			return false;
		});
		$("#blog .scrollright").click(function() {
			innerScrollBlogs('right');
			return false;
		});
	}

}


function loadProfiles_init(type_id,type)
{	
	loadStop = true;
	$.post("/site/ajax/loadprofiles.php?type_id="+type_id+"&type="+type+"&lan="+lan,
	
    function(data){
        if (data != "")
        {
			$("#profile_container").append(data);
			updateProfiles();
        }
        loadStop = false;
    });
};

function loadProfiles(type_id,type)
{	
	$("#profile_container").children().fadeOut(200,function(){

		$("#profile_container").empty();
		loadStop = true;
		
		$("#profile_container").append('<div class="profile_loader"><img src="/site/gfx/loadingAnimation.gif" alt="loading" /></div>');
		$.post("/site/ajax/loadprofiles.php?type_id="+type_id+"&type="+type+"&lan="+lan,
		
	    function(data){
	        if (data != "")
	        {
	        	$("#profile_container .profile_loader").remove();
				$("#profile_container").append(data);
				$("#profile_container").children().fadeOut(0);
				updateProfiles();
				$("#profile_container").children().children().fadeIn(400);
	        }
	        loadStop = false;
	    });
   });
};



function updateProfiles()
{
	if($('#profiles_content').length > 0) {

		/* add navigation and place item HTML in <div class='inside'></div> */
		 
		html = '<a href="#" class="scrollleft fl"></a>\n' + '<a href="#" class="scrollright fl"></a>';
		$("#profiles .nav_arrows").empty();
		$("#profiles .nav_arrows").append(html);
		html_item = $('#profiles .container').html();
		item_inner_width = ($('#profiles .profile_item').length * 225) - 20;
		$('#profiles .container').html('<div class="inside" style="width:' + item_inner_width + 'px;overflow:hidden;">' + html_item + '</div>');

		/* deactivate navigation based on current margin */

		currentMargin = parseInt($("#profiles .inside").css('margin-left'));
		boundaryLeft = 0;
		boundaryRight = 900 - (225 * ( $("#profiles .profile_item").length));
		
		if( currentMargin == boundaryLeft ) {
			$("#profiles .scrollleft").addClass('inact');
		} else if( currentMargin == boundaryRight ) {
			$("#profiles .scrollright").addClass('inact');
		}

		/* scroll left or right ( default-direction : right ) */
		
		function innerScrollProfiles(direction) {
		
			// stop animations and define margins
			$("#profiles .inside").stop(1,1);
			currentMargin = parseInt($("#profiles .inside").css('margin-left'));
			newMargin = (direction == 'left') ? currentMargin + 225 : currentMargin - 225;
			// define boundaries
			boundaryLeft = 0;
			//boundaryRight = -225 * ( $("#profiles .profile_item").length - 1 );
			boundaryRight = 900 - (225 * ( $("#profiles .profile_item").length));
			if( newMargin > boundaryLeft || newMargin < boundaryRight ) {
				return false;
			} else {
				// remove inact class
				$("#profiles .scrollleft").removeClass('inact');
				$("#profiles .scrollright").removeClass('inact');

				// add inact class if necessary
				if( newMargin == boundaryLeft ) {
					$("#profiles .scrollleft").addClass('inact');
				} else if( newMargin == boundaryRight ) {
					$("#profiles .scrollright").addClass('inact');
				}
				$("#profiles .inside").animate({ marginLeft: newMargin }, 300 );
			}
		}		
		$("#profiles .scrollleft").click(function() {
			innerScrollProfiles('left');
			return false;
		});
		$("#profiles .scrollright").click(function() {
			innerScrollProfiles('right');
			return false;
		});
	}
}

function loadProfileDetail(id) {

	if(!loadStop){
		loadStop = true;
		$("#profile_detail_content").slideUp(500,function(){
	
			curActiveProfile = id;
			$("#profile_detail_content").empty();
			
			$.post("/site/ajax/loadprofiledetail.php?id="+id+"&lan="+lan,
			
		    function(data){
		        if (data != "")
		        {
					$("#profile_detail_content").append(data);
					$("#profile_detail_content").slideUp(0);
					updateProfilesDetail();
					$("#profile_detail_content").slideDown(500);
					scrollWin("profile_detail_content");
					tb_init('a.thickbox, area.thickbox, input.thickbox');
					initProjectsSelect();
		        }
		        loadStop = false;
		    });
		});
	}
}

function initProjectsSelect()
{
	$("#profile_projects").change(function () {
		var id = $("#profile_projects option:selected").val();
		scrollProjects(id);
	});
}

function scrollProjects(id){
	
	curProject = id;
	//scroll to id position
	var newMargin = id * -205;
	$("#profile_detail .inside2").animate({ marginLeft: newMargin }, 300, function(){
		// define boundaries
		boundaryLeft = 0;
		boundaryRight = -205 * ( $("#profile_detail .profiledetail_project_item").length - 1 );

		if( newMargin > boundaryLeft || newMargin < boundaryRight ) {
			return false;
		} else {

			// remove inact class
			$("#profile_detail .scrollleft2").removeClass('inact');
			$("#profile_detail .scrollright2").removeClass('inact');

			// add inact class if necessary
			if( newMargin == boundaryLeft ) {
				$("#profile_detail .scrollleft2").addClass('inact');
			} else if( newMargin == boundaryRight ) {
				$("#profile_detail .scrollright2").addClass('inact');
			}
		}
	});
}

function updateProfilesDetail(){


	/* add navigation and place item HTML in <div class='inside2'></div> */
	 
	html = '<a href="#" class="scrollleft2 fl"></a>\n' + '<a href="#" class="scrollright2 fl"></a>';
	$("#profile_detail .nav_arrows2").append(html);
	
	html_item = $('#profile_detail .container2').html();
	item_inner_width = $('#profile_detail .profiledetail_project_item').length * 205;
	$('#profile_detail .container2').html('<div class="inside2" style="width:' + item_inner_width + 'px;overflow:hidden;">' + html_item + '</div>');

	/* deactivate navigation based on current margin */

	currentMargin = parseInt($("#profile_detail .inside2").css('margin-left'));
	boundaryLeft = 0;
	boundaryRight = -205 * ( $("#profile_detail .profiledetail_project_item").length - 1 );
	
	if($('.profiledetail_project_item').length > 1)
	{
		if( currentMargin == boundaryLeft )
		{
			$("#profile_detail .scrollleft2").addClass('inact');
		}else if( currentMargin == boundaryRight )
		{
			$("#profile_detail .scrollright2").addClass('inact');
		}
	}else
	{
		$("#profile_detail .scrollright2").addClass('inact');
		$("#profile_detail .scrollleft2").addClass('inact');
	}
	
	/* scroll left or right ( default-direction : right ) */
	
	function innerScrollProjectdetail(direction)
	{	
		// stop animations and define margins
		$("#profile_detail .inside2").stop(1,1);
		currentMargin = parseInt($("#profile_detail .inside2").css('margin-left'));
		newMargin = (direction == 'left') ? currentMargin + 205 : currentMargin - 205;
		
		
		// define boundaries
		boundaryLeft = 0;
		boundaryRight = -205 * ( $("#profile_detail .profiledetail_project_item").length - 1 );

		if( newMargin > boundaryLeft || newMargin < boundaryRight ) {
			return false;
		} else {
			if(direction == 'left'){
				curProject--;
			}else{
			
				curProject++;
			}
			$("#profile_projects").val(curProject.toString());
			
			// remove inact class
			$("#profile_detail .scrollleft2").removeClass('inact');
			$("#profile_detail .scrollright2").removeClass('inact');

			// add inact class if necessary
			if( newMargin == boundaryLeft ) {
				$("#profile_detail .scrollleft2").addClass('inact');
			} else if( newMargin == boundaryRight ) {
				$("#profile_detail .scrollright2").addClass('inact');
			}

			$("#profile_detail .inside2").animate({ marginLeft: newMargin }, 300 );
		}
	}		
	$("#profile_detail .scrollleft2").click(function()
	{
		innerScrollProjectdetail('left');
		return false;
	});
	$("#profile_detail .scrollright2").click(function()
	{
		innerScrollProjectdetail('right');
		return false;
	});
}

function initMouseEnterSection(section) {
	$("#"+section+" .section_top_click").mouseenter(function()
	{	
		$("#"+section+" .section_arrow").addClass('arrow_hvr');
		$("#"+section+" .section_title").addClass(section+'_title_hvr');	
		//$("#"+section).next().animate({ marginTop:5 }, 100);
	});
}
function initMouseLeaveSection(section) {
	$("#"+section+" .section_top_click").mouseleave(function()
	{	
		$("#"+section+" .section_arrow").removeClass('arrow_hvr');
		$("#"+section+" .section_title").removeClass(section+'_title_hvr');	
		$("#"+section).next().animate({ marginTop:0 }, 100);
	});
}

function initClickSection(section) {
	$("#"+section+" .section_top_click").click(function()
	{
		if(curActiveSection != section)
		{	
			curActiveSection = section;
			
			$("#"+section+" .section_top_click").unbind("mouseenter");
			$("#"+section+" .section_top_click").unbind("mouseleave");
			showSection(section);
			$("#"+section).next().animate({ marginTop:0 }, 100);
		}
	});
}

function hideSection(section) {
	$("#"+section+" .section_nav").fadeOut(200,function(){
		$("#"+section+" .section_top_click").css("width","880px");
		if(section=="programme"){
			//show downloadlink
			$(".programme_download").css("display","none");
		}
	});
	$("#"+section+" .section_content").slideUp();
	$("#"+section+" .section_subcontent").hide();
	//curActiveSubSection = '';
	$("#"+section+" .section_arrow").removeClass('arrow_hvr');
	$("#"+section+" .section_arrow").removeClass('arrow_act');
	$("#"+section+" .section_title").removeClass(section+'_title_hvr');
	
	initMouseEnterSection(section);
	initMouseLeaveSection(section);
}

function showSection(section,sub) {
		
	$("#"+section+" .section_subcontent").hide();
	//close all other sections
	for(var i = 0; i < sectionArray.length; i++) {
		if(curActiveSection != sectionArray[i])
		{
			hideSection(sectionArray[i]);
		}
	}

	$("#"+section+" .section_nav").fadeIn(700);
	//check if section nav is available, if yes, make clickable area smaller
	if($("#"+section+" .section_nav").length > 0) {
		$("#"+section+" .section_top_click").css("width","450px");
		if(section == "programme"){
			//show downloadlink
			$(".programme_download").css("display","block");
		}
	}
	if(sub){
		$("#"+section+" .section_content").slideDown(300,function(){
		$('#'+section+' .section_subcontent').slideDown(200, function(){
		
			if(section == "about"){
				scrollWin("about_subcontent");
			}else{
				scrollWin(section);
			}

			});
		});
	}else{
		curActiveSubSection = '';
		$("#"+section+" .section_content").slideDown(400,function(){
			if(section == "about"){
				scrollWin("header");
			}else{
				scrollWin(section);
			}
		});
	}
	
	$("#"+section+" .section_arrow").addClass('arrow_act');

}

function readMore(section) {
	if(curActiveSubSection != section)
	{
		var sub = true;
		curActiveSection = section;
		curActiveSubSection = section;
		showSection(section,sub);
	
	}
}

function scrollProgramme(id){
	//scroll to id position
	var newMargin = id * -430;
	$("#programme .inside").animate({ marginLeft: newMargin }, 300, function(){
		// define boundaries
		boundaryLeft = 0;
		boundaryRight = -430 * ( $("#programme .programme_detail").length - 1 );

		if( newMargin > boundaryLeft || newMargin < boundaryRight ) {
			return false;
		} else {

			// remove inact class
			$("#programme .scrollleft").removeClass('inact');
			$("#programme .scrollright").removeClass('inact');

			// add inact class if necessary
			if( newMargin == boundaryLeft ) {
				$("#programme .scrollleft").addClass('inact');
			} else if( newMargin == boundaryRight ) {
				$("#programme .scrollright").addClass('inact');
			}
		}
	});
}

function updateProgramme(){

	if($('#programme').length > 0) {

		/* add navigation and place item HTML in <div class='inside'></div> */
		 
		html = '<a href="#" class="scrollleft fl"></a>\n' + '<a href="#" class="scrollright fl"></a>';
		$("#programme .nav_arrows").append(html);
		
		html_item = $('#programme .container').html();
		item_inner_width = $('#programme .programme_detail').length * 430;
		$('#programme .container').html('<div class="inside" style="width:' + item_inner_width + 'px;overflow:hidden;">' + html_item + '</div>');

		/* deactivate navigation based on current margin */

		currentMargin = parseInt($("#programme .inside").css('margin-left'));
		boundaryLeft = 0;
		boundaryRight = -430 * ( $("#programme .programme_detail").length - 1 );
		if( currentMargin == boundaryLeft ) {
			$("#programme .scrollleft").addClass('inact');
		} else if( currentMargin == boundaryRight ) {
			$("#programme .scrollright").addClass('inact');
		}

		/* scroll left or right ( default-direction : right ) */
		
		function innerScrollProgrammes(direction) {
			
			// stop animations and define margins
			$("#programme .inside").stop(1,1);
			currentMargin = parseInt($("#programme .inside").css('margin-left'));
			newMargin = (direction == 'left') ? currentMargin + 430 : currentMargin - 430;

			// define boundaries
			boundaryLeft = 0;
			boundaryRight = -430 * ( $("#programme .programme_detail").length - 1 );

			if( newMargin > boundaryLeft || newMargin < boundaryRight ) {
				return false;
			} else {

				// remove inact class
				$("#programme .scrollleft").removeClass('inact');
				$("#programme .scrollright").removeClass('inact');

				// add inact class if necessary
				if( newMargin == boundaryLeft ) {
					$("#programme .scrollleft").addClass('inact');
				} else if( newMargin == boundaryRight ) {
					$("#programme .scrollright").addClass('inact');
				}

				$("#programme .inside").animate({ marginLeft: newMargin }, 300 );
			}
		}		
		$("#programme .scrollleft").click(function() {
			innerScrollProgrammes('left');
			return false;
		});
		$("#programme .scrollright").click(function() {
			innerScrollProgrammes('right');
			return false;
		});
	}
}

$(document).ready(function()
{

	swfobject.embedSWF("/site/gfx/video.swf", "introvideo", "430", "320", "9.0.0");

	if(jQuery.browser.msie && parseInt(jQuery.browser.version, 10) == 6) {
	  try {
	    document.execCommand("BackgroundImageCache", false, true);
	  } catch(err) {}
	}
	
	loadProfiles_init(1,'Participants');
	loadBlogs_init(1,'all');

	$("#profile_categories").change(function () {
		var id = $("#profile_categories option:selected").val();
		var type =  $("#profile_categories option:selected").text();
		loadProfiles(id,type);
	});
	$("#blog_categories").change(function () {
		var id = $("#blog_categories option:selected").val();
		loadBlogs(id);
	});
	
	
		
	for(var i = 0; i < sectionArray.length; i++) {
		initMouseEnterSection(sectionArray[i]);
		initMouseLeaveSection(sectionArray[i]);
		initClickSection(sectionArray[i]);
	}
	
	showSection(curActiveSection);
	$("#"+curActiveSection+" .section_top_click").unbind("mouseenter");
	$("#"+curActiveSection+" .section_top_click").unbind("mouseleave");
	
	updateProgramme();	

});


















function el(id) { return document.getElementById(id) }

/* Contactform functions */

function toggleErrorLabel(label,state) {
	if(state == 'on') {
		document.getElementById(label).className = "errorlabel";
	}else{
		document.getElementById(label).className = "normallabel";
	}
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function ValidateForm(form) {

	var error = 0;
	var errorMessage = 'Je hebt dit formulier niet volledig ingevuld:\n\n';
		
	if(typeof(form.required)=='undefined') {
		return true;
	}
		
	
	var fieldnames = form.required.value.split(',');
	
	for(key in fieldnames) {
		if(el(fieldnames[key])) {
			type = el(fieldnames[key]).type
			label = el('label_'+fieldnames[key]).innerHTML;
			label = strip_tags(label);
			//label = fieldnames[key];
			if(type=='text' || type=='textarea') {
				if(el(fieldnames[key]).value == "") { 
					errorMessage += '- '+label+' is niet ingevuld\n';
					error = 1;
					toggleErrorLabel('label_'+fieldnames[key],'on');
				}else{
					toggleErrorLabel('label_'+fieldnames[key]);
				}
			} else if(type=='select-one') {
				if(el(fieldnames[key]).options[el(fieldnames[key]).selectedIndex].value=='' || el(fieldnames[key]).options[el(fieldnames[key]).selectedIndex].value=='0') {
					errorMessage += '- '+label+' is niet gekozen\n';
					error = 1;
					toggleErrorLabel('label_'+fieldnames[key],'on');
				}
			} else {
				alert(type)
			}
		}
	
	}

	if(error){
		alert(errorMessage);
		return false; 
	}else{
		return true;
	}
}

function strip_tags(str, allowed_tags) { // needed for validateform!
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Luke Godfrey
    // +      input by: Pul
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +      input by: Alex
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Marc Palau
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Eric Nagel
    // +      input by: Bobby Drake
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>');
    // *     returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
    // *     example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>');
    // *     returns 2: '<p>Kevin van Zonneveld</p>'
    // *     example 3: strip_tags("<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>", "<a>");
    // *     returns 3: '<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>'
    // *     example 4: strip_tags('1 < 5 5 > 1');
    // *     returns 4: '1 < 5 5 > 1'
 
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
 
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    };
 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }
 
    str += '';
 
    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
 
        // Save HTML tag
        html = matches[key].toString();
 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
 
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
 
            if (i != 0) { i = html.toLowerCasel().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCasel().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCasel().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
 
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
 
    return str;
}

function openSitel(URLStr)
{
    var maxx = screen.width;
    var maxy = screen.height;
    windowprops = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=100,height=100,left=100,top=100";
    window.open(URLStr+"&maxx="+maxx+"&maxy="+maxy,"",windowprops);
}

