
/*
/**
 * Check for global iucn namespace
 */
if(!$chk(wd)) {
	var wd = {}; 
}

wd.logoHeader = function () {
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="464" height="124" id="flashLogo" align="middle">\n');
    document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
    document.write('<param name="movie" value="/images/logo.swf" />\n');
    document.write('<param name="quality" value="high" />\n');
    document.write('<param name="bgcolor" value="#ffffff" />\n');
    document.write('<embed src="/images/logo.swf" quality="high" bgcolor="#ffffff" width="464" height="124" name="logo" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\n');
    document.write('</object>\n');
}

wd.updateEducationLevel = function(educationType) {
	//	Set the display
	switch(educationType){
		case 2:
			//	Voorgezet Onderwijs:
			$('educationlevel').setStyle('display', 'block');
			break;
		default:
			$('educationlevel').setStyle('display', 'none');
			break;
	}
}

wd.changePasswordStatus = function(checkbox) {
	if (checkbox.checked == true) {
		$('passwords').setStyle('display', 'block');
		if ($('password_error')) {
			$('password_error').setStyle('display', 'block');
		}
	} else {
		$('passwords').setStyle('display', 'none');
		if ($('password_error')) {
			$('password_error').setStyle('display', 'none');
		}
	}
}

/*wd.checkDelete = function(project_id) {
	//	Aantal projecten ophalen
	var postBody = {
		action: 'WDGetProjectCount',
		project_id: project_id
	}
	
	var getRequest = new Json.Remote('/sbeos/ajax/JsonCall.php', {onComplete:wd.checkDeleteWithCount.bindAsEventListener(wd)});
	getRequest.send(postBody);
}

wd.checkDeleteWithCount = function(responseData) {
    if(responseData.success === true){
		if (responseData.ProjectCount == 1){
			alert('U kunt niet alle projecten bij een organisatie verwijderen. Als u het hele profiel van uw organisatie wilt verwijderen, neem dan contact op met onze webmaster (info@werelddocent.nl).');
		} else {
			if (confirm('Weet u zeker dat u dit project helemaal wilt verwijderen?')) {
				wd.deleteProject(responseData.ProjectId);
			}
		}
	}
}*/
wd.checkDelete = function(project_id) {
	if (confirm('Weet u zeker dat u dit project helemaal wilt verwijderen?')) {
		wd.deleteProject(project_id);
	}
}

wd.deleteProject = function(project_id) {
	var postBody = {
		action: 'WDDeleteProject',
		project_id: project_id
	}
	
	var postObject = JSON.encode({'action': 'WDDeleteProject', 'project_id': project_id});
	
	var getRequest = new Request.JSON({
			url: '/sbeos/ajax/JsonCall.php', 
			data: {json: postObject},
			onComplete: wd.deleteProjectResult.bindWithEvent(wd)
	}).send();
}

wd.deleteProjectResult = function(responseData) {
    if(responseData.success === true){
    	id = 'projectblock' + responseData.ProjectId;
    	$(id).dispose();
    }
}

wd.removeImage = function(JsonObject) {
 	//$('imageContainer').empty();
 	$('imageContainer').set('html', '');
}

wd.removeFile = function(element) {
	if (confirm("Weet u zeker dat u het bestand wilt verwijderen?")) {
	 	element.set('html', '');
 	
 		wd.checkFileInputs();
 	}
}

wd.getFilecount = function(inputs) {
	var filecount = 0;
	inputs.each(function(element) {
		if (element.type !== 'text' ) {
			filecount++;
		}
	});
	return filecount;
}

wd.addMaterialInput = function() {
	//	Elementen maken
	var inputTextField = new Element('input');
	inputTextField.setProperty('name', 'material_title[]');
	inputTextField.setProperty('type', 'text');
	inputTextField.setProperty('style', "height: 16px;");

	var inputFileField = new Element('input');
	inputFileField.setProperty('name', 'material[]');
	inputFileField.setProperty('type', 'file');
	inputFileField.setProperty('onchange', "wd.checkFileInputs();");
	//inputFileField.setProperty('style', "height: 20px;");
	inputFileField.setProperty('class', "material-input");
	
	
	
	//	Element toevoegen
	container = $('materials');
	
	container.adopt(new Element('br'));
	container.appendText('Titel: ');
	container.adopt(inputTextField);
	container.appendText('\nBestand: ');
	container.adopt(inputFileField);
}

wd.showMaterialInput = function() {
	var inputs = $$('#materials input');
 	if (wd.getFilecount(inputs) < 5) {
 		wd.addMaterialInput();
 	}
}

wd.checkFileInputs = function() {
	var inputs = $$('#materials input');
	var filecount = wd.getFilecount(inputs);

	if (filecount < 5) {
		//	Checken of alle bestaande inputs wel gevuld zijn.
		var empty = false;
		
		inputs.each(function(element) {
			if (element.type !== 'text' && element.get('value') == '') {
				empty = true;
			}
		});

		if (empty == false) {
			wd.addMaterialInput();
		}
	}
}


