/*------------------------------------------------------------------------
 # mod_lofuserpanel - Lof UserPanel Module
 # ------------------------------------------------------------------------
 # author    LandOfCoder
 # copyright Copyright (C) 2010 loftheme.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 # Websites: http://www.landofcoder.com
 # Technical Support:  Forum - http://www.landofcoder.com/forum.html
-------------------------------------------------------------------------*/
 ////

if( typeof(LofUserPanel) == 'undefined' ){
	var LofUserPanel = new Class( {
		initialize:function( style, options ){
			this.setting = $extend({
			}, options || {} );
			
			this.closePanel = function(){ 
			
			};
		
		
			var panels = $$(".lof-panel");
			switch( style ){
				case "slide":
					this.slideUpDown( panels );
				break;

				case "fade":
					this.fadeEffect( panels );
				break;
				default:
					this.nonEffect( panels );

			}
			$(document).addEvent("click", this.closePanel);
			$$(".lof-panel").addEvents( {"mouseenter":function(){ 
											$(document).removeEvents( "click" );					   
										},
										 "mouseleave":function(){ 
											$(document).addEvent("click", this.closePanel);
										
										}.bind(this) } );
		},
		
		nonEffect:function( panels ){
			this.closePanel=function(){ 
				$$("#lof-userpanel .lof-button").removeClass("active");
				panels.hide();
			}
			$$("#lof-userpanel .lof-button a").addEvent( "click", function(){																   
			 	var panelId = this.get("href");
				var parent = this.getParent("div");
				panels.hide();
				if( parent.hasClass("active") ){
					$$("#lof-userpanel .lof-button").removeClass("active");
					return false;
				}
				$$("#lof-userpanel .lof-button").removeClass("active");parent.addClass("active");	
				if( $$(panelId) ){
				   $$(panelId).setStyles( { "display":"block","opacity":1 } ).show();
				}
				return false;
			} );
		
		},
		fadeEffect:function( panels ){ 
			this.closePanel=function(){ 
				$$("#lof-userpanel .lof-button").removeClass("active");
				panels.fade("out");
			}
			
			$$("#lof-userpanel .lof-button a").addEvent( "click", function(){											   
			 	var panelId = this.get("href");
				var parent = this.getParent("div");
				panels.fade("out");
				if( this.hasClass("active") ){
					$$("#lof-userpanel .lof-button").removeClass("active");
					return false;
				}
				$$("#lof-userpanel .lof-button").removeClass("active");parent.addClass("active");	
				if( $$(panelId) ){
				   $$(panelId).setStyles( { "display":"block","opacity":0 } ).fade("in");
				}
				return false;
			} );

		},
		
		slideUpDown:function( panels ){

		 	var heights = new Array();
			var fx = new Array();
			panels.setStyles({"display":"block","opacity":0}).each( function(panel, i) {  
				heights["#"+panel.id] = panel.getSize().y;															 															 
				fx["#"+panel.id] = new Fx.Morph( panel );
				fx["#"+panel.id].cancel().set( {"height":0} );
			} );
			
			
			var lastactive = '';
			this.closePanel=function(){ 
				$$("#lof-userpanel .lof-button").removeClass("active");
				if( lastactive !="" ){fx[lastactive].start( {"height":0} );	}
			}
			$$("#lof-userpanel .lof-button a").addEvent( "click", function(){			
		 		;
			 	var panelId = this.get("href");
				var parent = this.getParent("div");
				var height =  parent.getSize().y;
				
				if( parent.hasClass("active") ){
					$$("#lof-userpanel .lof-button").removeClass("active");
					fx[panelId].cancel().start( {"height":0} );
					return false;
				}
				
				$$("#lof-userpanel .lof-button").removeClass("active");parent.addClass("active");
				
				if( lastactive !="" ){fx[lastactive].start( {"height":0} );	}
				lastactive = panelId;
				if( $$(panelId) ){ 
 	 				var pwidth = $$(panelId).getWidth();
					var rwidth = 0;
					if( parent.getPosition().x> 900 ){
						rwidth = parseInt(pwidth) - parseInt(parent.getSize().x);
					}
				    $$(panelId).setStyles( { "opacity":1 } );
					fx[panelId].cancel().start( {"height":heights[panelId]} );
				}
				return false;
			} );
		}
		
		
	} );
}
