


// Classe x creare un singolo countdown
function cbCounter(divname, objname, serverdate, targetdate, mode, auctionended, days, hours, minutes, seconds) {
		if (!document.getElementById || !document.getElementById(divname)) return;
		if (serverdate == "" || targetdate == "") return;
		
		if (!document.getElementById(divname)) return;
		this.container  = document.getElementById(divname);
		
		this.serverdate = new Date(serverdate);
		this.targetdate = new Date(targetdate);
		this.startdate  = new Date();
		this.timesup = false;
		this.objname = objname;
		this.mode = mode;
		this.auctionended = auctionended;
		this.days = days;
		this.hours = hours;
		this.minutes = minutes;
		this.seconds = seconds;
		
		// Maximum seconds this counter will run before it is turned off.
		this.maxtime = (this.targetdate - this.serverdate) / 1000;
		
	
		
	
	this.updateTime = function() {
		//se manca la data d inizio o il tempo e' scaduto si fa nulla
		if (!this.startdate || this.timesup == true) {
			window.location.reload;
			return;
		}
		
		// How much time (in seconds) has elapsed since the start?
		var currentdate = new Date();
		this.timediff = (currentdate - this.startdate) / 1000;
		this.countdown = this.maxtime - this.timediff;
		
		
		// Check if time is up.
		if (this.timediff > this.maxtime) {
			this.timesup = true;
			this.container.innerHTML = this.formatresults();
			
		} else {
			this.displayCountdown();
			window.setTimeout(this.objname + ".updateTime();", 1000);
		}
	}
	
		
	
	
	this.displayCountdown = function() {
		// Divide seconds into days, hours, minutes and seconds.
		var oneMinute   = 60 //minute unit in seconds
		var oneHour     = oneMinute * 60 //hour unit in seconds
		var oneDay      = oneHour * 24 //day unit in seconds
		var dayfield    = Math.floor(this.countdown / oneDay)
		var hourfield   = Math.floor((this.countdown - dayfield * oneDay) / oneHour)
		var minutefield = Math.floor((this.countdown - dayfield * oneDay - hourfield * oneHour) / oneMinute)
		var secondfield = Math.floor((this.countdown - dayfield * oneDay - hourfield * oneHour - minutefield * oneMinute))
		
		// Display the counter.
		this.container.innerHTML = this.formatresults(dayfield, hourfield, minutefield, secondfield);
	}
	
	
	
	
	//formatta il contatore aggiungendo tag html e suffissi x ore minuti secondi
	this.formatresults = function() {
		if (this.timesup == false) { //if target date/time not yet met
			var displaystring = "<span>";
			if ((arguments[0] == 0) && (arguments[1] == 0)) {
				displaystring += "<font class='red_text'>"
			}
			if (arguments[0] > 0 || this.mode != "hideempty") {
				displaystring += arguments[0] + this.days + " "
			}
			if (((arguments[0] > 0) || (arguments[1] > 0)) || this.mode != "hideempty") {
				displaystring += arguments[1] + this.hours + " "
			}
			displaystring += arguments[2] + this.minutes + " " + arguments[3] + this.seconds
			if ((arguments[0] == 0) && (arguments[1] == 0)) {
				displaystring += "</font>"
			}
			displaystring += "</span>"
		}
		else { //else if target date/time met
			if ( this.auctionended == 'reload' ) {
				var displaystring = "<font class='red_text'>refreshing...</font>";
				document.location.reload()
			}
			else {
				var displaystring = "<font class='red_text'>" + this.auctionended + "</font>"
			}
		}
		return displaystring
	}
	
	
	
}



//#######################################################
//#######################################################
// ################### BOF Parte ajax x l'aggiornamento in tempo reale delle offerte
var myRequest = null;

function CreateXmlHttpReq(handler) {

	var req = 0;
	if (window.XMLHttpRequest && (!window.ActiveXObject)) {
		try {
			req = new XMLHttpRequest();
		}
		catch (e) { } 
	} else if (window.ActiveXObject) { 
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e) { } 
		} 
	}
	if (!req) alert("Cannot create XmlHttpRequest object"); 
	if (req) req.onreadystatechange = handler;
	return req;
}




function myHandler() {
	if (myRequest.readyState == 4) {
		if (myRequest.status == 200) {
			//mi viene restituito l'array articoli con 3 campi su ogni riga: id_asta--offerente--offerta
			eval( myRequest.responseText );
			var x = 0;
			while(x < articoli.length) {
				
				var offerente = articoli[x][0]+'offerente';
				var offerta = articoli[x][0]+'offerta';
				offerente = document.getElementById(offerente);
				offerta = document.getElementById(offerta);
				if((offerente.childNodes[0].nodeValue != articoli[x][1] || offerta.childNodes[0].nodeValue != articoli[x][2]) && (articoli[x][2] != 0 || articoli[x][2] != '0')){
					//in caso positivo li aggiorno
					offerente.childNodes[0].nodeValue = articoli[x][1];
					var euro = String.fromCharCode(8364);
					offerta.childNodes[0].innerHTML = articoli[x][2]+' '+euro;
					
				}
				++x;
			}			
		}
	}
}



function mus_update_bid() {
	var r = Math.random();
	var pagina = window.location.protocol+"//"+window.location.host+"/extras/homeupdt.php?auction1="+document.getElementById('auction1').className;
	
	var i = 2;
	while(null != document.getElementById('auction'+i)) {
		pagina += "&auction"+i+"="+document.getElementById('auction'+i).className;
		++i;
	}
	myRequest = CreateXmlHttpReq(myHandler);
	myRequest.open("GET",pagina+"&rand="+escape(r));
	myRequest.send(null);
	//ripeto la routine d aggiornamento ogni 5 secondi
	setTimeout("mus_update_bid()", 5000);
}
//################### EOF Parte ajax x l'aggiornamento in tempo reale delle offerte
//#######################################################
//#######################################################



function mus_orologio(server_time){

	src_div = document.getElementById('mustime');

	var ora = new Date();
	ora.setTime(server_time);
	var Hh, Mm, Ss, mm;
	Hh = ora.getHours();
	Hh = ( Hh < 10 ? "0" : "" ) + Hh;
	Mm = ora.getMinutes();
	Mm = ( Mm < 10 ? "0" : "" ) + Mm;
	Ss = ora.getSeconds();	
	Ss = ( Ss < 10 ? "0" : "" ) + Ss;
	
	src_div.innerHTML = Hh + ":" + Mm + ":" + Ss;
	
	server_time += 1000;//aggiungo un sec
	setTimeout("mus_orologio(" + server_time + ")", 1000);
}

function mus_switch_image(img, name){
	target = document.getElementById('mainpic');
	target = target.firstChild;
	target.src = img;
	
	link = document.getElementById('imglink');
	link.name = name;
}

function mus_popup() {
	img = document.getElementById('imglink');
	img = img.name;
	
	window.open('http://ampasta.com/images/' + img,'Ampasta[dot]com','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=700,height=525,top=150,left=150')
}
