


/*	
	Topimg
*/
(
	function() {
		
		$.Topimg = function(holder) { //コンストラクタ
			var me = this;
			me.holder = holder;
			
			//ローカルストレージから復元
			var picMode = $.DC.LS("picMode");
			if(picMode) me.picMode = picMode;
			
			var showPostInfoFlg = $.DC.LS("showPostInfoFlg");
			if(showPostInfoFlg != null) me.showPostInfoFlg = showPostInfoFlg;
			
			//忘年会用
			if($.isPivot() && document.URL.indexOf("endroll") != -1){
				var btn = $("<div id='EndrollBtnHolder'><button id='btn_Endroll'>Endroll</button></div>");
				btn.bind("click",function(){
					$.EndR = new $.Endroll();
					
				});
				$("body").prepend(btn);
			}
		}
		
		var P = $.Topimg.prototype;
		
		P.holder = null;
		P.postDataAry = [];
		P.updeteTimer = 0;
		P.postMax = 4;
		P.postPosAry = [];
		P.postRetsuMax = 1;
		
		P.picSize_Big = {w:300, h:225, max:40};
		P.picSize_Small = {w:110, h:77, max:100};
		P.picMode = 'Big';
		P.showPostInfoFlg = 0;
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.init = function(callback){
			var me = this;
			
			me.postRetsuMax = 1;
			me.holder.empty();
			
			me.updatePost(function(){
				
				var mode = me.getPicMode();
				me.holder.find(".postimg").css({width:mode.w, height:mode.h});
				me.onResize(function(){
					//インフォ
					//me.showPostInfo();
				});
				
				
					
				
				//タイマーで更新
				if(!$.isLocal() && !$.isDebug()) me.startCheckUpdateTmer(true);
				
				
				/*---------------------------------------------------------------------------------------
					画像の大きさ変更
				---------------------------------------------------------------------------------------*/
				$("#btn_imgsize").unbind("click").bind("click",function(e){ e.preventDefault(); e.stopPropagation();
					me.changePicMode();
					
				});
				
				
				/*---------------------------------------------------------------------------------------
					画像の詳細表示
				---------------------------------------------------------------------------------------*/
				$("#btn_imginfo").unbind("click").bind("click",function(e){ e.preventDefault(); e.stopPropagation();
					
					if(me.showPostInfoFlg == 1){
						me.showPostInfoFlg = 0;
					} else {
						me.showPostInfoFlg = 1;
					}
					me.showPostInfo();
					
				});	
				
				if(callback) callback();
			});
		}
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.showPostInfo = function(){
			var me = this;
			if(me.showPostInfoFlg == 1){
				$(".postusername").fadeIn("slow");
				$(".posttime").fadeIn("slow");
			} else {
				$(".postusername").fadeOut("slow");
				$(".posttime").fadeOut("slow");
			}
			
			
			$.DC.LS("showPostInfoFlg", me.showPostInfoFlg);
		}
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.startCheckUpdateTmer = function(flg){
			var me = this;
			if(flg){
				console.log("タイマー開始");
				clearInterval(me.updeteTimer);
				me.updeteTimer = setInterval(function(){
					me.checkUpdate();
				}, 3000);
			} else {
				console.log("タイマー停止");
				clearInterval(me.updeteTimer);
			}
		}
		/*---------------------------------------------------------------------------------------------------------*/
		P.checkUpdate = function(){
			var me = this;
			
			var event_id = null;
			if($.isPivot()) event_id = 17;
			
			
			$.IF.getLatestPost(event_id, function(d){
				
				var latest = d.latest;
				var currentLatest = me.postDataAry[0];
				//console.log("latest.id "+latest.id+"  currentLatest.id "+currentLatest.id);
				
				console.log("latest.event_id "+latest.event_id);
				
				if(me.postDataAry.length == 0 || latest.id > currentLatest.id){
					console.log("更新あり ");
					
					
					
					me.addPost(latest);
					me.updatePostPositionAndMove();
					
					//イベントビューに通知してイベントラインの投稿を更新する
					$.EventV.updateLinePost(latest.event_id);
					
					//エンドロールに通知
					if($.EndR) $.EndR.onGetNewPost(latest);
					
				} else {
					//console.log("更新なし "+latest.id+"/"+currentLatest.id);
				}
				
				//グッドも取得した
				//$.DC.iine = d.iine;
				
				
				//開発
				//me.startCheckUpdateTmer(false);
			});

		}
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.changePicMode = function(){
			var me = this;
			
			if(me.picMode === 'Big'){
				me.picMode = 'Small';
			} else {
				me.picMode = 'Big';
			}
			$.DC.LS("picMode", me.picMode);
			
			me.init();
		}
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.onResize = function(callback){
			var me = this;
			
			var winW = $(window).width();
			var currentPostRetsuMax = me.postRetsuMax;
			var holder = me.holder;
			var mode = me.getPicMode();
			
			var postRetsuMax = Math.ceil(winW/mode.w);
			postRetsuMax = Math.floor(winW/mode.w);
			
			$("#picHolder").css({width:(mode.w*postRetsuMax)});
			
			//表示列数が変更されたら、再配置
			if(postRetsuMax != currentPostRetsuMax){
				me.postRetsuMax = postRetsuMax;
				me.updatePostPositionAndMove(callback);
			}
			
			
		}
		/*---------------------------------------------------------------------------------------------------------*/
		//投稿画像エリア全体を再描画
		P.updatePost = function(callback){
			var me = this;
			var holder = me.holder;
			var mode = me.getPicMode();
			
			me.updatePostData(function(){
				
				var node = "";
				
				//ポストデータ配列を更新
				me.updatePostPosAry();
				
				var ary = me.postDataAry;
				
				for(var i=0, len = ary.length ; i < len; i++) {
					var d = ary[i];
					node += me.getPostNode(d);
				}
				holder.empty().append(node);
				
				
				
				if(callback) callback();
			});
		}
		
		/*---------------------------------------------------------------------------------------------------------*/
		//投稿データを取得し、コンテナに格納
		P.updatePostData = function(callback){
			var me = this;
			var member_id = "";
			var event_id = "";
			
			//開発
			event_id = 0;
			
			$.IF.getPost(member_id, event_id,  function(d){
				
				var node = "";
				var ary = d.ary;
				for(var i=0, len = ary.length ; i < len; i++) {
					$.DC.setData("eventpost", ary[i]);
				}
				
				
				if(callback) callback();
			});
					
		}
		
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.getPicMode = function(){
			
			//return this.picSize_Small;
						
			return this["picSize_"+this.picMode];
		}
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.getPostNode = function(d){
			var me = this;
			var node = "";
			var memberD = $.DC.getData("member",d.member_id);
			var username = "とうろくなし人";
			if(memberD){
				username = memberD.name;
			}
			
			var url = me.getImgUrl(d.imgurl);
			var date = d.date.replaceAll("_", ".");
			
			var body = d.body;
			var good = me.getGood(d.id);
			
			var mode = me.getPicMode();
			var picW = mode.w;
			var picH = mode.h;
			var picmode = "";
			if(me.picMode == "Big"){
				picmode = "postBig";
			} else {
				picmode = "postSmall";
			}

			
			
			node += "<div class='post "+picmode+"' num='"+d.id+"' good='"+good+"''>";
				node += "<div class='postimg'><img src='"+url+"' width='"+picW+"'  alt='' title=''></div>";
				
				node += "";
				node += "<p class='posttime'>"+date+"</p>";
				node += "<p class='postusername'>"+username+"</p>";
				node += "<p class='postbody'>"+body+"</p>";
				node += "<p class='btn_iine'><button>グッドね</button></p>";
				node += "<p class='post_good'>Good : "+good+"</p>";
				
			node += "</div>";
			return node;
		}
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.getImgUrl = function(url){
			var me = this;
			
			
			if(me.picMode == "Big"){
				url = url.replace("/normal/", "/endth/");
			} else {
				url = url.replace("/normal/", "/th/");
			}
			
			if($.isLocal) url = "http://toshick.com/"+url;
			
			return url;
		}
		
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.updateGood = function(post_id){
			var me = this;
			//node += "<p class='post_good'>Good : "+me.getGood(d.id)+"</p>";
			var good = me.getGood(post_id);
			$(".post[num="+post_id+"] .post_good").empty().append("Good : "+good).attr("good", good);;
			
		}
		/*---------------------------------------------------------------------------------------------------------*/
		P.getGood = function(post_id){
			var me = this;
			var count = 0;
			/*
var ary = $.DC.iine;
			for(var i=0; i < ary.length; i++) {
				var d = ary[i];
				if(d.eventpost_id == post_id){
					count++;
				}
			}
*/
			return count;
		}
		
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.addPost = function(d){
			var me = this;
			$.DC.setData("eventpost", d);
			
			var url = me.getImgUrl(d.imgurl);
			var node = me.getPostNode(d);
			$("#picHolder").prepend(node);
			
			//me.moveToSelfPosition();
			
		}
		
		
		/*---------------------------------------------------------------------------------------------------------*/
		P.updatePostPosAry = function(){
			var me = this;
			var mode = me.getPicMode();
			var event_id = null;
			if($.isPivot()) event_id = 17;
			
			var ary = $.DC.getEventPost(event_id, mode.max);
			me.postDataAry = ary;
		}
		
		/*---------------------------------------------------------------------------------------------------------*/
		// ポジション配列を更新してから移動しよう
		P.updatePostPositionAndMove = function(callback){
			this.updatePostPositionAry();
			this.moveToSelfPosition(callback);
		}
		/*---------------------------------------------------------------------------------------------------------*/
		//ポジション配列を更新しよう
		P.updatePostPositionAry = function(){
			console.log("updatePostPositionAry");
			var me = this;
			var gyo = 0;
			var retsu = 0;
			var retsuMax = me.postRetsuMax;
			var mode = me.getPicMode();
			var picH = mode.h;
			var picW = mode.w;
			
			//ポストデータ配列を更新
			me.updatePostPosAry();
			var ary = me.postDataAry;
				
			me.postPosAry = [];//写真の位置情報を格納した配列
			for(var i=0, len = ary.length; i < len; i++) {
				var d = ary[i];
				
				var x = retsu*picW;
				var y = gyo*picH;
				
				me.postPosAry.push({x:x, y:y});
				
				retsu++;
				if(retsu == retsuMax && i < len-1){
					gyo++;
					retsu = 0;
				}
			}
			
			me.holder.css("height", (gyo+1)*picH);
		}
		
		/*---------------------------------------------------------------------------------------------------------*/
		//自分の位置へ移動しよう
		P.moveToSelfPosition = function(callback){
			var me = this;
			var holder = me.holder;
			
			var posAry = me.postPosAry.concat();
			var moveCompelteCount = 0;
			var moveCompelteCountMax = posAry.length;
			var delayCount = 0;
			holder.find(".post").each(function(){
				var self = $(this);
				
				//詳細
				if(me.showPostInfoFlg == 1){
					self.find(".postusername").hide();
					self.find(".posttime").hide();
				}
				var pos = posAry.shift();
				if(!pos) return true;
				
				delayCount += 50;
				
				var delay = Math.round(delayCount);
				self.stop(false, false).css("position","absolute").delay(delay).animate({ top : pos.y, left : pos.x}, {duration: 'slow', easing: 'easeInOutCubic', complete:function(){
					
					var self2 = $(this);
					//詳細
					if(me.showPostInfoFlg == 1){
						self2.find(".postusername").fadeIn("slow");
						self2.find(".posttime").fadeIn("slow");
					}
					
					moveComplete();
				}});

				
			});
			
			/*-----------------------------------------------------------------
				moveComplete
			------------------------------------------------------------------*/
			function moveComplete(){
				
				moveCompelteCount++;
				if(moveCompelteCount == moveCompelteCountMax){
					console.log("moveComplete");
					if(callback) callback();
				}
			}
		}

		
		
		
	}
)();


