AuctionLib = function(){};

AuctionLib.prototype={
	
	//AJAX 接続情報
	ajaxConfig:{"cache":false,"targetUrl":"/apl/ajax/auctionsearch"}
	
	//定数
	,config:{"open":1, "gloan":2, "close":3,"asc":1,"desc":2,"sortLen":6,"sortClass":"idx0"}
	
	//各画面用ページ番号保持
	,pageNumber:{"open":1,"gloan":1,"close":1}
	
	//POST用パラメータ
	,param:{"target":"all","pageNo":1,"sort":2,"sortname":"enddate","category":"open"}

	
	,changeTab:function(target,pageNo){

		if(target == "open"){
			$("#auc_all").attr("class","btn_all_auc");
			$("#auc_open").attr("class","btn_open_auc_on");
			$("#auc_close").attr("class","btn_close_auc");
		}else if(target == "close"){
			$("#auc_all").attr("class","btn_all_auc");
			$("#auc_close").attr("class","btn_close_auc_on");
			$("#auc_open").attr("class","btn_open_auc");		
		
		}else{
			$("#auc_all").attr("class","btn_all_auc_on");
			$("#auc_open").attr("class","btn_open_auc");
			$("#auc_close").attr("class","btn_close_auc");

		}
	}
	,changeCategory:function(target,pageNo){

		this.changeTab(target,pageNo);

		this.paging(target,pageNo);
		
		this.param.category = target;
	}
	
	//ページン用グファンクション
	,paging:function(target,pageNo){
		//alert(this.param.target);	
		
		if(this.param.target != target){
			this.param.sortname = "enddate";
			this.param.sort = 2;

		}
		
		this.param.pageNo = pageNo;
		this.param.target = target;

		this.pageNumber[target] = pageNo;
		
		this.changeTab(target,pageNo);
		
		this.getAuctionsList();
	}
	
	//ソート用ファンクション
	,sorting:function(sortname,tag){

		var target = "." + tag +" span"; 
		var ret = $(target).attr("class");
		var targetclass;
		var nowClass;
		var downClass = "sort_down";
		var upclass = "sort_up";

		
		for(i=1; i <= this.config.sortLen; i++){
			
			targetClass = ("idx0" + i);
			nowClass = "." + targetClass +" span"; 
			
			if(targetClass == tag){

				if(ret == downClass)
					$(nowClass).attr("class",upclass);

				else
					$(nowClass).attr("class",downClass);

			}else{
			
				$(nowClass).attr("class",downClass);
			}
		}
		
		if(this.param.sortname != sortname){
			this.param.pageNo = 1;
			this.param.sort=2;			
		}else{
			
			if(this.param.sort==1){
				this.param.sort=2;
			
			}else if(this.param.sort==2){
				this.param.sort=1;
	
			}else{
				this.param.sort=2;			
			}
		}
		
		this.param.sortname = sortname;
		
		this.getAuctionsList();
	}

	//TODOページcount<>の場合
	,pagingSide:function(distance){
		
		var target= this.param.target;
		
		if(distance=="N"){
			this.pageNumber[target]++;
			this.param.pageNo = this.pageNumber[target];
		
		}else{
			this.pageNumber[target]--;
			this.param.pageNo = this.pageNumber[target];
			
			if(this.pageNumber[target] == 0){
				this.pageNumber[target] = 1;
				this.param.pageNo=1;
			}
		}
		this.getAuctionsList();	
	}
	
	//セットするパラメータのURLを作る
	,makeParam:function(){
		return "type=" + this.param.target 
				+"&sort="+ this.param.sort
				+"&sortname="+ this.param.sortname
				+"&pageno="+ this.param.pageNo;

	}
	
	//ナンバーフォーマット
	,nuberFormat:function(str) {
		var num = new String(new Number(str)).replace(/,/g, "");
		while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
		return num;
	
	}
	
	//AJAXでオークションの情報を取得
	,getAuctionsList:function(param){
		if(param == null){
			
			$.ajax({
				type: "post"
				,url: this.ajaxConfig.targetUrl
				,data: this.makeParam()
				,dataType: "json"
				,cache : false
				//,ifModified: true
				,success:function(json){
					AuctionLib.prototype.view(json);
				}
				,error:function(){
					AuctionLib.prototype.errorView();
				}
		 	});
		}else{
			$.ajax({
				type: "get"
				,url: this.ajaxConfig.targetUrl
				,data: this.makeParam()
				,dataType: "jsonp"
				,cache : false
				//,ifModified: true
				,success:function(json){
					AuctionLib.prototype.view(json);
				}
				,error:function(){
					AuctionLib.prototype.errorView();
				}
		 	});
		}
	}
	
	,getLoanIcon:function(listing_type){
		if(listing_type=="quick"){
			return '<img src="/images/ico_auc_quick.gif" width="43" height="12" border="0" alt="クイック・ローン" title="クイック・ローン"/>';
		
		}else if(listing_type=="gloan"){
			return '<img src="/images/ico_auc_gloan.gif" width="44" height="12" border="0" alt="G-Loan" title="G-Loan"/>';
		
		}else if(listing_type=="cs"){
			return '<img src="/images/ico_auc_cs.gif" width="44" height="12" border="0" alt="cooperative" title="cooperative"/>';
		
		}else{
			return "";
		}
	}
	
	,getWindowTarget:function(listing_type){
		if(listing_type=="cs"){
			return ' target="'+listing_type+'"';
		
		}else{
			return '';
		
		}
	}
	
	,getLenderRate:function(lender_rate, bidstartrate, listing_type,funded){
		var rslt="";
		rslt = (lender_rate == 0 ? bidstartrate : lender_rate) + "&#37";
		if(listing_type != "quick" && listing_type != "gloan" && listing_type != "cs" && funded == "100")
			rslt = rslt +'<br>&nbsp;<img src="/images/ico_auc_down.gif" width="14" height="14" border="0" title="金利低下中" alt="金利低下中"/>';	
		return rslt;
	}
	
	,setPaging:function(json){
		var targetID = ".auctionPaging";
		var tarm= json.count + "件中&nbsp;" + json.tarm.limit_from + "-" + json.tarm.limit_to + "件目を表示中";
		$(targetID).html("");
		$(targetID).append("<p>" + tarm + "</p>");
		$(targetID).append("<ul class='num'><li>" + json.first + "</li><li>" +  json.paging  + "</li><li>" + json.last + "</li></ul>");
	}
	
	,view:function(json){
		
		//下のページン情報を消す
		$("#lastPaging").css("display","none");
		if(json == null){
			this.errorView();
			return;
		}
		
		if(json.count!=0){
			this.setPaging(json);
			
			$("#auctionLists").html("");
			
			var target = "#auctionLists TABLE";
			
			$("#auctionLists").append("<table cellspacing='0' cellpadding='0' border='0'></table>");

			$.each(json.data,function(i,$item){
				$(target).append('<tbody><tr class="auc_area" id="aucIdx'+ $item.auction_id +'"></tr></tbody>'+"\n");

				$(target+" #aucIdx" + $item.auction_id).append('<td class="auc_img"></td>');

				if($item.picpath==""){
					$(target+" #aucIdx" + $item.auction_id + " td").append('<a href="/apl/auction/detail?id=' + $item.auction_id + '">' 
						+ '<img src="/makethumb.jsp?pic=common2/images/img/img_noimg.gif&&w=43&sq=Y&b=N" border="0" width="43" height="43"/></a>');
				}else{
					$(target+" #aucIdx" + $item.auction_id + " td").append('<a href="/apl/auction/detail?id=' + $item.auction_id + '"'+ AuctionLib.prototype.getWindowTarget($item.listing_type) +'>' 
						+ '<img src="/makethumb.jsp?pic='	+ $item.picpath + '&w=43&sq=Y&b=N" border="0" width="43" height="43"/></a>');
				}
				
				$(target+" #aucIdx" + $item.auction_id).append('<td class="auc_ttl"><div><a href="/apl/auction/detail?id=' + $item.auction_id + '" id="auctionlink" '+ AuctionLib.prototype.getWindowTarget($item.listing_type) +'>' + $item.itemname + '</a><br>' + AuctionLib.prototype.getLoanIcon($item.listing_type) + '</div></td>');

				$(target+" #aucIdx" + $item.auction_id).append('<td class="auc_money"><span>¥' + AuctionLib.prototype.nuberFormat($item.bidstart)+'<span></td>');
				if($item.bidstart > 9999999){
					$(target+" #aucIdx" + $item.auction_id+ " td.auc_money span").css("font-size", "90%");	
				}

				$(target+" #aucIdx" + $item.auction_id).append('<td class="auc_rate" nowrap="nowrap">' + AuctionLib.prototype.getLenderRate($item.lender_rate, $item.bidstartrate, $item.listing_type,$item.funded) + '</td>');

				$(target+" #aucIdx" + $item.auction_id).append('<td class="auc_period" nowrap="nowrap">' + $item.loan_duration + 'ヵ月</td>');

				$(target+" #aucIdx" + $item.auction_id).append('<td class="auc_ms" nowrap="nowrap">' + $item.score + '</td>');

				$(target+" #aucIdx" + $item.auction_id).append('<td class="auc_tender"><div class="prog-border"><div class="prog-bar" style="width:'+ $item.funded + '%;"></div></div><div>'+$item.funded+'&#37;</div>' + '</td>');

				$(target+" #aucIdx" + $item.auction_id).append('<td class="auc_limit">'+ $item.endview+'</td>');

			});
			$("#lastPaging").css("display","block");
			//$("#sortmode").html(this.param.sortname +":"+this.param.sort);
		}else{
			$("#auctionLists").append('<table><tr colspan="7"><td>検索結果が見つかりませんでした。</td></tr></table>');
		}
	}
	
	,errorView:function(){
		$("#auctionLists").html("");
		$("div.auctionPaging").html("");
		$("#auctionLists").append('<table><tr colspan="7"><td>検索結果が見つかりませんでした。</td></tr></table>');
	}
};
