
var urlGet_live = "/ajax/auction.txt";
//var urlGet_live = "/test_ajax/auction.txt";
var timeLive = 5000; //<==== 5 Sec. Refreshing
var TmpSKU = "";
var TmpStatus = "";
var ShowClock = "Y";

var tmpShow_Live_Image1 = "";
var tmpShow_Live_Image2 = "";
var tmpShow_Live_Product_Name = "";
var tmpShow_Live_Time = "";
var tmpShow_Live_price = "";
var tmpShow_Live_buy_button = "";

var tableOpen = "<table border=0 cellspacing=0 cellpadding=0 width=100%>";
var textTime1 = "<span class=left2><b>TIME LEFT:</b><font face=Arial style='font-size: 15px; font-weight: 700; color: 000000'> ";
var textTime1_2 = "<span class=left2><b>TIME LEFT:</b><font face=Arial style='font-size: 15px; font-weight: 700; color: 000000'> ";
var textTime2 = "</span>";

var nowRemainTime = 0;

function popFullDetails(sku,type)
{
	//fullUrl = "/Jewellery/fulldetail.aspx?Type="+type+"&SKU="+sku;
	fullUrl = "/index.php?task=jewelleryItemDisplay&action=display&sku="+sku;
	var w=window.open(fullUrl,'FullDetails',"left=150, top=100 , height=440, width=900,status= no, resizable= no, scrollbars=no, toolbar=no,location=no,menubar=no");
	w.focus();
}

function formatCountDown()
{
	tmpTime = "";
	tmpH = Math.floor(nowRemainTime/60);
	tmpS = nowRemainTime - (tmpH*60);
	if (tmpS<10)
	{
		tmpTime = textTime1+tmpH+":0"+tmpS+textTime2;
	}
	else
	{
		tmpTime = textTime1+tmpH+":"+tmpS+textTime2;
	}
	return tmpTime;
}

function live_countDown() 
{
	if (ShowClock=="Y")
	{
		if (nowRemainTime>0)
		{
			/*
			tmpTime = "";
			tmpH = Math.floor(nowRemainTime/60);
			tmpS = nowRemainTime - (tmpH*60);
			if (tmpS<10)
			{
				tmpTime = textTime1+tmpH+":0"+tmpS+textTime2;
			}
			else
			{
				tmpTime = textTime1+tmpH+":"+tmpS+textTime2;
			}
			*/
			tmpTime = formatCountDown();
			try {
				$('live_time').innerHTML = tmpTime;
			} catch (e) {}
			nowRemainTime = nowRemainTime-1;
		}
		else
		{
			try {
				$('live_time').innerHTML = textTime1_2+"0:00"+textTime2;
				//$('live_time').innerHTML = textTime1_2+"-"+textTime2;
			} catch (e) {}
		}
	}
	setTimeout("live_countDown()", 1000)
}

function live()
{
	if (document.location.href.indexOf("watchLive") < 0) {
		return false;
	}
	showLoading_live();
	ajaxReq(urlGet_live);
	self.setInterval("ajaxReq(urlGet_live)", timeLive)
	live_countDown();
}

function ajaxReq(url) 
{
	var ran = Math.floor(Math.random()*10000000)
	url = url + "?r="+ran;
	var al =  new Ajax.Request(url,
		{
			method: 'get',
			parameters: '',
			onComplete: getResponse_live,
			onFailure: showLoading_live
		}
	);
}

function getResponse_live(t) 
{
	if (200 == t.status) showLive(t.responseText);
	else showLoading_live();
}

