/* =======================================

	Common Javascript

 -----------------------------------------
	INDEX
 -----------------------------------------
 	1.Mouse Over
	
	2.Cookie Script
	
	3.Chenge Font Size
	
	4.List Color
	
	5.Smooth Scroll

========================================== */


/* 1.Mouse Over
========================================== */
$(function() {
  var image_cache = new Object();
  $(".swap").each(function(i) {
    var imgsrc = this.src;
    var dot = this.src.lastIndexOf('.');
    var imgsrc_on = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
    image_cache[this.src] = new Image();
    image_cache[this.src].src = imgsrc_on;
    $(this).hover(
      function() { this.src = imgsrc_on; },
      function() { this.src = imgsrc; });
  });
});


/* 2.Cookie Script
==========================================
http://www.quirksmode.org/js/cookies.html
========================================== */
function createCookie(name,value,days){
   if (days){
   var date = new Date();
   date.setTime(date.getTime()+(days*24*60*60*1000));
   var expires = "; expires="+date.toGMTString();
   }
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name){
   var nameEQ = name + "=";
   var ca = document.cookie.split(';');
   for(var i=0;i < ca.length;i++)
   {
   var c = ca[i];
   while (c.charAt(0)==' ') c = c.substring(1,c.length);
   if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   }
   return null;
}


/* 3.Chenge Font Size
========================================== */
$(function(){
	// サイズ切り替えがある場合のみ実行
	if($('dl#changeFontSize').size()){
		$('dl#changeFontSize dd').css('cursor','pointer');
		
		var currentstyle = readCookie('supplesFontStyle');
		if (currentstyle){
			switchFont(currentstyle);
		}else{
			switchFont("changeS");
		};
			
		$(".sizeL").click(function(){
			switchFont("changeL");
			return false;
		});
		
		$(".sizeM").click(function(){
			switchFont("changeM");
			return false;
		});
		
		$(".sizeS").click(function(){
			switchFont("changeS");
			return false;
		});
	}
});

function switchFont(className){
   $('#wrapper').removeAttr('class').addClass(className);
   $('#changeFontSize').removeAttr('class').addClass(className);
   createCookie('supplesFontStyle', className, 365);
};


/* 4.List Color
========================================== */
$(function(){
	$('.review li:odd').addClass('odd');
	$('.review li:even').addClass('even');
	$('.alternate01 .homeBox:odd').addClass('odd');
	$('.alternate01 .homeBox:even').addClass('even');
	$('.alternate02 .homeBox:odd').addClass('odd');
	$('.alternate02 .homeBox:even').addClass('even');
	$('.commodity .borderBox03:odd').addClass('odd');
	$('.commodity .borderBox03:even').addClass('even');
	$('.review .borderBox03:odd').addClass('odd');
	$('.review .borderBox03:even').addClass('even');
	$('.arrowList li:odd').addClass('odd');
	$('.arrowList li:even').addClass('even');
	$('table.listupTable tr:odd').addClass('odd');
});


