function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function isValidName(str) {
    return (str != "Your name:") && (str != null) && (str != "");
}

/*************************************************************
    Renders and manage Accordion
    faber.init();
*************************************************************/


var faber = 
{
	makeAccordion: function()
	{
		var accordion = new Accordion
		(
			'dt.toggle',
			'dd.element',
			{
				display: 0,
				opacity: false,
				alwaysHide: false,
				onActive: function(toggler, element)
				{
					toggler.addClass('toggle_style');
				},
				onBackground: function(toggler, element)
				{
					toggler.removeClass('toggle_style');
				}
			}
		);
	},
	animateRollover: function()
	{
		var list = $$('div#navigation dd ul li a');
		list.each
		(
			function(element)
			{
				var fx = new Fx.Styles(element, {duration:200, fps:60, wait:false});
				element.addEvent
				(
					'mouseenter',
					function()
					{
						fx.start({'padding-left':30});
					}
				);
 
				element.addEvent
				(
					'mouseleave',
					function()
					{
						fx.start({'padding-left':20});
					}
				);
			}
		);
	},
	init: function()
	{
		this.makeAccordion();
		this.animateRollover();
	}
};


var render_window =
{
	complete: function(){
		var tmp = new Element('div').setProperties({id:'tmp'}).setStyles({display:'none'}).injectInside(document.body);
		tmp.setHTML(this.content.response.text);
        var title = $('tmp_title').innerHTML
        var title_array = $('tmp_title').innerHTML.split(':');
        if(title_array.length > 1){
            title = title_array[0] + '<span class="replaced">:</span><em>' + title_array[1] + '</em>';
        }
        $('form_sendafriend_title').setHTML(title);
		var slice = $('tmp').getElement('#sendpage_form');
		$('loading').replaceWith(slice);
		tmp.remove();
        this.manageForm();
	},

	loadHTML: function(e,url){
		this.content = new Ajax(url, {
								method: 'get',
								onComplete: this.complete.bind(this),
								autoCancel: true,
								evalScripts: false
							}).request();
	},

	init: function(){
		var links = $$('.pophtml');
		links.each(function(link){
			var url = link.getProperty('href');
            var title = link.getProperty('title');
			link.addEvent('click', function(e){
                var tmp_title = new Element('span').setProperties({id:'tmp_title'}).setStyles({display:'none'}).setHTML(title).injectInside(document.body);
				new Event(e).stop();																			// overide default behaviour
				var a = new Element('div',{'id':'translucent-mask'}).setHTML('&nbsp;').injectTop($E('body'));  	// create the elements
				var b = new Element('div',{'id':'pop-window'}).setHTML('&nbsp;').injectTop($E('body'));
				var c = new Element('div',{'id':'loading'}).setHTML('<p><span class="loading replaced">Loading</span></p>').injectTop($('pop-window'));
				this.loadHTML(e,url);
				this.closeWindow();
			}.bind(this));
		}.bind(this));
	},
	
	closeWindow: function(){
		$('translucent-mask').addEvent('click', function(e){
			new Event(e).stop();
            if(confirm("Do you want to close the current form?")){
    			$('translucent-mask').remove();
    			$('pop-window').remove();
                $('tmp_title').remove();
            }
		});

	},
    
    resultForm: function(){
        var tmp = new Element('div').setProperties({id:'tmp'}).setStyles({display:'none'}).injectInside(document.body);
        tmp.setHTML(this.content.response.text);
        var title = $('tmp_title').innerHTML
        var title_array = $('tmp_title').innerHTML.split(':');
        if(title_array.length > 1){
            title = title_array[0] + '<span class="replaced">:</span><em>' + title_array[1] + '</em>';
        }
        $('form_sendafriend_title').setHTML(title);        
        var slice = tmp.getElement('#sendpage_form');
        $('pop-window').setHTML('<div id="sendpage_form">' + slice.innerHTML +'</div>');
        tmp.remove();
        this.manageForm();
        if($('form_sendafriend_close')){
            $('form_sendafriend_close').addEvent('click',function(e){
                new Event(e).stop();
                $('translucent-mask').remove();
                $('pop-window').remove();
                $('tmp_title').remove();
            });
        }

        
    },

    responseForm : function(e,url){
        this.content = new Ajax(url,{
            postBody: $('form_sendafriend_id'),
            method: 'post',
            onComplete: this.resultForm.bind(this),
            autoCancel: true,
            evalScripts: false
        }).request();
    },
    
    manageForm : function(){
        if($('form_sendafriend_id')){
            $('form_sendafriend_id').addEvent('submit',function(e){
                new Event(e).stop();
                var url = $('form_sendafriend_id').getProperty('action')
                this.responseForm(e,url)
                $('pop-window').setHTML('<p><span class="loading replaced">Loading</span></p>');
            }.bind(this));
        }
    }
    
    
	
}


