//Pattern Aファイル
var ajaxURL = "/access_log/ranking/3";
var count;//初期表示数
var n;//リストセルの要素数
var setting;

$(document).ready( function(){
	$.ajaxSetup({ cache: false });
	moreButtonDispatch();
	
	$("form.replyForm").hide();
	//初期設定取得
	setting =eval( "(" + $('#setting').val() + ")" );
	
	//自動更新
	$("div.arrivalCellBox").everyTime( parseInt(setting.newCterm), 'control', function(){
		newContentsReload();
		//$(this).prepend('<div>  auto re-load  </div>');
	});

	setHoverEventHandler();

	$("input:button").mouseover(function(){
					$(this).removeClass("button");
					$(this).addClass("button_on");
				    });
	$("input:button").mouseout(function(){
				       $(this).removeClass("button_on");
				       $(this).addClass("button");
				   });
	$("#comment").keyup(function(){
				countString($(this), $(this).val().length, setting.comment_max_length);
			    });
	$("#title").keyup(function(){
			      countString($(this), $(this).val().length, setting.title_max_length);
			  });
	$("#tid").focus(function(){
			    this.select();
			});
	$("#pass").focus(function(){
			     this.select();
			 });
	$("#text_tid").focus(function(){
				 this.select();
			     });
	$("#text_pass").focus(function(){
				  this.select();
			      });
	$("#fileTweet").click(function(){
				  callTweet();
			      });
	$("#capture_title").keyup(function(){
				      countString($(this), $(this).val().length, setting.title_max_length);
				  });
	$("#capture_comment").keyup(function(){
					countString($(this), $(this).val().length, setting.comment_max_length);
				    });
	$("#captureTweet").click(function(){
				     callCaptureTweet();
				 });
	$("#text_comment").keyup(function(){
				     countString($(this), $(this).val().length, setting.comment_max_length);
				 });
	$("#textTweet").click(function(){
				  callTextTweet();
			      });
	$("#todayBtn").click(function(){
		ajaxURL = "/access_log/ranking/0";
		callAjaxReq();
	});
	$("#weekBtn").click(function(){
		ajaxURL = "/access_log/ranking/1";
		callAjaxReq();
	});
	$("#monthBtn").click(function(){
		ajaxURL = "/access_log/ranking/2";
		callAjaxReq();
	});
	$("#arrBtn").click(function(){
		$("div.arrivalCellBox").stopTime('control');
		ajaxURL = "/access_log/ranking/3";
		callAjaxReq();
	});

	$("#searchText").keyup(function(){
		//alert( $(this).val());
	});
	$("#searchBtn").click(function(){
		$("#arrBox").empty();
		ajaxURL = "/access_log/ranking/4";
		$.ajax({
			url : ajaxURL ,
			    type : "post",
			    data: {"word":$("#searchText").val()},
			    success: function(request) {
			    var obj = eval( "(" + request + ")" );
			    var cType;
			   if (obj.data != false){
				   if(obj[0]){
				   for(var i=0;i<=obj.length;i++){
						try{	   
							   if (obj[i][0].sf_type==1){
								   cType='movie';
							   }else{
								   cType='photo';
							   }
						   }catch(e){
							   continue;
						}
					   var contentsBox = '<div class="arrivalCell"><div class="thumbBox"><h3><img src="/img/icon_'+cType+'.gif" alt="画像"></h3><p class="thumb"><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'"><img src="'+obj[i][0].thumbnail_path+'" alt="'+obj[i][0].sf_comment+'"></a></p></div><div class="contBox"><div class="twitBox"><h5><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'へ">'+obj[i][0].sf_title+'</a></h5><p class="twit">'+obj[i][0].sf_comment+'</p></div><div class="infoBox"><p class="icon"><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ"><img src="'+obj[i][0].mb_profile_image_url+'" alt="'+obj[i][0].mb_name+'"></a></p><div class="attrBox"><h4><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ">'+obj[i][0].mb_name+'</a></h4><p><span class="header">投稿</span><span class="data">'+obj[i][0].sf_created+'</span><span class="header">再生</span><span class="data">'+obj[i][0].view_count+'回</span><span class="header">お気に入り</span><span class="data">'+obj[i][0].sf_favorite+'人</span></p></div></div></div></div>';
					   $("div.arrivalCellBox").append(contentsBox);
					   setHoverEventHandler();
				   }
					 if( obj.length < 10 ){
							 $("#moreBtn").hide();
						 }
						 obj=null;
						 $("div.noResult").remove();
				   }else{
					   showResultNotFound();
				   } 
			   }
			}
		    });
	});
	
	tabInitializer();
	
});

