$(document).ready(function(){
	//if (!$.browser.msie || $.browser.version != '6.0')
	//{
		
	refresh_items();
	setTimeout('ping()', 600000);
	
	//alert ('asda');


	
	
	if ( $('#auctionpage').length ) setTimeout('checkauctions()', 15*60*1000);
});

function ping()
{
	setTimeout('ping()', 600000);
	$.get("functions.php", { action: "ping" } );
}

	
	
function refresh_items()
{
		get_items();
	//alert(timing);
	//var quick_time = new Date();
	//var quick_time = quick_time.getTime();
	//counter = counter+1;
	//if (counter == 1)
	//{
		
	//	setTimeout('refresh_items('+counter+')', 1000);
	//}
	//else
	//{
	//	if (counter == 10) counter = 0;
		setTimeout('refresh_items()', 1000);
	//}
	//alert('test');

}
function get_items()
{
	var quick_time = new Date();
		var quick_time = quick_time.getTime();
	var items = $("#id_list").text();
	var all_active =$("#active_id_list").text();
	var get_options = '';
	if (items == '')
	{
		//if ($.browser.msie && $.browser.version == '6.0') return;
		var get_item = "timer.php";
	}
	else
	{
		if ($("#choosexml").text() == "big")
		{
			var get_item = "tooted_big.php";
			get_options = 'special='+items+"&all="+all_active+"&ts="+quick_time;
		}
		else 
		{
			var get_item = "tooted.php";
			var get_options = "ids="+items+"&ts="+quick_time;
		}
	}
	//if ($.browser.msie)
	//{
		
		
		//$("#timming").text(get_options);
		//alert(get_options);
	//}
	//	alert(get_options);
	
	var id = $("#id").text();
	var self_best = "Conduci la moment";
	var self_winner = "Felicitari, ati cīstigat!";
	
	
	/*
a	response
b	auction_item
c	item
d	status
e	price
f	time
g	current
h	liider
i	osal
j	winner
k	limited
l	measurement
m	current_time
n	current_time_small
	*/
	//var countertest = counter % 2;
	//if (countertest == 0)
	//{
		$.ajax({
			async: true,
			type: "GET",
		  url: get_item,
		  processData: false,
		  data: get_options,
		  success: function(xml)
			{
				
				//$("#response").html (quick_time+"<br>"+$("o", xml).text()+"<br>"+$("n", xml).text());
				var timing = parseFloat($("n", xml).text());
				
				timing = 1000 - (1000 * timing);
				$("#timming").text(timing);
				
				setTimeout('create_ctime_master()', timing);
				$("b", xml).each(function(id)
				{
					
				
					
					itemid = $("c", this).text();
					if ($("f", this).text() != '')
					{
						$("#item"+itemid+"time_num").text($("f", this).text());
					}
					
					if ($("d", this).text() != '')
					{
						if ($("d", this).text() != '')$("#item"+itemid+"status").text($("d", this).text());
						if ($("d", this).text() == 'r') $("#item"+itemid+"activate").fadeOut('fast');
						
					}
					if ($("e", this).text() != '')
					{
						$("#item"+itemid+"price").text($("e", this).text());
						if (parseFloat($("e", this).text()) >= 1000) $("#pricetext"+itemid).text('');
					}
					
					if ($("bl", this).text() != '')
					{
						ar = $("bl", this).text().split('~');
						//alert (ar);
						var bd='';
						for(i=0;i<ar.length-1;i++) {
						  	var bd =bd+'<p>'+ar[i]+'</p>';
						  }
						  $("#list_biders").html(bd);
					}
					
						if ($("k", this).text() != '')
					{
						$("#item"+itemid+"limited").text($("k", this).text());
						$("#item"+itemid+"limited").fadeIn('fast');
					}
					else {  $("#item"+itemid+"limited").fadeOut('fast'); }
					
					if ($("w", this).text() != '')
					{
						$("#item"+itemid+"time_num").html('');
						$("#item"+itemid+"time_left_num").html('Vandut');
						$("#item"+itemid+"lider_id").html($("w", this).text());
						$("#item"+itemid+"bid").html("");
						 $("#item"+itemid+"activate").fadeOut('fast');
						//$("#item"+itemid+"osale").attr("src", "img/osale2.png");
						//$("#item"+itemid+" .toode_link").attr("href", "?5,"+itemid);
					}
					$("#item"+itemid+"lider_id").html($("g", this).text());
					
				});
				//$("#response").text ($("m", xml).text());
				
				if ($("m", xml).text() != '') setTimeout('create_clock("'+$("m", xml).text()+'")', 0);
				if ($("o", xml).text() != '') setTimeout('$("#id_list").data("timestamp", "'+$("o", xml).text()+'")', timing);
			}
		});

}