/**********************************************************************
Handles the search form in the masthead
**********************************************************************/
var search_form = 
{
	init: function(){
        var field = $('search_query')
        if(field){
    		field.addEvent('click', function(e){
    			field.setProperty('value','');
    		});
    		field.addEvent('blur', function(e){
    			var query = field.getProperty('value');
    			if(!query){
    				field.setProperty('value','Search...');
    			}
    		});
        }
        $('site_search').addEvent('submit',function(e){
           new Event(e).stop();
           if(field.value != "Search..."){
            $('site_search').submit()
           }
        });
	}
	
}


/**********************************************************************
Opens links with .popup in a new window
**********************************************************************/
var poplink =
{
	init: function(){
		var pops = $$('.popup');
		pops.each(function(pop){
			pop.addEvent('click', function(e){
				new Event(e).stop();
				var url = pop.getProperty('href');
				window.open(url);
			});
		});
	}
}


/**********************************************************************
Manage Ajax Sort and Refine Page
**********************************************************************/
var sortPage = 
{
    complete: function(){
        var tmp = new Element('div').setProperties({id:'tmp'}).setStyles({display:'none'}).injectInside(document.body);
        tmp.setHTML(this.content.response.text);
        var slice = tmp.getElement('#listing');
        var listing_tmp = new Element('div').setProperties({id:'listing_tmp'});
        $('listing_container').replaceWith(listing_tmp).setHTML(slice.innerHTML).addClass('clearfix');
        //$('listing_container').setHTML('<div id="listing" class="clearfix">'+slice.innerHTML+'</div>');
        tmp.remove();
        $('listing_tmp').setProperty('id','listing');
        paginationLinks.init();
        this.init();
    },

    loadHTML : function(e,url,post_element){
    	
        this.content = new Ajax(url,{
            postBody: post_element,
            method: 'get',
            onComplete: this.complete.bind(this),
            autoCancel: true,
            evalScripts: false
        }).request();
    },
	
	

	
    init : function(){
    	
        if($('form_sort_submit')){
            $('form_sort_submit').setStyle('display','none');
            var ajaxed = new Element('input').setProperties({name:'ajaxed',type:'hidden'}).injectInside('form_sort_id');
            ajaxed.setProperty('value','0');
            $('form_sort_id').addEvent('submit',function(e){
                var url = $('form_sort_id').getProperty("action");
                new Event(e).stop();
                
                this.loadHTML(e,url,$('form_sort_id'));
                var listing_container = new Element('div').setProperties({id:'listing_container','class':'clearfix'});
                $('listing').replaceWith(listing_container);
                $('listing_container').setHTML('<p><span class="loading replaced">Loading</span></p>');
            }.bind(this));
			
			$('sort').addEvent('change',function(e){
                var url = $('form_sort_id').getProperty("action");
                new Event(e).stop();
                //this.loadHTML(e,url,$('form_sort_id'));
                var listing_container = new Element('div').setProperties({id:'listing_container','class':'clearfix'});
                $('listing').replaceWith(listing_container);
                $('listing_container').setHTML('<p><span class="loading replaced">Loading</span></p>');
            }.bind(this));
			
        }
        
        if($('form_refine_submit')){
            $('form_refine_submit').setStyle('display','none');
            var ajaxed = new Element('input').setProperties({name:'ajaxed',type:'hidden'}).injectInside('form_refine_id');
            ajaxed.setProperty('value','0');
            $('form_refine_id').addEvent('submit',function(e){
                var url = $('form_refine_id').getProperty("action");
                new Event(e).stop();
                this.loadHTML(e,url,$('form_refine_id'));
                var listing_container = new Element('div').setProperties({id:'listing_container'});
                $('listing').replaceWith(listing_container);
                $('listing_container').setHTML('<span class="loading replaced">Loading</span>');
            }.bind(this));
			
			$('refine').addEvent('change',function(e){
                var url = $('form_refine_id').getProperty("action");
                new Event(e).stop();
                this.loadHTML(e,url,$('form_refine_id'));
                var listing_container = new Element('div').setProperties({id:'listing_container'});
                $('listing').replaceWith(listing_container);
                $('listing_container').setHTML('<span class="loading replaced">Loading</span>');
            }.bind(this));
        }
    }
}

