/*
*	general.js
*	@requires jQuery v1.4.2
*
*	Copyright (c) 2010 cyclops.co.,ltd.
*	Date: 2010.08.05
*
*/


/**
*	Initialization
*/
$(function(){
	$.cyc.init();
});

$(window).load(function () {
	$.cyc.load();
});


/**
*	Initialization
*/

$.cyc = {
	init: function() {
		for (module in $.cyc) {
			if ($.cyc[module].init){
				$.cyc[module].init();
			}
		}
	},
	load:function(){
		for (module in $.cyc) {
			if ($.cyc[module].load){
				$.cyc[module].load();
			}
		}
	}
};


/**
*	externalLinks (add target _blank)
*/
$.cyc.externalLinks = {
	init: function() {
		$('a[rel="external"]').attr("target","_blank").filter(':not(:has("img"))').addClass("external");
	}
};


/**
*	swap img
*/
jQuery.fn.rollover = function(settings){
	settings = jQuery.extend({
		suffix: "-o"  //画像ファイル名につける接尾語の初期設定値
	}, settings);
	return this.each(function(){
		var default_img = $(this).attr("src");
		if (!default_img.match((settings.suffix))) {
			var point = default_img.lastIndexOf(".");
			var mouseover_img = default_img.slice(0, point) + settings.suffix + default_img.slice(point);
			var preload_img = new Image();
			preload_img.src = mouseover_img;
			$(this).hover(
				function(){
					$(this).attr("src", mouseover_img);
				},
				function(){
					$(this).attr("src", default_img);
				}
			);
		};
	});
};

$.cyc.swap = {
	init: function(){
		$("img.swap").rollover(); //接尾語は初期設定値のまま
	}
}




/**
*	pagetop
*/
$.cyc.pagetop = {
	init: function() {
		$('.pagetop a[href*=#]').click(function() {
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')&& location.hostname == this.hostname.replace(/:.*$/,'')) {
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					var targetOffset = $target.offset().top;
					$('html,body').animate({scrollTop: targetOffset}, 500);
					return false;
				}
			}
		});	
	}
};



/**
 *	table-zebra
 */
$.cyc.zebra = {
	init:function(){
		$('tbody','table.zebra').each(function(){
				$("tr",this).removeClass("odd").filter(':odd').addClass("odd");
		});
	}
};


/**
*	table-trHover
*/
$.cyc.trHover = {
	init:function(){
		//hover
		$("tr","table.trhover tbody").hover(function() {
			$(this).addClass("hover");
		},
		function() {
		$(this).removeClass("hover");
		} ); 
	}
};




/**
*	thumbnails list height fix
*/


$.cyc.thumbnails = {
	init:function(){
	 	$("li",".thumbnails").css('height','auto');
	 },
	load:function(){
		/*list height even*/
		$(".thumbnails").autoHeight(true);
	}
};


/*main navigation*/

