/**
 * Document management functions
 * used by the GUI
 *
 * @version $Id: documents_functions.js 698 2008-12-22 19:47:09Z constantin $
 * @author Constantin Iosseliani
 */

// jSSdoc.panelToggle
var jSSdoc = {
	
	
	
	// toggles the panels for the manager...
	panelToggle : {
		
		// add the panel ids here...
		panelNames : ['idea_step', 'document_step', 'link_step', 'video_link_step', 'video_embed_step', 'document_tree', 'idea_tree', 'link_tree', 'video_tree', 'description_row'],

		
		// closes all the tabs 
		closeAll : function() {
			
			for (var i = 0; i < this.panelNames.length; i++) {
				$('#' + this.panelNames[i]).css('display', 'none');
			}	
				
		},	
		
		
		open : function(tab) {
			
			this.closeAll();
			$('#' + tab).css('display', 'block');
			$('#tree_cats_div').css('display', 'block');
			$('#description_row').css('display', 'block');
			
			if (tab == 'idea_step') {
				
				// hide the cats altogether
				$('#tree_cats_div').css('display', 'none');
				//$('#idea_tree').css('display', 'block');
				
				// hide the description
				$('#description_row').css('display', 'none');
					
				return;
			}
			

			if (tab == 'document_step') {
				$('#document_tree').css('display', 'block');	
				return;
			}	
			
			if (tab == 'link_step') {
				$('#link_tree').css('display', 'block');	
				return;
			}	

			if (tab == 'video_link_step') {
				$('#video_tree').css('display', 'block');	
				return;
			}
			
			if (tab == 'video_embed_step') {
				$('#video_tree').css('display', 'block');	
				return;
			}
			
		},
		
		
		init : function() {
			
			if ($('#type_idea').attr('checked')) {
				this.open('idea_step');
				return;
			} 
			
			if ($('#type_document').attr('checked')) {
				this.open('document_step');
				return;
			} 
			
			if ($('#type_link').attr('checked')) {
				this.open('link_step');
				return;
			} 
			
			if ($('#type_video_link').attr('checked')) {
				this.open('video_link_step');
				return;
			} 
			
						
			if ($('#type_video_embed').attr('checked')) {
				this.open('video_embed_step');
				return;
			} 
		}	
	},
	
	
	

	docUpload : {
		
		init : function() {
			setTimeout('jSSdoc.docUpload.monitor();', 1000);
		},
		
		
		monitor : function() {
			$.get('/webservices/monitor_uploaded_file.service.php', jSSdoc.docUpload.callback);		
		},
				
		
		manualUploadUpdate : function(state) {
			
			if (state == 1) {
				$('#pzloader').css('visibility', 'visible');
				$('#pzloader').css('left', (938/2)-195);
				$('#pzloader').css('top', (window.screen.height/2)-300);
				$('#flashul_state').val('');

			} else if (state == 2) {
				$('#pzloader').css('visibility', 'hidden');	
				jSSdoc.docUpload.init();
			}
		},
		
		
		
		callback : function(data) {
			
			try {

				if (data.length) {
					eval("var result = " + data);
				}

				
				if (typeof result.error != 'undefined' && result.error != '') {
				
					alert('Erreur: ' + result.error);
					return false;
					
				} else if (typeof result.file != 'undefined' && result.file != '') {
				
					// since we've found the file, we are going to substitute the					
					$('#user_file').val(result.file);
					
					// fill out optional fields..
					if (typeof result.size != 'undefined' && result.size != '') {
						$('#ul_file_size').html(result.size);
						$('#ul_file_size_in').val(result.size);
					}
			
					if (typeof result.name != 'undefined' && result.name != '') {
						$('#ul_file_name').html(result.name);
						$('#ul_file_name_in').val(result.name);
					}
										
					if (typeof result.mime != 'undefined' && result.mime != '') {
						$('#ul_file_mime').html(result.mime);
						$('#ul_file_mime_in').val(result.mime);
					}		
					
					return false;
				} else {
					//alert('Le téléchargement du fichier a échoué. SVP recommencez.');
				}
			}
			catch (ex) {
				alert('Erreur: ' + ex.message);
			}
			
		}
		
	},
	

	
	// handles the user type juggling                      		
	userTypes : {
		
		// only one list this time...
		left_list 	: 	null,

		
		counter : 0,
		
		// allows not to screw the submitted form... ;)
		initialhit : true,
		

		init : function() {
			
			this.left_list 	=	document.getElementById('all_user_types');
			
			var ids = new Array(
				'user_type_student',
				'user_type_parent',
				'user_type_teacher',
				'user_type_specialist'
			);

		
			for (var i=0; i < ids.length; i++) {

				if ($('#' + ids[i]).attr('checked') == true) {
					$('#' + ids[i]).click();
					$('#' + ids[i]).attr('checked', true);
					break;
				}
			}
			
			var old_values = $('#user_type').val().split(',');

			// add here link to populate the div below..
			
			$('#user_types_button').css({
				cursor	:	'pointer'
			});
			
			
			$('#user_types_button').click(function(){
				
				var value = $('#all_user_types').val();
				
				if (value.match(/^[a-zéèêêôûùîà0-9\-\s\']+$/i)) {
					
					jSSdoc.userTypes.setValue(value);
				}	
				
			});
			
			// load up stuff that is already there
			var old_values = $('#user_type').val().split(',');
			
			for (var i = 0; i < old_values.length; i++) {
	
				if (old_values[i] != '') {
					this.appendDiv(old_values[i]);
				}
			}						
		},
				
		
		
		appendDiv : function(value) {
			
			this.counter++;
			
			$('#user_types_div').append('<div><img id=inst_span_' + this.counter + ' src="/images/btn_remove_small.jpg" />&nbsp;'+ value + '</div>');

			
			$('#inst_span_' + this.counter).click(function(){
				jSSdoc.userTypes.removeValue(value);
				$(this).parent().css('display', 'none');
			});
		
			$('#inst_span_' + this.counter).css({
				cursor	: 'pointer'
			});
			
			return true;
			
		},
		
		
		
		setValue : function(value) {
			
		
			var old_values = $('#user_type').val().split(',');
			
			if (old_values.length > 3) {
				return false;	
			}
			
			for (var i = 0; i < old_values.length; i++) {

				if (old_values[i] == value) {
					return false;
				}
			}

			$('#user_type').val($('#user_type').val() + ',' + value);


			this.appendDiv(value);

			return true;
			
		},
		
		removeValue : function(value) {
			
			var old_values = $('#user_type').val().split(',');
			
			if (old_values.length == 0) {
				return false;	
			}
			
			var new_values = '';
			
			for (var i = 0; i < old_values.length; i++) {
					
				if (old_values[i] != value && old_values[i] != '') {
					new_values += ',' + old_values[i]
				}
			}			
			
			$('#user_type').val(new_values);
				
			var divs = $('#user_types_div').find('div');
			
			for (var i = 0; i < divs.lenth; i++) {
				
				if (divs[i].html.indexOf('&nbsp;' + value) > -1) {
					divs[i].css({
						display : 'none'
					});
				}
			}
			
			$('#user_types_button').css({
				display	:	''
			});
			
			return true;	
			
		},		
		
		
		add : function(select_box, user_type) {

			if (typeof(user_type) != 'Object') {
				
				var option = document.createElement('OPTION');
				option.setAttribute('VALUE', user_type);
				option.innerHTML = user_type;
				
				user_type = option;
			}

			select_box.appendChild(user_type);
			
		},
	
		
		remove : function(select_box, user_type) {
			
			if (typeof(user_type) == 'Object') {
				user_type = user_type.innerHTML;
			}
			
			
	    	nodes = select_box.childNodes;
	    				
	    	for (var i in nodes) {
	    		
	    		if (typeof nodes[i] != 'undefined' && nodes[i].nodeName == 'OPTION' && nodes[i].innerHTML == user_type) {
	    			select_box.removeChild(nodes[i]);
	    		}	
	
	    	}				
		},
		
		
		// populates the dropdown
		populate : function (user_type) {
						
			var left_list = this.left_list;
			
			// remove all children from the dropdown
			while (left_list.hasChildNodes()) {
				left_list.removeChild(left_list.firstChild);
			}
			
			// we don't want the tags to be deleted on first load past post..
			if (this.initialhit == true) {
				
				this.initialhit = false;
				
			} else {
				// remove everything from the temporary holder
				$('#user_types_div').html('');
				
				
				// remove everything from the value box..
				$('#user_type').val('');
			}

			// populate the required fields

			// handle the frenchness... :)
			var reg = new RegExp('user_type_');
			var type_to_use = user_type.id.replace(reg, '');

			if (typeof user_types[type_to_use] != 'undefined') {

				var old_values = $('#user_type').val().split(',');

				var skip = false;

				for (var i in user_types[type_to_use]) {

					skip = false;

					if (old_values.length > 0) {

						for (var j = 0; j < old_values.length; j++) {
							if (old_values[j] == i) {
								skip = true;
								break;
							}
						}
					}

					if (skip == false) {
						this.add(left_list, i);
					}
				}
			}

		}
		
	}
}