/**********************************************************************
Handles the Ajax pagination Links
**********************************************************************/
var paginationLinks =
{
    complete: function(){
        var tmp = new Element('div').setProperties({id:'tmp'}).setStyles({display:'none'}).injectInside(document.body);
        tmp.setHTML(this.content.response.text);
        var slice = tmp.getElement('#listing');
        $('listing_container').setHTML('<div id="listing" class="clearfix">'+slice.innerHTML+'</div>');
        tmp.remove();
        this.init();
        sortPage.init();
    },

    loadHTML: function(e,url){
		this.content = new Ajax(url, {
            method: 'get',
            onComplete: this.complete.bind(this),
            autoCancel: true,
            evalScripts: false
        }).request();
	},

    init : function(){
        $$('div#pagination_links a').each(function(link){
            link.addEvent('click',function(e){
                new Event(e).stop();
                var href = link.getProperty('href');
                //href += "&ajaxed=1";
                this.loadHTML(e,href);
                var listing_container = new Element('div').setProperties({id:'listing_container'});
                $('listing').replaceWith(listing_container);
                $('listing_container').setHTML('<span class="loading replaced">Loading</span>');
            }.bind(this));
        }.bind(this));
    }
}


/**********************************************************************
PNG Fix for IE6
**********************************************************************/
var pngFix = 
{
	init : function(){
		if(window.ie6){
			$$('img[src$=png]').each(function(img){
                var src = img.getProperty('src')
                img.setProperty('src', '/site-media/img/kids/transparent.gif');
				img.setStyles({
					'background' : 'none',
					'filter':	'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\', sizingMethod=\'scale\')',
					'width':	img.getProperty('width'),
					'height':	img.getProperty('height')
				});
			});
		}
	}
}


/**********************************************************************
Handles subscribe form 
**********************************************************************/
var mail_form = 
{
	init: function(){
		if($('mail_name_id')){
			var field_name = $('mail_name_id');
			var txt_name ="Your name:"
			field_name.addEvent('click', function(e){
                if(field_name.getProperty('value') == txt_name){
                    field_name.setProperty('value','');
                }
			});
			field_name.addEvent('blur', function(e){
				var query = field_name.getProperty('value');
				if(!query){
					field_name.setProperty('value',txt_name);
				}
			});
			var field_email = $('mail_email_id');
			var txt_email = "Your email address:";
			field_email.addEvent('click', function(e){
                if(field_email.getProperty('value') == txt_email){
                    field_email.setProperty('value','');
                }
			});
			field_email.addEvent('blur', function(e){
				var query = field_email.getProperty('value');
				if(!query){
					field_email.setProperty('value',txt_email);
				}
			});
		}
	},
    
    sendForm: function(){
        if($('mail_submit_id')){
            $('mail_submit_id').addEvent('click',function(e){
                new Event(e).stop();
                var email = $('mail_email_id').getProperty('value');
                var name = $('mail_name_id').getProperty('value');;
                if(isValidEmail(email)){                // validate fields
                    if((isValidName(name))){
                        $('mail_form_errors').setHTML("");
                        var thanks = new Element('h4',{'id':'mailform_status_id'}).setHTML('Thank you for Subscribing!').injectAfter('mail_form');
                        var slideArticle = new Fx.Slide('mail_form');
                        slideArticle.slideOut().chain(function(){
                            $('mail_form_id').send({
                                onSuccess: function(){
                                    thanks.setHTML('Success!');
                                }
                            });
                            //$('mail_form_id').submit();
                        });
                    }else{
                        $('mail_form_errors').setHTML("Please enter a valid name");
                    }
                }else{
                    $('mail_form_errors').setHTML("Please correct your email address");
                }

            });
        }
    }
	
}

