var bgSizeH = 2364;
var offsetY = 0;
function onresize(){
	var buy = ($(window).height() - bgSizeH) / 2 + offsetY;
	$('#container').css("backgroundPosition","center " + buy + "px" + "");
}
function onscroll(){
	offsetY = - $('html, body').offset().top * (1/$.view.zoom);//;
	onresize();
//	var buy = ($(window).height() - bgSizeH) / 2;
//	$('#container').css("backgroundPosition","center " + buy + "px" + "");
}

function updatemenu(value){
	alert($.nav._select(value))
//	return $.nav._select(value);
}

(function($){

	$._ua = (function(){
	 return {
	  ltIE6:typeof window.addEventListener == "undefined" && typeof document.documentElement.style.maxHeight == "undefined",
	  ltIE7:typeof window.addEventListener == "undefined" && typeof document.querySelectorAll == "undefined",
	  ltIE8:typeof window.addEventListener == "undefined" && typeof document.getElementsByClassName == "undefined",
	  mobile:/android|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())
	 }
	})();

	if($._ua.mobile){
		$('html').addClass('mobile');
		bgSizeH = 1500;
	}

	//debug
	if(location.search.indexOf('ie6=true') != -1){
		$('html').addClass('ie6');
	}

	$.device = {};
	var agent = navigator.userAgent;
	if(agent.search(/iPhone/) != -1){
		//iphone
		$.device.iphone = true;
	}else if(agent.search(/Android/) != -1){
		//android
		$.device.android = true;
	}
	$.device.smartphone = ($.device.iphone || $.device.android);
	$.device.tablet_android = false;
	if($.device.android){
		$.device.tablet_android = !(/mobile/.test(String(agent).toLowerCase()));
		//alert($.device.tablet_android)
		//android tablet 判定
		$.device.smartphone = !$.device.tablet_android;
	}	

	if($.device.smartphone){
		setTimeout(scrollTo, 100, 0, 1);
		$('.content').each(function(){
			$(this).append($('<a href="#pagetop" onclick="return $.util.scrollTo(\'#pagetop\')" class="scrolltotop");">scrollToTop</a>'))
		});
	}
	
	$.view = {};
	$.view.zoom = Number($('body').css('zoom'));

	//flash
	$.flash = {};
	$.flash.version = swfobject.getFlashPlayerVersion().major;
	$.flash.installed = Boolean($.flash.version) && $.flash.version >= 10 ? true : false;
	if(location.search.indexOf('flashinstalled=false') != -1){
		$.flash.installed = false;
	}
	//$quq.flash.installed = false;
	/*if($.device.tablet_android){
		$.flash.installed = false;
	}*/

		
	//イベントオブジェクト
	$.e = $('<div></div>');
	
	$.menuname = {}
	$.menuname.home = "home";
	$.menuname.home_tab = "home_tab";
	$.menuname.about = "about";
	$.menuname.works = "works";
	$.menuname.activity = "activity";
	$.menuname.contact = "contact";
	$.menuname.list = [
						$.menuname.home,
						$.menuname.home_tab,
						$.menuname.about,
						$.menuname.works,
						$.menuname.activity,
						$.menuname.contact
						];
	$.contents = {};
	$.contents.home;
	$.contents.home_tab;
	$.contents.about;
	$.contents.works;
	$.contents.activity;
	$.contents.contact;
	$.contents.list = [];
	
	/**
	 * サイト初期化
	 */
	$.siteinit = {currentmenu:$.menuname.home,currentcontent:undefined};
	$.fn.siteinit = function(option){
	
		$.contents.home = $("#chome:first");
		$.contents.about = $('#cabout:first');
		$.contents.works = $('#cworks:first');
		$.contents.activity = $('#cactivity:first');
		$.contents.contact = $('#ccontact:first');
		$.contents.list.push($.contents.home);
		$.contents.home_tab = $("#chome_tab:first");
		$.contents.list.push($.contents.home_tab);
		$.contents.list.push($.contents.about);
		$.contents.list.push($.contents.works);
		$.contents.list.push($.contents.activity);
		$.contents.list.push($.contents.contact);
		$('.content').each(function(){
			if(!$.device.smartphone){
				if($(this).html() == $('#chome').html()) return;
				$(this).css({display:'none'});
			}
		});
		$.siteinit.currentcontent = $.contents.home;
		$('#contents').removeClass("hidden");
	
		//アドレス
		if(!$.device.smartphone){
		$.address.init(function(event) {
			//パーマリンク
			if($.address.pathNames()){
				if($.menuname[$.address.pathNames()[0]]){
					//$.siteinit.currentmenu = $.menuname[$.address.pathNames()[0]];
					$.nav._select($.menuname[$.address.pathNames()[0]]);
				}			
			}
			//$.address.value();
			//$.address.path();
			//$.address.pathNames();
			//$.address.parameterNames();
			//$.address.queryString();
		}).change(function(event) {
		    
		    //パーマリンク
		    if(event.pathNames){
				if($.siteinit.currentmenu != $.menuname[event.pathNames[0]]){
					if($.menuname[event.pathNames[0]]){
						$.nav._select($.menuname[event.pathNames[0]]);
					}else{
						$.nav._gohome();
					}
				}
			}
		    
		    /*
		    log('change: ' + serialize(event, /parameters|parametersNames|path|pathNames|queryString|value/));
		    var names = $.map(event.pathNames, function(n) {
		        return n.substr(0, 1).toUpperCase() + n.substr(1);
		    }).concat(event.parameters.id ? event.parameters.id.split('.') : []);
		    var links = names.slice();
		    var match = links.length ? links.shift() + ' ' + links.join('.') : 'Home';
		    $('a').each(function() {
		        $(this).toggleClass('selected', $(this).text() == match);
		    });
		    $.address.title([title].concat(names).join(' | '));
		    */
		    	
		});	
		}
	
		//メニュー切り替え
		$.e.bind("menuChanged",function(e,data){
		
			if(data == $.menuname.home && !$.flash.installed){
				data = $.menuname.home_tab;
			}
		
			if($.siteinit.currentmenu && !$.device.smartphone){
				if($.siteinit.currentmenu == $.menuname[data]) return;
			}
			
			if(data == $.menuname.activity){
				$.activity.loadRss();
			}

			//現在メニューを記憶
			var _tmpoldmenu = $.siteinit.currentmenu;
			$.siteinit.currentmenu = $.menuname[data];

			//顔メッセージ
			var add = "";
			if(!$.device.smartphone){
			if(data == $.menuname.home || data == $.menuname.home_tab){
				$.miniface.hide_full();
				$.message.hide();
			}else{
				$.miniface.show();
				$.message.update({message:$('.message:first',$.contents[String(data).toLowerCase()]).html()});
				add = '/' + data.toLowerCase() + '/';
			}
			}			
			
			if($.device.smartphone){
				$.util.scrollTo('#c' + $.siteinit.currentmenu);
	/*			$('html, body').animate(
							{ scrollTop: $.contents[$.siteinit.currentmenu].offset().top * $.view.zoom }
							);*/
				return;
			}
			$.address.value(add);

			
			//方向
			var reverse = false;
			var l = $.contents.list.length;
			var tmpflg = false;
			for(var i=0;i<l;i++){
				if($.contents.list[i] == $.siteinit.currentcontent){
					tmpflg = true;
				}
				if($.menuname.list[i] == $.siteinit.currentmenu){
					if(!tmpflg) reverse = true;
					break;
				}
				
			}
			
			//表示中のコンテンツを非表示 :
			if($.siteinit.currentcontent){
				var _tmpoldcontent = $.siteinit.currentcontent;
				
				if(_tmpoldcontent == $.contents.home){
					_tmpoldcontent.css({display:'none'});
					$('#chomeinner').html('');
				}else{
				
					_tmpoldcontent.
					stop(true).
					animate({marginTop:(reverse ? "" : "-") + '100px'}, {duration: 800, easing:"easeInExpo",
		    		 complete: function(){
		    		 	_tmpoldcontent.css({display:'none'})
		    		 	if($[_tmpoldmenu]){
		    		 		if($[_tmpoldmenu].stop){
			    		 		$[_tmpoldmenu].stop();
		    		 		}
		    		 	}
		    		}
				    });
				    
			    };
			}
			
			//コンテンツを表示 :下からスライドイン
			$.siteinit.currentcontent = $.contents[data];
			if($.siteinit.currentcontent == $.contents.home){
				setTimeout(function(){
					$.siteinit.currentcontent.css({display:'block'});
					embedSwf();
				}, _tmpoldcontent ? 800 : 1);
				
				//$.siteinit.currentcontent.css({visibility:'visible'});
				
			}else{			
			
				$.siteinit.currentcontent.
				stop(true).
				delay(900).
				animate({marginTop:(reverse ? "-" : "") + '100px'}, {duration: 1, easing:"linear",
	    		 complete: function(){
	    		 	$.siteinit.currentcontent.css({display:'block'});
	    		 	hideall($.siteinit.currentcontent);
	    		 	_tmpoldcontent.css({display:'none'})
	    		 	if($[$.siteinit.currentmenu]){
	    		 		if($[$.siteinit.currentmenu].start){
		    		 		$[$.siteinit.currentmenu].start();
	    		 		}
	    		 	}
	    		 }
			    }).
				animate({'marginTop': '0px'},800,'easeOutExpo');
				
			};

				
		})
		
		//コンテンツエリア調整
		if(!$.device.smartphone){
		$(window).bind("resize",onresize);
		function onresize(evt,data){
			$('.content').each(function(){
			
				if($(this).html() == $('#chome').html()) return;
					
				var masize = ($('#container').height() - $(this).height()) / 2;
				
				var animate = false;
				if(data) animate = data.animate;
				
				if(animate) $(this).stop(true).animate({paddingTop:masize + 'px'/*,marginBottom:masize + 'px'*/},600,'easeOutExpo');
				else $(this).stop(true).css({paddingTop:masize + 'px'/*,marginBottom:masize + 'px'*/});
			});
		}
		onresize();
		};
		
		$('.mail',this).each(function(){
			$(this).html('<a href="mailto:hello@quq.jp">hello@quq.jp</a>');
		})
		
		
		//コンテンツを非表示に
		function hideall(ex){
			var l = $.contents.list.length;
			for(var i=0;i<l;i++){
				if($.contents.list[i] != ex){
					$.contents.list[i].css({display:'none'})
				}
			}
		}
	
		//Flashの取り付け
		function embedSwf(){
			if(!$.flash.installed) return;
			var params = {
		  	scale:'noScale',
		  	salign:'lt',
		  	menu:'false',
		 	allowfullscreen :'true',
		 	wmode:'transparent'
			};
			swfobject.embedSWF("preloader.swf?v2", "chomeinner", "300", "200", "9.0.0","expressInstall.swf", {}, params, {});
		}
		if($.flash.installed) embedSwf();	
				
	};



	/*****************************************************************************************************/
	/* ACTIVITY *******************************************************************************************/
	/*****************************************************************************************************/
	$.activity = {isrssloaded:false};	
	$.fn.activity = function(option) {
		option = !option ? {} : option;
		$.activity.target = $(this);
		
		if($.device.smartphone) $.activity.loadRss();
		
		return $(this);
	}
	$.activity.loadRss = function(){
		if($.activity.isrssloaded) return;
		$.activity.isrssloaded = true;
	    $.ajax({
	        url: "http://quq.jp/blogrss.php",
	        type:"GET",
	        dataType:"xml",
	        timeout:1000,
	        cache: false,
	        error:function(){
				//alert("BLOG FEEDの読み込みに失敗しました。");
	        },
	        /* 成功時 */
	        success:function(xml){
	        
	        	var _html = "<ul>";
				$(xml).find("item").each(function(){
					var item_link=$(this).find("link").text();
					var item_title=$(this).find("title").text();
					_html += '<li><a href="' + item_link + '" target="_blank">' + item_title + '</a></li>'
				});
	        	_html += "</ul>";
	        	updateView(_html);
	        }
	    });
	    
	    function updateView(data){
	    	setTimeout(function(){
		    	updateViewExe(data);
	    	},1500)
	    }
	    function updateViewExe(data){
			$('.blogfeedloading',$.activity.target).html(data);
			
			//サイズ変更通知
			$(window).trigger("resize",[{animate:true}]);
	    }
	}
		
		
		
	/*****************************************************************************************************/
	/* CONTACT *******************************************************************************************/
	/*****************************************************************************************************/
	$.contact = {};	
	$.contact.target;
	$.contact.pd;
	$.contact._pdul;
	$.contact.form_locked = false;
	$.contact.nowsending = false;
	$.contact.prgrm = 'http://quq.jp/php/formcontact.php';
	$.contact.all_forms = $('#fm_name,#fm_mail,#fm_title,#fm_text');
	$.contact.isopened = false;
	$.fn.contact = function(option) {
			
		option = !option ? {} : option;
		var target = $(this);
		$.contact.target = target;
		$.contact.all_forms = $('#fm_cname,#fm_name,#fm_email,#fm_subject,#fm_text');
		$.contact.btn_submit = $('.submit a:first',$.contact.target);
		$.contact.btn_submit.click(function(){
			_close();
			$.contact.submit();
			return false;
		});
		
		//プルダウンの変更
		var _pd = $('select',this);
		$.contact.pd = _pd;
		_pd.css({display:'none'})

		var _pdhtml = '<ul class="pulldown">##li##</ul></p>';
		var tmphtml = '';
		var l = $('option',_pd).length;
		var cnt = 0;
		$('option',_pd).each(function(){
			if(cnt == 0) _pdhtml = '<p><a href="" class="pdswitch">' + $(this).html() + '</a>' + _pdhtml;
			else if(cnt == l-1) tmphtml += '<li class="hidden"><a href="" class="listlast" rel="'+cnt+'">' + $(this).html() + '</a></li>';
			else tmphtml += '<li class="hidden"><a href="" class="list" rel="'+cnt+'">' + $(this).html() + '</a></li>';
			cnt ++;
		});
		_pdhtml = _pdhtml.replace('##li##',tmphtml);
		var _pdul = $(_pdhtml);
		$.contact._pdul = _pdul;
		_pd.after(_pdul);
		
		$('.pdswitch',_pdul).click(function(e){	
			e.preventDefault();
			_open();
		})

		$('li a',_pdul).click(function(e){	
			e.preventDefault();
			_updateForm(Number($(this).attr('rel')));
			_updateView($(this).html().replace('・',''));
			_close();
		})
			
		$('input,textarea',target).each(function(){
			$(this).stop(true).focus(function(){
				_close();
			});
		});

		function _open(){
			if($.contact.isopened){
			_close();
			return;
			}
			if($.contact.form_locked) return;
			$.contact.isopened = true;

			$('li',_pdul).each(function(){
				if($(this).hasClass('hidden')){
					$(this).removeClass('hidden');
				}
			});
			
			$('input,textarea',target).each(function(){
				$(this).stop(true).animate({opacity:'.2'},200);
			});

			$('.pulldown',target).stop(true).animate({marginTop:'-10px'},0).animate({marginTop:'0px'},300,'easeOutExpo');

		}
		
		$.e.bind("menuChanged",_close);
		function _close(){
			if(!$.contact.isopened) return;
			$.contact.isopened = false;
			$('.pulldown',target).stop(true).animate({marginTop:'-20px'},{duration: 50, easing: "easeInExpo",
			complete:function(){
				$('li',_pdul).each(function(){
					if(!$(this).hasClass('hidden')){
						$(this).addClass('hidden');
					}
				});
			}
			});
			
			$('input,textarea',target).each(function(){
				$(this).stop(true).animate({opacity:'1'},200);
			});
		}
		
		function _updateForm(num){
			_pd.attr('selectedIndex', num);
		}
		
		function _updateView(val){
			$('.pdswitch',_pdul).html(val);
		}
		
		return $(this);
			
	}
	$.contact.start = function(){
	}
	$.contact.submit = function(){
		if($.contact.nowsending){
			alert('送信中です。');
			return;
		}
		if($.contact.checkdata()){
			//ajax 送信 
			/*alert($('#fm_cname').val());
			alert($('#fm_name').val());
			alert($('#fm_subject').val());
			alert($('#fm_email').val());
			alert($('#fm_text').val());*/
			
			//フォームをロック
			$.contact.setSendingView();
			$.contact.formlock(true);
			$.contact.nowsending = true;
			$.ajax({
			type:"post",
			url: $.contact.prgrm,
			data :{'submit':"true",'companyname':$('#fm_cname').val(),'contactname':$('#fm_name').val(),'subject':$('#fm_subject').val(),'email':$('#fm_email').val(),'message':$('#fm_text').val()},
			error:function(){
				$.contact.nowsending = false;
				//フォームをアンロック
				$.contact.formlock(false);
			},
	        success:function(msg,status){
				$.contact.nowsending = false;
				//フォームをアンロック
				$.contact.formlock(false);
				
				var result="";
				$(msg).find("result").each(function(){
					result = $(this).text();
				});
				if(result == "true"){
					//
					$.contact.form_reset();
					$.contact.setSuccessView();
				}else{
					alert('送信エラー\n\nしばらくたってから再度送信してください。');
					$.contact.setDefaultView();
				}
			},
			error:function(){
			}
			});
		}
		return false;
	}
	//リセット
	$.contact.form_reset = function(){
		$.contact.pd.attr('selectedIndex', 0);
		$('.pdswitch',$.contact._pdul).html('お問い合わせ内容');
		$('#fm_cname').val('');
		$('#fm_name').val('');
		$('#fm_email').val('');
		$('#fm_subject').val('');
		$('#fm_text').val('');
	}
	//内容チェック
	$.contact.checkdata = function(){
	
		var basemsg = '入力内容に不備があります。' + "\n"; 
		var errmsg = basemsg;

		//company name
		// ｰ> チェックなし
	
		//name
		if($('#fm_name').val() == ''){
			errmsg += "\n" + "・お名前を入力してください";
		}
		
		//mail
		if($('#fm_email').val() == ''){
			errmsg += "\n" + "・メールアドレスを入力してください";
		}else if(!$('#fm_email').val().match(/.+@.+\..+/)){
			//表記チェック
			errmsg += "\n" + "・メールアドレスの形式が正しくありません";
		}
		
		//種類
		if($('#fm_subject').val() == ''){
			errmsg += "\n" + "・種類を選択してください";;
		}
		
		//text
		if($('#fm_text').val() == ''){
			errmsg += "\n" + "・お問い合わせの内容を入力してください";
		}
		
		if(errmsg == basemsg){
			return true;
		}else{
			errmsg += "\n\n" + '確認してもう一度送信してください。'; 
			alert(errmsg);
			return false;
		}	
	}
	//
	$.contact.formlock = function(b){
		$.contact.form_locked = b;
		if(b){
			//lock
			$.contact.all_forms.each(function(){
				$(this).attr('disabled','disabled');
			});
			$.contact.btn_submit.addClass('lock');
		}else{
			//unlock
			$.contact.all_forms.each(function(){
				$(this).attr('disabled','');
			});
			$.contact.btn_submit.removeClass('lock');
		}
	}
	$.contact.sendingsiid;
	$.contact.setSendingView = function(b){
		$.contact.btn_submit.html("送信中");
		clearInterval($.contact.sendingsiid);
		var cnt = 0;
		$.contact.sendingsiid = setInterval(function(){
			if(cnt == 10){
				$.contact.btn_submit.html("送信中");
				cnt = 0;
			}
			cnt ++;
			$.contact.btn_submit.html($.contact.btn_submit.html() + ".");
		},1000);
	}
	$.contact.setDefaultView = function(b){
		clearInterval($.contact.sendingsiid);
		$.contact.btn_submit.html("送信");
	}
	$.contact.setErrorView = function(b){
		clearInterval($.contact.sendingsiid);
		$.contact.btn_submit.html("送信エラー");
	}
	$.contact.setSuccessView = function(b){
		clearInterval($.contact.sendingsiid);
		$.contact.btn_submit.html("送信完了！");
		setTimeout($.contact.setDefaultView,3000);
	}








	/*****************************************************************************************************/
	/* home for tablet ***********************************************************************************/
	/*****************************************************************************************************/
	$.home_tab = {};
	$.home_tab.list_face = [];
	$.home_tab.list_trans = [];
	$.fn.home_tab = function(option) {		
			
		option = !option ? {} : option;
		var target = $(this);
		$.home_tab.target = $(this);
		$.home_tab.innerbox = $('<div>' + $.home_tab.target.html() + '</div>');
		target.html('');
		target.append($.home_tab.innerbox);
		$.home_tab.facebox = $('.facebox:first',$(this));
		$.home_tab.face.sizeWidth = $.home_tab.facebox.width();
		
		$.home_tab.list_face.push($.home_tab.face.facedefault);
		$.home_tab.list_face.push($.home_tab.face.face3d_0);
		$.home_tab.list_face.push($.home_tab.face.face3d_1);
		$.home_tab.list_face.push(function(){});
		$.home_tab.list_face.push($.home_tab.face.sleep);
		$.home_tab.list_face.push($.home_tab.face.afraid);
		$.home_tab.list_face.push($.home_tab.face.up);
		$.home_tab.list_face.push($.home_tab.face.fight);
		
		$.home_tab.list_trans.push($.home_tab.transition.rotate);
		$.home_tab.list_trans.push($.home_tab.transition.rightIn);
		$.home_tab.list_trans.push($.home_tab.transition.leftIn);
		$.home_tab.list_trans.push($.home_tab.transition.faceRandom);
		$.home_tab.list_trans.push($.home_tab.transition.down);
		$.home_tab.list_trans.push($.home_tab.transition.vibe);
		$.home_tab.list_trans.push($.home_tab.transition.up);
		$.home_tab.list_trans.push($.home_tab.transition.rightIn);
		
	};
	
	$.home_tab.loopid;
	$.home_tab.start = function(){
		$.home_tab.next();	
	}
	$.home_tab.stop = function(){
		$.home_tab.exeindex = 0;
		clearTimeout($.home_tab.loopid);
		$.home_tab.transition.reset();		
	}
	
	$.home_tab.exeindex = 0;
	$.home_tab.next = function(){
		$.home_tab.exeindex = $.home_tab.exeindex >= $.home_tab.list_face.length ? 0 : $.home_tab.exeindex;
		$.home_tab.transition.reset();
		setTimeout($.home_tab.list_face[$.home_tab.exeindex],1);
		setTimeout($.home_tab.list_trans[$.home_tab.exeindex],1);
		$.home_tab.exeindex++;
		$.home_tab.loopid = setTimeout($.home_tab.next,4000);
	}
	
	
	//動き
	$.home_tab.transition = {};
	$.home_tab.transition.reset = function(){
		$.home_tab.innerbox.stop(true);;
		clearInterval($.home_tab.transition.siid);
		$.home_tab.innerbox.css({
			zoom:1,
			marginLeft:'0px',
			marginRight:'0px',
			opacity:'1'
		});
	}
	$.home_tab.transition.vibe = function(repeat){
		if(repeat){
		$.home_tab.innerbox.stop(true).
		delay(1000).
		animate({marginLeft:'-20px'},{duration: 1}).
		delay(10).
		animate({marginLeft:'20px'},{duration: 1}).
		delay(10).
		animate({marginLeft:'-20px'},{duration: 1}).
		delay(10).
		animate({marginLeft:'20px'},{duration: 1,complete:function(){$.home_tab.transition.vibe(true)}});
		}else{
		$.home_tab.innerbox.stop(true).
		animate({marginLeft:'-20px'},{duration: 1}).
		delay(10).
		animate({marginLeft:'20px'},{duration: 1}).
		delay(10).
		animate({marginLeft:'-20px'},{duration: 1}).
		delay(10).
		animate({marginLeft:'20px'},{duration: 1,complete:function(){$.home_tab.transition.vibe(true)}});
		}
	}
	$.home_tab.transition.down = function(){
		$.home_tab.innerbox.stop(true).
		animate({marginTop:'-400px',opacity:"0"},{duration: 0}).
		delay(10).
		animate({marginTop:'0px',opacity:"1"},{duration: 600,easing:"easeInOutExpo"});
	}
	$.home_tab.transition.up = function(){
		$.home_tab.innerbox.stop(true).
		animate({marginTop:'400px',opacity:"0"},{duration: 0}).
		delay(10).
		animate({marginTop:'0px',opacity:"1"},{duration: 600,easing:"easeInOutExpo"});
	}
	$.home_tab.transition.leftIn = function(){
		$.home_tab.innerbox.stop(true).
		animate({marginLeft:'-400px',opacity:"0"},{duration: 0}).
		delay(10).
		animate({marginLeft:'0px',opacity:"1"},{duration: 600,easing:"easeInOutExpo"});
	}
	$.home_tab.transition.rotate = function(){
		$.home_tab.innerbox.stop(true).
		animate({opacity:"0"},{duration: 0}).
		rotate('0deg').animate({'rotate': 1080,"opacity":"1"},1300,'easeOutExpo');
	}
	$.home_tab.transition.rightIn = function(){
		$.home_tab.innerbox.stop(true).
		animate({marginLeft:'400px',opacity:"0"},{duration: 0}).
		delay(10).
		animate({marginLeft:'0px',opacity:"1"},{duration: 600,easing:"easeInOutExpo"});
	}
	$.home_tab.transition.faceRandom = function(){
		$.home_tab.transition.siid = setInterval($.home_tab.face.random,100);
		$.home_tab.innerbox.stop(true).
		animate({opacity:"0"},{duration: 0}).
		animate({opacity:"1"},{duration: 3000,easing:"easeInOutExpo"});
	}

	//顔
	$.home_tab.face = {};
	$.home_tab.face.count = 7;
	$.home_tab.face.sizeWidth = 0;
	$.home_tab.face.facedefault = function(){
		$.home_tab.face.set(0,0);
	}
	$.home_tab.face.face3d_0 = function(){
		$.home_tab.face.set(- $.home_tab.face.sizeWidth);
	}
	$.home_tab.face.face3d_1 = function(){
		$.home_tab.face.set(- $.home_tab.face.sizeWidth * 2);
	}
	$.home_tab.face.afraid = function(){
		$.home_tab.face.set(- $.home_tab.face.sizeWidth * 3);
	}
	$.home_tab.face.sleep = function(){
		$.home_tab.face.set(- $.home_tab.face.sizeWidth * 4);
	}
	$.home_tab.face.up = function(){
		$.home_tab.face.set(- $.home_tab.face.sizeWidth * 5);
	}
	$.home_tab.face.fight = function(){
		$.home_tab.face.set(- $.home_tab.face.sizeWidth * 6);
	}
	$.home_tab.face.random = function(num){
		var trgX = -1 * $.home_tab.face.sizeWidth * Math.floor(Math.random()*($.home_tab.face.count));
		$.home_tab.face.set(trgX,0);
	}
	$.home_tab.face.set = function(tx,ty){
		ty = ty == undefined ? 0 : ty;
		$.home_tab.facebox.css({backgroundPosition:tx + 'px ' + ty + 'px'});	
	}
	






			
	/* ロゴ部 *******************************************************************************************/
	$.nav = {target:undefined};	
	$.fn.nav = function(option) {
			
		option = !option ? {} : option;
		var target = $(this);
		
		$.nav.home_kazaguruma = $('.kazaguruma:first');
		$.nav.kazaguruma = $('#nav .kazaguruma:first');
		$.nav.list = [$.menuname.about,$.menuname.works,$.menuname.activity,$.menuname.contact];
		$.nav.l = $.nav.list.length;
		$.nav.buttons = [];
		
		$("#mark a:first").click(function(e){
			e.preventDefault();
			$.nav._gohome();
		});	
		$('#navmenu li a').each(function(){
			$.nav.buttons.push($(this));
			$(this).click(function(e){
				if($.nav._select($(this).html())){
					e.preventDefault();
				}
			})
		});
		
		$('a.menulink').each(function(){
			$(this).click(function(e){
				$.nav._select($(this).attr('href').replace('#',''));
				e.preventDefault();
			})
		});
		
		
		return $(this);	
	}
	$.nav._gohome = function(){
		$.nav._clearall();
		$.e.trigger("menuChanged",[$.menuname.home]);
		
		if(!$.device.smartphone){
		$.nav.home_kazaguruma.removeClass('hidden');
		$.nav.home_kazaguruma.stop(true).rotate('0deg').animate({'rotate': 360},1000,'easeOutExpo');
		}
	}
	$.nav._select = function(_name){
		var index = 0;
		for(var i=0;i<$.nav.l;i++){
			if(_name == $.nav.list[i]){
				index = i;
				break;
			}
		}
		if(i==$.nav.l) return false;
		$.e.trigger("menuChanged",[_name]);
		
		if(!$.device.smartphone){
			$.nav._clearall();
			$.nav.buttons[index].addClass('selected');
			$.nav.kazaguruma.addClass('m' + index);
			$.nav.kazaguruma.stop(true).rotate('0deg').animate({'rotate': 360},1000,'easeOutExpo');
			$.nav.home_kazaguruma.addClass('hidden');
		}else{
			return false;
		}
	}
	$.nav._clearall = function(){
		var index = 0;
		for(var i=0;i<$.nav.l;i++){
			 $.nav.buttons[i].removeClass('selected');
			 $.nav.kazaguruma.removeClass('m' + i);
		}
	}
		
	/**
	 * ロゴ部
	 */	
	$.logo = {};	
	$.fn.logo = function(option) {
			
		option = !option ? {} : option;
		var target = $(this);
			
		//クックジェイピー文字
		var _txttrg = $('#text',this);
		var _txtflg = 0;
		setInterval(updateTextLogo,6000)
		function updateTextLogo(){
			var trgY = _txtflg == 1 ? -176 : -151;
			_txttrg.stop(true).animate({backgroundPosition:'(0px ' + trgY + 'px)'},{duration: 300/*, easing: "easeInOutExpo"*/,
			complete:function(){
				if(_txtflg == 1){
					_txttrg.stop(true).animate({backgroundPosition:'(0px -126px)'},{duration: 0});
					_txtflg = 0;
				}else{
					_txtflg = 1;
				}
			}
			});
		}
		
		return $(this);
			
	}	
	
	/* ロゴ部 顔 *******************************************************************************************/
	$.miniface = {isshow:false,sleepsiid:undefined,issleep:false,faceobj:$('<p></p>')};	
	$.fn.miniface = function(option) {			
		option = !option ? {} : option;
		$.miniface.target = $(this);
		return $(this);
	}	
	$.miniface.sleepTimerUpdate = function(){
		clearTimeout($.miniface.sleepsiid);
		if($.miniface.issleep){
			$.miniface.face_default();
			$.miniface.issleep = false;
		}
		$.miniface.faceobj.stop().delay(8000).callfn($.miniface.face_sleep);
	}
	$.miniface.show = function(){
		$.miniface.sleepTimerUpdate();
		if($.miniface.isshow){
			var func = $.miniface.face_picpic;
			func();
			return;
		}
		$.miniface.isshow = true;
		$.miniface.target.stop(true).
		css({backgroundPosition:'-48px 0px'}).
		animate({backgroundPosition:'0px 0px'},{duration: 1000});
	}
	$.miniface.hide_half = function(){
		$.miniface.isshow = false;
		$.miniface.face_default();
		$.miniface.target.stop(true).animate({backgroundPosition:'-12px 0px'},{duration: 400});	
	}
	$.miniface.hide_full = function(){
		if(!$.miniface.isshow) return;
		clearTimeout($.miniface.sleepsiid);
		$.miniface.isshow = false;
		var psY = 0;
		$.miniface.target.stop(true).
		css({backgroundPosition:'0px ' + '-480px'}).
		delay(300).
		animate({backgroundPosition:'-48px -480px'},{duration: 400});	
	}
	//顔：標準
	$.miniface.face_default = function(){
		var psY = 0;
		$.miniface.target.stop(true).css({backgroundPosition:'0px ' + psY + 'px'});	
	}
	//顔：ランダム
	$.miniface.face_random = function(){
		var cnt = 8;
		var psY = -Math.floor(Math.random()*(cnt)) * 60;
		$.miniface.target.stop(true).css({backgroundPosition:'0px ' + psY + 'px'},{duration: 400});	
	}
	//顔：上にぴくぴく
	$.miniface.face_picpic = function(){
		if(!$.miniface.isshow) return;
		$.miniface.face_default();
		var psY = -360;
		$.miniface.target.stop(true).
		delay(1000).
		animate({backgroundPosition:'0px ' + psY + 'px'},{duration:0}).
		delay(100).
		animate({backgroundPosition:'0px ' + '0px'},{duration:0}).
		delay(100).
		animate({backgroundPosition:'0px ' + psY + 'px'},{duration:0}).
		delay(100).
		animate({backgroundPosition:'0px ' + '0px'},{duration:0}).
		delay(100);
	}
	//顔：眠り
	$.miniface.face_sleep = function(){
		if(!$.miniface.isshow) return;
		$.miniface.issleep = true;
		var psY = -240;
		$.miniface.target.stop(true).css({backgroundPosition:'0px ' + psY + 'px'});	
	}
		
		
		
	/* メッセージ表示 *******************************************************************************************/
	$.message = {isshow:false};	
	$.fn.message = function(option) {
		option = !option ? {} : option;
		$.message.toptarget = $(this);
		$.message.target = $('.textbox:first',this);
		var tmpdom = $('<div></div>');
		$.message.target.append(tmpdom);
		$.message.target = tmpdom;
		$.message._msgtxt;
		$.message.l;
		$.message.c = 0;
		$.message.siid;
		return $(this);
	}
	$.message.update = function(o){
		$.message._msgtxt = o.message;
		
		//
		if(!$.message.isshow) $.message.toptarget.css({visibility:'visible'});
		
		//位置計算
		$.message.target.html($.message._msgtxt);
		var txtheight = $.message.target.height();
		var topps = 80 - txtheight + (Math.floor(Math.random()*(8)) - 4);
//		$.message.toptarget.css({top:topps + 'px'});
//		$.message.toptarget.stop().animate({opacity:'0',top:topps + 'px'},0).animate({opacity:'1'},300);
		$.message.toptarget.stop(true).
		animate({opacity:'0',top:(topps + 10) + 'px',left:'140px'},0).
		delay($.message.isshow ? 100 : 800).
		animate({opacity:'1',top:topps + 'px',left:'150px'},300);
		
		//メッセージレンダ
		$.message._msgtxt = $.message._msgtxt.replace('<br>','@')
		$.message.l = $.message._msgtxt.length;
		$.message.reset();
		//$.message.target.animate({opacity:'0'},0).animate({opacity:'1'},300);
		$.message.startRender();
		
		$.message.isshow = true;
	}
		
	$.message.startRender = function(){
		$.message.stopRender();
		$.message.siid = setTimeout(function(){
			$.message.siid = setInterval($.message.render,20);
		},1000);
		$.message.render();
	}
		
	$.message.stopRender = function(){
		clearInterval($.message.siid);
		clearTimeout($.message.siid);
	}
		
	$.message.render = function(){
		if($.message.c == $.message.l+1){
			$.message.stopRender();
			return;
		}
		var _fm0 = '<span style="color:#000;">##MSG##</span>';
		var _fm1 = '<span style="color:#c6c6c6;">##MSG##</span>';
		var _html = _fm0.replace('##MSG##', $.message._msgtxt.substr(0,$.message.c));  
		_html += _fm1.replace('##MSG##', $.message._msgtxt.substr($.message.c));
		_html = _html.replace('@','<br>');
		$.message.target.html(_html);
		$.message.c ++;
	}
		
	$.message.reset = function(){
		$.message.c = 0;
	}

	$.message.hide = function(){
		$.message.isshow = false;
		if(jQuery.support.opacity) $.message.toptarget.stop(true).animate({opacity:'0'},300);
		else $.message.toptarget.css({visibility:'hidden'});
	}		
	
	
	
	
	
	
	/* 時間表示 *******************************************************************************************/
	$.quqtime = {};	
	$.quqtime.eventrunning = false;
	$.quqtime.closetimeid = undefined;
	$.fn.quqtime = function(option) {
			
		option = !option ? {} : option;
		var target = $(this);
		
		var box_h = [$('<div class="num"></div>'),$('<div class="num"></div>')];
		var box_s = [$('<div class="num"></div>'),$('<div class="num"></div>')];
		var box_am = $('<div class="num t_am"></div>');
		var box_pm = $('<div class="num t_pm hidden"></div>');
		var boxs = [];
		var isPM = false;
		var quqtimelock = false;
		boxs = boxs.concat(box_h);
		boxs = boxs.concat(box_s);
		var t_nums = [0,0,0,0];
		var tmp_t_nums = ['z','z','z','z'];
		
		
		//番号画像位置を取得
		for(var i=0;i<=9;i++){
		var _tmp = $('<div class="n' + i + ' hidden"></div>');
		target.append(_tmp);
		option['n' + i + "_bgpy"] = $.util.getBackgroundPositionY(_tmp) * ($.device.smartphone ? 1 / $.view.zoom : 1);
		_tmp.remove();
		}	
			
		//レイアウト	
		target.append(box_h[0]);
		target.append(box_h[1]);
		target.append($('<div class="t_colon num"></div>'))
		target.append(box_s[0]);
		target.append(box_s[1]);
		target.append(box_am);
		target.append(box_pm);
			
		setInterval(render,1000);
		render();
		if(location.search.indexOf('quqtime=true') != -1){
			setTimeout(show,5000);
		}
		
		function render(){
			var _d = new Date();
			var _d_h = _d.getHours();
			var tmpPM = _d_h >= 12 ? true : false;
			_d_h = _d_h >= 12 ? String(_d_h - 12) : String(_d_h);
			var _d_m = String(_d.getMinutes());
			t_nums[0] = _d_h.length == 1 ? '0' : Number(_d_h.substr(0,1));
			t_nums[1] = _d_h.length == 1 ? Number(_d_h.substr(0,1)) : Number(_d_h.substr(1,1));
			t_nums[2] = _d_m.length == 1 ? '0' : Number(_d_m.substr(0,1));
			t_nums[3] = _d_m.length == 1 ? Number(_d_m.substr(0,1)) : Number(_d_m.substr(1,1));

			/*t_nums[0] = '0';
			t_nums[1] = '9';
			t_nums[2] = '2';
			t_nums[3] = '9';*/
			
			//quqtime
			if(t_nums[1] == '9' && t_nums[2] == '2' && t_nums[3] == '9' && !quqtimelock){
				if(!$.quqtime.eventrunning){
					show();
				}
					
			}
			
			for(var i=0;i<4;i++){
				if(tmp_t_nums[i] != t_nums[i]){
					var n_yp = option['n' + t_nums[i] + "_bgpy"];
					var animtime = Math.abs(n_yp - $.util.getBackgroundPositionY(boxs[i])) * 20; 
					var animate = !$.device.smartphone;
					if(!animate) boxs[i].stop(true).css({backgroundPosition:'0px ' + String(n_yp) + 'px'});
					else boxs[i].stop(true).animate({backgroundPosition:'0px ' + String(n_yp) + 'px'},animtime);
				}
			}
			
			if(tmpPM != isPM){
				isPM = tmpPM;
				if(isPM){
					$('.t_am:first',target).addClass('hidden');
					$('.t_pm:first',target).removeClass('hidden');
				}else{
					$('.t_am:first',target).removeClass('hidden');
					$('.t_pm:first',target).addClass('hidden');
				}
			}
			
			tmp_t_nums = t_nums.concat([]);
		}
		
		//ロックの解除
		function clearQuqTimeLock(){
			quqtimelock = false;
		}
		
		function reset(){
			$('.ttext').addClass('hide');
			$('.ttext').css({display:'block'});
			$('.t929').addClass('hide');
			$('.ttitle').addClass('hide');
			$('.tface').addClass('hide');
			$('.sec0').addClass('hide');
			$('.sec1 .t0').addClass('hide');
			$('.sec1 .t1').addClass('hide');
			$('.tsample').addClass('hide');
			$('.sec3').addClass('hide');
			$('.btn_close').addClass('hide');
		}
			
		//表示
		function show(){
			if($.quqtime.eventrunning) return;
			$.quqtime.eventrunning = true;
			
			//発生後一定期間lock
			quqtimelock = true;
			setTimeout(clearQuqTimeLock,120000);
			
			$('#quqtimev').css({display:'block'})

			$('.ttext').delay(400).
			callChfn('removeClass',['hide']).
			delay(1500).
			callChfn('css',[{display:'none'}]);
				
			$('.t929').delay(1900).callChfn('removeClass',['hide']);
			$('.ttitle').delay(2200).callChfn('removeClass',['hide']);
			$('.tface').delay(2600).callChfn('removeClass',['hide']);
			$('.sec0').delay(4200).callChfn('removeClass',['hide']);
			
			$('.sec1 .t0').delay(4500).callChfn('removeClass',['hide']);
			$('.sec1 .t1').delay(4500).callChfn('removeClass',['hide']);
			$('.tsample').delay(4500).callChfn('removeClass',['hide']);
						
			$('.sec3').delay(5500).callChfn('removeClass',['hide']);
			$('.btn_close').delay(6500).callChfn('removeClass',['hide']);
			
			$.quqtime.closetimeid = setTimeout(hide,30000);
			
			
			/* 旧：
			
			setTimeout(function(){$('.ttext').removeClass('hide');} , 400);
			setTimeout(function(){
				$('.ttext').css({display:'none'});
			} , 1900);

			setTimeout(function(){$('.t929').removeClass('hide');} , 1900);
			setTimeout(function(){$('.ttitle').removeClass('hide');} , 2200);
			setTimeout(function(){$('.tface').removeClass('hide');} , 2600);
			setTimeout(function(){$('.sec0').removeClass('hide');} , 4200);


			setTimeout(function(){$('.sec1 .t0').removeClass('hide');} , 4500);			
			setTimeout(function(){$('.sec1 .t1').removeClass('hide');} , 4500);			
			setTimeout(function(){$('.tsample').removeClass('hide');} , 4500);			
			
			setTimeout(function(){$('.sec3').removeClass('hide');} , 5500);			
			setTimeout(function(){$('.btn_close').removeClass('hide');} , 6500);			
			*/
		}
		
		//非表示
		$('#quqtimev a.btn_close:first').click(hide);
		function hide(){
			clearTimeout($.quqtime.closetimeid);
			$.quqtime.eventrunning = false;
			$('#quqtimev').css({display:'none'});
			reset();
			return false;
		}
					
		return $(this);
			
	}
	
	
	/**
	 * Utils
	 */	
	$.util = {};
	$.util.getBackgroundPositionY = function(target){
		try{
			return Number(target.css('backgroundPosition').split(" ")[1].split('px')[0]);
		}catch(err){
			return Number(target.css('backgroundPositionY').replace('px',''));
		}
	}
	$.util.getBackgroundPositionX = function(target){
		try{
			return Number(target.css('backgroundPosition').split(" ")[0].split('px')[0]);
		}catch(err){
			return Number(target.css('backgroundPositionX').replace('px',''));
		}
	}

	$.util.scrollTo = function(trgid){
		var topp = 0;
		var hashid = trgid;
		var obj = {}
		obj.duration = 500;
		obj.easing   = "easeInOutExpo";
		obj.complete = function(){
			$(window).trigger('scroll');
		}
		if(trgid == "#pagetop"){
			topp = 0;
		}else{
			topp += $(trgid + ':first').offset().top * $.view.zoom;
		}
		$('html,body').stop(true).animate(
							{ scrollTop: topp }, 
							obj
							);
		return false;
	}
	
	$(window).bind("resize",onresize);
	onresize();
			
	/*
	* 開始
	*/
	if($._ua.ltIE6) return;
	
	$(document).nav();	
	$(document).siteinit();
	$('#logo').logo();
	$('#miniface').miniface();
	$('#message').message();
	$('#quqtime').quqtime();
	if(!$.flash.installed){
		$("#chome_tab").home_tab();
	}
	$('#ccontact').contact();
	$('#cactivity').activity();

	$(window).bind("scroll",onscroll);
	onscroll();
  			
})(jQuery)
