// ==================================================================
//
// $.spec.newsSlide  トップニュースのスライド
// $.spec.imageFade   メインイメージのフェード表示
//
// ==================================================================
(function($) {
	$(function(){
		//$.spec.newsSlide(1,300,4000);
		//$.spec.imageFade(1,4000,"start");
		$.spec.bnrSlideView(1,4000,"start");
		$.spec.LinkImageHightLight();
		//$.spec.formInputCheck();
	});
	$.spec = {
		newsSlide: function(i,s,to)
		{
			if(i != 1)
			{
				clearTimeout(NewsSlide);
				var NewsMax = $("dl#slideNews dd li").length;
				if(NewsMax > 1)
				{
					var NewsAreaTop = $("dl#slideNews dd ul").css("top").replace(/([0-9]+)px/ , "$1")
					var Move = parseInt(NewsAreaTop - $("dl#slideNews dd li").height());
					var addNews = $("dl#slideNews dd ul li#news"+ parseInt(i-1)).html();
					$("dl#slideNews dd ul").append('<li id="news'+ parseInt(NewsMax + 1)+'">'+ addNews +'</li>');
					$("dl#slideNews dd ul").animate(
						{top: Move +"px"},
						s
					);
				}
			}
			if(NewsMax > 1)
			{
				var nextNO = parseInt(++ i);
				NewsSlide = setTimeout("$.spec.newsSlide('"+nextNO+"','"+s+"','"+to+"')",to);
			}
		},
		imageFade: function(i,to,s)
		{
			var imageLength = $(".fadeBox ul li").length;
			if(s != "start")
			{
				clearTimeout(fadeImage);
				$(".fadeBox ol li").removeClass("on");
				$(".fadeBox ul li").fadeOut();
				$(".fadeBox ul li.fade"+ i).fadeIn();
				$(".fadeBox ol li.fade"+ i).addClass("on");
				var nextNO = ++i;
			}
			else
			{
				$(".fadeBox ul li:first-child").css('display' , 'block');
				var nextNO = 2;
			}
			if(imageLength > 1)
			{
				if(i != parseInt(imageLength + 1))
				{
					fadeImage = setTimeout("$.spec.imageFade('"+nextNO+"','"+to+"')",to);
				}
				else
				{
					fadeImage = setTimeout("$.spec.imageFade('1','"+to+"')",to);
				}
			}
		},
		bnrSlideView: function(i,to,s)
		{
			var imageLength = $(".topImage ul li").length;
			var areaHeight = $(".topImage").height();
			if(s == "start")
			{
				var bnrSlideViewBtn = '<ol class="bnrSlideViewBtn">';
				for(var j=1; j<=imageLength; j++)
				{
					if(j == 1)
					{
						bnrSlideViewBtn = bnrSlideViewBtn + '<li class="bnrSlideViewBtn'+ j +'"><a href="javascript:bnrSlideViewBtn('+ j +','+ to +');" class="on">'+ j +'</a></li>';
					}
					else
					{
						bnrSlideViewBtn = bnrSlideViewBtn + '<li class="bnrSlideViewBtn'+ j +'"><a href="javascript:bnrSlideViewBtn('+ j +','+ to +');">'+ j +'</a></li>';
					}
				}
				bnrSlideViewBtn = bnrSlideViewBtn + '</ol>';
				$(".topImage").prepend(bnrSlideViewBtn);
				$(".topImage ol.bnrSlideViewBtn").css({
					'position': 'absolute',
					'z-index': '100',
					'top' : '5px',
					'right'  : '5px'
				});
				$(".topImage ol.bnrSlideViewBtn li").css({
					'margin': '0',
					'list-style': 'none',
					'float': 'left'
				});
				$(".topImage ol.bnrSlideViewBtn li a").css({
					"width": '20px',
					"height": '20px',
					'margin-left': '5px',
					"text-align" : "center",
					"display" : "block",
					'font-size': '120%',
					'font-weight': 'bold',
					'text-decoration': 'none',
					'line-height': '1.5',
					'border' : '1px solid #CCCCCC'
				});
				$(".topImage ul").css({
					'position': 'relative',
					'z-index': '50'
				});
			}	
			if(s != "start")
			{
				clearTimeout(slideAction);
				var imageLength = $(".topImage ul li").length;
				var imageWidth = $(".topImage ul li#image"+ parseInt(i-1)).width();
				var left = parseInt(i-1) * parseInt(-imageWidth);
				$(".topImage ul").animate({ left: left } , 600);
				$(".topImage ol.bnrSlideViewBtn li a").removeClass();
				var n = ++i;
				if(parseInt(i-1) <= imageLength)
				{
					$(".topImage ol.bnrSlideViewBtn li.bnrSlideViewBtn"+parseInt(n-1)+" a").addClass("on");
				}
				else
				{
					$(".topImage ol.bnrSlideViewBtn li.bnrSlideViewBtn1 a").addClass("on");
				}
			}
			else
			{
				var n = 2;	
			}
			if(imageLength > 1)
			{
				if(n > imageLength)
				{
					slideAction = setTimeout("$.spec.bnrSlideView('1','"+to+"')",to);
				}
				else
				{
					slideAction = setTimeout("$.spec.bnrSlideView('"+n+"','"+to+"')",to);
				}
			}
		},
		LinkImageHightLight : function()
		{
			$("a img.hightLight").mouseover(function(){
				$(this).parents("a").css({
					"display": 'block',
					"background": '#000000'
				});
				$(this).animate({opacity: "0.75"},100);
			});
			$("a img.hightLight").mouseout(function(){
				$(this).animate({opacity: "1"},100);
			});
		},
		formInputCheck : function()
		{
			$('form .require').blur(function(){
				var TD = $(this).parents("td");
				var Feild = $(this).parents("tr").find("th").html().replace(/\<br(\ \/)?\>/ , "$1");
				var Color = new Array(4);
				Color['e_ibg'] = '#fbdede';
				Color['e_tbg'] = 'rgb(255, 201, 201)';
				Color['ibg']   = '#FFFFFF';
				Color['tbg']   = '#F4F4F3';
				
				if($(this).val() == "")
				{
					$(this).css({"background-color": Color['e_ibg']});
					TD.css({"background-color": Color['e_tbg']});
					$(this).parents("tr").find("td span.notes").remove();
					TD.append('<span class="notes"><br /><span class="alert">'+Feild+'の入力をお願いします。</span></span>');
				}
				else
				{
					var Error = 0;
					if($(this).hasClass("num"))
					{
						if(is_num($(this).val()) == false)
						{
							Error = 1;	
						}
						$(this).parents("tr").find("td span.notes").remove();
						TD.append('<span class="notes"><br /><span class="alert">'+Feild+'は半角数字で入力してください。</span></span>');
					}
					if($(this).hasClass("halfwidthcharacter"))
					{
						if(is_halfwidthcharacter($(this).val()) == false)
						{
							Error = 1;	
						}
						$(this).parents("tr").find("td span.notes").remove();
						TD.append('<span class="notes"><br /><span class="alert">'+Feild+'は半角英数字で入力してください。</span></span>');
					}
					if(Error == 0)
					{
						$(this).css({"background-color": Color['ibg']});
						TD.find('.require').each(function(){
							if($(this).val() == '')
							{
								Error = 1;
								return false;
							}
						});
					}
					if(Error == 0)
					{
						TD.css({"background-color": Color['tbg']});
						TD.find('.require').css({"background-color": Color['ibg']});
						$(this).parents("tr").find("td span.notes").remove();
					}
					else
					{
						TD.css({"background-color": Color['e_tbg']});
						$(this).css({"background-color": Color['e_ibg']});
					}
				}
			});
			
			function is_num(d)
			{
				if(/[0-9]+/.test(d) == true)
				{
					return true;
				}
				else
				{
					return false;
				}
				
			}
			function is_halfwidthcharacter(d)
			{
				if(/[a-zA-Z0-9]+/.test(d) == true)
				{
					return true;
				}
				else
				{
					return false;
				}
				
			}
			function min_length(d,l)
			{
				if(d.length < l)
				{
					return false;
				}
				else
				{
					return true;
				}
				
			}
			function max_length(d,l)
			{
				if(d.length <= l)
				{
					return true;
				}
				else
				{
					return false;
				}
				
			}
		}
	};
})(jQuery);
function bnrSlideViewBtn(i,to)
{
	clearTimeout(slideAction);
	var imageLength = $(".topImage ul li").length;
	var imageWidth = $(".topImage ul li#image"+ parseInt(i-1)).width();
	var left = parseInt(i-1) * parseInt(-imageWidth);
	$(".topImage ul").animate({ left: left } , 600);
	$(".topImage ol.bnrSlideViewBtn li a").removeClass();
	$(".topImage ol.bnrSlideViewBtn li.bnrSlideViewBtn"+i+" a").addClass("on");
	var n = ++i;
	if(imageLength > 1)
	{
		if(n > imageLength)
		{
			slideAction = setTimeout("$.spec.bnrSlideView('1','"+to+"')",to);
		}
		else
		{
			slideAction = setTimeout("$.spec.bnrSlideView('"+n+"','"+to+"')",to);
		}
	}
}