function tabInitializer(){
//ログイン状態にあるときの追加画面処理
	if($("#fav_tab")){

	    arrTabBtnInit();//新着タブボタンの初期化
	    favTabBtnInit();//お気に入りタブボタンの初期化
	    tlTabBtnInit();//タイムラインタブボタンの初期化
	    favMoreBtnDispatch();//もっとみるボタンにイベント発行

		//他のボタンにホバーイベント追加
		addHover( $("#fav_tab") );
		addHover( $("#tl_tab") );
	    /*
	    $("#tl_tab").mouseover(function(){
		    $(this).addClass('on');
		});
        */
	}
	
}

//お気に入り削除処理
function favDel(l,ele){
	
	//Ajax処理
	$.get(l+"");
	
	//表示を消去
	ele.parent().parent().parent().parent().parent().hide('slow');
	ele.parent().parent().parent().parent().parent().empty();
}

function setHoverEventHandler()
{
    $(".arrivalCell").hover(
			    function(){
				$(this).addClass('on');
			    },
			    function(){
				$(this).removeClass('on');
			    }
			    );
    $(".favoriteCell").hover(
			     function(){
				 $(this).addClass('on');
			     },
			     function(){
				 $(this).removeClass('on');
			     }
			     );
    $(".timelineCell").hover(
			     function(){
				 $(this).addClass('on');
			     },
			     function(){
				 $(this).removeClass('on');
			     }
			     );
}

function addHover(elm){
	elm.removeClass('on');
    elm.hover(
	    	function (){
	    		$(this).addClass('on');
	    	},
	    	function (){
	    		$(this).removeClass('on');
	    	}
	    );
	
}

