document.addEvent('domready', function(){
	Element.implement({
	    resetFormValue : function(){
	    	if(this.get('tag')=='form'){
	    		this.getElements('input[type="text"],textarea').resetFormValue();
	    		return this;
	    	}
	        var def_value = this.get('value');
	        this.addEvents({
	            'focus':function(){
	                var cur_value = this.get('value');
	                if(cur_value == def_value) {
	                    this.set('value', '');
	                }
	            },
	            'blur':function(){
	                var cur_value = this.getProperty('value');
	                if(cur_value == '') {
	                    this.set('value', def_value);
	                }
	            }
	        });
	        return this;
	    }
	});	
});

var Sliders = new Class({
    Implements: [Events, Options],
    options:{
        button:'.toggle',
        slideClass:'.slider',
        autoHide:'',
        openFirst:'',
        showImage:'',
        hideImage:'',
        showText:'',
        hideText:'',
        imageClass:'',
        textClass:''
    },
    initialize:function(options){
        this.setOptions(options);
        
        this.sliders = $$(this.options.slideClass).map(function(e){
           
           if(Browser.Engine.gecko){
				//need to use FF hack so the slide doesn't flicker because it wraps around scrolling divs
				return new Fx.Slide(e,{overflow:'auto'});
			}else{
				//don't hack for others as causes scroll bars to appear during slide in IE
				
				return new Fx.Slide(e);
			};			
        });
        
        $$(this.options.button).each(function(toggler,index){
			toggler.addEvent('click',function(){
				this.sliders[index].toggle();		
				//change show/hide image
				if(this.options.imageClass != ''){
					if($$(this.options.imageClass)[index]){
						var indicatorImage = $$(this.options.imageClass)[index];
						if(indicatorImage.get('src').indexOf(this.options.showImage) > -1){
							indicatorImage.set('src',this.options.hideImage);						
						}else if(indicatorImage.get('src').indexOf(this.options.hideImage) > -1){
							indicatorImage.set('src',this.options.showImage);
						}
					}
				}
				//change show/hide text
				if(this.options.textClass != ''){					
					if($$(this.options.textClass)[index]){
						var showHideText = $$(this.options.textClass)[index];
						if(showHideText.get('text').indexOf(this.options.showText) > -1){
							showHideText.set('text',this.options.hideText);						
						}else if(showHideText.get('text').indexOf(this.options.hideText) > -1){
							showHideText.set('text',this.options.showText);
						}
					}
				}
			}.bind(this));			
        },this);
        
        if(this.options.autoHide){					
			$$(this.options.button).each(function(e,index){
				if(!this.options.openFirst){
					e.fireEvent('click');
				} else if(this.options.openFirst && index != 0){
					e.fireEvent('click');
				}
			},this);
		};
		
		if(this.options.showFirst && !this.options.autoHide){$$(this.options.button)[0].fireEvent('click');}
        
    }
});

/* Session Timeout Warning Class */

var timeoutWarning = new Class({
	Implements: [Events, Options],
	options:{
        sessionLength:20, //in minutes
        redirectUrl:'/'
    },
    initialize:function(options){
		this.setOptions(options);
		this.options.sessionLength = this.options.sessionLength * 60 * 1000; //change to milliseconds for the timers.
		var timeoutID = this.displayWarning.delay(this.options.sessionLength - 360000); //show the warning 6 mins before the end of the session
		var timeoutRedirectID = this.redirect.delay(this.options.sessionLength - 60000,this.options.redirectUrl); //redirect 1 min before the end of the session
		
		//*** TEST ***//
		//this.options.sessionLength = 7000; //change to milliseconds for the timers.
		//var timeoutID = this.displayWarning.delay(this.options.sessionLength - 3000); //show the warning 6 mins before the end of the session
		//var timeoutRedirectID = this.redirect.delay(this.options.sessionLength,this.options.redirectUrl); //redirect 1 min before the end of the session                    
	},
	displayWarning:function(){
		var mainWarningDiv = new Element('div').set({
			'id':'main-warning-div',
			'styles':{
				'width':'100%',
				'height':'100px',
				'background-color':'#000',
				'visibility':'visible',
				'position':'fixed',
				'top':'-100px',			
				'left':'0',
				'z-index':'1000',
				'opacity':'0.8'
			}
		});
		
		var mainWarningDivInner = new Element('div').set({
			'id':'main-warning-inner',
			'styles':{
				'width':'100%',
				'height':'100px',
				'visibility':'visible',
				'position':'fixed',
				'top':'-100px',			
				'left':'0',
				'z-index':'1001',
				'color':'#fff'
			},
			'html':'<div id="time-warning-inner"><p>Your session is about to expire, you should click the "Save current" or "Save my Profile" button to ensure you don\'t lose any of your changes.</p></div>'
		});
		
		//IE6 doesn't support fixed position so have to go with absolute instead and just have the warning appear right at the top of the page.
		if(Browser.Engine.trident && Browser.Engine.version < 5){
			mainWarningDiv.setStyle('position','absolute');	
			mainWarningDivInner.setStyle('position','absolute');
		};
				
		//inject the new elements into the dom
		mainWarningDivInner.inject($$('body')[0],'top');			
		mainWarningDiv.inject($$('body')[0],'top');			
		
		//slide in the warning
		mainWarningDiv.tween('top','0');
		mainWarningDivInner.tween('top','0');
		
	}, //end displayWarning()
	
	redirect:function(){		
		location.href = this;
	}
});