// JavaScript Document
var jqueryuifile = document.createElement('script');
jqueryuifile.setAttribute('type', 'text/javascript');
jqueryuifile.setAttribute('src', '/seckill/js/jquery-ui-1.8.6.custom.min.js');
document.getElementsByTagName("head").item(0).appendChild(jqueryuifile);
var jquerycss = document.createElement('link');
jquerycss.setAttribute('rel', 'stylesheet');
jquerycss.setAttribute('type', 'text/css');
jquerycss.setAttribute('href', '/seckill/css/vader/jquery-ui-1.8.6.custom.css');
document.getElementsByTagName("head").item(0).appendChild(jquerycss);

jQuery(function(){
	if (jQuery("#seckill"))
	jQuery.ajax({
		url:"/SecKill/service.do",
		type:"get",
		data:"getdata=true",
		cache:false,
		dataType:"json",
		success:function(data){
			if (!jQuery.isEmptyObject(data))
			{
				var btntext = '';
				switch(data.statue){
					case 0: break;
					default:
						switch (data.statue){
						case 1:
							btntext = 'Wait For 00:00:00';break;
						case 2:
							btntext = 'SELL OFF';break;
						case 3:
							btntext = 'Purchased By ' + data.info;break;
						}
						
						var timervalue0 = new Object();
						timervalue0.timer = timervalue0.timershow = null;
						timervalue0.value = data.time;
						timervalue0.id = data.id;
						
						jQuery("#seckill").append('<h1>SecKill</h1>');
						jQuery("#seckill").append('<a href="/SecKill/"><img border=0 src="'+ data.image +'" width=150 /></a>');
						jQuery("#seckill").append('<div class="productSalePrice">Just '+ curout + ' ' + formatNumber(mul(data.price,curid),2) +'</div>');
						jQuery("#seckill").append('<button id="skbtn'+ data.id +'">'+ btntext +'</button>');
						jQuery("#skbtn"+ data.id).button({icons: {primary: "ui-icon-cart"},disabled:true});
						
						if (data.statue == 1){
							
							showtime(timervalue0);
							countdown(timervalue0);
						}
				}
			}
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(errorThrown);
		}
	});
});

function showtime(timeobj)
{
	if (timeobj.value >= 0)
	{
		var hour = parseInt(timeobj.value / 3600);
		var minute = parseInt((timeobj.value % 3600) / 60);
		var second = parseInt(timeobj.value % 60);
		if (hour<10) hour = '0' + hour;
		if (minute<10) minute = '0' + minute;
		if (second<10) second = '0' + second;
		jQuery("#skbtn" + timeobj.id).button({label:'Wait For ' + hour + ':' + minute + ':' + second});
		timeobj.timershow = setTimeout(function(){showtime(timeobj)},500);
	}
}
function countdown(timeobj)
{
	timeobj.value -= 1;
	timeobj.timer = setTimeout(function(){countdown(timeobj)},1000);
	if (timeobj.value <= 0)
	{
		clearTimeout(timeobj.timershow);
		clearTimeout(timeobj.timer);
		jQuery("#skbtn" + timeobj.id).click(function(){seckill(timeobj.id);});
		jQuery("#skbtn" + timeobj.id).button({label:'BUY NOW',disabled:false});
	}
}
function seckill(id)
{
	jQuery.ajax({
		url:"/SecKill/service.do",
		type:"post",
		data:"proid="+id,
		cache:false,
		success:function(data){
			jQuery("#skbtn" + id).unbind( "click" );
			
			if (data!='error')
			{
				jQuery("#skbtn" + id).button({label:'LUCK!!!'});
				SKCar(id,data,null);
				jQuery( "#dialog-message" ).html('Goods have been placed in the shopping cart.').dialog({
					modal: true,
					title:'LUCK!!!',
					buttons: {
						Ok: function() {
							jQuery(this).dialog( "close" );
						}
					}
				});
			}
			else
			{
				jQuery("#skbtn" + id).button({label:'WITHOUT LUCK'});
				jQuery("#dialog-message").html('Wait for the next opportunity.').dialog({
					modal: true,
					title:'WITHOUT LUCK',
					buttons: {
						Ok: function() {
							jQuery(this).dialog( "close" );
						}
					}
				});
			}
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(errorThrown.text);
			jQuery("#skbtn" + id).unbind( "click" );
			jQuery("#skbtn" + id).button({label:'WITHOUT LUCK'});
			jQuery("#dialog-message").html('Wait for the next opportunity.').dialog({
				modal: true,
				title:'WITHOUT LUCK',
				buttons: {
					Ok: function() {
						jQuery(this).dialog( "close" );
					}
				}
			});
		}
	});
}
