//===================================
//関数定義
//===================================

//サムネイルのhref生成用関数
function ThumbsHref(){
	$(".thumbs a").each(function(i, v){
		//サムネイルのhrefを取得
		var thumbs_href = $(this).attr("href");
		//hrefの頭の「#」を削除
		thumbs_href = thumbs_href.replace(/^\#/, "");
		//サムネイルのhrefにアイテム用ハッシュがあるか判定
		var match_re = thumbs_href.match(/i_[^\&]*/);
		//サムネイルのhrefにアイテム用ハッシュがある場合、マッチした部分からパスを成型
		if(match_re) var new_thumbs_href = '#c=' + val + '&' + match_re;
		//サムネイルのhrefにアイテム用ハッシュがない場合、hrefをそのまま成型
		if(!match_re) var new_thumbs_href = '#c=' + val + '&' + thumbs_href;
		//href差し替え
		$(this).attr("href", new_thumbs_href);
	});
}

//表示切り替え用関数
function ChangeList(key, val){
	//初期表示をnoneに
	$(".finish div.item").css("display", "none");
	$(".newproduct div.item").css("display", "none");

	//表示切り替え
	if(val=='INDEX' || !val){
		$(".now_on_sale .item").css("display", "block");
		$(".thumbs a").css("display", "inline");
		$(".thumbs a[class='FINISH']").css("display", "none");
		$(".thumbs a[class='NEWPRODUCT']").css("display", "none");
		$(".now_on_sale .item .pagetop").css("display", "block");
	}else{
		//一覧部分
		$("[class^='item ']").css("display", "none");
		$("[class^='item'][class$='" + val + "']").css("display", "block");
		//サムネイル部分
		$(".thumbs a").css("display", "none");
		$(".thumbs [class='" + val + "']").css("display", "inline");
		//アイテム要素数をカウント
		$("[class^='item'][class$='" + val + "']").each(function(i, v){
			count = i;
		});
		//アイテム要素数と一致したブロックの「Page Top」を非表示
		$("[class^='item'][class$='" + val + "'] .pagetop").each(function(i, v){
			if(count==i) $(v).css("display", "none");
		});
		ThumbsHref();
	}
	
	if(val == 'NEWPRODUCT'){
		//$(window).bind('load', function() {
			$('div#newProduct div.item').removeAttr('style');
			$('div#newProduct div.item').autoHeight({column:2,clear:1});
			//$('div#newProduct div.item').css('display','none');
		//});
	}
	
	
	
	
	

	$(".tab a").each(function(i, v){
		var src = $("img", this).attr("src");
		var src_off = src.replace(/^(.+)_on(\.[a-z]+)$/, "$1$2");
		myclass = $(this).attr("class");
		if(myclass == val){
			var src = $("img", this).attr("src");
			var src_on = src.replace(/^(.+)(\.[a-z]+)$/, "$1_on$2");
			if(!src.match(/_on(\.[a-z]+)$/)){
				$("img", this).attr("src", src_on);
				$("img", this).removeClass("ro");
			}
		}else{
			var src = $("img", this).attr("src");
			var src_off = src.replace(/^(.+)_on(\.[a-z]+)$/, "$1$2");
			$(this).attr("src", src_off);
			$("img", this).addClass("ro");
		}
		if(!val){
			var src = $(".INDEX img").attr("src");
			var src_on = src.replace(/^(.+)(\.[a-z]+)$/, "$1_on$2");
			if(!src.match(/_on(\.[a-z]+)$/)){
				$(".INDEX img").attr("src", src_on);
				$(".INDEX img").removeClass("ro");
			}
		}
	});

}

//ページロード時の表示切り替え用関数
function CategoryLoad(){
	//ハッシュを取得
	if(location.hash) var l_hash = location.hash;
	if(l_hash){
		//アイテム用ハッシュを判定
		var match_re = l_hash.match(/\&i_[^\&]*/);
		//アイテム用ハッシュを除去
		if(match_re) var l_hash = l_hash.replace(/\&i_[^\&]*/, "");
		//ハッシュの頭の「#」を削除
		l_hash = l_hash.replace(/^\#/, "");
		//ハッシュ文字列を「=」で分割、配列格納
		var arr_l_hash = l_hash.split('=');
		key = arr_l_hash[0];
		val = arr_l_hash[1];
		ChangeList(key, val);
	}else{
		ChangeList('c', 'INDEX');
	}
}

//===================================
//実行
//===================================

$(function(){

	CategoryLoad();

	$(".tab a").click(function(){
		var href = $(this).attr("href");
		//hrefの頭の「#」を削除
		href = href.replace(/^\#/, "");
		//href文字列を「=」で分割、配列格納
		var arr_href = href.split('=');
		key = arr_href[0];
		val = arr_href[1];
		ChangeList(key, val);
		

		$(".tab a").each(function(i, v){
			myclass = $(this).attr("class");
			if(myclass == val){
				var src = $("img", this).attr("src");
				var src_off = src.replace(/^(.+)_on(\.[a-z]+)$/, "$1$2");
				$("img", this).removeClass("ro");
			}else{
				var src = $("img", this).attr("src");
				var src_off = src.replace(/^(.+)_on(\.[a-z]+)$/, "$1$2");
				$("img", this).attr("src", src_off);
				$("img", this).addClass("ro");
			}
		});

	});

	$(".thumbs a").click(function(){
		var href = $(this).attr("href");
		href = href.match(/i_[^\&]*/);
		$.scrollTo($("#" + href), 0);
	});

	$(".thumbs a").click(function(){
		var href = $(this).attr("href");
		href = href.match(/i_[^\&]*/);
		$.scrollTo($("#" + href), 0);
	});

	$("[href='#top']").click(function(){
		$.scrollTo($("#top"), 0);
	});

	$(".ro").hover(
		function(){
			var this_class = $(this).attr("class");
			var arr_this_class = this_class.split(" ");
			$.each(arr_this_class, function(i, v){
				if(v == 'ro'){
					flag = 1;
					return false;
				}else{
					flag = 0;
				}
			});
			if(flag){
				var src = $(this).attr("src");
				if(!src.match(/_on(\.[a-z]+)$/)){
					var src_on = src.replace(/^(.+)(\.[a-z]+)$/, "$1_on$2");
					$(this).attr("src", src_on);
				}
			}
		},
		function(){
			var this_class = $(this).attr("class");
			var arr_this_class = this_class.split(" ");
			$.each(arr_this_class, function(i, v){
				if(v == 'ro'){
					flag = 1;
					return false;
				}else{
					flag = 0;
				}
			});
			if(flag){
				var src = $(this).attr("src");
				if(src.match(/_on(\.[a-z]+)$/)){
					var src_off = src.replace(/^(.+)_on(\.[a-z]+)$/, "$1$2");
					$(this).attr("src", src_off);
				}
			}
		}
	);

});

