var win;
var hg,nhg;
var pnow,pmove;
var obj,cnt;
var myTimer,iniTimer;
var graphic_ref = new Array("グラフィック","graphic","パッケージ","package","デザイン","design","ロゴ","logo","広告","チラシ","パンフレット","リーフレット","名刺","看板","キャラクター","マスコット","ツール");
var graphic_obj = new Array("graphic","package","logo","goods");
var web_ref = new Array("ウェブ","WEB","サイト","site","ムービー","movie","フラッシュ","flash","スマートフォン","smartphone","イベント","event","プロモーション","promotion","モバイル","mobile","携帯","販促","広告","アイフォン","iphone","制作","企画","CM","テレビ","TV","ラジオ","radio");
var web_obj = new Array("web","movie","promotion","event");
$(function(){
	var ua = navigator.userAgent;
	if(ua.indexOf('iPhone') > -1 || ua.indexOf('iPad') > -1 || ua.indexOf('iPod')  > -1 || ua.indexOf('Android') > -1 || ua.indexOf('MSIE') > -1){
		/* event rollover catch chage */
		var cobj = $("#maincontents .sub_wrapper .itembox");
		for(var i=0; i<8; i++){
			var _cobj = $(cobj)[i];
			$(_cobj).attr("name","0"+(i+1));
			$(_cobj).hover(function(){
				$(".textBox").animate({"top":46+"px","left":22+"px"},500)
				var nm =  "txtbox" + String($(this).attr("name"));
				var rep = $(".textBox img").attr("src").match(".+/(.+?)\.[a-z]+$")[1];
				var url = $(".textBox img").attr("src").replace(rep,nm);
				$(".textBox").html("");
				$("<img />").attr("src",url).appendTo(".textBox");
			},function(){})
		}
	}else{
		// initialize
		if(ua.indexOf('IE') > -1){$("#footer").css({"position":"static","bottom":"","left":""})}
		else{$("#footer").css({"position":"fixed","bottom":"0","left":"0"});}
		cnt = 0;
		$("#maincontents .sub_wrapper div").css("opacity",0);
		$.post('ref.php',{mode:'referer',refer:document.referrer},
			function(res){
				res = res.split(" ");
				if(!res=="" && res!='NO'){
					/* create sort object */
					var _sobj=new Array();
					var _obj = $("#maincontents .sub_wrapper .itembox");
					var _cnt = new Array($(_obj).length);
					var boxcnt=1;
					/* GRAPHIC */
					/* KEYWORD SWITCH */
					var sw=false,gsw=false;
					$(res).each(function(i, elem){
						if(graphic_ref.indexOf(elem.toLowerCase())>-1 || graphic_ref.indexOf(elem.toUpperCase())>-1){
							sw=true
						}
					});
					if(sw){
						/* MATCH */
						$(_obj).each(function(i, elem){
							$(res).each(function(j, key){
								$(graphic_obj).each(function(l,w){
									if(($(elem).attr("class").indexOf(key.toLowerCase())>-1 || $(elem).attr("class").indexOf(key.toUpperCase())>-1) && ($(elem).attr("class").indexOf(w.toLowerCase())>-1 || $(elem).attr("class").indexOf(w.toUpperCase())>-1)){
										/* set object */
										_sobj[boxcnt]=elem;
										boxcnt++
										_cnt[i]=i;
										gsw=true;
									}
								});
							});
						});
					}
					/* WEB */
					/* KEYWORD SWITCH */
					sw=false;var wsw=false;
					$(res).each(function(i, elem){
						if(web_ref.indexOf(elem.toLowerCase())>-1 || web_ref.indexOf(elem.toUpperCase())>-1){
							sw=true
						}
					});
					if(sw){
						/* MATCH */
						$(_obj).each(function(i, elem){
							$(res).each(function(j, key){
								$(web_obj).each(function(l,w){
									if(($(elem).attr("class").indexOf(key.toLowerCase())>-1 || $(elem).attr("class").indexOf(key.toUpperCase())>-1) && ($(elem).attr("class").indexOf(w.toLowerCase())>-1 || $(elem).attr("class").indexOf(w.toUpperCase())>-1)){
										/* set object */
										_sobj[boxcnt]=elem;
										boxcnt++;
										_cnt[i]=i;
										wsw=true;
									}
								});
							});
						});
					}
					if(gsw){
						/* GRAPHIC RELATION */
						$(_obj).each(function(i, elem){
							$(graphic_obj).each(function(j, key){
								if($(elem).attr("class").indexOf(key.toLowerCase())>-1 || $(elem).attr("class").indexOf(key.toUpperCase())>-1){
									/* sort count */
									if(typeof _cnt[i]=='undefined'){
										/* set object */
										_sobj[boxcnt]=elem;
										boxcnt++;
										_cnt[i]=i;
									}
								}
							});
						});
					}
					if(wsw){
						/* WEB RELATION */
						$(_obj).each(function(i, elem){
							$(web_obj).each(function(j, key){
								if($(elem).attr("class").indexOf(key.toLowerCase())>-1 || $(elem).attr("class").indexOf(key.toUpperCase())>-1){
									/* sort count */
									if(typeof _cnt[i]=='undefined'){
										/* set object */
										_sobj[boxcnt]=elem;
										boxcnt++;
										_cnt[i]=i;
									}
								}
							});
						});
					}
					/* OTHER */
					$(_obj).each(function(i, elem){
						/* sort count */
						if(typeof _cnt[i]=='undefined'){
							/* set object */
							_sobj[boxcnt]=elem;
							boxcnt++;
							_cnt[i]=i;
						}
					});
					/* CATCH BOX */
					$("#maincontents .sub_wrapper .catchbox").each(function(i, elem){
						_sobj[0]=elem;
					});
					/* sort by referer */
					$("#maincontents .sub_wrapper").html($(_sobj));
				}
				/* event rollover catch chage */
				var cobj = $("#maincontents .sub_wrapper .itembox");
				for(var i=0; i<8; i++){
					var _cobj = $(cobj)[i];
					$(_cobj).attr("name","0"+(i+1));
					$(_cobj).hover(function(){
						$(".textBox").animate({"top":46+"px","left":22+"px"},500)
						var nm =  "txtbox" + String($(this).attr("name"));
						var rep = $(".textBox img").attr("src").match(".+/(.+?)\.[a-z]+$")[1];
						var url = $(".textBox img").attr("src").replace(rep,nm);
						$(".textBox img").attr("src",url);
					},function(){})
				}
				/* enjoy the time of eve ? */
				obj = $("#maincontents .sub_wrapper div");
				iniTimer = setInterval("init()",100);
			}
		);
	}
});
function init(){
	if(cnt!=$(obj).size()){
		var _obj = $(obj)[cnt];
		$(_obj).animate({"opacity":1},1000,"easeOutCubic");
		cnt++;
	}else{
		clearInterval(iniTimer);
		$("#scroll").fadeIn(500);
		/* dragable */
		hg = $(window).height();
		nhg = (hg - 384);
		if(nhg > 105){
			var pp = nhg + "px";
			$("#maincontents").css({"height":pp,"overflow":"hidden"});
			$("#scroll li").css({"height":pp});
			$("#scroll img").draggable({
				axis:"y",
				containment:$("#scroll li"),
				drag:function(){
					pnow = Number($(this).css("top").replace("px","")) / (nhg-103);
					pmove = - (($("#maincontents .sub_wrapper").height()-nhg+20) * pnow);
					$("#maincontents .sub_wrapper").css({"top":pmove+"px"});
				}
			});
			/** Initialization code. 
			 * If you use your own event management code, change it as required.
			 */
			if (window.addEventListener) window.addEventListener('DOMMouseScroll', wheel, false);
			window.onmousewheel = document.onmousewheel = wheel;
		}
		
		/* change window size */
		win = $(window).width();
		myTimer = setInterval("background()",200);
	}
}
function handle(delta) {
	if (delta < 0){
		// 下方向にまわした場合の処理
		if(Number($("#scroll img").css("top").replace("px",""))+10 < (nhg-103)){
			$("#scroll img").css("top",(Number($("#scroll img").css("top").replace("px",""))+10)+"px");
			pnow = Number($("#scroll img").css("top").replace("px","")) / (nhg-103);
			pmove = - (($("#maincontents .sub_wrapper").height()-nhg+20) * pnow);
			$("#maincontents .sub_wrapper").css({"top":pmove+"px"});
		}
	}else{
		// 上方向にまわした場合の処理
		if(Number($("#scroll img").css("top").replace("px",""))-10 > 0){
			$("#scroll img").css("top",(Number($("#scroll img").css("top").replace("px",""))-10)+"px")
			pnow = Number($("#scroll img").css("top").replace("px","")) / (nhg-103);
			pmove = - (($("#maincontents .sub_wrapper").height()-nhg+20) * pnow);
			$("#maincontents .sub_wrapper").css({"top":pmove+"px"});
		}else{
			$("#scroll img").css("top",0+"px");
			pnow = Number($("#scroll img").css("top").replace("px","")) / (nhg-103);
			pmove = - (($("#maincontents .sub_wrapper").height()-nhg+20) * pnow);
			$("#maincontents .sub_wrapper").css({"top":pmove+"px"});
		}
	}
}
/** Event handler for mouse wheel event.*/
function wheel(event){
	var delta = 0;
	if (!event) /* For IE. */ event = window.event;
	if (event.wheelDelta) { /* IE/Opera. */
		delta = event.wheelDelta/120;
		if (window.opera) delta = -delta;
	} else if (event.detail) { /** Mozilla case. */ delta = -event.detail/3;}
	/** If delta is nonzero, handle it.
	 * Basically, delta is now positive if wheel was scrolled up,
	 * and negative, if wheel was scrolled down.
	 */
	if (delta) handle(delta);
	if (event.preventDefault) {event.preventDefault();}
	event.returnValue = false;
}
function background(){
	var nwin = $(window).width();
	var nhg = $(window).height();
	if(nwin!=win || nhg!=hg){
		win = $(window).width();
		hg = $(window).height();
		nhg = hg - 384;
		if(nhg > 105){
			var pp = nhg + "px";
			$("#maincontents .sub_wrapper").css({"top":"0"});
			$("#scroll img").css({"top":"0"});
			$("#maincontents").css({"height":pp,"overflow":"hidden"});
			$("#scroll li").css({"height":pp});
			pnow = Number($("#scroll img").css("top").replace("px","")) / (nhg-103);
			pmove = - (($("#maincontents .sub_wrapper").height()-nhg+20) * pnow);
			$("#maincontents .sub_wrapper").css({"top":pmove+"px"});
			$("#scroll img").draggable({
				axis:"y",
				containment:$("#scroll li"),
				drag:function(e, ui){
					pnow = Number($(this).css("top").replace("px","")) / (nhg-103);
					pmove = - (($("#maincontents .sub_wrapper").height()-nhg+20) * pnow);
					$("#maincontents .sub_wrapper").css({"top":pmove+"px"});
				}
			});
		}
	}
}
function gotoURL(url){
	location.href=url;
}
