

(function($)
{
	$.fn.rollovers = function(config)
	{
		/**
		 *	
		 */
		config = $.extend(
		{
			type: undefined,
			suffix_rollout: '_o',
			suffix_hover: '_h',
			ignore_class: 'active'
		}, config);
		
		var elTarget = this;
		var TYPE_IMG   = 'img';
		var TYPE_CLASS = 'class';
		
		
		/**
		 *	
		 */
		var Rollovers = function(_elTarget, _config)
		{
			this.target = _elTarget;
			this.config = _config;
		};
		
		Rollovers.prototype = {
			/**
			 *	
			 */
			fire: function(type)
			{
				if (this.config.type === undefined)
				{
					this.config.type = TYPE_CLASS;
				};
				
				switch(this.config.type)
				{
					case TYPE_IMG: this.rolloverImage(); break;
				};
			},
			/**
			 *	
			 */
			rolloverImage:function()
			{
				$(elTarget).each(function(i,elem)
				{
					$(elem).bind('mouseover', function(e)
					{
						if (elem.className == config.ignore_class)
						{
							return false;
						};
						var img =　$('img', elem).get(0);
						if (img !== undefined)
						{
							var extention = img.src.match( new RegExp('\..{3}$', 'i') )[0];
							$(img).attr(
								'src',
								img.src.replace(
									config.suffix_rollout+extention,
									config.suffix_hover  +extention
								)
							);
						};
					});
					$(elem).bind('mouseout', function(e)
					{
						if (elem.className == config.ignore_class)
						{
							return false;
						};
						var img =　$('img', elem).get(0);
						if (img !== undefined)
						{
							var extention = img.src.match( new RegExp('\..{3}$', 'i') )[0];
							$(img).attr(
								'src',
								img.src.replace(
									config.suffix_hover  +extention,
									config.suffix_rollout+extention
								)
							);
						};
					});
				})
			}
		};
		
		
		/**
		 *	
		 */
		(new Rollovers(elTarget, config)).fire();
	};
	
	
	$.fn.comelnav = function(config)
	{
		/**
		 *	
		 */
		config = $.extend(
		{
			gn: $('header nav > ul'),
			sn: $('#sidebar > ul'),
			class_prefix: 'open'
			
		}, config);
		
		var path = location.pathname;
		var query = location.search;
		var dir = 
		{
			home: 		{path: ''},
			company: 	{path: 'company'},
			news: 		{path: 'news', categoryId: '(5|9)'},
			business: 	{path: 'business'},
			recruit: 	{path: 'recruit'},
			contact: 	{path: 'contact'}
		};
		
		$.each(dir, function(key, category)
		{
			category['elem'] = $('.g-'+category.path, config.gn);
		});
		var curDir = new RegExp('\cat='+dir.news.categoryId).test(query) ? dir.news :
					 new RegExp('\^\/'+dir.company.path ).test(path) ? dir.company :
					 new RegExp('\^\/'+dir.news.path 	).test(path) ? dir.news :
					 new RegExp('\^\/'+dir.business.path).test(path) ? dir.business :
					 new RegExp('\^\/'+dir.recruit.path ).test(path) ? dir.recruit : 
					 new RegExp('\^\/'+dir.contact.path ).test(path) ? dir.contact : 
					 new RegExp('\^\/'+dir.home.path 	).test(path) ? dir.home : undefined;
		
		
		/**
		 *	
		 */
		var ComelNav = function()
		{
		};
		
		ComelNav.prototype = {
			/**
			 *	
			 */
			fire: function()
			{
				if (curDir !== undefined)
				{
					this.setupGnav(curDir);
					this.setupSnav(curDir);
				};
			},
			
			/**
			 *	
			 */
			setupGnav: function(_dir)
			{
				var children = $('li li a', config.gn);
				this.setActiveClass(children);
				
				config.gn.addClass(config.class_prefix+'-'+_dir.path);
			},
			
			/**
			 *	
			 */
			setupSnav: function(_dir)
			{
				var children = $('li li a', config.sn);
				this.setActiveClass(children);
			},
			
			setActiveClass: function(children)
			{
				$.each(children, function(i, a)
				{
					if (a.href == location.href)
					{
						$(a).addClass(config.class_prefix);
					};
				});
			},
			
			_:{}
		};
		
		
		/**
		 *	
		 */
		(new ComelNav()).fire();
	};
})(jQuery);
