/**
 *
 * @copyright	(c) luft co.,ltd.
 * @link	http://www.luft.co.jp/
 * @date	2011.08.11
 * @author	Shigeki Hosomi
 * @version	1.1.2
 * @required	jQuery 1.6.x or newer.
 * 
 * @update	2011.08.19
 * 		2011.11.17	
 * 
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 */

;(function($)
{
	var plugname = 'com';
	$[plugname] = jQuery.sub();
	
	/* -----------------------------
	 * RollOver
	 * ----------------------------- */
	$[plugname].fn.RollOver = function()
	{
		return this.each(function()
		{
			var $el = $(this);
			var $el_scr = $el.attr("src");
			var preload = new Image();
			var scrRep = $el_scr.replace(/^(.+)_off(\.[a-z]+)$/, "$1_on$2");
			preload.src = scrRep;
			
			$el.hover(function()
			{
				$el.attr('src', $el_scr.replace(/^(.+)_off(\.[a-z]+)$/, "$1_on$2"));
			},
			function()
			{
				$el.attr('src',$el_scr.replace(/^(.+)_on(\.[a-z]+)$/, "$1_off$2"));
			});
		});
	};
	
	/* -----------------------------
	 * PageScroll
	 * ----------------------------- */
	$[plugname].fn.PageScroll = function(options)
	{
		options = $.extend({
			speed: 'slow',
			easing: 'swing',
			arrivalY: 0,
			arrivalX: 0,
			axisX: false  /* iPhoneの挙動があやしい? PCならOK! */
		}, options);
		
		return this.each(function()
		{
			var $body = $("html,body");
			var $doc = $(document);
			var $el = $(this);
			var el_href = $(this.hash);
			
			$el.click(function(event)
			{
				var docSize = {
					w: $doc.width(),
					h: $doc.height()
				};
				var targetSize = {
					w: $(el_href).get(0).offsetWidth,
					h: $(el_href).get(0).offsetHeight
				};
				var targetPos = {
					x: parseInt($(el_href).offset().left + options.arrivalX),
					y: parseInt($(el_href).offset().top + options.arrivalY)
				};
				
				targetPos.x = ((docSize.w - targetSize.w) < targetPos.x && targetPos.x < docSize.w) ? docSize.w - targetSize.w : targetPos.x;
				targetPos.y = ((docSize.h - targetSize.h) < targetPos.y && targetPos.y < docSize.h) ? docSize.h - targetSize.h : targetPos.y;
				
				if(!options.axisX)
				{
					$body.stop().animate({scrollTop: targetPos.y},options.speed,options.easing,false);
				}
				else
				{
					$body.stop().animate({scrollTop: targetPos.y,scrollLeft: targetPos.x},options.speed,options.easing,false);
				}
				scrollCancel();
				return false;
			});
						  
			function onKeyPress(e)
			{
				if(e.keyCode == 38 || e.keyCode == 40)
				{
					scrollStop();
				}				  
			}
			
			function scrollStop()
			{
				$body.stop();
				scrollRemove();
			}
			
			function scrollCancel()
			{
				if(document.addEventListener)
				{
					if($.browser.mozilla)
					{
						document.addEventListener('DOMMouseScroll', scrollStop, false);
					}
					else
					{
						document.addEventListener('mousewheel', scrollStop, false);
						document.addEventListener('touchend', scrollStop, false);
					}
				}
				else if(document.attachEvent)
				{
					document.attachEvent('onmousewheel', scrollStop);
				}
				$doc.bind(($.browser.mozilla ? 'keypress' : 'keydown'), onKeyPress);
			}
			
			function scrollRemove()
			{
				if(document.removeEventListener)
				{
					if($.browser.mozilla)
					{
						document.removeEventListener('DOMMouseScroll', scrollStop, false);
					}
					else
					{
						document.removeEventListener('mousewheel', scrollStop, false);
						document.removeEventListener('touchend', scrollStop, false);
					}
				}
				else if(document.attachEvent)
				{
					document.detachEvent('onmousewheel', scrollStop);
				}
				$doc.unbind(($.browser.mozilla ? 'keypress' : 'keydown'));
			}
		});
	};
	
	/* -----------------------------
	 * Stripes
	 * ----------------------------- */
	$[plugname].fn.Stripes = function(options)
	{
		options = $.extend({
			isEvenOdd: 'odd',
			addClass: 'odd',
			isTableOption: 'tr'
		}, options);
		
		return this.each(function()
		{
			var $el = $(this);
			var el_tagName = $el.get(0).tagName;
			
			if(el_tagName == 'TABLE')
			{
				$el.find(options.isTableOption).filter(':'+options.isEvenOdd).addClass(options.addClass);
			}
			else
			{
				$el.contents().filter(':'+options.isEvenOdd).addClass(options.addClass);
			}
		});
	};
	
	/* -----------------------------
	 * InputHint
	 * ----------------------------- */
	$[plugname].fn.InputHint = function(options)
	{
		options = $.extend({
			defaultColor: '#bbb',
			foucsColor: '#666',
			attribute: 'title',
			addFocusClass: 'inputFocus'
		}, options);
		
		return this.each(function()
		{
			var $el = $(this);
			var el_hint = $el.attr(options.attribute);
			
			if($el.val() == "")
			{
				$el.val(el_hint).css('color', options.defaultColor);
			}
			else if($el.val() == el_hint)
			{
				$el.css('color', options.defaultColor);
			}
			
			$el
			.focus(function()
			{
				if($el.val() == el_hint)
				{
					$el.val("").css('color', options.foucsColor);
				}
				$el.addClass(options.addFocusClass);
			})
			.blur(function()
			{
				if($el.val() == "")
				{
					$el.val(el_hint).css('color', options.defaultColor);
				}
				else if($el.val() == !el_hint)
				{
					$el.css('color', options.foucsColor);
				}
				$el.removeClass(options.addFocusClass);
			});
		});
	};
	
	/* -----------------------------
	 * FocusAll
	 * ----------------------------- */
	$[plugname].fn.FocusAll = function()
	{
		return this.each(function()
		{
			var $el = $(this);
			
			$el
			.focus(function()
			{
				$el.select();
			})
			.click(function()
			{
				$el.select();
				return false;
			});
		});
	};
	
	/* -----------------------------
	 * CheckAll
	 * ----------------------------- */
	$[plugname].fn.CheckAll = function(options)
	{
		options = $.extend({
			customTrigger: 'all'
		}, options);
		
		return this.each(function()
		{
			var $el = $(this);
			
			$el.find('.'+options.customTrigger).click(function()
			{
				if(this.checked)
				{
					$el.find("input").prop('checked',true);
				}
				else
				{
					$el.find("input").prop('checked',false);
				}
			});
		});
	};
	
	/* -----------------------------
	 * PopupWindow
	 * ----------------------------- */
	$[plugname].fn.PopupWindow = function(options)
	{
		options = $.extend({
			width: 450,
			height: 380,
			scrollbars: 'yes'
		}, options);
		
		return this.each(function()
		{
			var $el = $(this);
			var el_href = $el.attr("href");
			
			$el.click(function()
			{
				window.open(el_href,'createWindow','width='+options.width+',height='+options.height+'toolbar=no,menubar=no,location=no,status=no,scrollbars='+options.scrollbars+',resizable=no');
				return false;
			});
		});
	};
	
	/* -----------------------------
	 * PopupFullScreen
	 * ----------------------------- */
	$[plugname].fn.PopupFullScreen = function()
	{
		return this.each(function()
		{
			var $el = $(this);
			var el_href = $el.attr("href");
			
			$el.click(function()
			{
				var createWin = window.open(el_href,'createWindow','toolbar=no,menubar=no,location=no,status=no,scrollbars=no,resizable=no,fullscreen=yes');
				createWin.moveTo(0,0);
				createWin.resizeTo(screen.availWidth, screen.availHeight);
				createWin.focus();
				return false;
			});
		});
	};
	
})(jQuery);

$(function()
{
	$.com("a").find("img[src*='_off.']").RollOver();
});

	/* -----------------------------
	 * faq
	 * ----------------------------- */
$(function(){
	$("#faq dl dd").hide();
	$("#faq dl dt a").click(function(){
		if ( $(this).parent().parent().hasClass("opened") ) {
		$("#faq dl.opened dd").slideUp();
		$("#faq dl.opened").removeClass("opened");
		return false;
		}
		$("#faq dl dd").stop();
		$("#faq dl dt a").removeClass("selected");
		$("#faq dl").removeClass("opened");
		$(this).addClass("selected");
		$(this).parent().parent().addClass("opened");
		$("#faq dl dd").slideUp();
		$("#faq dl.opened dd").css("height","auto");
		$("#faq dl.opened dd").slideDown();
		return false;
	});
});
