// JavaScript Document

$(document).ready(function(){
	
	
	$("#loginemail").val('e-mail address');
	$("#loginparool").val('password');
	
	$("#loginemail").focus(function() {
			if($(this).val() == 'e-mail address') {
				$(this).val('');
			}
		});
	$("#loginemail").blur(function() {
			if($(this).val() == '') {
				$(this).val('e-mail address');
			}
		});
	$("#loginparool").focus(function() {
			if($(this).val() == 'password') {
				$(this).val('');
			}
		});
	
	refresh_page()
	
});



function refresh_page()
{
	var time_out = 1000;
	var products = $("#listofproducts").text();
	var isbigprod = $("#isbigproduct").text();
	var getpar ='';
	
	if (isbigprod == '1')
		getpar = "action=info&products="+products+"&big=1"
	else
		getpar = "action=info&products="+products;
	
	$.ajax({
			type: "GET",
		  	url: "ajaxfunctions.php",
			cache: false,
		  	processData: false,
		  	data: getpar,
		  	success: function(xml)
			{
				
				$("#servertime").text($("a", xml).text());
				time_out = 1000 - parseInt($("m", xml).text());
				if (time_out<100) time_out = 1000;
				
				
				$("p", xml).each(function(id)
				{
					var product = $("k", this).text();
					//	0  - toote oksjon pole alanud
					// -1  - aeg ei mahu alguse ja lopu vahele
					// -2  - toode puudub
					// -3  - myydud
					// -4  - peatatud
					//	24 - aktiivne ning 24h
					//	1  - aktiivne tavaline
					var seis = $("s", this).text();
					var aeg = $("t", this).text();
					var aeg_min = sec_to_min(aeg);
					
					$("#prod_"+product+"_price").text($("h", this).text()+'€');	//hind
					$("#start").val($("h", this).text()+'€');
					$("#end").val(parseInt($("h", this).text()) + 1.00 +'€');
					$("#max").val(25);
					
					//aeg
					var aeg_txt = '';
					$("#prod_"+product+"_time").removeClass();
					if (seis < 1 && seis!=-3) {
						$("#prod_"+product+"_time").text(aeg_min);
						$("#prod_"+product+"_time").addClass("timel_value");
					}
					else if (seis==-3) {
						$("#prod_"+product+"_time").text('0:00');
						$("#prod_"+product+"_time").addClass("timel_value");
					}
					else if (aeg<-5) {
						$("#prod_"+product+"_time").text("0:00");
						$("#prod_"+product+"_time").addClass("timel_value");
					}
					else if (aeg<1) {
						$("#prod_"+product+"_time").text("Control bids");
						$("#prod_"+product+"_time").addClass("timel_value_red");
					}
					else if (aeg<30) {
						$("#prod_"+product+"_time").text(aeg_min);
						$("#prod_"+product+"_time").addClass("timel_value_red");
					}
					else if (aeg<60) {
						$("#prod_"+product+"_time").text(aeg_min);
						$("#prod_"+product+"_time").addClass("timel_value2");
					}
					else {
						$("#prod_"+product+"_time").text(aeg_min);
						$("#prod_"+product+"_time").addClass("timel_value");
					}

					
					var int_sek = $("i", this).text();
					$("#prod_"+product+"_int").text(sec_to_min(int_sek));	//intervall
									
					
					$("#prod_"+product+"_best").text($("b", this).text());	//parim
					
					var seis_txt = '';
						 if (seis == 0)   seis_txt = '<span class="red">Active '+ $("l", this).text() +'</span>'
					else if (seis == 1)   seis_txt = 'Auction is open: '+ $("l", this).text()
					else if (seis == 24)  seis_txt = 'Auction is open <img src="images/bid_24.jpg" alt="24h" width="26" height="26" border="0" />'
					else if (seis == -1)  seis_txt = '<span class="red">Auction is open: '+ $("l", this).text() +'</span>'
					else if (seis == -2)  seis_txt = ''
					else if (seis == -3)  seis_txt = ''
					else if (seis == -4)  seis_txt = '<span class="red">Auction is stopped!</span>';
					
					$("#prod_"+product+"_info").html(seis_txt);	//info
					
					
					var nupp_txt = '';
					var nupp_class = '';
					if (seis == 0 || seis == -1 || seis == -2 || seis == -4) {
						nupp_txt = 'Not yet activated!';
						nupp_class = 'inactive';
					} else if (seis == -3) {
						nupp_txt = 'SOLD OUT';
						nupp_class = 'sold';
					} else if ((seis == 1 || seis == 24) && aeg<-5) {
						nupp_txt = 'SOLD OUT';
						nupp_class = 'sold';
					} else {
						nupp_txt = 'Place your bid';
						nupp_class = 'online';
					}
					
					$("#prod_"+product+"_button").removeClass();
					$("#prod_"+product+"_button").addClass(nupp_class);
					$("#prod_"+product+"_button").text(nupp_txt);
					
					var last_bids = '-';
					if ($("v", this).text()!='' && $("v", this).text()!='-') {
						last_bids = '';
						$("w", this).each(function(id)
						{
							last_bids = last_bids + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + $("d", this).text() + "&nbsp;&nbsp;&nbsp;" + $("o", this).text() +"<br />";
						});
					}
					$("#last_bids").html(last_bids);
					

				});
	
			}
		   });

	setTimeout('refresh_page()', time_out);
}


function sec_to_min(tsek)
{
	var tmin = '0:00';
	if (tsek>0) {
		tmin = Math.floor(tsek/60);
		var tmin_sek = (tsek-tmin*60);
		tmin_sek = tmin_sek.toString();
		if (tmin_sek.length == 1) {tmin_sek = "0"+tmin_sek;}
		tmin = tmin + ':' + tmin_sek;
	}
	return tmin;
}

var alertwin;
function send_bid(toode)
{
	$.ajax({
			type: "GET",
		  	url: "ajaxfunctions.php",
			cache: false,
		  	processData: false,
		  	data: "action=bid&bid="+toode,
		  	success: function(xml)
			{
				var respond = $("r", xml).text();
				var krediit = $("c", xml).text();
				var teade = '';
				//  1 - ok
				//  0 - myydud
				// -1 - pole aktiivne
				// -2 - krediit otsas
				// -3 - tehniline viga
				// -4 - pole sisseloginud
				if (respond==1) {
					teade = 'Your bid was successful!';
				} else if (respond==0) {
					teade = 'SOLD OUT';
				} else if (respond==-1) {
					teade = 'Not yet activated!';
				} else if (respond==-2) {
					teade = 'Not enough credit, please add more credit!';
				} else if (respond==-3) {
					teade = 'Sorry, Please try again later!';
				} else {
					teade = 'Only for registered members, please log in or send SMS!';
				}
				
				clearTimeout(alertwin);
				$("#alert").html(teade);
				if($("#alert").is(":hidden")) $("#alert").fadeIn("fast");
				alertwin = setTimeout('$("#alert").fadeOut("fast");', 5000);
				
				$("#credit").text(krediit);
				
			}
	});

}