$.cyc.backgroundPosition = {
	init:function(){
		var rover = "easeOutQuad";
		var rover2 = "easeOutCubic";
		var rout = "easeOutBack";
		var rout2 = "easeOutQuad";
		var mmID = '#mm-' + $('body').attr('id');
		
		$('li','#contents-navigation').removeClass('current');
		$('li'+mmID,'#contents-navigation').addClass('current');
		
		$("#contents-navigation li#mm-news a").mouseover(function () {$(this).stop().animate({ backgroundPosition: "-4px -41px" }, 250, rover);});
		$("#contents-navigation li#mm-news a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-4px 0px" }, 300, rout);});
		$("#contents-navigation li#mm-news.current a").animate({ backgroundPosition: "-4px -82px" }, 300, rout);
		$("#contents-navigation li#mm-news.current a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-4px -82px" }, 300, rout);});
		
		$("#contents-navigation li#mm-creed a").mouseover(function () {$(this).stop().animate({ backgroundPosition: "-122px -41px" }, 250, rover);});
		$("#contents-navigation li#mm-creed a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-122px 0px" }, 300, rout);});
		$("#contents-navigation li#mm-creed.current a").animate({ backgroundPosition: "-122px -82px" }, 300, rout);
		$("#contents-navigation li#mm-ccreed.current a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-122px -82px" }, 300, rout);});
		
		$("#contents-navigation li#mm-order a").mouseover(function () {$(this).stop().animate({ backgroundPosition: "-240px -41px" }, 250, rover);});
		$("#contents-navigation li#mm-order a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-240px 0px" }, 300, rout);});
		$("#contents-navigation li#mm-order.current a").animate({ backgroundPosition: "-240px -82px" }, 300, rout);
		$("#contents-navigation li#mm-order.current a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-240px -82px" }, 300, rout);});
		
		$("#contents-navigation li#mm-readymade a").mouseover(function () {$(this).stop().animate({ backgroundPosition: "-358px -41px" }, 250, rover);});
		$("#contents-navigation li#mm-readymade a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-358px 0px" }, 300, rout);});
		$("#contents-navigation li#mm-readymade.current a").animate({ backgroundPosition: "-358px -82px" }, 300, rout);
		$("#contents-navigation li#mm-readymade.current a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-358px -82px" }, 300, rout);});
		
		$("#contents-navigation li#mm-style a").mouseover(function () {$(this).stop().animate({ backgroundPosition: "-476px -41px" }, 250, rover);});
		$("#contents-navigation li#mm-style a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-476px 0px" }, 300, rout);});
		$("#contents-navigation li#mm-style.current a").animate({ backgroundPosition: "-476px -82px" }, 300, rout);
		$("#contents-navigation li#mm-style.current a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-476px -82px" }, 300, rout);});
		
		$("#contents-navigation li#mm-shop a").mouseover(function () {$(this).stop().animate({ backgroundPosition: "-594px -41px" }, 250, rover);});
		$("#contents-navigation li#mm-shop a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-594px 0px" }, 300, rout);});
		$("#contents-navigation li#mm-shop.current a").animate({ backgroundPosition: "-594px -82px" }, 300, rout);
		$("#contents-navigation li#mm-shop.current a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-594px -82px" }, 300, rout);});
		
		$("#contents-navigation li#mm-qanda a").mouseover(function () {$(this).stop().animate({ backgroundPosition: "-712px -41px" }, 250, rover);});
		$("#contents-navigation li#mm-qanda a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-712px 0px" }, 300, rout);});
		$("#contents-navigation li#mm-qanda.current a").animate({ backgroundPosition: "-712px -82px" }, 300, rout);
		$("#contents-navigation li#mm-qanda.current a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-712px -82px" }, 300, rout);});
		
		$("#contents-navigation li#mm-blog a").mouseover(function () {$(this).stop().animate({ backgroundPosition: "-830px -41px" }, 250, rover);});
		$("#contents-navigation li#mm-blog a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-830px 0px" }, 300, rout);});
		$("#contents-navigation li#mm-blog.current a").animate({ backgroundPosition: "-830px -82px" }, 300, rout);
		$("#contents-navigation li#mm-blog.current a").mouseout(function () {$(this).stop().animate({ backgroundPosition: "-830px -82px" }, 300, rout);});
		
		$('#second-nav li a').css({backgroundPosition: '0px -45px'});//default(CSSでの記述を上書きし、js offの環境ではCSSが優先される)
		$('#second-nav li a').mouseover(function () {$(this).stop().animate({backgroundPosition: '0px 5px'}, 250, rover);});
		$('#second-nav li a').mouseout(function () {$(this).stop().animate({backgroundPosition: '0px -45px'}, 300, rout2);});
	}
};




/**
#main-visual
http://jquery.malsup.com/cycle/lite/
*/

$.cyc.cycle = {
	init:function(){
		$('#main-400').cycle({ 
			timeout: 5000,
			random: 0,
			sync: 1,
			slideExpr: "img"
		}),
		$('#main-100').cycle({ 
			timeout: 4000,
			random: 0,
			sync: 1,
			slideExpr: "img"
		}),
		$('#main-60-1').cycle({ 
			timeout: 3750,
			random: 0,
			sync: 1,
			slideExpr: "img"
		}),
		$('#main-60-2').cycle({ 
			timeout: 3500,
			random: 0,
			sync: 1,
			slideExpr: "img"
		}),
		$('#main-165').cycle({ 
			timeout: 4500,
			random: 0,
			sync: 1,
			slideExpr: "img"
		}),
		$('#others').cycle({ 
			timeout: 5000,
			random: 1,
			sync: 1,
			slideExpr: "img"
		});
	}
};






/**
"clickable"クラスを持つUL要素のLI要素のエリア全面をクリックできるようにする
クリックアクションは、LI要素に内包するaタグのhref要素を呼び出す
*/
$.cyc.clickable = {
	init:function(){
		$('.clickable').find('li:has(a)').click(function(){
			if($(this).hasClass("liblank")){
				window.open($(this).find("a").attr("href"),"_blank");
			}else{
				window.open($(this).find("a").attr("href"),"_self");
			}
		}).hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
	}
};
/*
$.cyc.blank = {
	init:function(){
		$('.clickable.blank').find('li:has(a)').click(function(){
				window.open($(this).find("a").attr("href"),"_blank");
			});
	}
};
*/