//新着タブの初期化処理
function arrTabBtnInit(){
	$("#arr_tab").click(function(){
		$("#arrBox").empty();
		$("#arrTab").show('slow');
		$("#favTab").hide();
		$("#moreBtn").show();
		$("#arr_tab").addClass('on');
		$("#arr_tab").unbind();
		//他のボタンにホバーイベント追加
		 addHover( $("#fav_tab") );
		 addHover( $("#tl_tab") );
		 if( $("#searchText").val() ){
			 $("#searchText").val("");
		 }
		 
		//新着の再読み込み処理
		$("div.arrivalCellBox").stopTime('control');
		ajaxURL = "/access_log/ranking/3";
		callAjaxReq();
		 
		//TLBtn
		$("#tlTab").remove();
		
		//再帰処理開始
		arrTabBtnInit();
	});
}
//お気に入りタブの初期化処理
function favTabBtnInit(){
    $("#fav_tab").click(function(){
			    $("div.arrivalCellBox").stopTime('control');
			    //他のタブを隠す
			    $("#arrTab").hide();
			    $("#tlTab").remove();
			    //お気に入りタブを表示
			    $("#favBox").empty();
			    $("#favTab").show('slow');
			    $("#fav_tab").unbind();
			    $("#fav_tab").addClass('on');
			    //他のボタンにホバーイベント追加
			    addHover( $("#arr_tab") );
			    addHover( $("#tl_tab") );
			    //ajax処理
			    ajaxURL = "/access_log/ranking/6";
			    $.ajax({
				    url : ajaxURL ,
					type : "get",
					success: function(request) {
					var obj = eval( "(" + request + ")" );
					var cType;
					if (obj.data != false){
					    if(obj[0]){
						if (obj[0].mb_id === 0){
						    showResultNoLogin();
						} else {
						    for(var i=0;i<=obj.length;i++){
							try{	   
							    if (obj[i][0].sf_type==1){
								cType='movie';
							    }else{
								cType='photo';
							    }
							}catch(e){
							    continue;
							}
							var contentsBox = '<div class="favoriteCell"><div class="thumbBox"><h3><img src="/img/icon_'+cType+'.gif" alt="画像"></h3><p class="thumb"><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'"><img src="'+obj[i][0].thumbnail_path+'" alt="'+obj[i][0].sf_comment+'"></a></p></div><div class="contBox"><div class="twitBox"><h5><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'へ">'+obj[i][0].sf_title+'</a></h5><p class="twit">'+obj[i][0].sf_comment+'</p></div><div class="infoBox"><p class="icon"><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ"><img src="'+obj[i][0].mb_profile_image_url+'" alt="'+obj[i][0].mb_name+'"></a></p><div class="attrBox"><h4><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ">'+obj[i][0].mb_name+'</a></h4><p><span class="header">投稿</span><span class="data">'+obj[i][0].sf_created+'</span><span class="header">再生</span><span class="data">'+obj[i][0].view_count+'回</span><span class="header">お気に入り</span><span class="data">'+obj[i][0].sf_favorite+'人</span><span class="dellink" onclick="favDel(\''+obj[i][0].del_link+'\',$(this) )">お気に入りを削除</span></p></div></div></div></div>';
							
							$("div.favoriteCellBox").append(contentsBox);
						    }
						    
						    if( obj.length < 10 ){
							$("#favMoreBtn").hide();
						    }
						    obj=null;//AJAXで取得したDOMを初期化
						    $("div.noFav").remove();
						}
					    }else{
						showResultFavNotFound();
					    }
					}
				    },
					complete : function(XMLHttpRequest, status){
					setHoverEventHandler();
				    }
				});
			    //再帰処理
			    favTabBtnInit();
			});
}
function tlTabBtnInit() {
    $("#tl_tab").click(function(){
        $("div.arrivalCellBox").stopTime('control');
        //他のタブを隠す
        $("#arrTab").hide();
        $("#tlTab").remove();
        //お気に入りタブを隠す
        $("#favBox").empty();
        $("#favTab").hide();
        $("#tl_tab").unbind();
        $("#tl_tab").addClass('on');
        //他のボタンにホバーイベント追加
        addHover( $("#arr_tab") );
        addHover( $("#fav_tab") );
        //ajax処理
        ajaxURL = "/twitter/getFriendsTimeline";

        $("div.arrivalTabBox").append('<div class="arrivalTab" id="tlTab"><div class="timelineBox"></div></div>');
        $.ajax({
            url : ajaxURL ,
            type : "get",
            success: function(request) {
                var obj = eval( "(" + request + ")" );
                if (obj.data != null && obj.data.auth != null && obj.data.auth == 'false') {
                        $("div.timelineBox").append('<div style="text-align:center">ログインして下さい。</div>');
                } else if (obj.data != false){
                    layoutTimeline(obj);
                }
            },
            complete : function(XMLHttpRequest, status){
                setHoverEventHandler();
                toggleReplyForm();
            }
        });
        //再帰処理
        tlTabBtnInit();
    });
}

function toggleReplyForm() {
		$("form.replyForm").hide();
		
		$("span.reply").toggle(function() {
			var index = $("span.reply").index(this);
			$("form.replyForm:eq("+index+")").show("slow");
			
		},function() {
			var index = $("span.reply").index(this);
			$("form.replyForm:eq("+index+")").hide("slow");
		});
}

