
/*if (!window.console) {
	window.console = { log: function() {} };
}
*/


;(function($) {
	
	$(".logos").delay(500).animate({ opacity: 1 ,marginLeft: "5px"}, 1000 , "easeOutExpo");
	startpage();
	//scrollsmenu();
	function indexani(){
	$('.index_img img:gt(0)').hide();  
	clearInterval(keseweID);
	var keseweID = 0;
    keseweID = setInterval(function() {  
      $('.index_img :first-child').fadeOut(4000).next('img').fadeIn(4000).end().appendTo('.index_img');}, 8000); 
	}
	
	function startpage() {
		$("h1").delay(800).animate({ opacity: 1 ,marginLeft: "5px"}, 600 , "easeOutExpo"),
		$(".sidebar").delay(1000).animate({ opacity: 1 }, 500 , "linear"),
		$("#contents").delay(1500).animate({ opacity: 1 }, 500 , "linear");
		//replaceContents("", "images/img_title_main.png", true);
		//indexani();
		Effectcontents();	
	}
	var domain = location.hostname,
	$contents = $("#contents");
	
	if ( location.hash ) {
		replaceContents(location.hash);
	}
	
	function titleChage(){
		if (location.hash == '#service/index.html') {
		handler("images/img_title_service.jpg", true),$("#menu_service a").addClass("selected")
	}
	
	}
	
	function button_bind(e){e.preventDefault().attr("href","#"); }
	function button_inbind(){//$("a[href]").bind('click', button_bind); //ボタン使える
	//alert("ボタン閉じる")
	}	
	function button_unbind(){//$("a[href]").unbind('click', button_bind); //ボタン使えない
	//$(document).undelegate();
	//alert("ボタンおわり")
	}
	
	
	
	
	$.fn.getHref = function() {
		//alert("getHref");
		$(this).each(function() {
		$(this).filter(function() {return this.href.indexOf(domain) !== -1;})
		.attr("href", "#" + $(this).attr("href").replace(domain, ""));
		//.attr("href", "#" + $(this).attr('href').substr(0,$(this).attr('href').length-5));
		});
		//window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);	
	}
	

	
	
	function handler(imgurl, states, buttonnum) {
	$("a:not([href*=#])").getHref();
	$(window).unbind();
	$(window).bind("hashchange", function() {replaceContents(location.hash, imgurl, states)});
	$(".nav > li > a").removeClass("selected");
	$(buttonnum).addClass("selected");
	}
	//,$("#menu_main a").addClass("selected")
	
	$(document).delegate('.logos a','click', function(){handler("images/img_title_main.jpg"), button_inbind(),$('.index_img img:gt(0)').remove()});
	$(document).delegate('.menu_service a','click', function(){handler("images/img_title_service.jpg", true,".menu_service a"), button_inbind()});
	$(document).delegate('.menu_work a','click', function(){handler("images/img_title_work.jpg", true, ".menu_work a"), button_inbind(),Effectcontents()});
	$(document).delegate('.menu_aboutus a','click', function(){handler("images/img_title_about.jpg", true, ".menu_aboutus a"), button_inbind()});
	$(document).delegate('.menu_others a','click', function(){handler("images/img_title_main.png", true, ".menu_others a"), button_inbind()} );
	$(document).delegate('.menu_works a','click', function(){handler("images/img_title_work.jpg", ".menu_works a"), button_inbind()});
	$(document).delegate('.h2_button a','click', function(){handler("images/img_title_work.jpg", false, ".menu_works a"), button_inbind() ,Effectcontents()});
	
	

	function LangEffects() {
      $(".lang_button").toggle(  
        function () {
		  $(this).find(".langs").remove();
          $(this).append($("<span class=langs>Japanese</span>"));
          $(".lang_show_en").animate({opacity: '1'}, 'fast');
		  $(".lang_show_jp").animate({opacity: '0'}, 'slow');    
        },   
        function () {
		  $(this).find(".langs").remove();
          $(this).append($("<span class=langs>English</span>"));              
          $(".lang_show_en").animate({opacity: '0'}, 'fast');
		  $(".lang_show_jp").animate({opacity: '1'}, 'slow');  
        }    
      );	
	}	
 
	
	function replaceContents(hash,h1img,Bstates) {
		
		
		var url = hash.replace("#", "");
		//var url = hash.attr("#").replace("",".html"); alert(url);
		$.ajax({
			type: "GET",
			dataType: "html",
			url: url,
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				if ( textStatus === "error" ) {
					//alert("Not found");
					location.href = domain;
					
				}
			},
			success:function(html) {
				
				var $div = $("<div>" + html + "</div>"),
				$containerContents = $("#contents > div", $div);
				//$("h1").animate({'opacity': 0 ,'marginLeft': "100px"}, 1000 , "easeInOutExpo");
				
				if (Bstates == true) {
					function loadContent() {
						$contents.append($containerContents);
						$contents.animate({ opacity: 1, marginTop: "0px", marginLeft: "0px"}, 1000 , "easeInOutExpo");
						$("h1").delay(1500).animate({ opacity: 1 ,marginLeft: "5px"}, 600 , "easeOutExpo", button_unbind());
						$('#load').fadeOut('normal');
						$("#work_menu").preloader();	
						Effectcontents();
						LangEffects();
						}
					function RloadContent() {
						$contents.empty()
						$div.remove();
						$("h1 img").attr("src",h1img);
						$contents.animate({'opacity': 0 ,'marginTop': '0px', 'marginLeft': '700px'}, 100 , "linear", loadContent);
						$("#load").remove();
						$(".header").append('<span id="load">LOADING...</span>');
						$("#load").fadeIn('normal');
						}
					
					$contents.animate({'opacity': 0 ,'marginLeft': "700px"}, 1000 , "easeInOutExpo", RloadContent );
					$("h1").delay(500).animate({'opacity': 0 ,'marginLeft': "-50px"}, 500 , "easeInOutExpo");//, scrollsmenu
					$div.remove();
					
					
					//return false;
					}
				else {
					function loadContent() {
						$contents.append($containerContents)
						$contents.animate({ opacity: 1, marginTop: "0px", marginLeft: "0px"}, 1000 , "easeInOutExpo");
						$("h1").delay(1500).animate({ opacity: 1 ,marginLeft: "5px"}, 500 , "easeOutExpo", button_unbind());
						$('#load').fadeOut('normal');
						$("#work_menu").preloader();
						LangEffects();
						}
						
					function RloadContent() {
						$contents.empty()
						$div.remove();
						$("h1 img").attr("src",h1img);
						$contents.animate({ opacity: 0}, 100 , "linear", loadContent);
						$("#load").remove();
						$(".header").append('<span id="load">LOADING...</span>');
						$("#load").fadeIn('normal');
					}
					
					$contents.animate({ opacity: 0 ,marginTop: "200px"}, 1000 , "easeInOutExpo", RloadContent );
					$("h1").delay(500).animate({ opacity: 0 ,marginLeft: "-50px"}, 500 , "easeInOutExpo");//,scrollsmenu
					$div.remove();
					}
			}
		});
	}//replaceContents
	
	

}(jQuery));



