
// ----------------------------------------------------------------------------
// Roll Over
// ----------------------------------------------------------------------------
var KF = {};

KF.funcs = function(obj){
	obj.rollOver = function(options){
		
		var roll = options.roll,
			unroll = options.unroll;
		
		function find_child(elm){

			var collect_img = {},
				collect_a = {},
				counter_img = 0,
				counter_a = 0;
	
			function get_elms(elm){
				var element = elm;
				
				elm.children().each(function(){
					
					if($(this).attr('class') !== unroll){
						if(this.tagName === 'IMG' || this.tagName === 'INPUT'){
							collect_img[counter_img] = $(this);
							counter_img++;
						}else if(this.tagName === 'A'){
							var tag_a = $(this);
							tag_a.find('img').each(function(){
								if($(this).attr('class') !== unroll){
									collect_img[counter_img] = $(this);
									counter_img++;
								}
							});
							
							collect_a[counter_a] = tag_a;
							counter_a++;
							
							return true;
						}
						get_elms($(this));
					}else{
						return true;
					}
				});
			}
			
			get_elms(elm);
			
			return [collect_img,collect_a];
		}
		
		$('.' + roll).each(function(){
			var items = find_child($(this)),
				items_img = items[0],
				items_link = items[1],
				onmouse_check = false;

			$.each(items_img, function(i){
				
				var btn = items_img[i];
				
				if(btn[0].tagName === 'INPUT' && btn.attr('type') === 'text'){
					return true;
				}
					
				btn.mouseover(function(){
					if (!onmouse_check) {
						onmouse_check = true;
						btn.attr('src', btn.attr('src').replace(/(\.[^\.]+$)/, "_o$1"));
						
					}
				});
				btn.mouseout(function(){
					btn.attr('src', btn.attr('src').replace(/_o/, ""));
					onmouse_check = false;
				});
			});
			
			$.each(items_link, function(i){
				var btn = items_link[i],
					imgs = btn.find('img');
			});	
		});
	};
};

KF.funcs(KF);

$(function(){
	KF.rollOver({roll : 'roll', unroll : 'unroll'});
});
