/* ===================================================================
JS information

 script info :index（ Use jQuery ）
 
 *Modified: 2011-10-11
=================================================================== */


$(function() {


	//------------------------------------------------
	// タブナビゲーション
	//------------------------------------------------
	if(jQuery.fn.tabs != undefined) {
		$(".col_tab > ul").tabs();
	}	

	//------------------------------------------------
	// お気に入りに追加
	//------------------------------------------------
	$(".okini a").bind("click", function(){
		$a = $(this);

		// お気に入りに未登録の場合
		if ($a.find("img").attr("src") == "/pc/img/common/bt_okini.gif") {

			// サーバにデータを送る
			$.ajax({
				type: 'POST',
				// dataType: 'json',
				dataType: 'html',
				url: $a.attr("rel"),
				async: false,
				cache: false,
				success: function(data) {
					// 画像を変更する
					$a.find("img").attr("src", "/pc/img/common/bt_okini_sumi.gif");
				},
				error: function(request, status, error){
				}
			});
			return false;

		// お気に入りに登録済みの場合
		} else {
			return true;
		}
	});

	//------------------------------------------------
	// 画像のセンター揃え
	//------------------------------------------------
	jQuery.extend(jQuery.fn, {
		imgCenter: function() {
			var getImageSize = function(image) {
				var w = image.width;
				var h = image.height;
				// firefox, safari, chrome
				if ( typeof image.naturalWidth !== 'undefined' ) {
					w = image.naturalWidth;
					h = image.naturalHeight;
				// ie
				} else if ( typeof image.runtimeStyle !== 'undefined'){
					var run = image.runtimeStyle;
					var mem = {w: run.width, h: run.height};
					run.width	= "auto";
					run.height = "auto";
					w = image.width;
					h = image.height;
					run.width	= mem.w;
					run.height = mem.h;
				 // opera
				} else {
					var mem = { w: image.width, h: image.height };
					image.removeAttribute("width");
					image.removeAttribute("height");
					w = image.width;
					h = image.height;
					image.width	= mem.w;
					image.height = mem.h;
				}
				return {width:w, height:h};
			}

			var center = function(img) {
				$(img).show();
				var size = getImageSize(img);
				var $parent = $(img).parent();
				var img_width = size.width;
				var img_height = size.height;
				var parent_width = $(img).parent().width();
				var parent_height = $(img).parent().height();
				if (img_width < parent_width && img_height < parent_height) {
				} else if (img_width >= img_height) {
					img_height = img_height * parent_width / img_width;
					img_width = parent_width;
				} else if (img_width <img_height) {
					img_width = img_width * parent_height / img_height;
					img_height = parent_height;
				}
				var pl = Math.floor((parent_width - img_width)/2);
				var pt = Math.floor((parent_height - img_height)/2);
				$(img).width(img_width).height(img_height).css({"paddingLeft":pl+"px", "paddingTop":pt+"px"});
			}

			this.each(function(){
				var img = this;
				$(img).hide();
				$(img).width(0).height(0);
				var timer = setInterval(function(){
					if (img.complete) {
						clearInterval(timer);
						center(img);
					}
				}, 200);
			});
		}
	});
	$("img.center").imgCenter();

	//------------------------------------------------
	// アコーディオン
	//------------------------------------------------
	jQuery.extend(jQuery.fn, {
		accordion: function(row) {
			this.each(function(){
				$parent = $(this).parent();
				$ul1 = $parent.children('ul');

				$ul2 = $ul1.clone(true);
				if (!$(this).hasClass("aco-close")) {
					$ul2.addClass('hideAcc');
				}
				$ul2.insertBefore(this);

				$ul1.children("li:gt("+(row-1)+")").remove();
				$ul2.children("li:lt("+(row)+")").remove();

				$parent.find(".hideAcc").hide();

				$(this).find("a").click( function() {
					if($(this).parent().hasClass("aco-close")) {
						// classを変更
						$(this).parent().removeClass("aco-close");
						$(this).parent().addClass("aco-open");
						// アコーディオンを表示
						$(this).parent().prev().animate({height:"hide"}, 300, "easeOutQuad");
					} else {
						// classを変更
						$(this).parent().removeClass("aco-open");
						$(this).parent().addClass("aco-close");
						// アコーディオンを非表示
						$(this).parent().prev().animate({height:"show"}, 300, "easeOutQuad");
					};
					return false;
				});
			});
		}
	});


	//------------------------------------------------
	// ie6向け処理
	//------------------------------------------------
	if ($.browser.msie && $.browser.version < 7) {
		
		// min-widthの設定
		if (document.getElementById("wrapper")) {
			var min_max_width = function(){
				var w = $(window).width();
				if( w < 980 ) {
					$("#wrapper").css({width:"980px"})
				} else {
					$("#wrapper").css({width:"auto"})
				}
			};
			attachEvent("onload" , min_max_width);
			attachEvent("onresize", min_max_width);
		};

	};


});