wd.updateResults = function(){
	trefwoord = $('trefwoord').value;
	instelling = $('instelling').value;
	onderwijstype = $('onderwijstype').value;
	thema = $('thema').value;
	course = $('course').value;

	provincie = $('provincie').value;
	niveau = $('niveau').value;
	plaats = $('plaats').value;
	
	var postObject = JSON.encode({
		'action': 'WDGetFilterCount', 
		'trefwoord':trefwoord,
		'instelling':instelling,
		'onderwijstype':onderwijstype,
		'course':course,
		'thema':thema,
		'provincie':provincie,
		'niveau':niveau,
		'plaats':plaats
	});
	
	var getRequest = new Request.JSON({
			url: '/sbeos/ajax/JsonCall.php', 
			data: {json: postObject},
			onComplete: wd.getFilterResults.bindWithEvent(window)
	}).send();
}

wd.getFilterResults = function(JsonObject){
	responseData = JsonObject;

    if(responseData.success === true){
		$$('.resultcounter').each(function(item,index){
			item.innerHTML = responseData.ProjectCount;
		});
	}
	
}

wd.GetEducationLevels = function(){
	onderwijstype = $('onderwijstype').value;

	var postObject = JSON.encode({'action':'WDGetEducationLevels','onderwijstype':onderwijstype});
	
	var request = new Request.JSON({
			url: '../sbeos/ajax/JsonCall.php',
			data: {json: postObject},
			onSuccess: wd.updateEducationLevels.bindWithEvent(window)
	}).send();

}

wd.updateEducationLevels = function(JsonObject){
	responseData = JsonObject;
	niveauDropDown = $('niveau');
	niveauDropDown.innerHTML = '';
	if(responseData.educationLevels.length > 0){
		option = new Element('option')
		option.value = '';
		option.innerHTML = 'Niveau';
		option.injectInside(niveauDropDown);
		
		responseData.educationLevels.each(function(item,index){
			option = new Element('option')
			option.value = item.id;
			option.id = 'level'+item.id;
			option.innerHTML = item.level_name;
			option.injectInside(niveauDropDown);
		});
		
		niveauDropDown.removeProperty('disabled'); 
	} else {
		option = new Element('option')
		option.value = '';
		option.innerHTML = 'n.v.t.';
		option.injectInside(niveauDropDown);
		niveauDropDown.setProperty('disabled', 'disabled');
	}
	if(displayItem('niveau') != ''){
		selectedLevel = displayItem('niveau');
		$('level'+selectedLevel).selected = true;
	}
}

wd.goToUrl = function(url){
	document.location.href = url;
}
wd.setMenuRoot = function(nodeId){
	if ($('menuRoot'+nodeId)) {
		menuRoot = $('menuRoot'+nodeId);
		menuRoot.setStyle('color','#804592');
	}
}



function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
//	this.getValue = function(s) {
//	this.get('value') = function(s) {
	this.getSelected = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
			}
			return false;
		}
		this.getParameters = function() {
			var a = new Array(this.getLength());
			for(var j=0; j < this.keyValuePairs.length; j++) {
				a[j] = this.keyValuePairs[j].split("=")[0];
			}
			return a;
		}
		this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString(key){
		var page = new PageQuery(window.location.search); 
		return unescape(page.getSelected(key)); 
}

function displayItem(key){
		if(queryString(key)=='false') 
		{
			return ('');
		}else{
			return (queryString(key));
		}
}

window.addEvent('domready', function() {
	if($chk($('filter'))){
		wd.GetEducationLevels();
		wd.updateResults();
	}
	$('ncdologo').addEvent('mouseover', function() {
		$('ncdologo').setProperty('src', '/images/ncdo-o.gif'); 
		
	
	});
	$('ncdologo').addEvent('mouseout', function() {
		$('ncdologo').setProperty('src', '/images/ncdo.gif');
	});
});

var SlidingFolder = new Class({
	initialize: function(container, button){
		this.slider = new Fx.Slide(container);
		this.button = $(button);
		this.button.addEvent('click', this.toggle.bind(this));
	},
	
	toggle: function(){
		this.slider.toggle();
	}
});

wd.saveSelection = function(contentIndex, nodeId){	
	var value = '';	
	var selectedCheckBox = $('chbox_'+contentIndex);
	
	if(selectedCheckBox != null) {
		if(selectedCheckBox.checked) {		
			value = 'open';
		}
		
		var postObject = JSON.encode({
			'action':'WDSaveDefaultFileSelection',
			'NodeId':nodeId,
			'ContentIndex':contentIndex,
			'Value':value
		 });
		
		
		var request = new Request.JSON({
			url: '../sbeos/ajax/JsonCall.php',
			data: {json: postObject},
			onSuccess: wd.updateSelection.bindWithEvent(window)
		}).send();		
	}
	else {
		return;
	}
}

