	//	Object with global variables
	GlobalVariables = new Object();
	//script on server that will send data
	GlobalVariables.url = '/ajaxs/updateauctions';

	// text that be printed
	GlobalVariables.auctionstatus = [];
	GlobalVariables.auctionstatus[3] = 'item pending...';
	GlobalVariables.auctionstatus[4] = 'Closed';
	GlobalVariables.auctionstatus[5] = 'Sold Out';
	
	
	GlobalVariables.wrongdata = 'Wrong Data! Please Verify!';
	// flag inficate when all data is loaded
	GlobalVariables.process_initialized = false;
	// auctions id	which must be requested
	GlobalVariables.all_auctions_on_page = [];
	// array of all opened auctions
	GlobalVariables.opened_auctions_on_page = [];
	// array of closed auctions
	GlobalVariables.closed_auctions_on_page = [];
	// boolean array for highlit new price
	GlobalVariables.isPriceChanged = [];
	// boolean array for highlit last time
	GlobalVariables.isTimeCritical = [];
	// critical time for higlite
	GlobalVariables.CriticalTime = '00:10';
	// get all active auctions

	// array for every active auction
	GlobalVariables.all_data = [];
	// flag for data update
	GlobalVariables.streaming = false;
	// flag for updating offered bid
	GlobalVariables.offeredbidtyping = false;
	// timeout for data update
	GlobalVariables.timeout = 2000;
	// global counter of request
	GlobalVariables.counter = 1;
	// last requested query
	GlobalVariables.lastRequest = 1;
	
	// problem with Opera when array is void
	GlobalVariables.closed_auctions_on_page[0] = -1;


function SetStreaming(){
	$('#streamstatus').empty();
	GlobalVariables.streaming = !GlobalVariables.streaming;
	if(GlobalVariables.streaming){
		$('#streamstatus').append("ON");
		GlobalVariables.StreamingInterval = setInterval(UpdateData,GlobalVariables.timeout);
	}else{
		$('#streamstatus').append("OFF");
		clearInterval(GlobalVariables.StreamingInterval);
	}
}

function CleanHighlite(){

	$.each(GlobalVariables.opened_auctions_on_page,function(j,data_opened){
		if(GlobalVariables.isPriceChanged[parseInt(data_opened)]){
			$('#highlite_' + data_opened).removeClass('prod_info_clrd');
			$('#highlite_' + data_opened).addClass('prod_info');
			GlobalVariables.isPriceChanged[parseInt(data_opened)] = false;
		}
		// checking if time is critical
		if(GlobalVariables.isTimeCritical[parseInt(data_opened)]){
			$('#highlite_' + data_opened + ' #table_time').removeClass('prod_time_red');
			$('#highlite_' + data_opened + ' #table_time').addClass('prod_time_blue');
			GlobalVariables.isTimeCritical[parseInt(data_opened)] = false;
		}
	});


}

function Highlite(){
	$.each(GlobalVariables.opened_auctions_on_page,function(j,data_opened){
		if(GlobalVariables.isPriceChanged[parseInt(data_opened)]){
			//$('#highlite_' + data_opened).css('background-color','#00ff00'); // green color
			$('#highlite_' + data_opened).removeClass('prod_info');
			$('#highlite_' + data_opened).addClass('prod_info_clrd');
		// checking if time is critical
		}else{
			if(GlobalVariables.isTimeCritical[parseInt(data_opened)]){
				$('#highlite_' + data_opened + ' #table_time').removeClass('prod_time_blue');
				$('#highlite_' + data_opened + ' #table_time').addClass('prod_time_red');
			}
				//$('#highlite_' + data_opened + '> .status').css('background-color','#ff0000'); // red color
		}
	});
}

// recalculating auctions that was closed during last request
function InitClosedAuctions(){
	
	var opened_auctions = GlobalVariables.opened_auctions_on_page;
	$.each(opened_auctions,function(i,data_all){
		// if current all_auctions_on_page is present in opened_auctions then nothing to do
		data_all = parseInt(data_all);
		var auction_data = GetAuctionData(data_all);
			// auction_data[5] - id statusa
			if((parseInt(auction_data[5]) == 4) || (parseInt(auction_data[5]) == 5)){ // sold out or closed
				// close auctions
				GlobalVariables.closed_auctions_on_page.push(data_all);
				// exclude from list for highliting
				GlobalVariables.isPriceChanged[data_all] = null;
				GlobalVariables.isTimeCritical[data_all] = null;
				// remove from opened auctions
				GlobalVariables.opened_auctions_on_page = $.grep(GlobalVariables.opened_auctions_on_page,function(val,i){
					return (data_all != val)
				});
			}
	});
}



function GetData(id){
	// id is auction id from opened_auctions array
	// find array with id auction as parameter id

	var auction = GetAuctionData(id);
	if(auction){
		//var temp = auction[0].split("||");
		// add auctions to opened auctions
		GlobalVariables.opened_auctions_on_page.push(parseInt(auction[0]));
		return auction;
	}else
		return null;
}

function GetAuctionData(id){
	var auction = 	$.grep(GlobalVariables.all_data,function(auct_arr){
			// auct_arr - string with data for auction 12||..auct_data..
			var tmp = auct_arr.split("||");
			return ( id == parseInt(tmp[0]));
		});

	if(auction[0]){
		return auction[0].split("||");
	}else{
		return null;
	}
	
}