function showMoreButton(){

	//$("#moreBtn").show();
	 moreButtonDispatch();
	$("div.noResult").remove();

}
function showResultNotFound(){
	$("div.noResult").remove();
	$("#moreBtn").hide();
	var mes = '<div class ="noResult">該当するコンテンツはありませんでした。</div>';
	$("#cBox").after(mes);
}
function showResultFavNotFound(){
	$("div.noResult").remove();
	$("#favMoreBtn").hide();
	var mes = '<div class ="noResult">該当するコンテンツはありませんでした。</div>';
	$("#favBox").after(mes);
}
function showResultNoLogin()
{
    $("div.noResult").remove();
    $("#favMoreBtn").hide();
    var mes = '<div class ="noResult" style="text-align:center">ログインしてください。</div>';
    $("#favBox").after(mes);
}
function moreButtonDispatch(){
	$("div.arrivalCellBox").stopTime('control');
	$("#moreBtn").click(function(){
		//$("div.arrivalCell").hide("fast");
		var moreAjaxURL = ajaxURL + "/10/"+($("div.arrivalCell").size());
		$.ajax({
			url : moreAjaxURL ,
			    type : "get",
			    success: function(request) {
			    var obj = eval( "(" + request + ")" );
			    var cType;
			   if (obj.data != false){
				   for(var i=0;i<=obj.length;i++){
						try{	   
							if (obj[i][0].sf_type==1){
								   cType='movie';
							   }else{
								   cType='photo';
							   }
						   }catch(e){
							   continue;
						}
					   
					   var contentsBox = '<div class="arrivalCell"><div class="thumbBox"><h3><img src="/img/icon_'+cType+'.gif" alt="画像"></h3><p class="thumb"><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'"><img src="'+obj[i][0].thumbnail_path+'" alt="'+obj[i][0].sf_comment+'"></a></p></div><div class="contBox"><div class="twitBox"><h5><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'へ">'+obj[i][0].sf_title+'</a></h5><p class="twit">'+obj[i][0].sf_comment+'</p></div><div class="infoBox"><p class="icon"><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ"><img src="'+obj[i][0].mb_profile_image_url+'" alt="'+obj[i][0].mb_name+'"></a></p><div class="attrBox"><h4><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ">'+obj[i][0].mb_name+'</a></h4><p><span class="header">投稿</span><span class="data">'+obj[i][0].sf_created+'</span><span class="header">再生</span><span class="data">'+obj[i][0].view_count+'回</span><span class="header">お気に入り</span><span class="data">'+obj[i][0].sf_favorite+'人</span></p></div></div></div></div>';
					   $("div.arrivalCellBox").append(contentsBox);
					   setHoverEventHandler();
				   }

					 if( obj.length < 10 ){
							 $("#moreBtn").hide();
					}
					 obj=null;
			   } 
			}
		    });
	});
}
function favMoreBtnDispatch(){

	$("#favMoreBtn").click(function(){
	//ajax処理
	var favmoreAjaxURL = ajaxURL +"/10/"+($("div.favoriteCell").size());
	$.ajax({
		url : favmoreAjaxURL ,
		    type : "get",
		    success: function(request) {
		    var obj = eval( "(" + request + ")" );
		    var cType;
		   if (obj.data != false){
			   if(obj[0]){
			   for(var i=0;i<=obj.length;i++){
					try{	   
						   if (obj[i][0].sf_type==1){
							   cType='movie';
						   }else{
							   cType='photo';
						   }
					   }catch(e){
						   continue;
					}
				   
				   var contentsBox = '<div class="favoriteCell"><div class="thumbBox"><h3><img src="/img/icon_'+cType+'.gif" alt="画像"></h3><p class="thumb"><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'"><img src="'+obj[i][0].thumbnail_path+'" alt="'+obj[i][0].sf_comment+'"></a></p></div><div class="contBox"><div class="twitBox"><h5><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'へ">'+obj[i][0].sf_title+'</a></h5><p class="twit">'+obj[i][0].sf_comment+'</p></div><div class="infoBox"><p class="icon"><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ"><img src="'+obj[i][0].mb_profile_image_url+'" alt="'+obj[i][0].mb_name+'"></a></p><div class="attrBox"><h4><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ">'+obj[i][0].mb_name+'</a></h4><p><span class="header">投稿</span><span class="data">'+obj[i][0].sf_created+'</span><span class="header">再生</span><span class="data">'+obj[i][0].view_count+'回</span><span class="header">お気に入り</span><span class="data">'+obj[i][0].sf_favorite+'人</span><span class="dellink" onclick="favDel(\''+obj[i][0].del_link+'\',$(this) )">お気に入りを削除</span></p></div></div></div></div>';

				   $("div.favoriteCellBox").append(contentsBox);
			   }
				 if( obj.length < 10 ){
						 $("#favMoreBtn").hide();
					 }
					 obj=null;
					 $("div.noResult").remove();
			   }else{
				   $("#favMoreBtn").hide();
				   showResultNotFound();
			   } 
		   }
		},
		    complete : function(XMLHttpRequest, status){
				setHoverEventHandler();
			}
	    });
	});
}

