// JavaScript Document
/**
 * @author Alexander Farkas
 * v. 1.02
 */

//タブ履歴関数
function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		$("#load").load(hash + ".html");
	} else {
		// start page
		$("#load").empty();
	}
}



$(function(){
		   
	//徐々に

	//背景プレビュー
    $('.tabBox .imgPattern img').mouseover(function(){
		var bglink = $(this).attr("src");		
		$(".tabBox").css("background-image","url("+bglink+")");
    });

    $('.tabBox .imgPattern img').mouseout(function(){
		$(".tabBox").css("background","#fff");
    });

	//背景プレビュー
    $('.tabBox2 .imgPattern img').mouseover(function(){
		var bglink = $(this).attr("src");		
		$(".tabBox2").css("background-image","url("+bglink+")");
    });

    $('.tabBox2 .imgPattern img').mouseout(function(){
		$(".tabBox2").css("background","#fff");
    });


	//背景プレビュー
    $('.tabBox .imgTexture img').mouseover(function(){
		var bglink = $(this).attr("src");		
		$(".tabBox").css("background-image","url("+bglink+")");
    });

    $('.tabBox .imgTexture img').mouseout(function(){
		$(".tabBox").css("background","#fff");
    });



	//TAB ボックス
	var $tabs = $('#patternData ul').tabs({ fx: { opacity: 'toggle', duration: 'normal'  } });
	
	$("#patternData .ui-tabs-panel").each(function(i){
	  var totalSize = $("#patternData .ui-tabs-panel").size() - 1;
	  if (i != totalSize) {
	      next = i + 2;
   		  $(this).append("<a href='#' class='next-tab mover' rel='Tab" + next + "'>Next Page &#187;</a>");
	  }
	  if (i != 0) {
	      prev = i;
   		  $(this).append("<a href='#' class='prev-tab mover' rel='Tab" + prev + "'>&#171; Prev Page</a>");
	  }
	});
	$('#patternData .next-tab,#patternData .prev-tab').click(function() {
           $tabs.tabs('select', $(this).attr("rel"));
           return false;
    });
	$("#patternData").show();



	//TAB ボックス
	var $tabs = $('#textureData ul').tabs({ fx: { opacity: 'toggle', duration: 'normal'  } });
	$("#textureData").show();



//ナビゲーションメニュー
	$('#gMenu .gMenuTexture a')
		.mouseover(function() {
			$(this).stop().animate({ backgroundPosition: "(-140px 0)" }, { duration: 500 })
		})
		.mouseout(function() {
			$(this).stop().animate({ backgroundPosition: "(0 0)" }, { duration: 200, complete: function() {$(this).css({ backgroundPosition: "0 0" })}
		})
	})

	$('#gMenu .gMenuHowto a')
		.mouseover(function() {
			$(this).stop().animate({ backgroundPosition: "(-140px -40px)" }, { duration: 500 })
		})
		.mouseout(function() {
			$(this).stop().animate({ backgroundPosition: "(0 -40px)" }, { duration: 200, complete: function() {$(this).css({ backgroundPosition: "0 -40px" })}
		})
	})


	$('#gMenu .gMenuLicense a')
		.mouseover(function() {
			$(this).stop().animate({ backgroundPosition: "(-140px -80px)" }, { duration: 500 })
		})
		.mouseout(function() {
			$(this).stop().animate({ backgroundPosition: "(0 -80px)" }, { duration: 200, complete: function() {$(this).css({ backgroundPosition: "0 -80px" })}
		})
	})


	$('#gMenu .gMenuAboutMe a')
		.mouseover(function() {
			$(this).stop().animate({ backgroundPosition: "(-140px -120px)" }, { duration: 500 })
		})
		.mouseout(function() {
			$(this).stop().animate({ backgroundPosition: "(0 -120px)" }, { duration: 200, complete: function() {$(this).css({ backgroundPosition: "0 -120px" })}
		})
	})


	$('#gMenu .gMenuContact a')
		.mouseover(function() {
			$(this).stop().animate({ backgroundPosition: "(-140px -160px)" }, { duration: 500 })
		})
		.mouseout(function() {
			$(this).stop().animate({ backgroundPosition: "(0 -160px)" }, { duration: 200, complete: function() {$(this).css({ backgroundPosition: "0 -160px" })}
		})
	})


	$('#gMenu .gMenuPattern a')
		.mouseover(function() {
			$(this).stop().animate({ backgroundPosition: "(-140px -200px)" }, { duration: 500 })
		})
		.mouseout(function() {
			$(this).stop().animate({ backgroundPosition: "(0 -200px)" }, { duration: 200, complete: function() {$(this).css({ backgroundPosition: "0 -200px" })}
		})
	})

	//スムーズスクロール
	// smooth scroller
	$("a[href^=#]").click(function() {
		var hash = this.hash;
		if(!hash || hash == "#")
			return false;
		$($.browser.safari ? 'body' : 'html')
			.animate({scrollTop: $(hash).offset().top}, 1000, "swing");
		return false;
	});


	//タブ履歴
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload);
	
	// set onlick event for buttons
	$("a[rel='history']").click(function(){
		// 
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		$.historyLoad(hash);
		return false;
	});
});



//タブ作成
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);



