/* -----------------------------------------------*
 *
 * @copyright	(c) luft co.,ltd.
 * @link		http://www.luft.co.jp/
 * @date		2011.12.01
 * @project		Senshu Kiwanis Official Website
 * @author		Shigeki Hosomi
 * 
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * -----------------------------------------------*/

var _loc = "http://www.senshu-kiwanis.com/wp-content/themes/senshu-kiwanis/";
var _tloc = "http://www.senshu-kiwanis.com/";

$(function()
{
	var _winW;
	var _winH;
	
	/* -----------------------------
	 * Resize
	 * ----------------------------- */
	function resizeCheck()
	{
		_winW = parseInt($(window).width());
		_winH = parseInt($(window).height());
		
		if(_winW < 860)
		{
			$("body").addClass('minW');
		}
		else
		{
			$("body").removeClass('minW');
		}
		
		$("#b_next a").css('height',_winH+'px');
		$("#b_prev a").css('height',_winH+'px');
	}
	resizeCheck();
	
	/* -----------------------------
	 * Mobile Check
	 * ----------------------------- */
	function mobileCheck()
	{
		var ua = [
			'iPhone',         // Apple iPhone
			'iPod',           // Apple iPod touch
			'Android',        // 1.5+ Android
			'dream',          // Pre 1.5 Android
			'CUPCAKE',        // 1.5+ Android
			'Windows.Phone',  // Windows Phone
			'blackberry9500', // Storm
			'blackberry9530', // Storm
			'blackberry9520', // Storm v2
			'blackberry9550', // Storm v2
			'blackberry9800', // Torch
			'webOS',          // Palm Pre Experimental
			'incognito',      // Other iPhone browser
			'webmate'         // Other iPhone browser
		];
		var pattern = new RegExp(ua.join('|'), 'i');
		return pattern.test(navigator.userAgent);
	}
	
	function ipadCheck()
	{
		var is_iPad = navigator.userAgent.match(/iPad/i) != null;
		return is_iPad;
	}
	
	
	/* -----------------------------
	 * Hover
	 * ----------------------------- */
	function thumbsHover()
	{
		var $el = $("#thumbs .photo a");
		
		return $el.each(function()
		{
			$el.live('mouseover mouseout', function(e)
			{
				if(e.type == "mouseover")
				{
					$(this).find("img:first").stop(true,false).animate({
						opacity: 0.85
					},{duration:100, easing:"easeOutCubic", queue:false});
					$(this).parent().parent().parent().find(".title a").addClass('lnkhover');
				}
				else if(e.type == "mouseout")
				{
					$(this).find("img:first").stop(true,false).animate({
						opacity: 1
					},{duration:600, easing:"easeOutCubic", queue:false});
					$(this).parent().parent().parent().find(".title a").removeClass('lnkhover');
				}
			});
			
			/*$(this).hover(
			function()
			{
				$(this).find("img:first").stop(true,false).animate({
					opacity: 0.85
				},{duration:100, easing:"easeOutCubic", queue:false});
				$(this).parent().parent().parent().find(".title a").addClass('lnkhover');
			},
			function()
			{
				$(this).find("img:first").stop(true,false).animate({
					opacity: 1
				},{duration:600, easing:"easeOutCubic", queue:false});
				$(this).parent().parent().parent().find(".title a").removeClass('lnkhover');
			});*/
			
		});
	}
	
	/* -----------------------------
	 * ToolTip
	 * ----------------------------- */
	function toolTip()
	{
		var $toolTrigger = $("li.tool a");
	
		if($.browser.msie)	// for IE
		{
		
			$toolTrigger.next("div").find("img").attr({
				src: _loc+'images/common/toolarrow.gif',
				width: 13,
				height: 7
			});
		}
		
		$toolTrigger.hover(
		function(){
			$(this).next("div").css({
				display: 'block',
				opacity: 0,
				bottom: '175px'
			});
			$(this).next("div").stop(true,false).animate({
				opacity: 1,
				bottom: '195px'
			},{duration:600, easing:"easeInOutQuint", queue:false});
		},
		function(){
			$(this).next("div").stop(true,false).animate({
				opacity: 0,
				bottom: '205px'
			},{duration:600, easing:"easeInOutQuint", queue:false, complete:
				function(){
					$(this).css('display','none');
				}
			});
		});
	}
	
	/* -----------------------------
	 * Scroll
	 * ----------------------------- */
	function totopShowHide()
	{
		var scrTop = parseInt($(this).scrollTop());
		
		if (scrTop > 500)
		{
			$("#totop").fadeIn(200);
		}
		else
		{
			$("#totop").fadeOut(200);
		}
	}
	
	/* -----------------------------
	 * Quick Pager
	 * ----------------------------- */
	function quickPager()
	{
		var $b_next = $("#b_next");
		var $b_prev = $("#b_prev");
		var $nextTarget = $("#b_next a");
		var $prevTarget = $("#b_prev a");
		var nextShowRange = ($.browser.msie && $.browser.version < 8) ? '-13px' : '0px';
		var prevHideRange = ($.browser.msie && $.browser.version < 8) ? '-87px' : '-37px';
		
		$nextTarget.css('marginLeft','50px');
		$prevTarget.css('marginLeft',prevHideRange);
		
		if(!ipadCheck())
		{
			setTimeout(quickPagerSet, 800);
		}
		else
		{
			setTimeout(function(){
				$nextTarget.animate({
					marginLeft: '6px'
				},{duration:800, easing:"easeInOutQuint", queue:false});
				
				$prevTarget.animate({
					marginLeft: '7px'
				},{duration:800, easing:"easeInOutQuint", queue:false});
			}, 800);
		}
		
		function quickPagerSet()
		{
			$nextTarget.animate({
				marginLeft: nextShowRange
			},{duration:800, easing:"easeInOutQuint", queue:false, complete:
				function(){
					$(this).animate({
						marginLeft: '50px'
					},{duration:800, easing:"easeInOutQuint", queue:false});
				}
			});
			
			$prevTarget.animate({
				marginLeft: '13px'
			},{duration:800, easing:"easeInOutQuint", queue:false, complete:
				function(){
					$(this).animate({
						marginLeft: prevHideRange
					},{duration:800, easing:"easeInOutQuint", queue:false});
				}
			});
			setTimeout(quickPagerHover, 1000);
		}
		
		function quickPagerHover()
		{
			$b_next.hover(
			function(){
				$nextTarget.stop(true,false).animate({
					marginLeft: nextShowRange
				},{duration:300, easing:"easeOutCubic", queue:false});
			},
			function(){
				$nextTarget.stop(true,false).animate({
					marginLeft: '50px'
				},{duration:500, easing:"easeOutCubic", queue:false});
			});
			
			$b_prev.hover(
			function(){
				$prevTarget.stop(true,false).animate({
					marginLeft: '13px'
				},{duration:300, easing:"easeOutCubic", queue:false});
			},
			function(){
				$prevTarget.stop(true,false).animate({
					marginLeft: prevHideRange
				},{duration:500, easing:"easeOutCubic", queue:false});
			});
		}
	}
	
	/* -----------------------------
	 * Swap Images
	 * ----------------------------- */
	function swapImg()
	{
		var $elTarget = $(".swapImg").find("img");
		var $elTrigger = $(".swapTrigger").find("a");
		var total = $elTrigger.length;
		var num = 0;
		var cnt = 0;
		
		return $elTrigger.each(function()
		{
			if(total>1)
			{
				var $el_src = $(this).attr("href");
				
				$(this).addClass(String(cnt));
				cnt++;
				
				$(this).click(function()
				{
					$elTrigger.filter(':eq('+num+')').css('opacity',1);
					$(this).css('opacity',0.3);
					num = $(this).attr('class');
					$elTarget.stop(true,false).animate({
						opacity: 0
					},{duration:250, easing:"easeInOutQuint", queue:false, complete:
						function(){
							$(this).attr('src', $el_src).animate({
								opacity: 1
							},{duration:600, easing:"easeInOutQuint", queue:false});
						}
					});
					return false;
				});
			}
			else
			{
				$(this).click(function(){
					return false;
				});
			}
		}).filter(':eq(0)').css('opacity',0.5);
	}
	
	/* -----------------------------
	 * Cookie Check
	 * ----------------------------- */
	function cookieCheck()
	{
		//var cookieEsse = true;
		
		if($.cookie('_user'))
		{
			$.cookie('_user', '', {expires:-1});
			$.cookie('_user', 'ac_user', {expires:3});
			Init();
		}
		else
		{
			$("#wrapper").css({'display':'none'});
			$.cookie('_user', 'ac_user', {expires:3});
			introShow();
			//cookieEsse = false;
		}
		//return cookieEsse;
	}
	
	/* -----------------------------
	 * Intro Show
	 * ----------------------------- */
	function introShow()
	{
		var imglist = [
				_loc+"images/intro/0.png",
				_loc+"images/intro/1.png",
				_loc+"images/intro/2.png",
				_loc+"images/intro/3.png",
				_loc+"images/intro/4.png",
				_loc+"images/intro/5.png",
				_loc+"images/intro/6.png",
				_loc+"images/intro/7.png",
				_loc+"images/intro/8.png",
				_loc+"images/intro/9.png",
				_loc+"images/intro/10.png",
				_loc+"images/intro/11.png",
				_loc+"images/intro/12.png",
				_loc+"images/intro/13.png",
				_loc+"images/intro/14.png",
				_loc+"images/intro/15.png",
				_loc+"images/intro/16.png",
				_loc+"images/intro/17.png",
				_loc+"images/intro/18.png",
				_loc+"images/intro/19.png",
				_loc+"images/intro/20.png",
				_loc+"images/intro/21.png",
				_loc+"images/intro/22.png",
				_loc+"images/intro/23.png",
				_loc+"images/intro/24.png",
				_loc+"images/intro/25.png",
				_loc+"images/intro/26.png",
				_loc+"images/intro/27.png",
				_loc+"images/intro/28.png",
				_loc+"images/intro/29.png",
				_loc+"images/intro/30.png",
				_loc+"images/intro/31.png",
				_loc+"images/intro/32.png",
				_loc+"images/intro/33.png",
				_loc+"images/intro/34.png",
				_loc+"images/intro/35.png",
				_loc+"images/intro/36.png",
				_loc+"images/intro/37.png",
				_loc+"images/intro/38.png",
				_loc+"images/intro/39.png",
				_loc+"images/intro/40.png",
				_loc+"images/intro/41.png",
				_loc+"images/intro/42.png",
				_loc+"images/intro/43.png",
				_loc+"images/intro/44.png",
				_loc+"images/intro/45.png",
				_loc+"images/intro/46.png"
				];
		
		var imgdata;
		var introCanvas = $('<div>', {
							id: "introCanvas",
							css:{
    							position: 'relative',
								width: '100%',
								height: parseInt($(window).height())-26+'px',
								backgroundImage: 'url('+_loc+'images/common/loading2.gif)',
								backgroundPosition: 'center center',
								backgroundRepeat: 'no-repeat'
							}
						});
		
		/* Loader
		 * ----------------------------- */
		function loader(i)
		{
			imgdata = $('<img>', {
    					src: imglist[i],
    					css:{
    						position: 'absolute',
    						top: '50%',
    						left: '50%',
    						marginTop: '-285px',
							marginLeft: '-413px',
    						opacity: 0
        					}
        				})
						.attr({
							width: 826,
							height: 570
						});
			imgdata.load(function()
			{
    			introCanvas.append(imgdata);
				introCanvas.trigger('imgloader',imgload(i+1));
			})
			.error(function(e)
			{
    			e.preventDefault();
				alert("Loading Error: 画像ファイルの読み込みに失敗しました。");
				
				introCanvas.fadeOut('slow', function()
				{
					$(window).unbind('resize', introCanvasResize);
					e.preventDefault();
					$(this).remove();
					
					$("#wrapper").fadeIn('slow',function(){
						Init();
					});
				});
			});
		}
		
		/* Loading
		 * ----------------------------- */
		function imgload(i)
		{
        	if(imglist[i])
			{
				loader(i);
			}
			else
			{
				loadComplete();
			}
		}
		
		/* Load Complete
		 * ----------------------------- */
		var timerEnd;
		 
		function loadComplete()
		{
			var cnt  = 0;
			
    		introCanvas.find('img:eq('+cnt+')').css({
				marginTop: '-265px'
			}).animate({
				marginTop: '-285px',
				opacity: 1
			},{duration:1800, easing:"easeInOutCubic", queue:false, complete:function()
    			{
        			introCanvas.css({
    					backgroundImage: '',
    					backgroundPosition: '',
    					backgroundRepeat: ''
        			});
        			setTimeout(loopImg, 1500);
    			}
			});
    		
    		function loopImg()
    		{
    			cnt++;
				
    			if(cnt < 44)
    			{
    				introCanvas.find('img:eq('+(cnt-1)+')').animate({
        				opacity: 0
        			},{duration:25, easing:"easeInCubic", queue:false});
            		introCanvas.find('img:eq('+cnt+')').animate({
        				opacity: 1
        			},{duration:25, easing:"easeOutCubic", queue:false, complete:loopImg});
    			}
    			else
    			{
					setTimeout(function()
					{
						introCanvas.find('img:eq('+(cnt-1)+')').animate({
							opacity: 0
						},{duration:3000, easing:"easeInCubic", queue:false});
						introCanvas.find('img:eq('+cnt+')').animate({
							opacity: 1
						},{duration:3000, easing:"easeOutCubic", queue:false, complete:enterShow});
					}, 1000);
    			}
				
				function enterShow()
				{
					introCanvas.find('img:eq(44)').animate({
						opacity: 0
					},{duration:3000, easing:"easeInCubic", queue:false});
					introCanvas.find('img:eq(45)').animate({
						opacity: 1
					},{duration:3000, easing:"easeOutCubic", queue:false, complete:loopEnd});
				}
				
				function loopEnd()
				{
					introCanvas.find('img:eq(45)').animate({
						opacity: 0
					},{duration:800, easing:"easeInCubic", queue:false});
					introCanvas.find('img:eq(46)').animate({
						opacity: 1
					},{duration:800, easing:"easeOutCubic", queue:false, complete:function()
						{
							introCanvas.css({
								cursor: 'pointer'
							});
							introCanvas.bind('click', mainShow);
							
							/* Main Show */
							timerEnd = setTimeout(function()
							{
								introCanvas.click();
							},4000);
						}
					});
				}
			}
		}
		
		/* Show
		 * ----------------------------- */
		function mainShow(e)
		{
			clearTimeout(timerEnd);
			introCanvas.unbind('click', mainShow);
			
			$(this).fadeOut('slow', function()
			{
				e.preventDefault();
				$(window).unbind('resize', introCanvasResize);
				$(this).remove();
				
				$("#wrapper").fadeIn('slow',function(){
					Init();
				});
			});
		}
		
		/* Resize
		 * ----------------------------- */
		function introCanvasResize()
		{
			var wH = parseInt($(window).height())-26;
			
			if(wH < 620)
			{
				$("#introCanvas").css('height', '620px');
			}
			else
			{
				$("#introCanvas").css('height', wH+'px');
			}
		}
		
		/* Routine
		 * ----------------------------- */
		$("body").append(introCanvas);
		$(window).bind('resize', introCanvasResize);
		introCanvas.trigger('imgloader', imgload(0));
	}
	
	/* -----------------------------
	 * Google Maps API Call
	 * ----------------------------- */
	function gmapCall()
	{
		return $("#map_canvas1").each(function()
		{
			if(mobileCheck())	// for SmartPhone
			{
				$("#map_canvas1,#map_canvas2").css('height','420px');
			}
			
			setGmaps();
		});
	}
	
	/* -----------------------------
	 * EqualHeights Call
	 * ----------------------------- */
	function eqHeightCall()
	{
		return $(".commentlist").each(function()
		{
			$(".commentlist").equalHeights();
		});
	}
	
	/* -----------------------------
	 * ScrollLoader Call
	 * ----------------------------- */
	function scrollLoaderCall()
	{
		return $("body#top").each(function()
		{
			ScrollLoader();
		});
	}
	
	/* -----------------------------
	 * Init
	 * ----------------------------- */
	function Init()
	{
		if(!ipadCheck())	// not:iPad
		{
			thumbsHover();
			toolTip();
		}
		
		resizeCheck();
		quickPager();
		gmapCall();
		eqHeightCall();
		scrollLoaderCall();
		$(window).bind('scroll', totopShowHide);
		$.com(".scroll").PageScroll({speed:1600, easing:"easeInOutQuint"});
	}
	
	/* -----------------------------
	 * Main Routine
	 * ----------------------------- */
	$.com(".inputHint").InputHint({foucsColor:"#333333", attribute:"title"});
	$(window).bind('resize', resizeCheck);
	$("#totop").hide();
	swapImg();
	
	if(mobileCheck())	// for SmartPhone
	{
		$("#b_next").hide();
		$("#b_prev").hide();
		gmapCall();
		eqHeightCall();
		scrollLoaderCall();
	}
	else if($.browser.msie && $.browser.version < 7)	// for IE6
	{
		$("#b_next").hide();
		$("#b_prev").hide();
		thumbsHover();
		toolTip();
		gmapCall();
		eqHeightCall();
		scrollLoaderCall();
	}
	else
	{
		if($.support.opacity)	// for not:IE8 under
		{
			cookieCheck();
		}
		else	// for IE7&8
		{
			Init();
		}
	}
	
});