function tlMoreBtnDispatch() {
    $("#tlMoreBtn").click(function(){
        //ajax処理
        //var page = Math.floor($("div.timelineCell").size() / 10)+1;
        var page = Math.ceil($("div.timelineCell").size() / 10)+1;
        var favmoreAjaxURL = ajaxURL +"/page:"+page;
        $.ajax({
            url : favmoreAjaxURL ,
            type : "get",
            success: function(request) {
                var obj = eval( "(" + request + ")" );
                if (obj.data != false){
                    layoutTimeline(obj);
                }
            },
            complete : function(XMLHttpRequest, status) {
                setHoverEventHandler();
                toggleReplyForm();
            }
        });
    });
}

function layoutTimeline(obj) {
    $("#tlMore").remove();
    if(obj[0]){
        for(var i=0;i<=obj.length;i++){
            try {
                var contentsBox = '<div class="timelineCell"><p class="icon"><a href="/contents/lists/' + obj[i].user.screen_name + '" title="' + obj[i].user.name + 'さんの作品一覧へ"><img src="' + obj[i].user.profile_image_url + '" alt="' + obj[i].user.name + '" /></a></p><div class="attrBox"><h4><a href="/contents/lists/' + obj[i].user.screen_name + '" title="' + obj[i].user.name + 'さんの作品一覧へ">' + obj[i].user.name + '</a></h4><p>' + obj[i].text + '</p><p><span class="header">' + obj[i].created_at + ' ' + obj[i].source + 'で</span><span class="reply" style="margin-left:10px;">返信する</span></p><div class="controlBox" style="float:left;"><form action="/twitter/updateStatus/" method="post" class="replyForm"><p class="controlCell"><textarea name="comment" class="comment" style="width: 400px;height: 2.5em;overflow: auto;vertical-align: middle;"></textarea><input type="hidden" name="class_id" class="class_id" value="3" /><input type="hidden" name="send_member_id" class="send_member_id" value="' + obj[i].user.screen_name + '" /><input type="hidden" name="twitter_comment_id" class="twitter_comment_id" value="' + obj[i].id + '" /><input type="submit" class="button" value="返信" style="margin-left:5px;"></p></form></div></div></div>';
                $("div.timelineBox").append(contentsBox);
            } catch(e) {
                continue;
            }
        }
        if (obj.length > 0) {
            $('.timelineBox').eq($('.timelineBox').size()-1).after('<div class="controlBox more" id="tlMore"><span class="link" id="tlMoreBtn">もっと見る</span></div>');
            //addUserTimelineClickEvent(obj, targetClassName, twitterApi);
            tlMoreBtnDispatch();
        }

        obj=null;//AJAXで取得したDOMを初期化
    }else{
        //showResultFavNotFound();
    } 
}


