(function($){
	$.fn.tabs = function( options ){
		
		var settings = { onselect: null };
		if( options ) $.extend( settings, options );
		var me = $(this);
		
		function _tab_hideall(){
			me.find('li').removeClass('selected');
			me.find('li a').each(function(){ $( $(this).attr('href') ).hide(); });
		}
		
		if( settings.action == 'hide' ) {
			_tab_hideall();
			return;
		}
		
		me.find('li').each(function(){
			var id = $(this).children('a').attr('href');
			$( id ).hide();
			$(this).click( function(){
				_tab_hideall();
				$(this).addClass('selected');
				$( id ).show();
				if( settings.onselect ) settings.onselect.call($(this).children('a')[0], null);
			});
		});
		
		var f = me.find('li').first();
		
		if( settings.init ){
			var a = me.find('li a');
			for( var i=0; i<a.length; i++ )
				if( $(a[i]).attr('href')==settings.init ) f = $(a[i]).parent();
		}
		
		f.addClass('selected');
		$( f.find('a').first().attr('href') ).show();
		if( settings.onselect ) settings.onselect.call(f.children('a')[0], null);
	}
})(jQuery);

(function($){
	$.fn.popup = function( url, options ){
		
		var settings = {
			legacy: false,
			overlay: true,
			overlay_opacity: 0.7,
			width: 550,
			height: 300,
			z_index: 20
		};
		
		if( options ) $.extend( settings, options );
		
		if( settings.legacy ){
			window.open( url,  "_new", "status=0, height="+settings.height+", width="+settings.width+", resizable=0" );
		}else{
			var me = $(this);
			
			var win = me.children( '#popup_window' );
			
			if( !(win && win.length>0) ){
					win = $('<div id="popup_window" style="position: absolute; top: 50%; left: 50%; width: '+settings.width+'px; height: '+settings.height+'px; margin-left: -'+settings.width/2+'px; margin-top: -'+settings.height/2+'px; z-index:'+(settings.z_index+2)+'; background-color: white; -moz-border-radius: 6px; -webkit-border-radius: 6px; -webkit-box-shadow: 2px 2px 12px black; -moz-box-shadow: 2px 2px 12px black;"></div>');
					me.append( win );
			}

			win.width( settings.width );
			win.height( settings.height );
			
			if( url == 'close' ){
				if( settings.overlay ){
					var ol = me.children('#popup_overlay');
					if( ol && ol.length>0 ) ol.hide();
				}
				win.hide();
			}else{
				win.load( url, function( res, stat ){
					if( stat == 'success' ){
						if( settings.overlay ){
							var ol = me.children('#popup_overlay');
							if( !(ol && ol.length>0) ){
								ol = $('<div id="popup_overlay" style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; background-color: black; opacity:'+settings.overlay_opacity+'; z-index:'+(settings.z_index+1)+';"></div>');
								ol.click( function(){ win.hide(); ol.hide(); });
								me.append( ol );
							}
							ol.show()
						}
						win.show();
					}
				});
			}	
		}
	}
})(jQuery);


(function($){
	$.fn.menu = function( control, options ){
		
		var settings = { speed: 200, func: null };
		
		if( options ) $.extend( settings, options );
		var menu = this;

		var menu_close_timer = -1;
		$(control).mouseenter( function(){
			if( menu_close_timer >= 0 ){
				clearTimeout( menu_close_timer );
				menu_close_timer = -1;
			} else {
				$(menu).css('right', (-1*(menu.width()+20))+'px' );
				$(menu).slideDown( settings.speed );
			}
		});

		$(menu).children('li').click( settings.func );
		$(menu).mouseleave( function(){ 
			if( menu_close_timer < 0 ){
				menu_close_timer = setTimeout( function(){
					$(menu).slideUp( settings.speed );
					menu_close_timer = -1;
				}, 500);
			}
		});
		$(control).mouseleave( function(){ 
			if( menu_close_timer < 0 ){
				menu_close_timer = setTimeout( function(){
					$(menu).slideUp( settings.speed );
					menu_close_timer = -1;
				}, 500);
			}
		});
		
		$(menu).hide();
	}
})(jQuery);

