//---------------------------------
// 最近見たアイテム
//  引数
//		obj.p : ページ番号
//		obj.m : モード
//---------------------------------
function showViewItem( obj ) {

	// 会員ならばcitem_areaがある
	if( $('div#citem_area').length ){
		$.ajax({
			url: '/api/_view_item?p='+obj.p+'&m='+obj.m,
			dataType:'json',
			timeout:10000,
			cache: false,
			success: showViewItemSuccess,
			error: showViewItemError
		});
		//ページ番号更新
		$('div#citem_p').empty().append(obj.p).hide();
	}
}
function showViewItemSuccess( json ) {

	if(json.ItemList.length < 1){
		// memcacheにない場合の表示
		$('div#citem_area').hide();
		$('div#citem_area_none').show();
		return;
	}

	$('div#citem_area').find('div.wsize').each(function(i){
		if(json.ItemList[i]){
			var item = json.ItemList[i];
			var item_title = decodeURIComponent(item.item_title);
			var RestTime   = decodeURIComponent(item.RestTime);
			var img;
			if(item.picture_num > 0){
				img = $('<img/>').attr({'src':item.src});
			}else{
				img = $('<img/>').attr({'src':'/image/main/noprinting_80.gif'});
			}
			//image
			var a_img  = $('<a/>').attr({href:'/item/item_'+ item.item_id +'.html?_SRC='+item.SRC }).append($(img));
			var div_center = $('<div>').addClass('center').append(a_img);

			//title
			var a_item_title = $('<a/>').attr({href:'/item/item_'+ item.item_id +'.html?_SRC='+item.SRC }).append(item_title);
			var dt_title = $('<dt>').addClass('itemtitle').append(a_item_title);

			//current_price bid_num RestTime
			var dd_txt = '現在価格:'+ addComma(item.current_price) +'円<br />残り時間:'+RestTime+'<br />入札件数:'+item.bid_num+'件';
			var dd = $('<dd>').addClass('font10').append(dd_txt);
			var dl = $('<dl>').append(dt_title).append(dd);
			$(this).empty().append(div_center).append(dl);
		}
		else{
			$(this).empty();
		}
	});
	//左スクロール表示／非表示
	if(json.p == 1){
		$('div#citem_scroll_l').hide();
	}else{
		$('div#citem_scroll_l').show();
	}
	//右スクロール表示／非表示
	if(json.p >= json.max_p){
		$('div#citem_scroll_r').hide();
	}else{
		$('div#citem_scroll_r').show();
	}
}
function showViewItemError() {
	$('div#citem_area').hide();
	$('div#citem_area_none').show();
}


//---------------------------------
// 最近見たカテゴリ
//---------------------------------
function showViewCateg() {

	// 会員
	if( $('div#ccategory_area').length ){
		$.getJSON( '/api/_view_categ', showViewCategCall );
	}
}

function showViewCategCall( json ) {

	var ccategory_area  = $('div#ccategory_area');
	var li_arr = ccategory_area.find('li.ccategory_path');

	for(i=0; i<json.CategList.length ; i++){
		var categ_path_arr = new Array();
		for(j=0; j<json.CategList[i].length ; j++){
			var categ_id = json.CategList[i][j].categ_id;
			var categ_name = decodeURIComponent( json.CategList[i][j].categ_name );
			var a = $('<a/>').attr({'href':'/category/categ_' + categ_id + '.html'}).append(categ_name);
			var a_html = $('<div/>').append(a).html();
			categ_path_arr.push(a_html);
		}
		var categ_path_str = categ_path_arr.join('⇒');
		$(li_arr[i]).empty().append(categ_path_str);
	}
}

//---------------------------------
// ウォッチリスト追加
//  引数
//		a : aオブジェクト
//		ev : eventオブジェクト
//---------------------------------
function addWatch(a, ev) {
	var item_id = a.attr('id').split("_")[1];
	$.ajax({
		url: '/api/_mywa?i='+item_id,
		dataType:'json',
		timeout:10000,
		cache: false,
		success: function(json){
			var opt = new Object();
			if(json.Err){
				if(json.TooMany){
					opt.message = '<p>登録上限数（'+ json.MaxNum +'）を超えたため、ウォッチリストに登録できません。<br>⇒<a href="/_my4">一覧へ</a></p>';
				}else if(json.Duplicate){
					opt.message = '<p>すでにウォッチリストに登録されています。<br>⇒<a href="/_my4">一覧へ</a></p>';
					a.replaceWith('ウォッチリストへ登録済み');
				}else if(json.NotLogin){
					opt.message = '<p>ログインしてから登録してください。</p>';
				}
			}else{
				opt.message = '<p>ウォッチリストに登録しました。<br>⇒<a href="/_my4">一覧へ</a></p>';
				a.replaceWith('ウォッチリストへ登録済み');
			}
			openDialog(opt, ev);
		},
		error: function(){
			var opt = new Object();
			opt.message = '<p>ご利用できない状態、もしくは現在混み合っており、利用できません。</p>';
			openDialog(opt, ev);
		}
	});
}
//---------------------------------
// ダイアログ表示
//  引数
//		opt.message : メッセージ
//		ev : eventオブジェクト
//---------------------------------
function openDialog(opt, ev) {
	// 古いオブジェクトは削除
	$('div#addwatch_dialog').remove();
	// 新しいオブジェクト作成
	var item_dialog = $('<div/>').attr({'id':'addwatch_dialog'}).html(opt.message);
	// ダイアログ作成
	item_dialog.dialog({
		position: [parseInt(ev.clientX)+80, parseInt(ev.clientY)-62],
		width:200,
		minHeight:50,
		close:function(){
			item_dialog.remove();
			clearTimeout(timer_id);
		}
	});
	var img = $('<img>').attr({ 'src':'/image/common/watch-close.gif','width':13,'height':13 });
	$('a.ui-dialog-titlebar-close').empty().append(img);
	$('div.ui-dialog').addClass('itemlist_add');

	var timer_id = setTimeout(
		function(){
			item_dialog.remove();
			clearTimeout(timer_id);
		},
		3000
	);
}
//---------------------------------
// 読み込み中画像
//---------------------------------
function makeLoadImg(){
	return $('<img/>').attr({'src':'/image/ajax_loader.gif'});
}
//---------------------------------
// 読み込み中画像
//---------------------------------
function makeLoadImg104x160(){
	return $('<img/>').attr({'src':'/image/ajax_loader104x160.gif'});
}

//---------------------------------
// div＋読み込み中画像
//---------------------------------
function makeLoadImgDiv(){
	return $('<div/>').addClass('loading_img').append( makeLoadImg() );
}
//---------------------------------
// 要素の中身を置き換えする
//  引数
//		elements : 
//		obj : 
//---------------------------------
function replaceElements(elements ,obj){
	elements.each(function(){
		$(this).empty().append( obj.clone() );
	});
}
//---------------------------------
// 価格用カンマ表記
//---------------------------------
function addComma(price){
	var str = String(price);
	var i;
	for(i = 0; i < str.length/3; i++){
	    str = str.replace(/^([+-]?\d+)(\d\d\d)/,"$1,$2");
	}
	return str;
}

