//alert(MooTools.version);

// Copyright 2008; this code may not be used without Read & View B.V.'s authorization.
//
// Note: depends on Mootools v1.11

var ViewSet = new Class({
    name: 'ViewSet'
});

 

ViewSet.implement({

	copyfrom: function (setIdCurrent) {
		var answer = confirm(
			'U gaat de huidige set vervangen door een kopie van de geselecteerde.\n' +
			'U verwijdert hiermee de inhoud huidige set.\n\n' +
			'Weet u het zeker?');
		
		if (answer == false) {
			return;
		}
		
		window.open(
			'/set/copyfrom/setId/' + setIdCurrent + 
			'/setIdSource/' + $('sets').options[$('sets').selectedIndex].value + 
			'/name/' + $('set_name').value + '/');
			 
	},


	submit: function () {	
		
		// delete the elements from a previous submit
		
		var old = $$('input[id$=set_fieldsetmodel]');
		old.each(
			function(item, index) {
				if (item.type == 'hidden') {
					$('formset').removeChild(item);
				}
			}
		);
		
		// create new elements to post
			
		var fieldsetmodels = $$('select[id^=fieldset_set_fieldsetmodel-]');
		fieldsetmodels.each(
			function(item, index) {
				var nr = $(item).id.substring(
								$(item).id.lastIndexOf('-') + 1, 
								$(item).id.length /* -1 */);
				
				var input = document.createElement('input');
				input.setAttribute('type', 'hidden');
				input.setAttribute('name', 'set_fieldsetmodel[' + nr + '][fields]');
				input.setAttribute('id', 'set_fieldsetmodel[' + nr + '][fields]');	
				
				var value = '';
							
				// this does not work in ie6!
				//$$($(item).options).each(
				//	function(item, index) {
				//		if (value.length > 0) {
				//			value += '_';
				//		}
				//		value += item.value;
				//	}
				//);
				
				// workaround for ie6
				var length = $(item).options.length;
				for (var i = 0; i < length; i++) {
					if (value.length > 0) {
							value += '_';
						}
						value += $(item).options[i].value;
				}
						
				input.value = value;
				$('formset').insertBefore(input, $('formset').firstChild);
			}
		);
		
   		$('method').value = 'save';
   		$('formset').send({onComplete: viewset.handleSubmitResponse});
	return false;
    },



    handleSubmitResponse: function (response) {
    	alert(response);
    	var res = Json.evaluate(response);
		
		if (res.update == 'refresh') {
			location.reload(true);
			return false;
		}
		
		$(res.update).setHTML(viewset.urlDecode(res.result));
    },



    addFieldSet: function (destination, xhtmlId, setId) {
		new Ajax('/set/ajaxaddfieldset/setId/' + setId + '/xhtmlId/' + xhtmlId, {
			method : 'post',
			onComplete: viewset.handleAddFieldSetResponse }).request();
    return false;
    },	



	handleAddFieldSetResponse: function (response) {
		// works fine in Mozilla, but not in IE7
		// the only option to fix this, is to NOT use tables.
		// IE cannot set the innerhtml of a table...
		/*
		var tbody = document.createElement('tbody');
		$('fieldsets').appendChild(tbody);
		$(tbody).empty().setHTML(response);
		*/

		var div = document.createElement('div');
		$('added').appendChild(div);
		$(div).empty().setHTML(response);

	    $$('a[id^=field_link_left-]').each( 
	    		function (el) {
	    			$(el).removeEvents();
	    			viewset.toLeft(el);
	    		});
	
	    $$('a[id^=field_link_right-]').each( 
	    		function (el) {
	    			$(el).removeEvents();
	    			viewset.toRight(el);
	    		});	

		return false;
	},



    urlDecode: function (url) {
		var exp = /\+/g;
		return String(unescape(String(url))).replace(exp, ' ');
    },



	toLeft: function(el) {
   		el.addEvent('click', function(e) {
   		    var left = $(String(el.id).replace('field_link_left-', 'fields-'));
			var right = $(String(el.id).replace('field_link_left-', 'fieldset_set_fieldsetmodel-'));
			
			if (right.selectedIndex >= 0) {
				right.removeChild(right.options[right.selectedIndex]);
				if (right.size > 1) {
					right.size -= 1;
				}
			}
			
   		    return false;
   		});
    },



	toRight: function(el) {
   		el.addEvent('click', function(e) {
   		    var left = $(String(el.id).replace('field_link_right-', 'fields-'));
   		    var right = $(String(el.id).replace('field_link_right-', 'fieldset_set_fieldsetmodel-'));
  		    		    
   		    if (left.selectedIndex > 0) {
   		    	var theId = left.id.replace('fields-', '');
   		    	//var theId = theId.substring(0, theId.length-1);	    	
   		    	var option = document.createElement('option');
   		    	
   		    	option.text = left.options[left.selectedIndex].text;
  				option.value = left.options[left.selectedIndex].value; //theId;
  					
  				    try {
    					right.add(option, right.options[right.options.length] ); // standards compliant; doesn't work in IE
  					} catch(ex) {
    					right.add(option, right.options.length); // IE only
				}
				
				if (right.options.length > 1) {
					right.size += 1;
				} 
  					
   		    }
   		    return false;
   		});
    }

});



var viewset = new ViewSet();



window.addEvent('domready', function() {
    $$('a[id^=field_link_left-]').each( 
    		function (el) {
    			viewset.toLeft(el);
    		});

    $$('a[id^=field_link_right-]').each( 
    		function (el) {
    			viewset.toRight(el);
    		});
});










