/**---------------------------------
 * 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).hasClass("current")) return;
			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.");
				}
		});
		
//////////////////////////////////////////////追加要素//////////////////////////////////////////////////
		//修正
		$("#item_image_switcher dd img").mouseover(function(){
			if ($(this).attr("src").match(/_off./)){
				$(this).attr("src",$(this).attr("src").replace("_off.", "_on."));
				return;
			}
		});
		
		//修正
		$("#item_image_switcher dd img[class!='current']").mouseout(function(){
			if($(this).hasClass("current")) return;
			if ($(this).attr("src").match(/_on./)){
				$(this).attr("src",$(this).attr("src").replace("_on.", "_off."));
				return;
			}
			if ($(this).attr("src").match(/_current./)){
				$(this).attr("src",$(this).attr("src").replace("_current.", "_current."));
				return;
			}
		});
		
//////////////////////////////////////////////追加要素//////////////////////////////////////////////////
		
		$("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, 'easeOutQuint'); 
			} 
			return false; 
		} 

		$("a.external").bind("click",windowOpen);
		$('a[href^=#]').bind("click",smoothScroll);
		
		if(use_list_marker){
			$(".list_opener > li").each(function(){
				if($(this).find(".onlink").length > 0){
					var $img = $(this).find("a img");
					$img.addClass("current");
					$img.attr("src",$img.attr("src").replace("_off.", "_on."));
				}
		   });
		}else{
			$(".onlink").removeClass("onlink");
		}
		$(".list_opener li:not('.onlink') ul").not(":has('.onlink')").hide();
		$(".list_opener  > li:has('ul:hidden') > a").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 use_list_marker = true;
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 lh = 0, mh =0, nh = 0;
				var le,me,ne;
				var index1 = 0, index2 = 0;
				for(j=0;j<colum;j++){
					le = $(ppL[j]);
					me = le.find(".item_disc");
					ne = le.find(".item_name");
					
					if(mh < me.height()){
						mh = me.height();
						index1 = j;
					}
					if(nh < ne.height()){
						nh = ne.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++){
					le = $(ppL[j]);
					le.find(".item_disc").height(mh);
					le.find(".item_name").height(nh);
					if(lh < le.height()){
						lh = le.height();
					}
				}
				for(j=0;j<colum;j++){
					$(ppL[j]).height(lh);
				}
				cnt = 0;
				ppL =[];
			}
		}
		
		var ppLlen = ppL.length
		if(ppLlen){
			lh  = 0, mh = 0, nh = 0;
			index1 = 0, index2 = 0;
			for(j=0;j<ppLlen;j++){
				le = $(ppL[j]);
				me = le.find(".item_disc");
				ne = le.find(".item_name");			
				if(mh < me.height()){
					mh = me.height();
					index1 = j;
				}
				if(nh < ne.height()){
					nh = ne.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++){
				le = $(ppL[j]);
				le.find(".item_disc").height(mh);
				le.find(".item_name").height(nh);
				if(lh < le.height()){
					lh = le.height();
				}
			}
			for(j=0;j<ppLlen;j++){
				$(ppL[j]).height(lh);
			};
		}
	})(jQuery)
}