function showLive(liveString) {
	splitString = liveString.split("#")
	var vProductName = splitString[0];
	var vPicURL = splitString[1];
	var vSKU = splitString[2];
	var vStatus = splitString[3];
	var vQty = splitString[4];
	var vPrice = splitString[5];
	var vUkHour = splitString[6];
	var vType = splitString[7];
	var vTime = splitString[8];
	var vCPrice = splitString[9];
	var vClockStatus = splitString[10];
	
	var vWebAuction = splitString[11];
	
	var vAucRef = splitString[12];
	
	//alert(splitString);

	if (vCPrice!="0")
	{
		vPrice = vCPrice;
	}

	nowRemainTime = parseInt(vTime);

	///=== Check Time Out ===///
	if (vType=="0"&&nowRemainTime==0)
	{
		vStatus = "5"; //Time out
	} 
	else if (vType=="1")
	{
		if (vQty=="0")
		{
			vStatus = "5"; //Out of stock
		}
		//if (vStatus=="2")
		//{
		//	vQty = "0";
		//	vStatus = "5"; //Out of stock
		//}
	} 


	///=== Check for New Game ===///
	vCheckNewGame = "Y";
	if (TmpSKU != "") {
		if (TmpSKU != vSKU) {
			TmpSKU = vSKU;
			vCheckNewGame = "Y";
		} else {
			vCheckNewGame = "N";
		}
	} else {
		TmpSKU = vSKU;
		vCheckNewGame = "Y";
	}
	if (vCheckNewGame == "N")
	{
		if (TmpStatus!= "") {
			if (TmpStatus != vStatus) {
				TmpStatus = vStatus;
				vCheckNewGame = "Y";
			} else {
				vCheckNewGame = "N";
			}
		} else {
			TmpStatus = vStatus;
			vCheckNewGame = "Y";
		}
	}

	///=== Check Show Clock ===///
	if (vType=="0") {
		if (vClockStatus=="0")
		{
			ShowClock = "N";
		} 
		else
		{
			ShowClock = "Y";
		}
	} else {
		ShowClock = "N";
	}


	///=== Check Status for Button ===///
	if (vStatus=="4") // Limited Stock
	{
		vBuyPic = "/style/website/images/button/limited-stock.gif";
	}
	else if (vStatus=="2")
	{
		//vBuyPic = "/images/button/sold-out.gif";
		vBuyPic = "/style/website/images/button/auction-close.gif";
	}
	else if (vStatus=="5")
	{
		vBuyPic = "/style/website/images/button/auction-close.gif";
	}
	else
	{
		if (vType=="2")
		{
			vBuyPic = "/style/website/images/button/buynow.gif";
		}
		else
		{
			vBuyPic = "/style/website/images/button/bid-now.gif";
		}
	}

	//$('live_status').innerHTML = "vType="+vType+", vQty="+vQty+", vStatus="+vStatus;

	///=== Check Live Time ===///
	vCheckLive = "Y";
	tmpUkHour = new Date (new Date().toDateString() + ' ' + vUkHour);
	tmpUkHourBegin = new Date (new Date().toDateString() + ' ' + '12:01:00 AM');
	tmpUkHourEnd = new Date (new Date().toDateString() + ' ' + '08:00:00 AM');
	//tmpUkHour = parseInt(vUkHour);
	//if (vUkHour=="15"||vUkHour=="16"||vUkHour=="17"||vUkHour=="18"||vUkHour=="19"||vUkHour=="20"||vUkHour=="21"||vUkHour=="22"||vUkHour=="23")
	if (tmpUkHour>=tmpUkHourBegin&&tmpUkHour<=tmpUkHourEnd)
	{
		vCheckLive = "N"; 
	}
	//vCheckLive = "Y"; 

	var newline = "\n\r";
	if (vCheckLive=="N")
	{
		vError = "WE ARE BROADCASTING ON SKY 666, FREESAT 817 (9:30AM - 12AM) AND FREEVIEW 40 (1PM- 6PM). PLEASE SELECT OPTIONS FROM OUR MAIN BAR TO SHOP ONLINE OUTSIDE THESE HOURS.";
		s = "<table border=0 cellspacing=0 cellpadding=0 width=168 height=250>"+newline;
		s += "<tr>"+newline;
		s += "	<td align=center><span class=detail-shop>"+vError+"</span></td>"+newline;
		s += "</tr>"+newline;
		s += "</table>"+newline;
		$('live').innerHTML = s;
		TmpSKU = "";
	}
	else
	{
		if (vProductName=="error")
		{
			vError = "Sorry, we are currently experiencing technical difficulties. Our engineers are working hard to resolve this situation. Normal service will resume shortly.";
			s = "<table border=0 cellspacing=0 cellpadding=0 width=168 height=250>"+newline;
			s += "<tr>"+newline;
			s += "	<td align=center><span class=detail-shop>"+vError+"</span></td>"+newline;
			s += "</tr>"+newline;
			s += "</table>"+newline;
			$('live').innerHTML = s;
			TmpSKU = "";
		}
		else if(vWebAuction=="0")
		{
		    vError = "SORRY, THIS AUCTION IS ONLY OPEN TO PHONE BIDDERS. PLEASE CALL 0844 854 3388 TO BID."
			s = "<table border=0 cellspacing=0 cellpadding=0 height=250 width=165>"+newline;
			s += "<tr>"+newline;
			s += "	<td align=center><span class=detail-shop>"+vError+"</span></td>"+newline;
			s += "</tr>"+newline;
			s += "</table>"+newline;
			$('live').innerHTML = s;
			TmpSKU = "";
		}
		else
		{
			/////////// Show Image1 //////////
			if (vStatus!="2"&&vStatus!="5") { 
				s = "<a href='#' onclick='popFullDetails(&#039;"+ vSKU +"&#039;,&#039;L&#039;);'>"+newline;
				s += "<img border=0 src=/style/website/images/more-info-h1.gif align=left  Alt='More Info' style=cursor:hand onmouseover=this.src='/style/website/images/more-info-h2.gif' onmouseout=this.src='/style/website/images/more-info-h1.gif' style='margin-top:5px; margin-left:3px;'/></a>"+newline;
			}
			else { // For Sold Out  and Time out)
				s = "<img border=0 src=/style/website/images/more-info-h1.gif align=left  Alt='More Info' onmouseover=this.src='/style/website/images/more-info-h2.gif' onmouseout=this.src='/style/website/images/more-info-h1.gif' style='margin-top:5px; margin-left:3px;'/>"+newline;
			}
			tmpShow_Live_Image1 = s;


			/////////// Show Image2 //////////
			if (vStatus!="2"&&vStatus!="5") { 
				s = "<a href='#' onclick='popFullDetails(&#039;"+ vSKU +"&#039;,&#039;L&#039;);'>"+newline;
				s += "<img border=0 src="+ vPicURL +" width=120 height=120 alt='More Info'></a>"+newline;
			}
			else { // For Sold Out and Time Out
				s = "<img border=0 src="+ vPicURL +" width=120 height=120 alt='More Info'>"+newline;
			}
			s += "<br><center><span class=detail-shop>"+ vProductName +"</span></center>";
			tmpShow_Live_Image2 = s;


			/////////// Show Product Name //////////
			//s = "<center><span class=detail-shop>"+ vProductName +"</span></center>";
			//tmpShow_Live_Product_Name = s;


			/////////// Show Time //////////
			if (vType=="0")
			{
				if (vClockStatus=="0")
				{
					tmpTime = formatCountDown();
					s = tmpTime;
				} 
				else if (vClockStatus=="1")
				{
					if (nowRemainTime==0)
					{
						s = textTime1_2+"0:00"+textTime2;
					}
					else
					{
						tmpTime = formatCountDown();
						s = tmpTime;
					}
				}
			} else if (vType=="1")
			{
				nowRemainTime = 0;
				if (vStatus!="2"&&vStatus!="5") 
				{
					s = "<span class=left2-1><b>QUANTITY LEFT:</b><font face=Arial style='font-size: 15px; font-weight: 700; color: 000000'> "+vQty+"</span>";
				}
				else 
				{
					s = "<span class=left2-1><b>QUANTITY LEFT:</b><font face=Arial style='font-size: 15px; font-weight: 700; color: 000000'> "+vQty+"</span>";
				}
			}
			else if (vType=="2")
			{
				nowRemainTime = 0;
				s = "";
			}
			tmpShow_Live_Time = s;


			/////////// Show Price //////////
			if (vType=="0")
			{
				s = "<span class=left><b>NOW AT:</b><font face=Arial style='font-size: 15px; font-weight: 700; color: 000'>&nbsp;&#163;"+ vPrice +"</font>";
			} else if (vType=="1")
			{
				s = "<span class=left-1><b>NOW AT:</b><font face=Arial style='font-size: 15px; font-weight: 700; color: 000'>&nbsp;&#163;"+ vPrice +"</font>";
			} else if (vType=="2")
			{
				s = "<span class=detail-shop><center><font face=Arial style='font-size: 15px; font-weight: 700; color: 000'>&nbsp;&#163;"+ vPrice +"</font></center>";
			}
			tmpShow_Live_price = s;


			/////////// Show BuyNow Button //////////
			if (vStatus!="2"&&vStatus!="5") {
				s = "<a href='https://www.rocksandco.com/index.php?_t=basket&action=buy&auction="+ vAucRef +"&sku="+ vSKU +"' target='_blank'>";
				s += "<img border=0 src="+ vBuyPic +" alt='Bid Now' hspace=2 vspace=2></a>"+newline;
			}
			else { // For Sold Out
				if (vStatus=="5") {
					s = "<p align=center><img border=0 src="+ vBuyPic +" alt='Auction Closed' hspace=2 vspace=2>"+newline;
				} else {
					s = "<p align=center><img border=0 src="+ vBuyPic +" alt='Sold Out' hspace=2 vspace=2>"+newline;
				}
			}
			tmpShow_Live_buy_button = s;


			if (vCheckNewGame == "Y")
			{
				s = "<table border=0 cellpadding=0 cellspacing=1 width=168 height=250>"+newline;
				s += "<tr>"+newline;
				s += "<td id=live_image1></td>"+newline;
				s += "</tr>"+newline;
				s += "<tr>"+newline;
				s += "<td align=center id=live_image2></td>"+newline;
				s += "</tr>"+newline;
				//s += "<tr>"+newline;
				//s += "<td align=center id=live_product_name></td>"+newline;
				//s += "</tr>"+newline;
				s += "<tr>"+newline;
				s += "<td height=20 valign=bottom id=live_price></td>"+newline;
				s += "</tr>"+newline;
				s += "<tr>"+newline;
				s += "<td id=live_time></td>"+newline;
				s += "</tr>"+newline;
				s += "<tr>"+newline;
				s += "<td align=center id=live_buy_button></td>"+newline;
				s += "</tr>"+newline;
				s += "</table>"+newline;
				$('live').innerHTML = s;

				$('live_image1').innerHTML = tmpShow_Live_Image1;
				$('live_image2').innerHTML = tmpShow_Live_Image2;
				//$('live_product_name').innerHTML = tmpShow_Live_Product_Name;
				$('live_time').innerHTML = tmpShow_Live_Time;
				$('live_price').innerHTML = tmpShow_Live_price;
				$('live_buy_button').innerHTML = tmpShow_Live_buy_button;

			}
			/////// Refresh the Price in same game //////// 
			else
			{
				if (vType=="1")
				{
					$('live_time').innerHTML = tmpShow_Live_Time;
				}
				$('live_price').innerHTML = tmpShow_Live_price;
				$('live_buy_button').innerHTML = tmpShow_Live_buy_button;
			}
		}
	}
}


function showLoading_live() 
{
	var imgPath = "/style/loading4.gif";
	var newline = "\n\r";
	s = "<table border=0 cellspacing=0 cellpadding=0 width=168 height=250>"+newline;
	s += "<tr>"+newline;
	s += "	<td><center><img src="+imgPath+"></center></td>"+newline;
	s += "</tr>"+newline;
	s += "</table>"+newline;
	if ($('live')) {
		$('live').innerHTML = s;
	}
}

