﻿
/*--------------------------------------------------------------------------*
 *  
 *  rollover.js
 *  created: 2007/03/13
 *  update : 2008/01/21
 *  Licensed under the GNU Lesser General Public License version 2.1
 *  
	inputButtonの不具合を修正
 *--------------------------------------------------------------------------*/

var yomotsuRollover = {
	
	main : function() {
		var img = document.images, ipt = document.getElementsByTagName('input'), i, preLoadImg = [];
		// img elements
		for (i = 0; i <img.length; i++) {
			if ((img[i].src.match(/.*_n\./))||(img[i].style.filter)){
				preLoadImg[preLoadImg.length] = new Image;
				preLoadImg[preLoadImg.length-1].src = img[i].src.replace('_n.', '_r.');

				img[i].onmouseover = yomotsuRollover.over;
				img[i].onmouseout  = yomotsuRollover.out;
				try {img[i].addEventListener('click', yomotsuRollover.click, false);}
				catch(e){img[i].attachEvent('onclick', (function(el){return function(){yomotsuRollover.click.call(el);};})(img[i]));}
			}
		}
		// input[image] elements
		for (i = 0; i <ipt.length; i++) {
			//trace(i+".src="+ipt[i].src);
			if ((ipt[i].src.match(/.*_n\./))&&(ipt[i].getAttribute('type')=='image')){
				//trace(ipt[i]);
				//trace(i);
				preLoadImg[preLoadImg.length] = new Image;
				//preLoadImg[preLoadImg.length-1].src = img[i].src.replace('_n.', '_r.');
				//以下に修正
				preLoadImg[preLoadImg.length-1].src = ipt[i].src.replace('_n.', '_r.');

				ipt[i].onmouseover = yomotsuRollover.over;
				ipt[i].onmouseout  = yomotsuRollover.out;
				try {ipt[i].addEventListener('click', yomotsuRollover.click, false);}
				catch(e){ipt[i].attachEvent('onclick', (function(el){return function(){yomotsuRollover.click.call(el);};})(ipt[i]));}
			}
		}
	}
	,
	
	over : function() {
		var imgSrc, preLoadImgSrc;
		if((this.style.filter)&&(this.style.filter.match(/_n\.png/)))//(IE5.5-6 && png)
			this.style.filter = this.style.filter.replace('_n.png', '_r.png');
		else
			this.src = this.src.replace('_n.', '_r.');
	},

	out : function(){
		if((this.style.filter)&&(this.style.filter.match(/_r\.png/)))//(IE5.5-6 && png)
			this.style.filter = this.style.filter.replace('_r.png', '_n.png');
		else
			this.src = this.src.replace('_r.', '_n.');
	},
	
	click : function(){
		if((this.style.filter)&&(this.style.filter.match(/_r\.png/)))//(IE5.5-6 && png)
			this.style.filter = this.style.filter.replace('_r.png', '_n.png');
		else
			this.src = this.src.replace('_r.', '_n.');
	},

	addEvent : function(){
		try {
			window.addEventListener('load', this.main, false);
		} catch (e) {
			window.attachEvent('onload', this.main);
		}
	}
}
yomotsuRollover.addEvent();


/*--------------------------------------------------------------------------*
 *  
 *  SmoothScroll JavaScript Library beta1
 *  
 *  MIT-style license. 
 *  
 *  2007 Kazuma Nishihata 
 *  http://www.webcreativepark.net
 *  
	減速動作に変更
 *--------------------------------------------------------------------------*/
 