var render_xhr =
{
	complete: function(){
		var tmp = new Element('div').setProperties({id:'tmp'}).setStyles({display:'none'}).injectInside(document.body);
		tmp.setHTML(this.content.response.text);
		var slice = $('tmp').getElement('#xhr_content');
		$('loading').replaceWith(slice);
		tmp.remove();
        this.manageForm();
        poplink.init()
	},

	loadHTML: function(e,url){
		this.content = new Ajax(url, {
								method: 'get',
								onComplete: this.complete.bind(this),
								autoCancel: true,
								evalScripts: false
							}).request();
	},

	init: function(){
		var links = $$('.render_link');
		links.each(function(link){
			var url = link.getProperty('href')+"?xhr";
			link.addEvent('click', function(e){
				new Event(e).stop();																			// overide default behaviour
				var a = new Element('div',{'id':'translucent-mask'}).setHTML('&nbsp;').injectTop($E('body'));  	// create the elements
				var b = new Element('div',{'id':'pop-window'}).setHTML('&nbsp;').injectTop($E('body'));
				var c = new Element('div',{'id':'loading'}).setHTML('<p><span class="loading replaced">Loading</span></p>').injectTop($('pop-window'));
				this.loadHTML(e,url);
				this.closeWindow();
			}.bind(this));
		}.bind(this));
	},
	
	closeWindow: function(){
		$('translucent-mask').addEvent('click', function(e){
			new Event(e).stop();
            if(confirm("Do you want to close the current form?")){
    			$('translucent-mask').remove();
    			$('pop-window').remove();
            }
		});

	},
    
    resultForm: function(){
        var tmp = new Element('div').setProperties({id:'tmp'}).setStyles({display:'none'}).injectInside(document.body);
        tmp.setHTML(this.content.response.text);
        var slice = tmp.getElement('#xhr_content');
        $('pop-window').setHTML('<div id="xhr_content">' + slice.innerHTML +'</xhr_content>');
        tmp.remove();
        this.manageForm();
        if($('close_window')){
            $('close_window').addEvent('click',function(e){
                new Event(e).stop();
                $('translucent-mask').remove();
                $('pop-window').remove();
            });
        }

        
    },

    responseForm : function(e,url){
        this.content = new Ajax(url,{
            postBody: $('post_comment_form'),
            method: 'post',
            onComplete: this.resultForm.bind(this),
            autoCancel: true,
            evalScripts: false
        }).request();
    },
    
    manageForm : function(){
        if($('post_comment_form')){
            $('post_comment_form').addEvent('submit',function(e){
                new Event(e).stop();
                var url = $('post_comment_form').getProperty('action')+"?xhr"
                this.responseForm(e,url)
                $('pop-window').setHTML('<p><span class="loading replaced">Loading</span></p>');
            }.bind(this));
        }
    }

}

var show_comments = 
{
    complete: function(){
		var tmp = new Element('div').setProperties({id:'tmp'}).setStyles({display:'none'}).injectInside(document.body);
		tmp.setHTML(this.content.response.text);
		var slice = $('tmp').getElement('#work-comments');
		$('comments_container').replaceWith(slice);
		tmp.remove();
	},
    
    loadHTML: function(e,url){
        this.content = new Ajax(url, {
								method: 'get',
								onComplete: this.complete.bind(this),
								autoCancel: true,
								evalScripts: false
							}).request();
    },
    init : function(){
        var links = $$('.view_comment_link')
        links.each(function(link){
            link.addEvent('click', function(e){
                new Event(e).stop();
                var url = link.getProperty('href')+'?allcomments';
                if($('work-comments-split')){
                    var slideComments = new Fx.Slide('work-comments-split');
                    slideComments.hide();
                }
                var comments_container = new Element('div').setProperties({id:'comments_container'}).injectInside('content');
                $('comments_container').setHTML('<p><span class="loading replaced">Loading</span></p>');
                this.loadHTML(e,url);
            }.bind(this));
        }.bind(this));
    }
    
}

