// JavaScript Document


var TourSelectMenu = Class.create({
		
	initialize : function ( element ) {
			
			var defaults = { name : 'interests'
						   , selected : []
						   , options : null
						   , button : null
						   , buttonText : 'I have more interests' };
			
			this.element = $(element);
			
			if ( !this.element ) return;
			
			this.options = Object.extend( defaults, arguments[1] || {} );
			
			if ( !this.options.options || this.options.options.length <= 0 )
			{
				alert('Can not create select menus without defining the initial options.');
				return;
			}
			
			var bttn = $(this.options.button);
			
			if ( bttn && (bttn.tagName == 'BUTTON' || (bttn.tagName == 'INPUT' && bttn.type == 'button')) )
				bttn.observe( 'click',function(){ this.addSelectBox(); }.bind(this) );
			else {
				var tmp = new Element('button',{type:'button',className:'addButton'}).update(this.options.buttonText);
					tmp.setStyle({marginTop:'8px'});
					tmp.observe('click',function(){ this.addSelectBox(); }.bind(this) );
					this.element.appendChild( tmp );
			}
			
			if ( this.options.selected && 0 < this.options.selected.length )
				for( var i=0; i<this.options.selected.length; i++ )
					this.addSelectBox( this.options.selected[i] );
			else
				this.addSelectBox();
		},
	
	addSelectBox : function ( arr ) {
			
			var tmp = new Element('div',{className:'tsm_div'}).setStyle({padding:'4px',background:'#EFEFEF',border:'1px solid #CCC',marginBottom:'8px'});
			
			var rm = this.element.select('.tsm_div');
			if ( rm && 1 <= rm.length )
			{
				rm = new Element('span').update('&times;');
				rm.setStyle({color:'#F00',fontWeight:'bold',cursor:'pointer',marginRight:'12px'});
				rm.observe( 'click', this.removeSelectBox.bind(this) );
				tmp.appendChild( rm );
			}
			
			var bttn = this.element.select('.addButton')[0];
			if ( bttn )
				this.element.insertBefore( tmp, bttn );
			else
				this.element.appendChild( tmp );
			
			//this.element.appendChild( tmp );
			
			if ( arr && 0 < arr.length )
				this.initAddSelectMenu( arr );
			else
				this.initAddSelectMenu();
		},
	
	removeSelectBox : function ( event ) {
			var ele = event.element();
			
			var fields = ele.parentNode.select('select');
			if ( !fields || fields.length<=0 ) return;
			
			var rx1 = new RegExp('^'+this.options.name+'\\[(\\d+)\\]');
			var rx2 = new RegExp('^'+this.options.name+'\\[(\\d+)\\]\\[(\\d+)\\]$');
			var index=1;
			
			for( var i=0; i<fields.length; i++ )
				if ( fields[i].name.match( rx1 ) )
				{
					var index = fields[i].name.replace( rx2, '$1' );
					break;
				}
			
			ele.parentNode.parentNode.removeChild( ele.parentNode );
			
			fields = this.element.select('select');
			if ( !fields || fields.length<=0 ) return;
			
			for( var i=0; i<fields.length; i++ )
			{
				if ( fields[i].name.match( rx1 ) &&
					 index < parseInt(fields[i].name.replace(rx2,'$1')) )
				{
					fields[i].name = fields[i].name.replace( rx2, this.options.name+'['+
																	 (parseInt(fields[i].name.replace(rx2,'$1'))-1)
																	 +'][$2]' );
				}
			}
			
		},
	
	initAddSelectMenu : function ( arr, index, ele ) {
			
			index = (!index||index<0) ? 0 : index;
			
			var tmp, p;
			
			tmp = this.addSelectMenu( ele );
			
			if ( !tmp ) return;
			
			if ( Object.isArray(arr) && arr[index] )
			{
				var regexp = new RegExp( arr[index].replace(/[^a-zA-Z0-9]+/g,'.') );
				
				for( var i=0; i<tmp.options.length; i++ )
					if ( tmp.options[i].value.toString().match( regexp ) )
					{
						tmp.options[i].selected = true;
						break;
					}
			}
			
			if ( ele )
			{
				p = ele.parentNode;
				p.appendChild( new Element('br') );
			} else {
				p = this.element.select('div.tsm_div');
				p = p[p.length-1];
			}
			
			p.appendChild( tmp );
			
			if ( Object.isArray(arr) && index < arr.length-1 )
				this.initAddSelectMenu( arr, (index+1), tmp );
			
		},
	
	addSelectMenu : function ( ele ) {
			
			var tmp, index, p, opts, name, sel;
			
			if ( ele )
			{
				if ( !ele.value || ele.value.match(/Itinerary/) ) return null;
				
				name = parseInt(ele.name.replace(/^.+\[(\d+)\]$/,'$1'))+1;
				name = ele.name.replace(/\[(\d+)\]$/,'['+name+']');
				
				new Ajax.Request( '/js/tourselectmenu/menucontents.php'
								 ,{method:'post'
								  ,parameters:'?q='+ele.value
								  ,asynchronous:false
								  ,onComplete: function ( oReq, oJsn ) {
									  opts = oJsn || eval( oReq.getHeader('X-JSON') );
									  
								  	}} );
				
			} else {
				
				p = this.element.select('div.tsm_div');
				index = p.length-1;
				p = p[index];
				tmp = p.select('select');
				
				if ( !tmp || tmp.length <= 0 )
					name = this.options.name+'['+index+'][0]';
				
				opts = this.options.options;
			}
			
			if ( !opts ) return null;
			
			sel = new Element('select',{name:name});
			
			sel.appendChild( new Element('option',{value:''}).update('- - - -') );
			
			for( var x in opts )
				sel.appendChild( new Element('option',{value:x}).update( opts[x] ) );
			
			sel.observe( 'change', this.onSelect.bind(this) );
			
			return sel;
			
		},
	
	onSelect : function ( event ) {
			
			var ele = event.element();
			if ( !ele ) return;
			
			var p = ele.parentNode;
			
			var ei = parseInt(ele.name.replace(/^.+\[(\d+)\]$/,'$1'));
			var ci, s;
			
			var c = p.select('select');
			
			if ( c && 0<c.length )
				for( var i=c.length-1; 0<=i; i-- )
				{
					ci = parseInt(c[i].name.replace(/^.+\[(\d+)\]$/,'$1'));
					if ( ei < ci )
					{
						s = c[i].previousSiblings()[0];
						if ( s.tagName == 'BR' )
							p.removeChild( s );
						p.removeChild( c[i] );
					}
				}
			
			var tmp = this.addSelectMenu( ele );
			if ( tmp )
			{
				p.appendChild( new Element('br') );
				p.appendChild( tmp );
			}
		},
	
	
	
	
	
	addSelectOptions : function ( val, title, sel ) {
			var tmp = new Element('div',{className:'tsm_id_'}).update(title);
			
		},
	
	clearSelectOptions : function ( div ) {
			var c = div.select('div.tsm','input');
			
			if ( c && 0<=c.length )
				for( var i=c.length-1; 0<=i; i-- )
					div.removeChild( c[i] );
			
		},
	
	addInput : function ( name, id ) {
			
			return new Element( 'input', {type:'hidden',name:name+'[]',value:id} );
			
		}


});