function Effectcontents() {
		/*あとでやるところ。ロードコンテンツ
				$.autopager({autoLoad: false});
        $('a[rel=next]').click(function() {
            $.autopager('load');
            return false;
        });	
		
		*/	
		
				
                $('#work_menu > li').live('mouseenter',function(){
					var $elem = $(this);
					$elem.css({'z-index':'200'});
					$elem.find('.sdt_subtitle').css({'z-index':'300'});
					//$elem.find('img').css({'-moz-box-shadow': '0px 0px 5px #000' ,'-webkit-box-shadow': '0px 0px 5px #000','box-shadow': '0px 0px 5px #000' });
					$elem.find('img').stop(true).animate({'width':'350px','left':'-40px','top':'-46px', 'borderWidth': '15px'},250,'easeOutQuint').andSelf()
						 .find('.sdt_wrap').stop(true).delay(0).animate({'color':'black','left':'-40px','width':'380px'},300,'easeOutQuint').andSelf()
						 .find('.sdt_descr').stop(true).delay(300).animate({'width': '100px'},300,'easeInOutQuint').html(' Go to Detail').andSelf()//
						 .find('.sdt_subtitle').stop(true).delay(400).animate({'opacity': 1, 'left':'-40px'},300,'easeInOutQuint');
						 /*.find('.sdt_active').stop(true).animate({'height':'170px'},200,function(){
								var $sub_menu = $elem.find('.sdt_box');
								if($sub_menu.length){
												var left = '170px';
								if($elem.parent().children().length == $elem.index()+1)
												left = '-170px';
							$sub_menu.show().animate({'left':left},200);
						}	
					});*/
				}).live('mouseleave',function(){
					var $elem = $(this);
					$elem.css({'z-index':'10'});
					//$elem.find('img').css({'-moz-box-shadow': '0px 0px 0px #000' ,'-webkit-box-shadow': '0px 0px 0px #000','box-shadow': '0px 0px 0px #000' });
					var $sub_menu = $elem.find('.sdt_box');
					if($sub_menu.length)$sub_menu.hide().css('left','0px');
					$elem.find('.sdt_active').stop(true).animate({'height':'0px'},250).andSelf()
						 .find('img').stop(true).animate({'width':'290px','left':'10px','top':'25px', 'borderWidth': '0px'},250,'easeOutQuint').andSelf()
						 .find('.sdt_wrap').stop(true).animate({'color':'none','left':'0px','width':'300px'},400,'easeOutQuint').andSelf()
						 .find('.sdt_subtitle').stop(true).delay(0).animate({'opacity': 0, 'left':'0px'},300,'easeInOutQuint').andSelf()//
						 .find('.sdt_descr').stop(true).animate({'width': '50px'},400,'easeOutQuint').html('Detail')
				});
}



