
/*
 * 商品詳細ページ用JS
 */

// DomContentLoaded
$(function(){
	
	// スクロール制御
	var scrollable  = $("#scrollable");
	var scroll_next = $("#scroll_next");
	var scroll_prev = $("#scroll_prev");
	var scroll_size = scrollable.find("ul:first li").size();
	
	seek = 0;
	
	if ( scroll_size <= 4 ) {
		scroll_next.find("img:first").css("display", "none" );
		scroll_next.find("img:last" ).css("display", "block");
	} else {
		scroll_next.find("img:first").css("display", "block");
		scroll_next.find("img:last" ).css("display", "none" );
		seek = 3;
		
		scroll_next.bind("click", function() {
			
			if ( seek < scroll_size-1 ) {
				seek+=1;
				scroll_prev.find("img:first").css("display", "block");
				scroll_prev.find("img:last" ).css("display", "none" );
			}
			
			var end = ( seek >= scroll_size-1 ) ? true : false;
			
			scroll_next.find("img:first").css("display", end ? "none"  : "block");
			scroll_next.find("img:last" ).css("display", end ? "block" : "none" );
		});
		
		scroll_prev.bind("click", function() {
			
			if ( seek > 3 ) {
				seek -= 1;
				scroll_next.find("img:first").css("display", "block");
				scroll_next.find("img:last" ).css("display", "none" );
			}
			
			var start = (seek < 4) ? true : false;

			scroll_prev.find("img:first").css("display", start ? "none"  : "block");
			scroll_prev.find("img:last" ).css("display", start ? "block" : "none" );
		});
		
		$("#scrollable").each(function(){
			var $$=$(this);
			$$.height(162);
			$("div.items",this).css({position:"absolute",overflow:"hidden",marginLeft:40,height:160,width:520 });
				var n = this.getElementsByTagName("li").length*129;
			$("ul",this).css({position:"absolute", height:127,width: n });
			$$.scrollable({ items:"ul", prev:"#scroll_prev", next:"#scroll_next", size:4});
		});
	
	}
	
	
	//商品詳細、関連する発言取得
	productName = encodeURIComponent($("#page_product_name").val());
	
	$("#list_tweet .tweetUserBox img").css("visibility","visible");
	$("#list_tweet .tweetUserBox")
	.load(__webroot + "javascripts/?func=get_product_tweets&product_name=" + productName
	,function(data){
		if (data.replace(/<.+>/,"")!=""){
			//twitterのもっと読むが仕様的に難しいので一時無効化
			//$("#list_tweet .btnMore p.rollover img").live("click", getProductNotes());
			$("#list_tweet .tweetUserBox").css("text-align","left");
		} else {
			$("#list_tweet .tweetUserBox").html("この商品に関連する発言が見つかりませんでした。");
		}
	});
	
	// 「お勧めされたユーザ」もっと読むボタン

	$("#list_osusume .btnMore img").live("click",function(){
		getProductNotes(
			__webroot + "javascripts/?func=get_product_users&product_id="
			+ $("#page_product_id").val()
			+ "&last_id=" + $("#list_osusume input.last_id").val()
		);
	});
	
	
	// 前回の選択エリアをロード
	var area = $.cookie('shoparea');
	
	if(area){
		// エリアに従ってセレクトボックスを変更する
		$('#selectarea').val(area);
		show_area(area);
	}
	
	$("div#scrollable").change(area_changed);
	
	/* 投稿エラーチェック */
	
	newPost   = $("#newpost");
	inputArea = $("#newInputArea");
	inputText = inputArea.find("span.inputText input");
	inputBtn  = inputArea.find("span.inputBtn input");
	
	errBox = $("#errorMessage");
	
	// エラー隠す
	errBox.fadeOut("slow");
	
	// submit先を保持
	formAction = newPost.attr("action");
	newPost.attr("action", "");
	
	inputBtn.bind("click", function(){
		
		errBox.hide("");
		
		if (inputText.val()=='') return false;
		if (inputText.val().length > 140) {
			errBox.html(__vals.error_wordover);
			errBox.fadeIn("slow");
			return false;
		} else {
			newPost.attr("action", formAction);
			return true;
		}
	});
	
});


function area_changed(){
	var area=$("#selectarea").val();
	$.cookie('shoparea',area,{path:'/',expires: 60});
	if(area!='###'){
		show_area(area);
	}
}

// エリアの位置までスクロールして光る
function show_area(area){
	var a = $(".shopList .area"+area);
	if(a.size()){
		$(".shopList").scrollTop( a.position().top - $(".shopList th").position().top );
		a.animate({backgroundColor:"#FFFF88"}).animate({backgroundColor:"#FFFFFF"});
	}
}


/* もっとよむ */

function getProductNotes(readmoreUrl) {
	
	var readBox = $("div.btnMore");
	var timerHtml = '<div id="js_timer" style="text-align:center;"><img src="'+ __webroot +'images/timergrey.gif" /></div>';
	
	// もっと読むボタンを読み込み中ボタンに書き換える
	readBox.html(timerHtml);
	
	$.get(readmoreUrl, function(data) {
		// 取得したHTMLを挿入
		readBox.after(data);
		readBox.remove();
	});
}

