/**---------------------------------
 * common.js
 * 
 * ...
 * author	: takaaki koyama
 * url 		: http://www.sph62.net
 * mail		: sph62.net@gmail.com
 *
 * @use jQuery 1.2.6 later
 ---------------------------------*/
;(function($){
	$(document).ready(function(){
		//rollover	
		// switching image xxx_off.xxx -> xxx_on.xxx
		// if image name is bnr_xxxx or btn_xxx which don't has neme _off , _on
		// fade effect on mouse over.
		$("a img[src*='_on']").addClass("current");
		
		$("a img,:image").mouseover(function(){
			if ($(this).attr("src").match(/_off./)){
				$(this).attr("src",$(this).attr("src").replace("_off.", "_on."));
				return;
			}
		});
	
		$("a img[class!='current'],:image").mouseout(function(){
			if ($(this).attr("src").match(/_on./)){
				$(this).attr("src",$(this).attr("src").replace("_on.", "_off."));
				return;
			}
		});
		
		//preload images
		var images = [];
		$("a img,:image").each(function(index){
				if($(this).attr("src").match(/_off./)){
					 images[index]= new Image();
					 images[index].src = $(this).attr("src").replace("_on.", "_off.");
				}
		});
		
		//CSS3 selector 
//		//last-child
//		$("li:last-child, dl > *:last-child, table > *:last-child, tr > *:last-child").addClass("last-child");
//		
//		//nth-child
//		$("ol,ul").each(function(){
//				$(this).children("li").each(function(index){
//					$(this).addClass("n"+(index+1)+"th-child");
//				});
//		});
//		$("dl").each(function(){
//				$(this).children("dt").each(function(index){
//					$(this).addClass("n"+(index+1)+"th-child");
//				});
//				$(this).children("dd").each(function(index){
//					$(this).addClass("n"+(index+1)+"th-child");
//				});
//		});
//		//:even, :odd
//		$("li:even,dt:even,dd:even,tr:even,tr > *:even").addClass("even");
//		$("li:odd,dt:odd,dd:odd,td:odd,td > *:odd").addClass("odd");

		$("input,textarea")
			.focus(function(){$(this).addClass("focus")})
			.blur(function(){$(this).removeClass("focus")})
			.hover(function(){$(this).addClass("hover")},function(){$(this).removeClass("hover")});

		
		// popup
		// class="popup400x600" -> window.open(this.href,"popup","width=400,height=600,...)
		$("a[class^='popup']").click(function(){
			if($.browser.safari ){
				window.open(this.href,"_blank");
				return false;
			}
			var className = $(this).attr("class").match(/^popup([0-9]{1,})x([0-9]{1,})/) ;	
			var width = RegExp.$1;
			var height = RegExp.$2;
			var state = "";
			var notHasSize = "yes"
			if(width!=null && height !=null){
				state += "width="+width+",height="+height+",";
				notHasSize = "no"
			}
			state += "location="+notHasSize+",toolbar="+notHasSize+",directories="+notHasSize+",";
			state += "status=yes,menubar=no,scrollbars=yes,resizable=yes,alwaysRaised=yes";
			window.name = document.domain + "root";
			window.open(this.href,"popup"+(new Date()).getTime().toString(),state);
			return false;
		});
		// open in popup parent window.
		// add class="openParentWin" on a-tag in a popup window.
		$("a.openParentWin").click(function(){
			window.open(this.href,document.domain + "root");
			return false;
		});
		
		// target _blank auto add
		var domains = [document.domain,"groupsitedomains"];
		var domain_selector = ""
		var left_str= ":not([href^=http://";
		var left_str_https= ":not([href^=https://";
		var right_str = "])";
		domain_selector = left_str+domains.join(right_str+left_str)+right_str;
		domain_selector+= left_str_https+domains.join(right_str+left_str_https)+right_str;
		$("a[href^=http]:not([class^=popup])"+domain_selector+", area[href^=http]:not([class^=popup])"+domain_selector)
		.addClass("external");
		
		//if has class .extenal -> open _blank window
		function windowOpen(){
			window.open(this.href,"_blank");
			return false;
		}
							
		//Smooth Scroll
		function smoothScroll() { 
			var target = $(this.hash); 
			if(target.size()) { 
				var top = target.offset().top;
				$($.browser.safari ? 'body' : 'html').animate({scrollTop:top}, 800, 'swing'); 
			} 
			return false; 
		} 

		$("a.external").bind("click",windowOpen);
		$('a[href^=#]').bind("click",smoothScroll);
		
		
		$(".list_opener ul").hide();
		$(".list_opener .has_list > a").unbind("click",smoothScroll).click(function(){
			$(this).nextAll("ul").slideToggle();
			return false;
		})
		
		if($("#item_list").length >0){
			if($(".index").length > 0){
				boxflat("#item_list li",3);
			}else if($(".sub").length > 0){
				boxflat("#item_list li",4);
			}
		}
		
	});
})(jQuery);
		
var boxflat = function (target,colum){ 
	(function($){
		var pp = $(target);
		var len = pp.length;
		if(!len) return;
		
		var ppL = [];
		var cnt = 0;
		for(var i= 0; i< len;i++){
			ppL.push(pp[i]);
			cnt++
			if(cnt == colum){
				var mh =0, nh = 0;
				var index1 = 0, index2 = 0;
				for(j=0;j<colum;j++){
					if(mh < $(ppL[j]).find(".item_disc").height()){
						mh = $(ppL[j]).find(".item_disc").height();
						index1 = j;
					}
					if(nh < $(ppL[j]).find(".item_name").height()){
						nh = $(ppL[j]).find(".item_name").height();
						index2 = j;
					}
				}
				if(index1!=index2){
					mh = $(ppL[index1]).find(".item_disc").height() - $(ppL[index1]).find(".item_name").height(); 
					mh += nh;
				}
				for(j=0;j<colum;j++){
					$(ppL[j]).find(".item_disc").height(mh);
					$(ppL[j]).find(".item_name").height(nh);
				}
				cnt = 0;
				ppL =[];
			}
		}
		
		var ppLlen = ppL.length
		if(ppLlen){
			mh = 0, nh = 0;
			index1 = 0, index2 = 0;
			for(j=0;j<ppLlen;j++){
				if(mh < $(ppL[j]).find(".item_disc").height()){
					mh = $(ppL[j]).find(".item_disc").height();
					index1 = j;
				}
				if(nh < $(ppL[j]).find(".item_name").height()){
					nh = $(ppL[j]).find(".item_name").height();
					index2 = j;
				}
			}
			
			if(index1!=index2){
				mh = $(ppL[index1]).find(".item_disc").height() - $(ppL[index1]).find(".item_name").height(); 
				mh += nh;
			}
			
			for(j=0;j<ppLlen;j++){
				$(ppL[j]).find(".item_disc").height(mh);
				$(ppL[j]).find(".item_name").height(nh);
			}
		}
	})(jQuery)
}