function swapImage(i , s)
{
	$('img'+i).attr('src', s);
}


// =================================
//
// viewMap (google map表示)
//
// =================================
function viewMap(name,address,gmap,img)
{
	var gmapWidth = "400";
	var gmapheight = "400";
	var gmapTag = '<iframe width="'+ gmapWidth +'" height="'+ gmapheight +'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.jp/maps?f=q&amp;source=s_q&amp;hl=ja&amp;geocode=&amp;q='+ gmap +'&amp;output=embed&amp;iwloc=B"></iframe>';
	var mapImgTag = '<img src="'+ img +'" width="'+ gmapWidth +'" height="'+ gmapheight +'" alt="'+ name +'" />';
	if(gmapTag != '')
	{
		var map = gmapTag;
	}
	else
	{
		var map = mapImgTag;
	}
	$('body').append('<div class="overRay"></div>');
	$('.overRay').css({
		"background":"#000000",
		"opacity":"0.20",
		"width":"100%",
		"height":$('.wrapper').height(),
		"position":"absolute",
		"top":"0",
		"left":"0"
	});
	$(".overRay").fadeIn();
	$('body').append('<div class="gmap">'+ map +'<p><strong>'+ name +'</strong><br />'+ address +'</p><p class="close"><a href="javascript:lightBoxfadeOut('+"'"+'.overRay'+"'"+','+"'"+'.gmap'+"'"+');"><img src="/u/pc/images/common/lightbox-btn-close.gif" width="66" height="22" alt="CLOSE" /></a></p></div>');
	$('.gmap').css({
		"position":"absolute",
		"top":parseInt(($(window).height() - gmapheight)/2 + $(window).scrollTop()) + "px",
		"left":($(window).width() - gmapWidth+parseInt(16))/2 + "px",
		"background":"#FFFFFF",
		"border":"3px solid #666666",
		"z-index":"1001",
		"display":"none",
		"width":gmapWidth + "px",
		"padding":"5px",
		"font-size":"75%"
	});
	$('.gmap p').css({
		"line-height":"1.8"
	});
	$('.gmap p.close').css({
		"text-align":"right"
	});
	$(".gmap").fadeIn();
	$(".overRay").click(function(event){
		lightBoxfadeOut('.overRay','.gmap');
	});
}

function lightBoxfadeOut(bg, box){
	$(bg).fadeOut();
	$(box).fadeOut();
	setTimeout("lightBoxRemove("+ bg +","+ box +")",500);
}
function lightBoxRemove(bg, box){
	$(bg).remove();
	$(box).remove();	
}