/* 5.Smooth Scroll
========================================== */
/**
 * jQuery.ScrollTo
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 2/19/2008
 *
 * @projectDescription Easy element scrolling using jQuery.
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 * Tested with jQuery 1.2.1. On FF 2.0.0.11, IE 6, Opera 9.22 and Safari 3 beta. on Windows.
 *
 * @author Ariel Flesler
 * @version 1.3.3
 *
 * @id jQuery.scrollTo
 * @id jQuery.fn.scrollTo
 * @param {String, Number, DOMElement, jQuery, Object} target Where to scroll the matched elements.
 *	  The different options for target are:
 *		- A number position (will be applied to all axes).
 *		- A string position ('44', '100px', '+=90', etc ) will be applied to all axes
 *		- A jQuery/DOM element ( logically, child of the element to scroll )
 *		- A string selector, that will be relative to the element to scroll ( 'li:eq(2)', etc )
 *		- A hash { top:x, left:y }, x and y can be any kind of number/string like above.
 * @param {Number} duration The OVERALL length of the animation, this argument can be the settings object instead.
 * @param {Object} settings Hash of settings, optional.
 *	 @option {String} axis Which axis must be scrolled, use 'x', 'y', 'xy' or 'yx'.
 *	 @option {Number} duration The OVERALL length of the animation.
 *	 @option {String} easing The easing method for the animation.
 *	 @option {Boolean} margin If true, the margin of the target element will be deducted from the final position.
 *	 @option {Object, Number} offset Add/deduct from the end position. One number for both axes or { top:x, left:y }.
 *	 @option {Object, Number} over Add/deduct the height/width multiplied by 'over', can be { top:x, left:y } when using both axes.
 *	 @option {Boolean} queue If true, and both axis are given, the 2nd axis will only be animated after the first one ends.
 *	 @option {Function} onAfter Function to be called after the scrolling ends. 
 *	 @option {Function} onAfterFirst If queuing is activated, this function will be called after the first scrolling ends.
 * @return {jQuery} Returns the same jQuery object, for chaining.
 *
 * @example $('div').scrollTo( 340 );
 *
 * @example $('div').scrollTo( '+=340px', { axis:'y' } );
 *
 * @example $('div').scrollTo( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } );
 *
 * @example var second_child = document.getElementById('container').firstChild.nextSibling;
 *			$('#container').scrollTo( second_child, { duration:500, axis:'x', onAfter:function(){
 *				alert('scrolled!!');																   
 *			}});
 *
 * @example $('div').scrollTo( { top: 300, left:'+=200' }, { offset:-20 } );
 *
 * Notes:
 *  - jQuery.scrollTo will make the whole window scroll, it accepts the same arguments as jQuery.fn.scrollTo.
 *	- If you are interested in animated anchor navigation, check http://jquery.com/plugins/project/LocalScroll.
 *	- The options margin, offset and over are ignored, if the target is not a jQuery object or a DOM element.
 *	- The option 'queue' won't be taken into account, if only 1 axis is given.
 */
;(function( $ ){

	var $scrollTo = $.scrollTo = function( target, duration, settings ){
		$scrollTo.window().scrollTo( target, duration, settings );
	};

	$scrollTo.defaults = {
		axis:'y',
		duration:1
	};

	//returns the element that needs to be animated to scroll the window
	$scrollTo.window = function(){
		return $( $.browser.safari ? 'body' : 'html' );
	};

	$.fn.scrollTo = function( target, duration, settings ){
		if( typeof duration == 'object' ){
			settings = duration;
			duration = 0;
		}
		settings = $.extend( {}, $scrollTo.defaults, settings );
		duration = duration || settings.speed || settings.duration;//speed is still recognized for backwards compatibility
		settings.queue = settings.queue && settings.axis.length > 1;//make sure the settings are given right
		if( settings.queue )
			duration /= 2;//let's keep the overall speed, the same.
		settings.offset = both( settings.offset );
		settings.over = both( settings.over );

		return this.each(function(){
			var elem = this, $elem = $(elem),
				t = target, toff, attr = {},
				win = $elem.is('html,body');
			switch( typeof t ){
				case 'number'://will pass the regex
				case 'string':
					if( /^([+-]=)?\d+(px)?$/.test(t) ){
						t = both( t );
						break;//we are done
					}
					t = $(t,this);// relative selector, no break!
				case 'object':
					if( t.is || t.style )//DOM/jQuery
						toff = (t = $(t)).offset();//get the real position of the target 
			}
			$.each( settings.axis.split(''), function( i, axis ){
				var Pos	= axis == 'x' ? 'Left' : 'Top',
					pos = Pos.toLowerCase(),
					key = 'scroll' + Pos,
					act = elem[key],
					Dim = axis == 'x' ? 'Width' : 'Height',
					dim = Dim.toLowerCase();

				if( toff ){//jQuery/DOM
					attr[key] = toff[pos] + ( win ? 0 : act - $elem.offset()[pos] );

					if( settings.margin ){//if it's a dom element, reduce the margin
						attr[key] -= parseInt(t.css('margin'+Pos)) || 0;
						attr[key] -= parseInt(t.css('border'+Pos+'Width')) || 0;
					}
					
					attr[key] += settings.offset[pos] || 0;//add/deduct the offset
					
					if( settings.over[pos] )//scroll to a fraction of its width/height
						attr[key] += t[dim]() * settings.over[pos];
				}else
					attr[key] = t[pos];//remove the unnecesary 'px'

				if( /^\d+$/.test(attr[key]) )//number or 'number'
					attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max(Dim) );//check the limits

				if( !i && settings.queue ){//queueing each axis is required					
					if( act != attr[key] )//don't waste time animating, if there's no need.
						animate( settings.onAfterFirst );//intermediate animation
					delete attr[key];//don't animate this axis again in the next iteration.
				}
			});			
			animate( settings.onAfter );			

			function animate( callback ){
				$elem.animate( attr, duration, settings.easing, callback && function(){
					callback.call(this, target);
				});
			};
			function max( Dim ){
				var el = win ? $.browser.opera ? document.body : document.documentElement : elem;
				return el['scroll'+Dim] - el['client'+Dim];
			};
		});
	};

	function both( val ){
		return typeof val == 'object' ? val : { top:val, left:val };
	};

})( jQuery );

	/* Setting
	------------------------------------------ */
	$(function() {
		// Pagetop
		$('#pageTop a, a.anchor').click(function(){
			var targetID = $(this).attr('href');
			$.scrollTo(targetID, '400');
			return false;
		});
	});