function scrollsmenu(){
	
	var offset = $(".sidebar").offset();
            var topPadding = 150;
            $(window).scroll(function() {
                if ($(window).scrollTop() > offset.top) {
                    $(".sidebar").stop().animate({marginTop: $(window).scrollTop() - offset.top + topPadding},{ duration: '1000', easing: 'easeOutCubic'});
                } else {$(".sidebar").stop().animate({marginTop: 0});
                };
            });
	}

$.fn.preloader = function(options){
	var defaults = { delay:100, preload_parent:"a", check_timer:100, ondone:function(){ }, oneachload:function(image){  }, fadein:300 };
	
	 var options = $.extend(defaults, options),
	 root = $(this) , images = root.find("img").css({"visibility":"hidden",opacity:0}) ,  timer ,  counter = 0, i=0 , checkFlag = [] , delaySum = options.delay ,
	 
	 init = function(){
		timer = setInterval(function(){
			if(counter>=checkFlag.length)
			{
			clearInterval(timer);
			options.ondone();
			return;
			}
			for(i=0;i<images.length;i++)
			{
				if(images[i].complete==true)
				{
					if(checkFlag[i]==false)
					{
						checkFlag[i] = true;
						options.oneachload(images[i]);
						counter++;
						delaySum = delaySum + options.delay;
					}
					$(images[i]).css("visibility","visible").delay(delaySum).animate({opacity:1},options.fadein,
					function(){ $(this).parent().removeClass("preloader");   });
				}
			}
			},options.check_timer) 
		 } ;
	
	images.each(function(){
		if($(this).parent(options.preload_parent).length==0)
		$(this).wrap("<a class='preloader' />");
		else
		$(this).parent().addClass("preloader");
		checkFlag[i++] = false;
		}); 
	images = $.makeArray(images); 
	var icon = jQuery("<img />",{
		id : 'loadingicon' ,
		src : 'images/loading.gif'
		}).hide().appendTo("body");
	
	timer = setInterval(function(){
		if(icon[0].complete==true)
		{
			clearInterval(timer);
			init();
			 icon.remove();
			return;
		}
		},500);
	
	}
