
var KF = {};

KF.funcs = function(obj){

	obj.fitHeight = function(options){

		var sortFnc = function (a,b){return b-a;};
		var childs_array01=[];
		var childs_array01=[];
		function getClass(classname){
			var allElm = document.body.getElementsByTagName("*");
			var allElmLength = allElm.length;
			var collect = [];
			var i;
			for (i=0; i<allElmLength; i++){
				var gotName = allElm[i].className.split(" ");
				var j;
				for(j=0; j<gotName.length; j++){
					if(gotName[j] === classname){
					
						collect.push(allElm[i]);
					
					}
				}
			}
			return collect;
		}
		
		function getChild(array){
			var collect = [];
			var childs = array.childNodes;
			var k;
			for(k=0; k<childs.length; k++){
			
				if(childs[k].nodeType === 1){
					collect.push(childs[k]);
				}
			}
			return collect;
		}
		
		function getHeight(array){
			var collect = [];
			var i;
			for(i=0 ; i < array.length ; i++){
			
				array[i].style.height = '';
			
				var gotHeight = array[i].offsetHeight;
				
				var padTop = 0;
				var padBtm = 0;
				var bdTop = 0;
				var bdBtm = 0;
				
				var myStyle = array[i].currentStyle || document.defaultView.getComputedStyle(array[i],"");
				if(myStyle.paddingTop){
					padTop = Number(myStyle.paddingTop.replace("px",""));
				}
				if(myStyle.paddingBottom){
					padBtm = Number(myStyle.paddingBottom.replace("px",""));
				}
				if(myStyle.borderTopWidth){
					if(myStyle.borderTopWidth.match(/[0-9]/) !== null){
						bdTop = Number(myStyle.borderTopWidth.replace("px",""));
					}else{
						bdTop = 0;
					}
				}
				if(myStyle.borderBottomWidth){
					if(myStyle.borderBottomWidth.match(/[0-9]/) !== null){
						bdBtm = Number(myStyle.borderBottomWidth.replace("px",""));
					}else{
						bdBtm = 0;
					}
				}
	
				var totalHeight = (gotHeight - padTop - padBtm - bdTop - bdBtm);
	
				collect.push(totalHeight);
			}
			return collect;
		}
		
		function setChecker(){
	
			if(!document.getElementById('FHChecker')){
				var container = document.createElement("div");
				var text = document.createTextNode("&nbsp;"); 
				
				container.setAttribute('id','FHChecker');
				
				container.appendChild(text);
				
				container.style.position = 'absolute';
				container.style.left = '-9999px';
				container.style.top = 0;
				
				document.body.appendChild(container);
			}
			
			var checker = document.getElementById('FHChecker');
					
			var def_height = checker.offsetHeight;
			
			setInterval(function(){
								 
				var i,j;
				
				if(def_height !== checker.offsetHeight){
					if(!groupBy){
						for(i=0 ; i < childs_array01.length ; i++){
							setNormal(childs_array01[i]);
						}
						
					}else{
						for(j=0 ; j < childs_array02.length ; j++){
							setGroup(childs_array02[j]);
						}
					}
					def_height = checker.offsetHeight;
				}
						
			},options.speed);			
		}
		
		function setHeight(elm,point){	
			var i ;
			for(i=0 ; i < elm.length ; i++){
				elm[i].style.height = point + 'px';
			}
		}
		
		function setNormal(childs){
			heights = getHeight(childs);
			heights.sort(sortFnc);
			setHeight(childs,heights[0]);
		}
		
		function setChilds(childs){
			heights = getHeight(elements);
			heights.sort(sortFnc);
			setHeight(elements,heights[0]);
		}
		
		function setGroup(childs){
			var total = 0;
			var counter;
			
			var quotient = Math.floor(childs.length/options.group);
			var remainder = childs.length%options.group;
			
			var i;
			for(i=0 ; i < quotient + remainder ; i++){
				
				var elms = [];
				
				if(total + options.group > childs.length){
					counter = childs.length;
				}else{
					counter = total + options.group;
				}
				
				var j;
				for(j=total ; j < counter ; j++){
					elms.push(childs[j]);	
				}
				
				total = total + options.group;
				
				heights = getHeight(elms);
				heights.sort(sortFnc);
				setHeight(elms,heights[0]);
			}
		}
		
		///////////////////////////////////////////////
		var elements = getClass(options.classname);
		var heights;
		
		if(options.settype ===  'type01'){
			setNormal(elements);
		}else if(options.settype ===  'type02'){
			
			var groupBy,
				 childs_array01 = [],
				 childs_array02 = [];
			
			var i;
			for(i=0 ; i < elements.length ; i++){
				var childs = getChild(elements[i]);
				childs_array01.push(childs);
			
				if(options.group === undefined){
					groupBy = false;			
				}else if(options.group !== undefined){
					groupBy = true;
				}
				
				if(!groupBy){
					setNormal(childs);
					childs_array01.push(childs);
				}else{
					setGroup(childs);
					childs_array02.push(childs);
				}
			}
		}
	
		setChecker();
		
		///////////////////////////////////////////////
	};
};

KF.funcs(KF);

$(document).ready(function(){	
	
	KF.fitHeight({
		classname : 'hl_parent',
		speed : 1000,
		settype : 'type02'
	});
	KF.fitHeight({
		classname : 'hl_parent01',
		speed : 1000,
		settype : 'type02'
	});
	KF.fitHeight({
		classname : 'hl_parent02',
		speed : 1000,
		settype : 'type02'
	});
	KF.fitHeight({
		classname : 'hl_parent03',
		speed : 1000,
		settype : 'type02'
	});
	KF.fitHeight({
		classname : 'hl_parent04',
		speed : 1000,
		settype : 'type02'
	});
	KF.fitHeight({
		classname : 'hl_parent05',
		speed : 1000,
		settype : 'type02'
	});	
	KF.fitHeight({
		classname : 'hl_parent06',
		speed : 1000,
		settype : 'type02'
	});	
	KF.fitHeight({
		classname : 'hl_clist',
		speed : 1000,
		settype : 'type02'
	});


	KF.fitHeight({
		classname : 'hl_fitter',
		speed : 1000,
		settype : 'type01'
	});
	KF.fitHeight({
		classname : 'hl_fitter01',
		speed : 1000,
		settype : 'type01'
	});
	KF.fitHeight({
		classname : 'hl_fitter02',
		speed : 1000,
		settype : 'type01'
	});
	KF.fitHeight({
		classname : 'hl_fitter03',
		speed : 1000,
		settype : 'type01'
	});
	KF.fitHeight({
		classname : 'hl_fitter04',
		speed : 1000,
		settype : 'type01'
	});
	KF.fitHeight({
		classname : 'hl_fitter05',
		speed : 1000,
		settype : 'type01'
	});
	KF.fitHeight({
		classname : 'hl_fitter06',
		speed : 1000,
		settype : 'type01'
	});
	KF.fitHeight({
		classname : 'hl_fitter07',
		speed : 1000,
		settype : 'type01'
	});
	KF.fitHeight({
		classname : 'hl_fitter08',
		speed : 1000,
		settype : 'type01'
	});
	KF.fitHeight({
		classname : 'hl_fitter09',
		speed : 1000,
		settype : 'type01'
	});
	/*KF.fitHeight({
		classname : 'hl_parent2',
		speed : 1000,
		settype : 'type02',
		group : 3
	});*/
});