/**
li:hoverを実装する
*/
$.cyc.lihover = {
	init:function(){
		$('li','.lihover').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
	}
};



/**
topics の newクラスでの
new画像の表示
*/
$.cyc.topicnew = {
	init:function(){
		$('ul#topics li.new','#db-news').append('<img class="new" src="images/icon-new.gif" alt="new" width="130" height="16" />');
		$('img.new','#db-news').fadeIn("slow");
	}
};


/**
page slide
http://srobbin.com/blog/jquery-pageslide/
*/
/*
$.cyc.slide = {
	init:function(){
		var bodyID = $('body').attr('id');
		if(bodyID =='home'){
			$('a.pageslide').pageSlide({
				width: "224px",
				direction: "left",
				modal: true
			});
		};
	}
};
*/


var bodyID = $('body').attr('id');
if(bodyID =='home'){
	$('a.pageslide').pageSlide({
		width: "224px",
		direction: "left",
		modal: true
	});
};



/**
shadowboxの設定
*/
$.cyc.shadowb = {
	init:function(){
		Shadowbox.init();
	}
};

/**
shadowbox option
target="_parent"
*/
$.cyc.parentLinks = {
	init: function() {
		$('ul.shop-link a','#style.detail').attr("target","_parent");
	}
};



/**
jcaption
http://www.gethifi.com/blog/jcaption-a-jquery-plugin-for-simple-image-captions
*/
$.cyc.slideefx = {
	init:function(){
		$("li img","#db-style").jcaption({
			copyStyle: true,
			animate: true,
			show: {height: "show"},
			hide: {height: "hide"}
		});
	}
};


/**
Galleriffic
http://www.twospy.com/galleriffic/example-2.html
*/

$.cyc.slidegalleriffic = {
	load:function(){
		
		var bodyID = ($('body').attr('id'))+($('body').attr('class'));
		if(bodyID != "styledetail")
		return;
		
		// Initially set opacity on thumbs and add
		// additional styling for hover effect on thumbs
		var onMouseOutOpacity = 0.67;
		$('ul.thumbs li','#style-thumb').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
				});
		
		// Initialize Advanced Galleriffic Gallery
		var gallery = $('#style-thumb','#style.detail').galleriffic({
			delay:                     2500,
			numThumbs:                 10,
			preloadAhead:              -1,
			enableTopPager:            false,
			enableBottomPager:         false,
			maxPagesToShow:            10,
			imageContainerSel:         '#slideshow',
			loadingContainerSel:       '#loading',
			renderSSControls:          false,
			renderNavControls:         true,
			enableHistory:             false,
			autoStart:                 false,
			syncTransitions:           true,
			defaultTransitionDuration: 900,
			onSlideChange:             function(prevIndex, nextIndex) {
				// 'this' refers to the gallery, which is an extension of $('#thumbs')
				this.find('ul.thumbs').children()
				.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
				.eq(nextIndex).fadeTo('fast', 1.0);
			},
			onPageTransitionOut:       function(callback) {
				this.fadeTo('fast', 0.0, callback);
			},
			onPageTransitionIn:        function() {
				this.fadeTo('fast', 1.0);
			}
		});
	}
};



/**
* dl list checkbox
*
* dlリストのdtにグループ化されたタイトルのチェックボックスがあり
* ddにグループ要素のチェックボックスがあった場合
* dtのチェックを入れるとグループ内の全チェックが入る
* dtのチェックが外れるとグループ内の全チェックが外れる
* ddのチェックが1つでも外れるとdtのチェックが外れる
*/
$.cyc.dlCheckbox = {
 init:function(){
   $('dl','#style .input-form').each(function(){
     var dd = $('dd input',this);
     var dt = $('dt input',this).click(function(){
       var val = $(this).attr('checked');
       if(val){
         $(dd).attr('checked','checked');
       }else{
         $(dd).attr('checked','');
       }
     });

     $(dd).click(function(){
       var val = $(this).attr('checked');
       if(!val){
         $(dt).attr('checked','');
       }
     });
   });
 }
};



/**
*	accordion
*/

$.cyc.accordion = {
	init:function(){
		var bodyid = $('body').attr('id');
		if(bodyid == 'qanda'){
			$('.question li', '#content').each(function(){
			 	var self = $(this);
				$('.answer' ,this).hide();
				$('a span' ,this).hide();
				$('h4 a' ,this).toggle(function(){
					$('a span' ,self).show();
					$('.answer' ,self).slideDown('3000');
				}, function(){
					$('a span' ,self).hide();
					$('.answer' ,self).slideUp('1500');
				});/*toggle*/
			});/*each*/
		}
	}
};


