// BRAIN FORCE NL General Javascrip (JQuerey based)
//Styling Issues
function BFNLStyling_init(){
	$("#Body").css("background-image", "url(http://www.brainforce.nl/images/bf-2009-bg-body.gif)"); 
	$('#SpecialLink').addClass('SpecialLink');
	$(".dialog").hide();
	$(':checkbox').css(
	{
		"border-bottom-style":"none"
	});
	$(':radio').css(
	{
		"border-bottom-style":"none"
	});
	$('#bfforms').css(
	{
		"background-color":"#f4f8f9"
		
	});
	$('.dialogLink').hover
		(
		function () 
					{
				      $(this).css(
					  {
					  	"background-color":"#d1dde6",
						"cursor":"pointer"
						
					  
					  });
				    },
		function () 
					{
				       $(this).css(
					    {
					  	"background-color":"#f4f8f9",
						"cursor":"default"
						
					  
					  });
				    }
		);
	$.ui.dialog.defaults.bgiframe = true;
	$.ui.dialog.defaults.autoOpen = false;
	$.ui.dialog.defaults.modal = true;

	//$("#dnn_ctr1098_ModuleContent").hide();

	$.datepicker.regional.nl = {
	            clearText: 'Wissen', clearStatus: 'Wis de huidige datum',
	            closeText: 'Sluiten', closeStatus: 'Sluit zonder verandering',
	            prevText: '←', prevStatus: 'Bekijk de vorige maand',	                
				prevBigText: '«', nextBigStatus: 'Bekijk het vorige jaar',
	            nextText: '→', nextStatus: 'Bekijk de volgende maand',	                
				nextBigText: '»', nextBigStatus: 'Bekijk het volgende jaar',
	            currentText: 'Vandaag', currentStatus: 'Bekijk de huidige maand',
                monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
                'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
                monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun',
	            'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
	            monthStatus: 'Bekijk een andere maand', yearStatus: 'Bekijk een ander jaar',
	            weekHeader: 'Wk', weekStatus: 'Week van het jaar',
	            dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
              	dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
                dayStatus: 'Stel DD in als eerste dag van de week', dateStatus: 'dd/mm/yy',
                dateFormat: 'dd/mm/yy', firstDay: 1,
				initStatus: 'Kies een datum', isRTL: false};
  	$.datepicker.setDefaults($.datepicker.regional.nl);
	//$(".datepicker-DDdMMyy").datepicker({  });
	//$(".datepicker-ddmmyy").datepicker({ beforeShowDay: $.datepicker.noWeekends });
	$(".datepicker-DDdMMyy").datepicker(
	{ 
	minDate: 0, maxDate: '+6M', showButtonPanel: true, dateFormat: 'DD, d MM, yy',beforeShowDay: $.datepicker.noWeekends  
	});
	$(".datepicker-ddmmyy").datepicker(
	{ 
	minDate: 0, maxDate: '+6M', showButtonPanel: true, dateFormat: 'dd-mm-yy' ,beforeShowDay: $.datepicker.noWeekends 
	});  
	

// Tabs
		$("#tabs").tabs({
				collapsible: true,
				select: function(event, ui) { 
				document.getElementById('filterCombo').selectedIndex = 0;
				$('ul#filterThis li').show('normal').removeClass('hidden');
				$('.filters input').attr('checked', 'checked');
				
				}
			});
		
}
//Toggles by class toggleDIV

function toggleDIV_init(){
	//hide the all of the element with class msg_body
	$("#toggleDIV_body").hide();
	//toggle the componenet with class msg_body
	$("#toggleDIV_head").click(function(){
		$(this).next("#toggleDIV_body").slideToggle(600);
	});
}

//JQUeryUI
//Accordion
function accordion_init(){

		$('.selector').accordion({ header: '#accordion_head' });
		$("#accordion").accordion(
			{
				autoHeight: false,
				collapsible: true,
				active: false,
				//icons: false,
				icons: { 'header': '', 'headerSelected': '' }
			}
		);
}