wd.updateSelection =  function(JsonObject){
	//returnvalue = Json.evaluate(JsonObject);	
	returnvalue = JsonObject;
        if(returnvalue.success == true) {
        	switch(returnvalue.ContentIndex){
        		case '1':
		        	slider1.toggle();
		        	break;
		        case '2':
		        	slider2.toggle();
		        	break;
		        case '3':
		        	slider3.toggle();
		        	break;
		        case '4':
		        	slider4.toggle();
		        	break;
		        case '5':
		        	slider5.toggle();
		        	break;
		        case '6':
		        	slider6.toggle();
		        	break;
	        	}        	
        }else {
            alert('An error occured!');
        }
}

wd.setPosition = function(){
	
	var listNodes = $$('#mainList .mainblock');
	for(var i = 0; i < positionsArray.length; ++i)
	{		
		//Disable first 'Move Up' button and last 'Move Down' button		
		if(i == 0){
			 var btnUp = $('btnUp_' + positionsArray[i].toString());
			 if(btnUp != null){
				 btnUp.style.cursor = 'default';
				 btnUp.style.opacity = 0.5;
			 }
		}
		else{
			var btnUp = $('btnUp_' + positionsArray[i].toString());
			if(btnUp != null){
				btnUp.style.cursor = 'pointer';
				btnUp.style.opacity = 1;
			}
		}
		
		if(i == 5){			
			var btnDown = $('btnDown_' + positionsArray[i].toString());
			if(btnDown != null){
				btnDown.style.cursor = 'default';
				btnDown.style.opacity = 0.5;
			}
		}
		else {
			var btnDown = $('btnDown_' + positionsArray[i].toString());
			if(btnDown != null){
				btnDown.style.cursor = 'pointer';
				btnDown.style.opacity = 1;
			}
		}
		
		//Set position of the item
		var id = 'mainBlock' + positionsArray[i].toString();		
		var itemIndex = listNodes.indexOf(($(id)));
		var listNode = listNodes[(itemIndex)];
		listNode.inject($('mainList'));
	}
}


//command - Up, Down
//position - position of the item,that we are going to move
//itemIndex - current item index of the item that we are going to move(is used if the position is empty)
//this function is using array positionsArray, that is initialized in the dossiers_list.tpl
wd.savePosition = function(command, position,  itemIndex, nodeId){
	
	if(position == ""){		
		position = itemIndex;
    }

	var indexInArray = position - 1;
	
	var oldPosition = indexInArray;
	var newPosition = 0;	
	
	if(command == "Down"){
		//It's not possible to move item down if it's a last item
		if(oldPosition >= 5){
			return;
		}	
		else {
			newPosition = oldPosition + 1;			
		}
	}
	else if(command == "Up"){
		//It's not possible to move item up if it's a first item
		if(oldPosition > 0){
			newPosition = oldPosition - 1;
		}
		else {
			return;
		}
	}
	
	var newIndex = parseInt(newPosition) + 1;
	var oldIndex = parseInt(oldPosition) + 1;	
	
	//Update onclick-event method parameters
	var oldDownEvent = "wd.savePosition('Down'," + oldIndex.toString()+ "," + "\"\"" + "," + nodeId + ")";
	var newDownEvent = "wd.savePosition('Down'," + newIndex.toString() + ","+ "\"\"" + "," + nodeId + ")";
	var oldUpEvent = "wd.savePosition('Up'," + oldIndex.toString() + "," + "\"\"" + "," + nodeId + ")";
	var newUpEvent = "wd.savePosition('Up'," + newIndex.toString() + "," + "\"\"" + "," + nodeId + ")";
	
	var downOldBtn = $('btnDown_'+ positionsArray[oldPosition].toString());
	downOldBtn.attributes["onclick"].value = newDownEvent;
	var downNewBtn = $('btnDown_'+ positionsArray[newPosition].toString());
	downNewBtn.attributes["onclick"].value = oldDownEvent;
	
	var upOldBtn   = $('btnUp_'+ positionsArray[oldPosition].toString());	
	upOldBtn.attributes["onclick"].value = newUpEvent;
	var upNewBtn   = $('btnUp_'+ positionsArray[newPosition].toString());
	upNewBtn.attributes["onclick"].value = oldUpEvent;
	
	//Store value of the item that will be replaced with new value after ordering
	var tempReplacedValue = '';
	tempReplacedValue = positionsArray[newPosition];
	
	positionsArray[newPosition] = positionsArray[oldPosition];
	positionsArray[oldPosition] = tempReplacedValue;
	
	var postObject = JSON.encode({
		'action':'WDSaveFilePositioning',
		'NodeId':nodeId,												
		'PositionOldValue':oldIndex,
		'PositionOldItem' :positionsArray[oldPosition],
		'PositionNewValue':newIndex,
		'PositionsNewItem':positionsArray[newPosition]
	 });
	
	
	var request = new Request.JSON({
		url: '../sbeos/ajax/JsonCall.php',
		data: {json: postObject},
		onSuccess: wd.updatePositioning.bindWithEvent(window)
	}).send();
}

wd.updatePositioning = function(JsonObject)
{
	returnvalue = JsonObject;
        
        if(returnvalue.success == true){
        	wd.setPosition();
        }
        else{
            alert('An error occured!');
        }
}