function callAjaxReq(){
	$("div.arrivalCellBox").stopTime('control');
	$("div.arrivalCell").hide("fast");
	$("div.arrivalCellBox").empty();

	$.ajax({
		url : ajaxURL ,
	    type : "post",
	    data: {"word":$("#searchText").val()},
		    success: function(request) {
		    	var obj = eval( "(" + request + ")" );
			    var cType;
				   if (obj.data != false){
					   
						   for(var i=0;i<=obj.length;i++){
							
							try{	   
								   if (obj[i][0].sf_type==1){
									   cType='movie';
								   }else{
									   cType='photo';
								   }
							}catch(e){
								   continue;
							}
							   var contentsBox = '<div class="arrivalCell"><div class="thumbBox"><h3><img src="/img/icon_'+cType+'.gif" alt="画像"></h3><p class="thumb"><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'"><img src="'+obj[i][0].thumbnail_path+'" alt="'+obj[i][0].sf_comment+'"></a></p></div><div class="contBox"><div class="twitBox"><h5><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'へ">'+obj[i][0].sf_title+'</a></h5><p class="twit">'+obj[i][0].sf_comment+'</p></div><div class="infoBox"><p class="icon"><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ"><img src="'+obj[i][0].mb_profile_image_url+'" alt="'+obj[i][0].mb_name+'"></a></p><div class="attrBox"><h4><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ">'+obj[i][0].mb_name+'</a></h4><p><span class="header">投稿</span><span class="data">'+obj[i][0].sf_created+'</span><span class="header">再生</span><span class="data">'+obj[i][0].view_count+'回</span><span class="header">お気に入り</span><span class="data">'+obj[i][0].sf_favorite+'人</span></p></div></div></div></div>';
							   $("div.arrivalCellBox").append(contentsBox).hide();
							   $("div.arrivalCellBox").show('fast');
							   setHoverEventHandler();
						   }
							 if( obj.length < 10 ){
								 $("#moreBtn").hide();
							 }else{
								 $("#moreBtn:hidden").show();
							 }
						 obj=null;
				}
				
			}
	});
}
function stopReload(){
	stopTime("newcontents", function(){alert("stop");});
}
function newContentsReload(){
	tt = new Date();
	tt.setTime((new Date()).getTime() -60000);
	yy = tt.getYear();
	mm = tt.getMonth() + 1;
	dd = tt.getDate();
	hh = tt.getHours();
	mn = tt.getMinutes();
	if (yy < 2000) { yy += 1900; }
	if (mm < 10) { mm = "0" + mm; }
	if (dd < 10) { dd = "0" + dd; }
	if (hh < 10) { hh = "0" + hh; }
	if (mn < 10) { mn = "0" + mn; }
	var nowTime = yy + "-" + mm + "-" + dd + "_" + hh +""+ mn +"00";

	$.ajax({
		url : "/access_log/ranking/5/0/0/"+nowTime,
		    type : "get",
		    success: function(request) {
		    	var obj = eval( "(" + request + ")" );
			    var cType;
				   if (obj.data != false){
					   
						   for(var i=0;i<=obj.length;i++){
							
							try{	   
								   if (obj[i][0].sf_type==1){
									   cType='movie';
								   }else{
									   cType='photo';
								   }
							}catch(e){
								   continue;
							}
							   var contentsBox = '<div class="arrivalCell"><div class="thumbBox"><h3><img src="/img/icon_'+cType+'.gif" alt="画像"></h3><p class="thumb"><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'"><img src="'+obj[i][0].thumbnail_path+'" alt="'+obj[i][0].sf_comment+'"></a></p></div><div class="contBox"><div class="twitBox"><h5><a href="/'+obj[i][0].sf_id+'" title="'+obj[i][0].sf_title+'へ">'+obj[i][0].sf_title+'</a></h5><p class="twit">'+obj[i][0].sf_comment+'</p></div><div class="infoBox"><p class="icon"><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ"><img src="'+obj[i][0].mb_profile_image_url+'" alt="'+obj[i][0].mb_name+'"></a></p><div class="attrBox"><h4><a href="/contents/lists/'+obj[i][0].mb_login_id+'" title="'+obj[i][0].mb_name+'さんのコンテンツへ">'+obj[i][0].mb_name+'</a></h4><p><span class="header">投稿</span><span class="data">'+obj[i][0].sf_created+'</span><span class="header">再生</span><span class="data">'+obj[i][0].view_count+'回</span><span class="header">お気に入り</span><span class="data">'+obj[i][0].sf_favorite+'人</span></p></div></div></div></div>';
							   $("div.arrivalCellBox").preppend(contentsBox);
							   setHoverEventHandler();
						   }
						 obj=null;
				}
				
			}
	});
	
}

//Timer
jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
				return;

			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			
			times = times || 0;
			belay = belay || false;
			
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			
			if (!timers[label])
				timers[label] = {};
			
			fn.timerID = fn.timerID || this.guid++;
			
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			
			handler.timerID = fn.timerID;
			
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			
			this.global.push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});

jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});
