var MailForm = new Class(    
    {
	options: 
	{	
        duration: 400 
	},		
	initialize: function(options){
	    
		this.setOptions(options);

		this.open = $('openMailForm');
		this.open.setStyle('cursor','pointer');
               
        var prm = Sys.WebForms.PageRequestManager.getInstance();        
        prm.add_beginRequest(this.onStart.bind(this));  
        prm.add_pageLoaded(this.onComplete.bind(this));
        
        this.clientState = 0;        
        this.backgroundImage = new Asset.image(this.options.backgroundImage);
        this.closeImage = new Asset.image(this.options.closeImage);
                  	    		    	          	
        this.box = new Element('div', {'styles': {'position':'absolute','left': 10, 'top': 10, 'visibility':'hidden', 'z-index': 66666}}).injectInside(document.body);
        this.cancel = new Element('div', {'styles': {'position':'absolute','left': 10, 'top': 10, 'visibility':'hidden', 'z-index': 66667}}).injectInside(document.body);
        this.form = $('mailForm');
        
        
        if(window.ie6 && (this.options.backgroundImage.indexOf('.png') > -1))
        {
            this.image = new Element('div', {'styles': {'width':10,'height':10,'filter':"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.options.backgroundImage + "',sizingMethod='scale')"}}).injectInside(this.box);                                            
        }
        else
        {
            this.image = new Element('img', {'styles': {'border':0,'width':10,'height':10}}).injectInside(this.box);         
        }
        if(window.ie6 && (this.options.closeImage.indexOf('.png') > -1))
        {                
            this.cancel.setStyles({'cursor':'pointer','filter':"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.options.closeImage + "',sizingMethod='scale')"});
        }
        else
        {            
            this.cancel.setStyles({'cursor':'pointer','background':'url(' + this.options.closeImage + ') top left'});
        } 
                                        
        this.open.addEvent('click',
            function() 
            { 
               this.zoomIn();
               return false;
                  
            }.bind(this)
        );
        this.cancel.addEvent('click',
            function()
            {
                if($defined(this.messageBox))
                {
                    if(this.messageBox.getClientState() == 1)
                    {
                        return;
                    }                    
                }
                if($defined(window.waterMarks))
                {
                    window.waterMarks.refresh('.mailform_watermark');
                }                
                this.zoomOut();      
            }.bind(this)            
        );
	    window.addEvent('resize',
            function() 
            { 
               this.setPosition(true);
                  
            }.bind(this)
        );  
	    window.addEvent('scroll',
            function() 
            { 
               this.setPosition(true);
                  
            }.bind(this)
        );               
                              
        this.fx = {
            boxIn:this.box.effects({duration: this.options.duration,transition: Fx.Transitions.Cubic.easeInOut,wait: false}),
            boxOut:this.box.effects({duration: this.options.duration,transition: Fx.Transitions.Cubic.easeIn,wait: false}),
            boxOpacity:this.box.effects({duration: this.options.duration,transition: Fx.Transitions.linear,wait: false}),                        
            image:this.image.effects({duration: this.options.duration,transition: Fx.Transitions.Cubic.easeIn,wait: false}),
            form:this.form.effects({duration: this.options.duration,transition: Fx.Transitions.linear,wait: false})
        };       
                   
		if (this.options.initialize) 
		{
		    this.options.initialize.call(this);
		}
	},
    onComplete:function(sender,args)
    { 
        var panels = args.get_panelsUpdated();
        if(panels.length > 0)
        {                                                                    
            if(panels[0].id.indexOf(this.options.controlsPrefix + '_updatePanel') > -1)
            {           
                this.spinner.stop();
                
                if($(this.options.controlsPrefix + '_isValid').value == 'True')
                {        
                    $(this.submitId).disabled = true;
                    if(!$defined(this.messageBox))
                    {
                        this.messageBox = new MessageBox();
                        this.messageBox.button.addEvent('click',function()
                        {
                            $(this.submitId).disabled = false;
                            if($defined(window.waterMarks))
                            {
                                window.waterMarks.refresh('.mailform_watermark');
                            }                             
                            this.zoomOut.delay(300,this);
                        }.bind(this));
                    }
                                                                            
                    this.messageBox.show('Hej! Din intresseanm&auml;lan &auml;r nu skickad. Vi &aring;terkommer till dig inom kort. Mvh Human Assistans','OK',this.box,'center',this.options.messageBoxOffsetX,this.options.messageBoxOffsetY);                                         
                }                                                                             
            }
        }            
    },          
    onStart:function(sender,args)
    {
        if(sender._updatePanelClientIDs.length > 0)
        {                                                                    
            if(args._postBackElement.id.indexOf(this.options.controlsPrefix) > -1)
            {         
                this.submitId = args._postBackElement.id;
                $(this.submitId).disabled = true;
                                                                                                     
                if(!$defined(this.spinner))
                {  
                    this.spinner = new Spinner({images:this.options.spinnerImages});
                }
                
                this.spinner.start(this.box,'center',this.options.spinnerOffsetX,this.options.spinnerOffsetY);                           
            }
        }  
    }, 	   	           		
	zoomIn: function()
	{        	
		if(this.clientState == 1)
	    {
	        return;
	    } 
	    this.clientState = 1;
	    
	    if($defined(this.OBJECTS))
        {
            this.OBJECTS.hide();
        }
	           
        var coords = this.open.getCoordinates();
        this.zoomStart = {};        
        this.zoomStart.x = coords.left;
        this.zoomStart.y = coords.top;
        this.zoomStart.w = coords.width;                
        this.zoomStart.h = coords.height;         
        
        if(!window.ie6)
        {                 
            this.image.setProperty('src',this.backgroundImage.src);     
        }
        this.image.setStyles({'width':this.zoomStart.w,'height':this.zoomStart.h});
        this.box.setStyles({'left':this.zoomStart.x,'top':this.zoomStart.y,'visibility':'visible','opacity':1});
          
        this.setPosition(false);
	},
	setPosition:function(isScrollResize)
	{
	    if(this.clientState == 1)
	    {
            var end =  
            {
                x:((window.getWidth() / 2) - (this.backgroundImage.width / 2)) + this.options.imageOffsetX,
                y:(((window.getHeight() / 2) - (this.backgroundImage.height / 2)) + window.getScrollTop())+ this.options.imageOffsetY,
                w:this.backgroundImage.width,
                h:this.backgroundImage.height
            };
            var closeImageCoords = 
            {                
                x:end.x + end.w - this.closeImage.width + this.options.closeOffsetX,
                y:end.y  + this.options.closeOffsetY,
                w:this.closeImage.width,
                h:this.closeImage.height
            };
            
            this.cancel.setStyles({'visibility':'hidden','top': closeImageCoords.y,'left': closeImageCoords.x,'width':closeImageCoords.w,'height':closeImageCoords.h});                                                                                                    
            
            if(isScrollResize)
            {
                var isMessageBoxOpen = false
                if($defined(this.messageBox))
                {
                    if(this.messageBox.getClientState() == 1)
                    {
                        this.messageBox.hide();
                        isMessageBoxOpen = true;
                           
                    }
                }
                                                                                        
                this.form.setStyles({'opacity':0,'top': end.y + this.options.formOffsetY,'left': end.x + this.options.formOffsetX});        
                this.fx.boxIn.start({'top': end.y,'left': end.x}).chain(
                    function()
                    {                                
                        this.fx.form.start({'opacity':1}).chain(
                            function()
                            {
                                this.cancel.setStyle('visibility','visible');
                                if(isMessageBoxOpen)
                                {
                                    this.messageBox.show('Hej! Din epost &auml;r nu skickad!','OK',this.box,'center',this.options.messageBoxOffsetX,this.options.messageBoxOffsetY);                                                                             
                                }

                            }.bind(this)
                        );
                    }.bind(this)
                );            
            }
            else
            {
                this.form.setStyles({'top': end.y + this.options.formOffsetY,'left': end.x + this.options.formOffsetX});        
                this.fx.boxIn.start({'top': end.y,'left': end.x});
                this.fx.image.start({'width': end.w,'height': end.h}).chain(
                    function()
                    {                                
                        this.fx.form.start({'opacity':1}).chain(
                            function()
                            {                                
                                this.cancel.setStyle('visibility','visible');
                            }.bind(this)
                        ); 
                    }.bind(this)
                );            
            }               
        }	
	},		
	zoomOut: function()
	{
	
		this.cancel.setStyle('visibility','hidden');
	    this.form.setOpacity(0);
        this.fx.boxOpacity.start({'opacity':0}).chain(
            function()
            {
                if($defined(this.OBJECTS))
                {
                    this.OBJECTS.show();
                }
                this.clientState = 0;
                
            }.bind(this)
        );     
	},
	getClientState:function()
	{
	    return this.clientState;
	},	
    addOBJECTHider:function(object)
    {
        this.OBJECTS = object;
    }	

});

MailForm.implement(new Options);