/**********************************************************************
Manage sliding paragraphs
**********************************************************************/
var slide_paragraphs =
{
    init: function(){
        if($$('p.slide')){
            var mySlide = []
            $$('p.slide').each(function(e,i){
                mySlide[i] = new Fx.Slide(e);
                mySlide[i].hide();
            });
        }
        if($$('h2.over_img')){
            $$('h2.over_img').each(function(e,i){
                e.addEvents({
                    'mouseenter': function(ev){mySlide[i].slideIn();},
                    'mouseleave': function(ev){mySlide[i].slideOut();}
                });
            });
        }
    }
}


var fallback =
{
	hide: function(){
		var elements = $$('.fallback');
		elements.each(function(element){
			element.setStyle('display','none');
		});
	}
}


var autosubmit = {
   init: function(){
       var autosubmit = $$('.autosubmit');
       $each(autosubmit,function(inner_form,index){
          
           var input_select = inner_form.getElement('select');
           if(input_select){
               input_select.addEvent('change',function(){
                   inner_form.submit();
               });
           }
       });
   }
} 



/*************************************************
 Handles favourite authors in the registration
 
var data_picker = 
{
 		init: function(){	 		
 			var dp = this;
 			if($('reg_form')){
 				dp.createJsForm(); 				
 				dp.hideSelectForm();
 				$('reg_author_input').addEvent('keydown', function(event){	
 					if(event.keyCode==13){
 						event.preventDefault();
 					}
				}); 			
	 			//$('reg_author_button').addEvent('click', function(e){
	 				 				
				        	//elems = $('reg_form').getElements('a[title=yahoo]');
				        	//elems = $('reg_form').getElements('option[value=3529]');			        	
				        	//elems = $('reg_form').getElements('option[innerHTML=Harry Hill]');
				        	//alert(elems)		        	
				        	//contributors = $$('.contributor option');	
				            //alert(contributors[1].innerHTML);   	
				        	 //alert('click'+ $$('option:value(3529)'));  			        	
		        //        	dp.loadHTML(e,'/accounts/favourite_contributors/')


		        //        })
 			}
 		},

 		createJsForm : function(){   
            $('favourite_contributors1').innerHTML='<li class="clearfix checkbox"><input id="reg_author_input" class="text input_autocomplete author" type="text" name="favourites_contributors" autocomplete="off" /><div class="autocompleter-loading" style="display: none;"/><div class="autocompleter-loading" style="display: none;"/></li><ul id="reg_author_display"></ul>';
 			$('favourite_contributors2').innerHTML='<li class="clearfix checkbox"><input id="reg_author_input" class="text input_autocomplete author" type="text" name="favourites_contributors" autocomplete="off" /><div class="autocompleter-loading" style="display: none;"/><div class="autocompleter-loading" style="display: none;"/></li><ul id="reg_author_display"></ul>';
 			$('favourite_contributors3').innerHTML='<li class="clearfix checkbox"><input id="reg_author_input" class="text input_autocomplete author" type="text" name="favourites_contributors" autocomplete="off" /><div class="autocompleter-loading" style="display: none;"/><div class="autocompleter-loading" style="display: none;"/></li><ul id="reg_author_display"></ul>';
 			$('favourite_contributors4').innerHTML='<li class="clearfix checkbox"><input id="reg_author_input" class="text input_autocomplete author" type="text" name="favourites_contributors" autocomplete="off" /><div class="autocompleter-loading" style="display: none;"/><div class="autocompleter-loading" style="display: none;"/></li><ul id="reg_author_display"></ul>';
 			$('favourite_contributors5').innerHTML='<li class="clearfix checkbox"><input id="reg_author_input" class="text input_autocomplete author" type="text" name="favourites_contributors" autocomplete="off" /><div class="autocompleter-loading" style="display: none;"/><div class="autocompleter-loading" style="display: none;"/></li><ul id="reg_author_display"></ul>';
 		},
 		
 		
 		hideSelectForm : function(){
 			$('id_favourites_contributors').setStyle('display', 'none');
 		}, 

	    loadHTML: function(e,url){
	    	//alert('load html')
	    	var dp = this;
	        this.content = new Ajax(url, {
									//postBody: $('reg_author_input'),
									postBody:'contributor='+ $('reg_author_input').getProperty('value'),
									method: 'get',
									onComplete: dp.showResult.bind(this),
									autoCancel: true,
									evalScripts: false
								}).request();
	    },

		showResult: function(){
			var dp = this;
			var result = this.content.response.text
		  if(!result==''){
			   var contributor_id = result.substring(result.indexOf('contributor_id')+15, result.indexOf('&'));
			   var contributor_name = result.substring(result.indexOf('contributor_name')+17);
			   if(! $('reg_author_display').getElement('a[id='+contributor_id+']')){
			      option = $('reg_form').getElements('option[value='+contributor_id+']')
				  option.setProperty('selected','selected')   
				   var result_element = new Element('li').setHTML(contributor_name + ' <a class="contributor_id" id='+contributor_id+'>delete</a>'); 
				   result_element.setProperty('id', 'contributor_'+contributor_id);
				   result_element.inject($('reg_author_display'),'top');
				   $('reg_author_input').value = '';
				   $('reg_author_display').getElements('a[id='+contributor_id+']').addEvent('click', function(event){
	 						 opt = $('reg_form').getElements('option[value='+contributor_id+']');
							 opt.setProperty('selected','');
							 $('reg_author_display').getElement('li[id=contributor_'+contributor_id+']').remove();
							 //console.log( $('reg_author_display').getElement('li[id=contributor_'+contributor_id+']'));
	 				})
			      
			   }
		  }
		}
}
 
 ***********************************************/
 
 var multiselect = 
 {
    init: function(){
        var multiselect = $$('.multiselect');
        $each(multiselect,function(original_select,index){
            size = {'width': '200px','height': '200px'}
            original_name = original_select.getAttribute('name');
            original_id = original_select.getAttribute('id');
            clon_name = original_name + '_clone';
            clon_id = original_id + '_clone';
            var new_select = new Element('select',{
                'name':original_name,
                'id': original_id
            });
            new_select.setAttribute('multiple','multiple');
            if(window.ie6){
                //ie6 hack
                new_select.options[0] = new Option('','');
                new_select.options[0].parentNode.removeChild(new_select.options[0]);
            }
            //should add selected items
            new_select.setStyles(size);
            original_select.setStyles(size);
            original_select.setAttribute('name',clon_name);
            original_select.setAttribute('id',clon_id);
            div_source = new Element('div',{
                'class':'stacked clearfix',
                'id':'div_source_id'
            });
            div_source_head = new Element('div',{
                'class':'stacked_top clearfix',
                'id':'div_source_head_id'
            }).setHTML('All Faber Authors');
            div_source.adopt(div_source_head);
            div_source_subhead = new Element('div',{
                'class':'stacked_top clearfix',
                'id':'div_source_subhead_id'
            }).setHTML('<img src="/site-media/img/base/selector-search.gif" alt="" />');
            div_source.adopt(div_source_subhead);
            var div_selected = new Element('div',{
                'class':'stacked clearfix',
                'id':'div_selected_id'
            });
            div_selected_head = new Element('div',{
                'class':'stacked_top clearfix',
                'id':'div_selected_head_id'
            }).setHTML('My Faber Authors');
            div_selected.adopt(div_selected_head);
            div_selected_subhead = new Element('div',{
                'class':'stacked_top clearfix',
                'id':'div_selected_subhead_id'
            }); //.setHTML('Select your choices');
            div_selected.adopt(div_selected_subhead);
            div_source.injectBefore(original_select);
            div_source.adopt(original_select);
            div_selected.injectAfter(div_source);
            div_selected.adopt(new_select)
            var input = new Element('input',{
                'name':'multiselect_autocomplete_'+index,
                'id': 'multiselect_autocomplete_'+index,
                'type': 'text',
                'class': 'multiselect_autocomplete'
            });
            input.setStyles({
                'width':'170px',
                'display':'block'
            });
            //contains default values
            //original_select.clone();
            var default_select = new Element('select',{
                'name':'default_select',
                'id': 'default_select_id'
            });
            original_select.getElements("option").each(function(e_default){
                e_default_clon = e_default.cloneNode(true);
                if(window.ie){
                    (e_default.selected) ? e_default_clon.selected = 1 : e_default_clon.selected = 0;
                }
                if(e_default_clon.selected){
                    e_default.selected = 0;
                    e_default_clon.selected = 0;
                    new_select.adopt(e_default);
                }else{
                    default_select.adopt(e_default_clon);
                }
            });
            default_select.setAttribute('id','multiselect_default_'+index);
            default_select.setAttribute('name','multiselect_default_'+index);
            input.addEvent('keyup',function(input_element){ 
                original_select.setHTML('');    // empty multiselect
                option_list = default_select.getElementsByTagName('option');
                query = String(input.value).toLowerCase();
                if(query.length<1){
                    for(var i=0;i<option_list.length;i++){
                        option = option_list[i];
                        var option_clon = option.cloneNode(true);
                        original_select.adopt(option_clon);
                    }
                }else{
                    if(window.ie){
                        //workaround for ie
                        for(var i=0;i<option_list.length;i++){
                            option = option_list[i];
                            option_value = String(option.innerHTML).toLowerCase();
                            if (option_value.indexOf(query) != -1){
                                var option_clon = option.cloneNode(true);
                                original_select.adopt(option_clon);
                            }
                        }
                    }else{
                        $each(option_list,function(option,index){
                            option_value = String(option.innerHTML).toLowerCase();
                            if (option_value.indexOf(query) != -1){ //compare and populate
                                option_clon = option.clone();
                                original_select.adopt(option_clon);
                            }
                        });
                    }
                }
            });
            div_source_subhead.adopt(input);
            // move elements
            div_arrows = new Element('div',{
                'class':'stacked slim clearfix'
            });
            div_arrows.injectAfter(div_source);
            var button_add = new Element('a',{
                'id': 'multiselec_add_button_id',
                'class': 'multiselect_button_left replaced'
            }).setHTML('Add');
            button_add.addEvent('click',function(ea){ // add elements
                new Event(ea).stop();
                original_select.getElements("option").each(function(e_add){
                    if(e_add.selected){
                        new_select.adopt(e_add);
                        e_add.selected = 0;
                        // remove from base
                        selector = "option[value=" + e_add.value + "]";
                        remove_list = default_select.getElements(selector);
                        remove_list.each(function(o_remove){
                            o_remove.remove();
                        });
                    }
                });
            });
            div_arrows.adopt(button_add);
            var button_remove = new Element('a',{
                'id': 'multiselec_remove_button_id',
                'class': 'multiselect_button_right replaced'
            }).setHTML('Remove');
            button_remove.addEvent('click',function(er){
                new Event(er).stop();
                new_select.getElements("option").each(function(e_remove){
                    if(e_remove.selected){
                        e_remove_clon = e_remove.clone();
                        original_select.adopt(e_remove);
                        //reincorporate base
                        default_select.adopt(e_remove_clon);
                        e_remove.selected = 0;
                    }
                });
            });
            div_arrows.adopt(button_remove);
            $('reg_submit').addEvent('click',function(){
                new_select.getElements("option").each(function(e_marked){
                    e_marked.selected = 1;
                });
            });
        });
    }

 }

var default_newsletter = 
{
    init: function() {
        var cb = $('id_newsletters_0');
        if (null != cb){
            cb.setProperty('checked', 'checked');
        }
      }
}

var faber_blog = {
    init: function(){
        var navLinks = $$('#navigation a');
        navLinks.each(function(item, index){
            if (item.text == 'The Faber Blog'){
                item.setProperty('target', '_blank');
            }
        });
    }
}
 
 /*************************************************
   init
  ************************************************/	
window.addEvent
(
	'domready',
	function()
	{
		//faber.init();
		render_window.init();
		search_form.init();
		poplink.init();
		autosubmit.init();
        //sortPage.init();
		if(window.ie6) pngFix.init();
        mail_form.init();
        mail_form.sendForm();
        //paginationLinks.init();
        render_xhr.init();
        show_comments.init();
        slide_paragraphs.init();
        fallback.hide();
        //data_picker.init();
        multiselect.init();
        default_newsletter.init();
        faber_blog.init();
	}
);


 