/* 6.Popup & Window Close
========================================== */
$(function(){
	$('a.popup').click(function(){
		window.open($(this).attr('href'),'','width=450,height=400,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1').focus();
		return false;
	});
	$('a.popup02').click(function(){
		window.open($(this).attr('href'),'','width=450,height=260,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1').focus();
		return false;
	});
	
	$('.windowClose').click(function(){
		window.close();
		return false;
	});
});


/* 7.004プルダウン表示切替
========================================== */
$(function(){
	// 初期化　全て非表示に
	$('.otherProduct .compare').css('display','none');
	
	// プルダウン選択時
	$('.otherProduct .select select').change(function(){
		// 選択されたoption全てに同様の処理
		$('.otherProduct .select select option:selected').each(function(){
			// valueの値を取得・セット
			compareDisplayNum = $(this).attr('value');
			
			// 他は隠す
			$('.otherProduct .compare').css('display','none');
		});
		
		// 表示
		$('.otherProduct .compare.'+compareDisplayNum).css('display','block');
	}).change();
});


/* 8.006ボタンで表示非表示
========================================== */
$(function(){
	/* ひとつめ以外
	---------------------------------------- */
	// 初期化 非表示に
	$('.p006Box01:not(:first) .tableBox').css('display','none');
	
	$('.p006Box01:not(:first) p.breakdown a').toggle(
		function(){ // 一回目ボタンクリック時
			p006this = $(this);
			p006funcShow(); // 表示
		},
		
		function(){ // 二回目ボタンクリック時
			p006this = $(this);
			p006funcHide(); // 隠す
		}
	);
	
	/* ひとつめ
	---------------------------------------- */
	// ボタンを[非表示に]
	$('.p006Box01:first p.breakdown a img:eq(0)').css('display','none');
	$('.p006Box01:first p.breakdown a img:eq(1)').css('display','block');
	
	// クリック時
	$('.p006Box01:first p.breakdown a').toggle(
		function(){ // 一回目ボタンクリック時
			p006this = $(this);
			p006funcHide(); // 隠す
		},
		
		function(){ // 二回目ボタンクリック時
			p006this = $(this);
			p006funcShow(); // 表示
		}
	);
	
	/* 表示用function
	---------------------------------------- */
	function p006funcShow(){
		// 表示する
		p006this.parent().parent().find('.tableBox').css('display','block');
		
		// ボタンを[閉じる]に
		p006this.find('img:eq(0)').css('display','none');
		p006this.find('img:eq(1)').css('display','block');
		return false;
	}
	
	// 非表示用
	function p006funcHide(){
		// 非表示にする
		p006this.parent().parent().find('.tableBox').css('display','none');
		
		// ボタンを元に戻す
		p006this.find('img:eq(1)').css('display','none');
		p006this.find('img:eq(0)').css('display','block');
		return false;
	}
});