new function(){

	/*
	 *イベント追加用
	  -------------------------------------------------*/
	function addEvent(elm,listener,fn){
		try{ // IE
			elm.addEventListener(listener,fn,false);
		}catch(e){
			elm.attachEvent(
				"on"+listener
				,function(){
					fn.apply(elm,arguments)
				}
			);
		}
	}

	/*
	 *スムーズスクロール
	  -------------------------------------------------*/
	function SmoothScroll(a){
		if(document.getElementById(a.rel.replace(/.*\#/,""))){
			//var e = document.getElementById(a.rel.replace(/.*\#/,""));
			//jQueryに置き換え
			var e=$("#"+a.rel.replace(/.*\#/,""));
		}else{
			return;
		}
		
		//移動位置
		//var end=e.offsetTop
		//jQueryに置き換え
		var end=e.offset().top;
		
		//現在位置
		var start=window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
		
		var flag=(end<start)?"up":"down";
		var scrollInterval;
		/*減速停止に変更
		function scrollMe(start,end,flag) {

			setTimeout(
				function(){
					if(flag=="up" && start >= end){
						start=start-(start-end)/20-1;
						window.scrollTo(0,start)
						arguments.callee(start,end,flag);

					}else if(flag=="down" && start <= end){
						start=start+(end-start)/20+1;

						window.scrollTo(0,start)
						arguments.callee(start,end,flag);

					}else{
						scrollTo(0,end);
					}
				}
				,200
			);
			
		}
		*/
		scrollMe(start,end,flag)
		
		function scrollMe(start,end,flag) {

			scrollInterval=setInterval(
				function(){
					if(Math.abs(end-start)>1){
						start+=(end-start)*0.3;
						window.scrollTo(0,Math.round(start))

					}else{
						window.scrollTo(0,end);
						clearInterval(scrollInterval);
					}
				}
				,30
			);
		
		}
		//減速停止に変更ここまで

	}

	/*
	 *スムーズ変換スクリプト
	  -------------------------------------------------*/
	addEvent(window,"load",function(){
		var anchors = document.getElementsByTagName("a");
		for(var i = 0 ; i<anchors.length ; i++){
			if(anchors[i].href.replace(/\#[a-zA-Z0-9]+/,"") == location.href.replace(/\#[a-zA-Z0-9]+/,"")){
				anchors[i].rel = anchors[i].href;
				anchors[i].href = "javascript:void(0)";
				anchors[i].onclick=function(){SmoothScroll(this)}
			}
		}
	});

}


/********************************
 * flash表示
 **************************/
function showFlash(){
	var flashvars = {};
	var params = {
		menu: "false"
	};
	var attributes = {
		id: "swfContent",
		name: "swfContent"
	};
	swfobject.embedSWF("main.swf","flashContent","949","370","9.0.0","common/js/swfobject/expressinstall.swf",flashvars,params, attributes);
}


/********************************
 * twitter
 * IE6、IE7、FF3、Safari3、Chrome4、Opera9で検証済
 **************************/


$(function(){
	
	var ng=[];
	var q="#dreamclub";
	var latestPostTime=0;
	var currentTime;
	
	$(".tweetsArea").html("<p>Loading...</p>");
	requestData();
	function requestData(){
		//console.log("requestData");
		$.ajax({
			dataType: "jsonp",
			data: {
				"q": q
			},
			timeout:10000,
			cache: true,
			url: "http://search.twitter.com/search.json",
			success: function (data) {
				if(data.results.length==0 && latestPostTime==0){
					$(".tweetsArea").html("<p>No result.</p>");
				}else{
					//currentTime=Date.UTC(new Date().getFullYear(),new Date().getMonth(),new Date().getDate(),new Date().getHours(),new Date().getMinutes(),new Date().getSeconds(),new Date().getMilliseconds());
					currentTime=new Date().getTime();
					for(var i=0; i<data.results.length; i++){
						//console.log($(".tweetsArea div:first").html());
						var node=data.results[data.results.length-i-1];
						if(parseDate(node.created_at)>latestPostTime){
							if($(".tweetsArea div:first").html()){
								$(".tweetsArea div:first").before(parseNode(node));
							}else{
								$(".tweetsArea").html(parseNode(node));
							}
						}
					}
				}
				//自動更新処理
				//setTimeout(requestData, 10000);
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				console.log("error",errorThrown);
			}
		});
	}
	function parseNode(node){
		//console.log(node);
		//console.log(node.from_user);
		var isNg=false;
		for(var i =0;i<ng.length;i++){
			//console.log(ng[i]);
			if(node.from_user==ng[i])isNg=true;
		}
		var postDate=parseDate(node.created_at);
		latestPostTime=postDate.getTime();
		//console.log(currentPostTime);
		var str="";
		if(!isNg){
			str+='<div class="tweetsBox clearfix">\n';
			str+='<p class="thumbs"><a href="http://twitter.com/'+ node.from_user +'" target="_blank"><img src="'+ node.profile_image_url +'" width="48" height="48" alt="'+ node.from_user +'" /></a></p>\n';
			str+='<div class="tweets">\n';
			str+='<p class="comment">'+ addLink(node.text) +'</p>\n';
			str+='<p class="data"><span class="time">'+setFormatDate(postDate) +'</span><span class="icon"><a href="#" target="_blank"><img src="common/images/icn_twttr.gif" alt="" /></a></span><span class="name"><a href="http://twitter.com/'+ node.from_user +'" target="_blank">'+ node.from_user +'</a></span></p>\n';
			str+='<img src="common/images/twttr_twts_bttm.gif" alt="" /></div>\n';
			str+='</div>\n';
		}
		return str;
	}
	function addLink(htmlStr){
		return htmlStr.replace(/(http:\/\/[\x21-\x7e]+)/gi, "<a href='$1' target='_blank'>$1</a>");
	}
	function parseDate(postDate){
		return new Date(postDate);
	}
	function setFormatDate(time){
		var str="";
		if(currentTime-time<1000*60){
			str=Math.round((currentTime-time)/1000)+"秒前";
		}else if(currentTime-time<1000*60*60){
			str=Math.round((currentTime-time)/(1000*60))+"分前";
		}else if(currentTime-time<1000*60*60*24){
			str=Math.round((currentTime-time)/(1000*60*60))+"時間前";
		}else{
			str=Math.floor((currentTime-time)/(1000*60*60*24))+"日前";
		}
		return str;
	}

})


/**************************************
intro
**************************************/

$(function(){
	//console.log("intro",document.cookie);
	var ary=document.cookie.split(";");
	var visited=false;
	for(var i in ary){
		var ary2=ary[i].split("=");
		if(ary2[0]=="dreamclubPortalVisited" && ary2[1]=="true")visited=true;
		if(ary2[0]==" dreamclubPortalVisited" && ary2[1]=="true")visited=true;
	}
	
	//console.log(visited);
	//初回のみ表示
	//(!visited)? showIntro(true): onHideIntro();
	//毎回表示

	var isFirst=true;
	//alert(document.referrer);
	var ary=document.referrer.split("/");
	if(ary[3]=="dreamclub-portal" || ary[3]=="dreamclub" || ary[4]=="dreamclub-portal" || ary[4]=="dreamclub"){
		isFirst=false;
	}

	var isAndroid = navigator.userAgent.indexOf('Android')>0;
	var isIphone = navigator.userAgent.indexOf('iPhone')>0;
	var isIpad = navigator.userAgent.indexOf('iPad')>0;
	if(isAndroid || isIphone || isIpad)isFirst=false;
	
	if(isFirst){
		showIntro(true);
	}else{
		onHideIntro();
	}
});

function showIntro(isFirst){
	if(!isFirst){
		$("#flash").html('<div id="flashContent"></div>');
		scrollTop($("html, body").scrollTop());
	}
	var _top=Math.round(($(window).height()-474)*0.5);
	//alert(top);
	//console.log(_top);
	
	var tag='';
	tag+='<div id="intro" style="position:absolute; padding-top:'+_top+'px; top:0; left:0; width:100%; height:'+($(window).height()-_top)+'px; background: url(common/images/introbg.jpg) #fdeff1 no-repeat center top;"><div style="margin:0 auto; width:950; height:474;"><div id="introContent"></div></div></div>\n';
	$("#contents").after(tag);
	
	$("#contents").css("display","none");
	
	setFlash();
	function setFlash(){
		
		var flashvars = {};
		var params = {
			menu: "false"
		};
		var attributes = {
			allowScriptAccess:"sameDomain",
			id: "swfContent",
			name: "swfContent"
		};
		swfobject.embedSWF("intro.swf","introContent","950","474","9.0.0","common/js/swfobject/expressinstall.swf",flashvars,params, attributes);
	}
	function scrollTop(start){
		//alert(start);
		var scrollInterval=setInterval(
			function(){
				if(Math.abs(0-start)>1){
					start+=(0-start)*0.3;
					window.scrollTo(0,Math.round(start))

				}else{
					window.scrollTo(0,0);
					clearInterval(scrollInterval);
				}
			}
			,30
		);
	}
}
function onHideIntro(){
	//console.log("onHideIntro");
	//coolieの期限を7日後に
	exp=new Date();
	exp.setTime(exp.getTime()+1000*60*60*24*7);
	document.cookie="dreamclubPortalVisited=true; expires="+exp.toGMTString();
	if($("#intro")){
		$("#contents").css("display","block");
		$("#header").css("visibility","visible");
		$("#contents").css("visibility","visible");
		$("#swfContent").remove();
		$("#intro").fadeOut(1000,function(){
			$("#intro").remove();
			//alert("remove");
		});
	}
	
	showFlash();
};