//Dialogs
function dialog_init(){
	
	// Filter
	$('#filterDialog').dialog({
                        autoOpen: false,
                        bgiframe: true,
						width: 300,
                        modal: false,
						position: 'top',
						title: 'Kies een branche of product...',
                        buttons: {
							'Sluit': function(){
								$(this).dialog('close');
							}
						}
                		}); 
	$('#filterDialog').dialog('open');
	
// References	
	$('.dialog').hide();
	var linkIndex = 0;
	var dialogIndex = 0;
	var dialogArrayList = [];
	var dialogLinkArrayList = [];
	var numberOfDialogs = $('.dialog').length;
	if (numberOfDialogs > 0) {
	
						dialogLinkArrayList = $.makeArray($('.dialogLink').get());
						$.each($('.dialog'), function(dialogIndex)
											{
												
												var thisDialog = $(this).get(0);
												//alert(thisDialog.id);
												dialogArrayList.push(thisDialog);
 												//dialogArrayList.push($(this).attr('id'));
												return (dialogIndex = numberOfDialogs); 
											});
						
						//alert(dialogArrayList.length + ' ' + dialogLinkArrayList.length + ' ' + dialogArrayList);

						$.each(dialogLinkArrayList, function(linkIndex){
						
						var thisDialogID = dialogArrayList[linkIndex].id;
						var thisDialogTitle = $('#'+thisDialogID).attr('title');
						$('#'+thisDialogID).dialog({
                        autoOpen: false,
                        bgiframe: true,
						width: 800,
                        title: thisDialogTitle,
                        modal: true,
                        buttons: {
							'Sluit': function(){
								$(this).dialog('close');
							}
						}
                		}); 
						
						
						$(this).click(function() 
						{
							
							//alert(linkIndex+' '+thisDialogID);
							$('#'+thisDialogID).css("visibility","visible");;
							$('#'+thisDialogID).dialog('open');

							
						}
						);
						//alert(linkIndex + ' ' + numberOfDialogs + ' ' + dialogArrayList);
						});

	}
	else
	{return false}	


// Tabs
		$("#tabs").tabs({
				collapsible: true
					
			});
		
}
// Another Filter
/**
 * Removes duplicates in the array 'a'
 * @author Johan Känngård, http://johankanngard.net/
 */
function unique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!contains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}
/**
 * Returns true if 's' is contained in the array 'a'
 * @author Johan Känngård, http://johankanngard.net/
 */
function contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}
function removeItems(array, item) {
	var i = 0;
	while (i < array.length) {
		if (array[i] == item) {
			array.splice(i, 1);
		} else {
			i++;
		}
	}
	return array;
}
function filterThis_init(){
	
	// everything hinges on creating a string of class names, 
	// so i'll create a variable to hold that first
	
	var stringOfClassNames = '';
	
	// grab the class name of each list item to build that string
	// 
	$('.filterThis > li').each( function (i) 
	{
		if ($(this).attr('class') != '') {
			var thisClassString = $(this).attr('class');
			stringOfClassNames = stringOfClassNames + '*' + thisClassString;
			//debug
			//alert('this classString:'+' '+thisClassString);
		}
		else
		{
			return;
		}
	});
	//debug
	//alert('strings of class names:'+' '+stringOfClassNames);
	// now i have a space-delimited string of all class names stored
	// in the stringOfClassNames variable.	
	// Trim spaces from the ends of that string:
	
	stringOfClassNames = jQuery.trim(stringOfClassNames);
	
	// i can't really do anything with it until it's an array, so
	// convert that string to an array.
	var arrayClasses = stringOfClassNames.split('*');
	//debug
	//alert('array of classes:'+' '+arrayClasses);
	

	
	// now for the isolating the filter that is common to all.
	// must do before extracting only the unique classes
	// one way to approach: count the number of times classes occur, and
	// if any occur the same number of times as how many list items i have,
	// assume that class is common to all list items, and remove it from
	// the filter list. duplicate class on same item = problem, but 
	// i'm not thinking about that right now.
	// i've also chosen sort the pre-unique'd array
	// instead of sorting the unique'd array.  i think i have to for the count.
	var arrayClasses = arrayClasses.sort();
	totalNumberOfItemsToFilter = $('.filterThis > li').length;
	// borrowed from http://stackoverflow.com/questions/348021/counting-results-in-an-array
	// for counting up items.  do i even need this?
	var result = new Object();
	for (var filterClass in arrayClasses) {
		if (result[arrayClasses[filterClass]] === undefined) {
			result[arrayClasses[filterClass]] = 1;
		} else {
			result[arrayClasses[filterClass]]++;
		}
	}
	var resultsToRemoveFromFilters = new Array();
	for (var item in result) {
		if (result[item] == totalNumberOfItemsToFilter) {
			resultsToRemoveFromFilters.push(item);
		}
	}
	// pull out only unique values from that array.  Otherwise
	// i'll end up with duplicate filter checkboxes.
	arrayUniqueClasses = (unique(arrayClasses));
	//debug
	//alert('array of unique classes:'+' '+arrayUniqueClasses);


	// and now remove classes that appear in every result from my 'unique
	// classes' array
	for (x in resultsToRemoveFromFilters) {
		arrayUniqueClasses = removeItems(arrayUniqueClasses,resultsToRemoveFromFilters[x]);
	}
	//$('.filterThis').before('<p><strong>Classes excluded from filters because they are common to all elements:<\/strong> '+resultsToRemoveFromFilters+'<\/p>');
	// we only want to create filters if there are multiple classes. check
	// length of that array to see if it worth going forward.  I need at least
	// two, so my value has to be greater than 1.
	if (arrayUniqueClasses.length > 1) {
		// it must be worth it, because everything else is
		// within the true side of this if statement.	
		// so since we're going to have some filters, 
		// lets give them a place to live
		//$('<div class="filters"><\/div>').insertBefore('.filterThis');
		// then build the filter checkboxes based on all the class names
		$('<div class="dropdownFilter"><select id="oplossingenFilter" />').appendTo('.filters');
		$('<option  value="filterAll">Toon alle referenties<\/option>').appendTo('#oplossingenFilter');	
		$.each(arrayUniqueClasses, function(){
			if (this != '') {
				// $('<div class="filter"><input class="dynamicFilterInput" type="checkbox" checked="checked" value="' + this + '" id="filterID' + this + '" /><label for="filterID' + this + '">' + this + '<\/label><\/div>').appendTo('.filters');
				$('<option  value="' + this + '">' + this + '<\/option>').appendTo('#oplossingenFilter');	
			}
			else {
				return;
			}
		});
		// lets throw in the 'show all' checkbox for giggles
		//$('<div class="filter"><input type="checkbox" checked="checked" value="filterAll" id="filterIDall" /><label for="filterIDall">Toon alle referenties<\/label><\/div>').appendTo('.filters');
		$('</select></div>').appendTo('.filters');
		// DropDown filter
		// now lets give those filters something to do
		$('#oplossingenFilter').change(function()
		{
		var dropdownFilterVal = $('#oplossingenFilter').val();
		var dropDownFilterStringValue = '.filterThis > li[class*=' + dropdownFilterVal + ']';
		var dropDownFilterStringValueOpposite = '.filterThis > li:not([class*=' + dropdownFilterVal + '])';	
		//debug
		//alert('dropdown Filter value: '+ dropdownFilterVal + ', '+$(dropDownFilterStringValue).length);
		$(window).scrollTo('#oplossingenFilter', 1000, {axis: 'y'});
		if (dropdownFilterVal == 'filterAll') 
		{
		$('.filterThis li').show('normal');
		}
		else 
		{
			$(dropDownFilterStringValue).show('normal');
			$(dropDownFilterStringValueOpposite).hide('normal');
		}
	});	
}
};

// Filter Framework 1.0
function filterCombo_init(){  
	var filterSelectCombo = $("#filterCombo");
	var filterOplossingen = $("#filterComboOplossingen");
	filterSelectCombo.change(function(){
		$(window).scrollTo('#filterCombo', 1000, {
			axis: 'y'
		});
		var filterVal = filterSelectCombo.val();
		if (filterVal == 'Alles') {
			$('ul#filterThis li.hidden').show('normal').removeClass('hidden');
			$('ul#filterThis').show('normal').removeClass('hidden');
		}
		else {
			
			$('ul#filterThis li').filter(function (filterIndex) {
                  return $(this).attr('sector') != filterVal;
                })
            .hide('normal').addClass('hidden');

			
			$('ul#filterThis li').filter(function (filterIndex) {
                 return $(this).attr('sector') == filterVal;
                }).show('normal').removeClass('hidden');
			
		}
	});	
	
		return false;
	
}
// Document Ready Function
$(document).ready(function()
						   {
						   		BFNLStyling_init();
							  	toggleDIV_init();
								accordion_init();
								filterCombo_init();
								filterThis_init();
								dialog_init();
								
						   });