function bid(item_id)
{
	
	
	$.get("bid.php", {id_item: item_id, bid_time: $("#id_list").data("timestamp")}, function(data)
	{
		
		if ($("price", data).text() != '')
		{
			$("#item"+item_id+"price").text($("price", data).text());
			if (parseFloat($("e", this).text()) >= 1000) $("#pricetext"+itemid).text('');
		}
		if ($("lider", data).text() != '') {  $("#item"+item_id+"lider_id").text($("lider", data).text()) };
		
		/*if ($("f", data).text() != '')
		{
			$("#item"+item_id+"time_num").text($("f", data).text())
		}
		*/
		
		if ($("text", data).text() != '')
		{		
			$("#alerts p").html($("text", data).text());
			if($("#alerts").is(":hidden")) $("#alerts").fadeIn("fast");
		}
		alerts_window = setTimeout('$("#alerts").fadeOut("fast");', 1000);
		
		if ($("credit", data).text() != '')
		{
			$('#credit').text($('credit', data).text());
		}
	});

}



function create_ctime_master()
{
	$(".auction_item").each(function(id)
	{
		
		
		var itemid = $(this).attr("id").substr(4);
		//$("#response").append('item='+itemid+'\n');
		var time = $("#item"+itemid+"time_num").text();
		//$("#response").text("#item"+itemid+"time_num");
		if ($("#item"+itemid+"time_num").text() !='')
		{
			var time_str = ctime(time);
			$("#item"+itemid+"time_left_num").html(time_str);
		}
		
	});
}



function create_clock(time)
{
	$("#server_time").text(time);
}

function ctime(time)
{
	var color1 = 't1';
	var color2 = 't2';
	var color3 = 't3';
	var color4 = 't4';
	var time1 = 60;
	var time2 = 30;
	var time3 = 10;
	
	var result = '';
	var color = '';
	if (time < time1) {
		if (time < time2) {
			if (time < time3) {
				color = color4;
				if (time <= 0) {
					result = '<span class="'+color+'">Verific</span>';
					return result;
				}
			}
			else color = color3;
		}
		else color = color2;
	}
	else color = color1;
	

	var minut = Math.floor ((time/60) % 60);
	//var ora = Math.floor((time/3600) % 24);
	var secunda= (time % 60);
	
	if (minut < 10) minut = '0'+minut;
	if (secunda < 10) secunda = '0'+secunda;
	
	result = result+'<span class="'+color+'">';
	result = result+minut+':'+secunda;
	result = result+'</span>';
	return result;
}



function checkauctions()
{
	$.get("functions.php", {action: 'auctions', list: $("#id_list").text() }, function(data)
	{
		if (data == '1')
		{
			clearTimeout(alerts_window);
			$("#alerts p").html('Window will refresh in 10 seconds');
			if($("#alerts").is(":hidden")) $("#alerts").show("fast");
			setTimeout('window.location.reload()', 10*1000);
		}
	});
	
	setTimeout('checkauctions()', 15*60*1000);
}