//core function
function UpdateData(){
	InitData();
	if(!GlobalVariables.process_initialized)
		return;
	// temp array for display previous query
	var temp_opened_auctions = GlobalVariables.opened_auctions_on_page;
	// empty opened arrays, new query contain other opened ouctions
	if(GlobalVariables.opened_auctions_on_page)
		GlobalVariables.opened_auctions_on_page = [];
	//var len = temp_opened_auctions.length;
	$.each(temp_opened_auctions,function(i,data){
		data = parseInt(data);
		// make active elements after user make bid
		DoActiveFormElements('auction_' + data,data);
		// find auction on the page with id = data
		var auction = $('#highlite_' + data);
		var auction_data = GetData(data);
		// check if data exist for this auction
		if(auction_data){
			// 1 - price, 2 - user name, 3 - time, 4 - offered bid
			var temp = '';

			if(parseInt(auction_data[5]) == 3){
				temp = ShowPendingDescription(auction_data[1],auction_data[2]);
				DoDisabledFormElements('auction_' + data,data);
			}else{
				temp = ShowDescription(auction_data[1],auction_data[2],auction_data[3]);
			}
			
			// check if data have been modified
			InitHighliteBid(auction_data);
			auction.empty().append(temp);
			InitHighliteTime(auction_data);
			if(!GlobalVariables.offeredbidtyping)
				$('#bid_' + data).val(auction_data[4]);
		}
	});
	// we have initialized opened auctions now we are initializing closed
	InitClosedAuctions();
	// change status for closed auctions
	CloseAuctions();
	Highlite();
	setTimeout(CleanHighlite,800);
}



function InitHighliteBid(auction_data){
	var oldprice = parseInt( $('#highlite_' + parseInt(auction_data[0]) + ' .prod_price').text() );
	if(oldprice != parseInt(auction_data[1])){
		GlobalVariables.isPriceChanged[parseInt(auction_data[0])] = true;

	} else {
		GlobalVariables.isPriceChanged[parseInt(auction_data[0])] = false;
	}
}

function InitHighliteTime(auction_data){
		// init auctions with critical time ( < 10 sec to closed state)
		var time = $('#highlite_' + parseInt(auction_data[0]) + ' #time').text();
		//var time = str.split(':');
		if(time <= GlobalVariables.CriticalTime){
			GlobalVariables.isTimeCritical[parseInt(auction_data[0])] = true;
		} else {
			GlobalVariables.isTimeCritical[parseInt(auction_data[0])] = false;
		}
}



function InitData(){
	//check if opened auctions exist
	if(GlobalVariables.opened_auctions_on_page.length == 0)
		return;
	// request ID
	GlobalVariables.counter++;
	var auction_id = GlobalVariables.opened_auctions_on_page.join(",");
	$.post(GlobalVariables.url,{ counter : GlobalVariables.counter, auctionsid: auction_id },function(str){
				var tempData = str.split("$$");
				// Variable GlobalVariables.lastRequest contain ID of request which was
				// changed data, if request with id 4 will came after request with id 5 then
				// then request with id 4 will be ignored.
				if( parseInt(tempData[0]) > GlobalVariables.lastRequest ){
					// set last requested ID
					GlobalVariables.lastRequest = parseInt(tempData[0]);
					str = tempData[1];
					GlobalVariables.all_data = str.split("%%");
					// data present
					GlobalVariables.process_initialized = true;
			}
	});
}

// Init all auctions on the page
function InitElements(){
	$('.clauction').each(function(){
			// get auction id
			var auctionid = $(this).attr("id").split("_");
			GlobalVariables.all_auctions_on_page.push(parseInt(auctionid[1]));
	});
	// first request of opened auctions is same as all auctions
	GlobalVariables.opened_auctions_on_page = GlobalVariables.all_auctions_on_page;
}

//when user make a bid doing disabled input and buttton elements of form
//change images
//function DoDisabledFormElements(formid,id){
//		var inputfield = $('#' + formid + ' #bid_' + id);
//		inputfield.attr('disabled','disabled');
//		inputfield.removeClass();
//		inputfield.addClass('prod_bid_input_ser');
//		var button = $('#' + formid + ' input:image');
//		button.attr('disabled','disabled');
//		button.attr('src','/img/bid_ser.gif');
//		var maindiv = $('#' + formid + ' .prod_bid');
//		maindiv.removeClass();
//		maindiv.addClass('prod_bid_ser');
//}

//activate form elements after new data arrived from server
function DoActiveFormElements(formid,id){
		var inputfield = $('#' + formid + ' #bid_' + id);
		inputfield.removeAttr('disabled');
		inputfield.removeClass();
		inputfield.addClass('prod_bid_input');
		var button = $('#' + formid + ' input:image');
		button.removeAttr('disabled');
		button.attr('src','/img/bid.gif');
		var maindiv = $('#' + formid + ' .prod_bid_ser');
		maindiv.removeClass();
		maindiv.addClass('prod_bid');

}

//check if bid is number
function CheckBid(bid){
	if(!isNaN(bid)){
		//var objRegExp = /^\d*$/;
		//alert( bid + ' : ' + objRegExp.test(bid));
		if(bid >= 1 )
			return true;
		else
			return false;
	}else
		return false;
}


function DoDisabledFormElements(formid,id){
	var inputfield = $('#' + formid + ' #bid_' + id);
	inputfield.attr('disabled','disabled');
	inputfield.removeClass();
	inputfield.addClass('prod_bid_input_ser');
	var button = $('#' + formid + ' input:image');
	button.attr('disabled','disabled');
	button.attr('src','/img/bid_ser.gif');
	var maindiv = $('#' + formid + ' .prod_bid');
	maindiv.removeClass();
	maindiv.addClass('prod_bid_ser');
}

