var BID = {};

function getFlashModule(name)
{
	if( navigator.appName.indexOf("Microsoft") != -1)
		return document.getElementById(name);
	else
		return document.embeds[name];
}

var bkMyBean = -1;

var sflagSound5 = 0;

var bkRemainedSec = -100;
var nRemainedSec = 0;
var nTraceUID = 0;
var nTraceUIDSoundOff = false;

var bModuleAwaked = false;

var buid,breq;
var bidContinuation = 15;
var nowBidding = false;

var bAuctionDone = false;

var currentPrice = 0;
var currentWinner = '';
var currentSaving = 0;
var mySaving = 0;

var calcTarget = 0;

var bidedPrice = 0;	// 현재 입찰 가격
var beanPrice = 0;	// 콩 가격

var perUnit = 0;
var orgcost = 0;
var auctionType = 0;
var beanType = "R";
var myID = '';

var sreqs;
var nextonloadsmodule = null;

var msgTimer = null;

BID.module = {
	calcSaving : function(curvalue)
	{
		if( currentPrice == curvalue )
			return;

		currentPrice = curvalue;

		// 얼마를 아끼게 되는가?
		if(myID == '')
			return;
		
		if( $('noticeSavingprice1') )
		{
			var template;
			if( myID == currentWinner )
			{
				// 현재 가격이 이익이다
				currentSaving = orgcost - bidedPrice - beanPrice;
				
				if(currentSaving > 0)
					$('noticeSavingprice1').innerHTML = GetMoneyType(currentSaving.toString());
				else
					$('noticeSavingprice1').innerHTML = "0";

				template = $('imwinner').innerHTML;
				
				if(currentSaving > 0)				
					$('savingNotice').innerHTML = GetMoneyType(currentSaving.toString());
				else
				{
					$('savingNotice').innerHTML = "0";
				}
				
				$('mySavingNotice').innerHTML = template;
	
				$('myprice').innerHTML = GetMoneyType( bidedPrice.toString() );
			}
			else
			{
				var nextPrice;			
				
				if(auctionType == 0)
				{
					nextPrice = parseInt(currentPrice)+parseInt(perUnit);
				}
				else if(auctionType == 1)
				{
					{
						var nBiddingcnt = parseInt( $('biddingcnt').innerHTML );
						nextPrice = ( (parseInt(bidedPrice)*nBiddingcnt) + parseInt(currentPrice) + parseInt(perUnit) ) / (nBiddingcnt+1);
					}
						
					nextPrice = parseInt(nextPrice);
				}
				
				currentSaving = orgcost - beanPrice - nextPrice;
				
				// 콩을 하나 더 써야한다..
				if(beanType == 'R')
					currentSaving -= 500;
				
				if(currentSaving > 0)
					$('noticeSavingprice2').innerHTML = GetMoneyType(currentSaving.toString());
				else
					$('noticeSavingprice2').innerHTML = "0";
					
				template = $('notwinner').innerHTML;
				
				if(currentSaving > 0)
					$('savingNotice').innerHTML = GetMoneyType(currentSaving.toString());
				else
					$('noticeSavingprice2').innerHTML = "0";
					
				$('mySavingNotice').innerHTML = template;			
				
				$('myprice').innerHTML = GetMoneyType( nextPrice.toString() );
			}
		}
	},	
	calcWinner : function(winner)
	{
		currentWinner = winner;
	},	
	setConst : function(_uid, _ty, _unit, _orgcost, _myid, _beanty)
	{
		calcTarget = _uid;

		auctionType = _ty;
		perUnit = _unit;
		orgcost = _orgcost;
		myID = _myid;
		beanType = _beanty;
	},
	setContinuation : function(n)
	{
		bidContinuation = n;
	},
	alert : function(b)
	{
		if(!b)
			myAlert("서버와의 통신에 실패하였습니다.");
	},
	msg : function(o)
	{
		subNoti(o);
	},
	subMsg : function(o)
	{
		if( $('subMsgViewer') )
		{
			if( msgTimer )
				clearTimeout(msgTimer);
				
			msgTimer = setTimeout( "BID.module.subMsgHide()" ,4000);
			
			$('subMsgViewer').innerHTML = o;
		}
	},
	biMsg : function(o)
	{
		if( $('bimsg') )
		{			
			if( !$("bilog") )
			{
				var hLayer = document.createElement('INPUT');
				hLayer.type="hidden";
				hLayer.id = "bilog";
				document.body.appendChild(hLayer);
			}
			
			var oLayer = $('bilog');
						
			if( oLayer.value != o )
			{
				oLayer.value = o;
				o = "<img src='/images/icon_qs.gif' align=absmiddle> <b>" + this.getID(o) + "</b>님 즉시보상구매 신청";
				$('bimsg').innerHTML = o;			
			}			
		}
	},
	subMsgHide : function()
	{
		msgTimer = null;
		$('subMsgViewer').innerHTML = "";
	},
	log : function(o, logs)
	{
		if( !$("hlog") )
		{
			var hLayer = document.createElement('INPUT');
			hLayer.type="hidden";
			hLayer.id = "hlog";
			document.body.appendChild(hLayer);
		}
		
		var hLayerObj = $("hlog");
		
		var logCost = "";
		for(i=0;i<logs.length;i++)
		{
			logCost += logs[i]["COST"] + ";";
		}
		
		if(hLayerObj.value != logCost)
		{
			if( $('tbLog') )
				$('logTemplate').removeChild($('tbLog'));
							
 			var table1=document.createElement("table");
   		with(table1)
   		{
   			id="tbLog";
   			width="100%";
    		border=0;
    		cellpadding=0;
    		cellspacing=0;
   		}

			// 태그 만들기
			var i;
			var values = "";
			for(i=0;i<logs.length;i++)
			{
	   		var tr1 = table1.insertRow();
	   		with(tr1)
	   		{
	   			height=16;
	   			align="center";
	   		}
	   			   		
	   		var td1 = tr1.insertCell();
	   		with(td1)
	   		{
	   			td1.style.fontFace="verdana";
	   			td1.style.color="#666666";	   			
	   			width="30%";
	   			align="center";
	   			innerHTML=GetMoneyType(logs[i]["COST"])+"원";
	   		}				
	   		td1 = tr1.insertCell();
	   		with(td1)
	   		{
	   			var ip = "<span style='padding-left:5px;font-face:verdana;color:#666666;font-size:10px;'>"+logs[i]["IP"]+"</span>";
	   			innerHTML= this.getID(logs[i]["BIDDER"]) + ip;
	   		}
			}			
			$('logTemplate').appendChild(table1);
			hLayerObj.value = logCost;
		}
	},
	mylog : function(o, logs)
	{
		this.log(o, logs);
	},
	lucky : function(o, logs)
	{
			if( $('tbLog') )
				$('logTemplate').removeChild($('tbLog'));
							
 			var table1=document.createElement("table");
   		with(table1)
   		{
   			id="tbLog";
   			width="100%";
    		border=0;
    		cellpadding=0;
    		cellspacing=0;
   		}

			// 태그 만들기
			var i;
			var values = "";
			for(i=0;i<logs.length;i++)
			{
	   		var tr1 = table1.insertRow();
	   		with(tr1)
	   		{
	   			height=16;
	   			align="center";
	   		}
	   		tr1.style.fontFace="verdana";
	   		tr1.style.color="#666666";	   		
	   		
	   		var td1 = tr1.insertCell();
	   		with(td1)
	   		{
	   			width="30%";
	   			align="center";
	   			innerHTML=GetMoneyType(logs[i]["COST"])+"원";
	   		}				
	   		td1 = tr1.insertCell();
	   		with(td1)
	   		{
	   			width="40%";
	   			align="center";
	   			innerHTML=this.getID(logs[i]["BIDDER"]);
	   		}
	   		td1 = tr1.insertCell();
	   		with(td1)
	   		{
	   			width="30%";
	   			align="center";	   			
	   			innerHTML=logs[i]["RBEAN"];
	   		}
	   		td1.style.color="#FF6600";
	   		td1.style.fontWeight="bold";
			}			
			$('logTemplate').appendChild(table1);
			
			if( !$("hlog") )
			{
				var hLayer = document.createElement('INPUT');
				hLayer.type="hidden";
				hLayer.id = "hlog";
				document.body.appendChild(hLayer);
			}
			
			var hLayerObj = $("hlog");
			hLayerObj.value = "-1";
	},
	buyitnow : function(o, logs)
	{
			if( $('tbLog') )
				$('logTemplate').removeChild($('tbLog'));

 			var table1=document.createElement("table");
   		with(table1)
   		{
   			id="tbLog";
   			width="100%";
    		border=0;
    		cellpadding=0;
    		cellspacing=0;
   		}

			// 태그 만들기
			var i;
			var values = "";
			for(i=0;i<logs.length;i++)
			{
	   		var tr1 = table1.insertRow();
	   		with(tr1)
	   		{
	   			height=16;
	   			align="center";
	   		}
	   		tr1.style.fontFace="verdana";
	   		tr1.style.color="#666666";	   		
	   		
	   		var td1;
	   		td1 = tr1.insertCell();
	   		with(td1)
	   		{
	   			width="100%"
	   			align="center";
	   			innerHTML="<img src='/images/icon_qs.gif' align=absmiddle> " + this.getID(logs[i]["BIDDER"]);
	   		}
			}			
			$('logTemplate').appendChild(table1);
			
			if( !$("hlog") )
			{
				var hLayer = document.createElement('INPUT');
				hLayer.type="hidden";
				hLayer.id = "hlog";
				document.body.appendChild(hLayer);
			}
			
			var hLayerObj = $("hlog");
			hLayerObj.value = "-1";
	},	
	myprice : function(o)
	{
		bidedPrice = parseInt(o);
	},
	biddingcntg : function(o)
	{
		$('biddingcntg').innerHTML = o;
	},	
	biddingcnt : function(o)
	{
		$('biddingcnt').innerHTML = o;
		var val;
		
		if(beanType == 'R')
			val = parseInt(o) * 500;	
		else
			val = 0;			
			
		beanPrice = parseInt(val);
			
		$('biddingcntCalced').innerHTML = GetMoneyType( val.toString() );
		
		if($('biddingcnt2'))
		{
			$('biddingcnt2').innerHTML = o;
			$('biddingcntCalced2').innerHTML = $('biddingcntCalced').innerHTML;
			
			var xCalced = orgcost - beanPrice;
			if(xCalced < 0)
				xCalced = 0;
			
			$('biddingcntCalced3').innerHTML = GetMoneyType( xCalced.toString() );
		}
	},
	mybeang : function(o)
	{
		if( $('beang') )
		{
			$('beang').innerHTML = o;
			
			$('bean2').innerHTML = parseInt(o)+parseInt($('bean1').innerHTML);
			$('myBean').innerHTML = document.getElementById('templateMyBean').innerHTML;
		}
	},	
	mybean : function(o)
	{
		if($('bean1'))
		{
			document.getElementById('bean1').innerHTML = o;
			document.getElementById('bean2').innerHTML = o;
			
			if( $('beang') == null )
				document.getElementById('myBean').innerHTML = document.getElementById('templateMyBean').innerHTML;
		}
		
		// 비딩 모듈을 가져온다.
		if(!bModuleAwaked)
		{
			bModuleAwaked = true;
			document.getElementById('bidmodule').getValue(buid,breq);			
		}
	},
	getMyBean : function(t)
	{
		getFlashModule('bidmodule').getMyBean(t);
	},
	status : function(uid, values)
	{
		try
		{
			var bks = document.getElementById('status'+uid).innerHTML;
			if(values != bks)
			{
				if(values == "Y")
				{
					$('procY'+uid).style.display = "";
					if( $('procN'+uid) )
						$('procN'+uid).style.display = "none";
					$('procD'+uid).style.display = "none";
					bks.innerHTML = "Y";
				}
				else if(values == "@")
				{
					$('procY'+uid).style.display = "none";
					if( $('procN'+uid) )
						$('procN'+uid).style.display = "none";
					$('procD'+uid).style.display = "";
					bks.innerHTML = "@";
					
					BID.module.countdown(uid, 0);
				}
			}
		}
		catch(ee)
		{
		}
		
		try
		{			
			if( nTraceUID == uid )
			{
				if(values == "Y")
				{				
					if(nRemainedSec != 0)
					{
						if( Math.abs(bkRemainedSec - nRemainedSec) < 10)
						{
							
						}
						else
						{
							var oDate = new Date();
							oDate = oDate.addSeconds(nRemainedSec);
							var msg = oDate.toString("yyyy/MM/dd HH:mm:ss");
							
							$('etime').innerHTML = msg;
						
							if( $('estimatedTime').style.display == "none" )
								$('estimatedTime').style.display = "";											
						}					
						
						bkRemainedSec = nRemainedSec;
					}
				}
				else
				{
					if( $('estimatedTime').style.display == "" )
						$('estimatedTime').style.display = "none";
				}
			}
		}
		catch(e2)
		{
			// none;
		}			

	},
	doneBidding : function(uid)
	{
		nowBidding = false;
	},
	failBidding : function(uid,msg)
	{
		nowBidding = false;
		subNoti(msg);		
	},
	bid : function(uid)
	{		
		nowBidding = true;
		getFlashModule('bidmodule').bid(uid);
	},
	getID : function(values)
	{
		var sval = values.substr(values.indexOf(">")+1);
		
		var oval = "<a target='_blank' href='/credit.aspx?seller=";
		oval += sval;
		oval += "'><font color='336699'><b>";
		oval += values;
		oval += "</b></font></a>";
		
		return oval;		
	},
	bidder : function(uid, values)
	{
		if(calcTarget.toString() == uid.toString())
			this.calcWinner(values);
		
		if(document.getElementById('bidder' + uid))
		{
			if( !$("hbidder" + uid) )
			{
				var hLayer = document.createElement('INPUT');
				hLayer.type="hidden";
				hLayer.id = "hbidder"+uid;				
				document.body.appendChild(hLayer);
			}
			
			var sval = values;			
			var hLayerObj = $("hbidder" + uid);

			if( sval != hLayerObj.value )
			{
				hLayerObj.value = sval;
				document.getElementById('bidder' + uid).innerHTML = this.getID(values);
			}
		}
	},	
	luckyzone : function(uid, values)
	{
		return;
		
		if(nTraceUID == uid)
		{
			// 럭키존..
			var msg = "곧 럭키스팟에 도달합니다. <font color='#669900'><strong>"+GetMoneyType(values)+"</strong></font>원에 입찰하신 분에게 완두콩 2개를 드립니다.";
			subNoti(msg);			
		}		
	},	
	cost : function(uid, values)
	{
		if(calcTarget.toString() == uid.toString())
			this.calcSaving(values);

		if(nTraceUID == uid && document.getElementById('imgViewer2') && !bAuctionDone)
			document.getElementById('imgViewer2').NextCost(values);			

		if( !$("hcost" + uid) )
		{
			var hLayer = document.createElement('INPUT');
			hLayer.type="hidden";
			hLayer.id = "hcost" + uid;
			document.body.appendChild(hLayer);
		}
		
		var hLayerObj = $("hcost" + uid);
		if(hLayerObj.value != values)
		{
			hLayerObj.value = values;
			
			if(document.getElementById('cvalue' + uid))
				document.getElementById('cvalue' + uid).setValue(values);			
		}
	},
	countdown : function(uid, values)
	{
		if(values == -1)
			return;
		
		if( nTraceUID == uid )
			nRemainedSec = values;
		
		if(nTraceUID == uid && document.getElementById('bidalarm'))
		{
			if(bidContinuation > values)
			{
				if(document.getElementById('bidalarm').style.display == "none")
					document.getElementById('bidalarm').style.display = "";
			}
			else
			{
				if(document.getElementById('bidalarm').style.display == "")
					document.getElementById('bidalarm').style.display = "none";
			}
			
			if( $('subtimer'+uid) )
			{
				$('subtimer'+uid).setValue(values);
			}
		}
		
		if( !nTraceUIDSoundOff && nTraceUID == uid )
		{
			if(values == 5)
			{
				if(sflagSound5++ == 0)
					document.getElementById('soundM').play5();
			}
			else
				sflagSound5 = 0;
		}		
		
		if(document.getElementById('dtimer' + uid))
			document.getElementById('dtimer' + uid).setValue(values);
	},
	load : function(uid,req,bAwake)
	{
		buid=uid;
		breq=req;
		
		bModuleAwaked = bAwake;
		
		document.write("<div style='position:absolute;'>");
		flex(10,10,'/swf/BidModule10.swf','bidmodule');
		document.write("</div>");

		AddOnloadFunc(BID.module.onLoadPage);
	},
	sload : function(_sreqs,_nextonloadsmodule)
	{
		sreqs = _sreqs;
		if(_nextonloadsmodule)
			nextonloadsmodule = _nextonloadsmodule;
		
		document.write("<div style='position:absolute;'>");
		flex(10,10,'/swf/BidModule10.swf','sbidmodule');
		document.write("</div>");

		AddOnloadFunc(BID.module.onLoadPage2);
	},
	onLoadPage : function()
	{
		if(bModuleAwaked)
		{
			document.getElementById('bidmodule').getValue(buid,breq);
		}
			
	},
	onLoadPage2 : function()
	{
		if(nextonloadsmodule)
			nextonloadsmodule();
			
		document.getElementById('sbidmodule').getValue('',sreqs);
	},
	setReq : function(req, module)
	{
		if(!module)
			document.getElementById('bidmodule').setReq(req);
		else
			document.getElementById(module).setReq(req);
	},
	setUid : function(uid, module)
	{
		if(!module)
			document.getElementById('bidmodule').setUid(uid);
		else
			document.getElementById(module).setUid(uid);
	}	
};