var now=new Date();
var theclock=new Date();
var base_clock_freq=15000;
var partial_load=0;

function Clock(){
	var now_ref=new Date();
	var t_now=Math.floor(now.getTime()/1000);
	var t_now_ref=Math.floor(now_ref.getTime()/1000);

	if(t_now_ref!=t_now){
	 	now=now_ref;
	 	var aika=$('#js_clock').text();
		var time = new Date(aika);
		var time_diff=t_now_ref-t_now;
		time.setSeconds(time.getSeconds()+time_diff);
		theclock=time;

		var txt="";
		var month=time.getMonth();
		if(month==0){ txt="Jan "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==1){ txt="Feb "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==2){ txt="Mar "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==3){ txt="Apr "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==4){ txt="May "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==5){ txt="Jun "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==6){ txt="Jul "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==7){ txt="Aug "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==8){ txt="Sep "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==9){ txt="Oct "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==10){ txt="Nov "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		if(month==11){ txt="Dec "+time.getDate()+" "+time.getFullYear()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); }
		$('#js_clock').html(txt);

	}
	var ID=0;
	if(counttime.length>0){
		for(x in counttime){

			ID=counttime[x];
			if(ID>0){  }else{ continue; }
			UpdateTimer(ID);
			if(typeof prices[ID] === 'undefined' || !prices[ID]) {
		 		//INITIAL LOAD!
		 		if(kohteet[ID] === 'undefined' || !kohteet[ID]) { continue; }
		 		prices[ID]=kohteet[ID].auction.current_price;
		 		LoadVals(ID,true);
		 	}
		 	else{
		 		if(prices[ID]!=kohteet[ID].auction.current_price){
		 			//LOAD VALS
		 			LoadVals(ID,false);
		 			prices[ID]=kohteet[ID].auction.current_price;
		 		}
		 	}
		}
	 }
}

function BidderName(ID){
	if(kohteet[ID].bidders[0] === 'undefined' || !kohteet[ID].bidders[0]) { return ".."; }
	if(kohteet[ID].bidders[0].country === 'undefined' || !kohteet[ID].bidders[0].country) { return ".."; }
	if(kohteet[ID].bidders[0].country==country){
		return kohteet[ID].bidders[0].local_name;
	}
	else{
		return kohteet[ID].bidders[0].external_name;
	}
}

function FormatBidderName(ID,bidder){
	if(kohteet[ID].bidders[bidder] === 'undefined' || !kohteet[ID].bidders[bidder]) { return ".."; }
	if(kohteet[ID].bidders[bidder].country === 'undefined' || !kohteet[ID].bidders[bidder].country) { return ".."; }
	if(kohteet[ID].bidders[bidder].country==country){
		return kohteet[ID].bidders[bidder].local_name;
	}
	else{
		return kohteet[ID].bidders[bidder].external_name;
	}
}

function AuctionPrice(ID){
	if(kohteet[ID] === 'undefined' || !kohteet[ID]) { return "0.00"; }
	if(kohteet[ID].auction.current_price === 'undefined' || !kohteet[ID].auction.current_price) { return FormatAuctionPrice(0.00); }
	var price=(kohteet[ID].auction.current_price*currency_divider)/100;
	var hinta=roundNumber(price,2);
	var return_hinta=price;
	if(hinta<1){
		//return_hinta='0'+hinta;
	}
	return FormatAuctionPrice(return_hinta);
}

function GetAuctionPrice(ID,bidder){
	if(kohteet[ID].bidders[bidder] === 'undefined' || !kohteet[ID].bidders[bidder]) { return ".."; }
	if(kohteet[ID].bidders[bidder].price === 'undefined' || !kohteet[ID].bidders[bidder].price) { return FormatAuctionPrice(0.00); }
	var price=(kohteet[ID].bidders[bidder].price*currency_divider)/100;
	var hinta=roundNumber(price,2);
	var return_hinta=price;
	if(hinta<1){
		//return_hinta='0'+hinta;
	}
	return FormatAuctionPrice(return_hinta);
}

function FormatAuctionPrice(price){
		if(currency_right==1){
			return currency_symbol+price;
		}
		else{
			return price+currency_symbol;
		}
}

function LoadVals(ID,skip_flash){
	if(typeof current_tab === 'undefined' || !current_tab) {
	 		var timer='time';
	 		var box='box';
	 		var bidder='bidder';
	 		var price='price';
	 		var prod_card=true;
	 }
	 else{
	 		var prod_card=false;
		 if(current_tab!='front'){
		 	if(current_tab!='x'){
		 		var timer='time';
		 		var box='box';
		 		var bidder='bidder';
		 		var price='price';

		 	}
		 	else{
		 		var timer='Xtime';
		 		var box='Xbox';
		 		var bidder='Xbidder';
		 		var price='Xprice';
		 	}
		 }
		 else{
		 		var timer='Ftime';
		 		var box='Fbox';
		 		var bidder='Fbidder';
		 		var price='Fprice';
		 }
	 }

	 //DEFINED CHECKS


	 if(!skip_flash && $('#'+box+ID).length>0 && !prod_card){ $('#'+box+ID).fadeOut('fast'); }
	 $('#'+bidder+ID).text(BidderName(ID));
	 if($('#H'+bidder+ID).length>0){
	 	$('#H'+bidder+ID).text(BidderName(ID));
	 }
	 $('#'+price+ID).html(AuctionPrice(ID));
	 if($('#H'+price+ID).length>0){
	 	$('#H'+price+ID).html(AuctionPrice(ID));
	 }
	 if(typeof current_tab === 'undefined' || !current_tab){
	 	for(var i=0;i<=24;i++){
	 		if(kohteet[ID].bidders[i] === 'undefined' || !kohteet[ID].bidders[i]) { return ".."; }
	 		if(kohteet[ID].bidders[i].added === 'undefined' || !kohteet[ID].bidders[i].added) { return ".."; }
	 		var ident=i+1;
	 		var t_added=new Date();
	 		t_added.setTime((kohteet[ID].bidders[i].added)*1000);
	 		var t_min=t_added.getMinutes(); if(t_min<10){ var minutes='0'+t_min; }else{ var minutes=t_min; }
	 		var t_hour=t_added.getHours(); if(t_hour<10){ var hours='0'+t_hour; }else{ var hours=t_hour; }
	 		var clock_to_add=hours+':'+minutes;
	 		$('#bid'+ident+'_time'+ID).html(clock_to_add);
	 		$('#bid'+ident+'_name'+ID).html(FormatBidderName(ID,i));
			$('#bid'+ident+'_price'+ID).html(GetAuctionPrice(ID,i));
	 	}
	 }
	 else{
		 if(current_tab=='front'){
		  if(kohteet[ID].auction.status=="released"){
		  	$('#'+bidder+ID).text(GetReleasedName(ID));
		  	 if($('#H'+bidder+ID).length>0){
				 	$('#H'+bidder+ID).text(GetReleasedName(ID));
				 }
		  }
		  else{
		 		$('#'+bidder+ID).text(BidderName(ID));
		 		if($('#H'+bidder+ID).length>0){
				 	$('#H'+bidder+ID).text(BidderName(ID));
				 }
		 	}
		  $('#'+price+ID).html(AuctionPrice(ID));
		  if($('#H'+price+ID).length>0){
			 	$('#H'+price+ID).html(AuctionPrice(ID));
			 }
		 }
		 else{
		 	$('#'+bidder+ID).text(BidderName(ID));
		  $('#'+price+ID).html(AuctionPrice(ID));
		  if($('#H'+price+ID).length>0){
			 	$('#H'+price+ID).html(AuctionPrice(ID));
			 }
		  
		  if($('#H'+bidder+ID).length>0){
				 	$('#H'+bidder+ID).text(BidderName(ID));
				 }
		 }
	 }
	 if(!skip_flash && $('#'+box+ID).length>0 && !prod_card){
	 	if(box=='Xbox'){
	 		$('#'+box+ID+':visible').fadeIn('fast');
	 	}
	 	else{
	 		$('#'+box+ID).fadeIn('fast');
	 	}
	 }
	 
	 //DBL CHECK BIDBATTLE =)
	 /*var bb_arr_check=new Array();
	 var bb_can_init=false;
	 for(var i=0;i<=9;i++){
	 		if(kohteet[ID].bidders[i] === 'undefined' || !kohteet[ID].bidders[i]) { continue; }
	 		if(kohteet[ID].bidders[i].added === 'undefined' || !kohteet[ID].bidders[i].added) { continue; }
	 		if(kohteet[ID].bidders.length>=10){ bb_can_init=true; }
	 		bb_arr_check[i]=kohteet[ID].bidders[i];
	 }
	 var bb_unique=unique(bb_arr_check);
	 if(bb_can_init && bb_unique.length==2 && !battle_on[ID]){
	 	  //SET BB ON
	 	  InitBidbattle(ID);
	 	  battle_on[ID]=true;
	 }
	 if(bb_can_init && bb_unique.length>2 && battle_on[ID]){
	 	  //SET BB OFF
	 	  InitBidbattle(ID);
	 	  battle_on[ID]=false;
	 }*/
	 
	 decideBB(ID,kohteet[ID].bidders,skip_flash);
}

function GetReleasedName(ID){
	if(kohteet[ID].bidders[0].country === 'undefined' || !kohteet[ID].bidders[0].country) { return ".."; }
	if(kohteet[ID].bidders[0].country==country){
		return kohteet[ID].bidders[0].local_name;
	}
	else{
		return kohteet[ID].bidders[0].external_name;
	}
}

function roundNumber(number,decimal_points) {
	if(!decimal_points) return Math.round(number);
	if(number == 0) {
		var decimals = "";
		for(var i=0;i<decimal_points;i++) decimals += "0";
		return "0."+decimals;
	}

	var exponent = Math.pow(10,decimal_points);
	var num = Math.round((number * exponent)).toString();
	return num.slice(0,-1*decimal_points) + "." + num.slice(-1*decimal_points)
}


function UpdateTimer(ID){
	 //battle_on[ID]
	 if(battle_on[ID] === 'undefined' || !battle_on[ID]) { battle_on[ID]=false; }
	 if(kohteet[ID] === 'undefined' || !kohteet[ID]) { return ".."; }
	 if(kohteet[ID].auction.ends === 'undefined' || !kohteet[ID].auction.ends) { return ".."; }
	 var clock=Math.floor(theclock.getTime()/1000);
	 //var left=kohteet[ID].auction.ends - clock +clock_correction;
	 var left=kohteet[ID].auction.ends - clock +(tzo_offset_server-tzo_offset_client);
	 var timer='time';



	 var tmp=left;
	 var days=Math.floor(tmp/(24*60*60)); tmp=tmp-(24*60*60)*days;
	 var hours=Math.floor(tmp/(60*60)); tmp=tmp-(60*60)*hours;
	 var minutes=Math.floor(tmp/(60)); tmp=tmp-(60)*minutes;
	 var secs=Math.floor(tmp);
	 var text='';

	 if(!product_page){
		 if(days>0){ text+=days+' '+CLOCK_LANG[0]+' '; }
		 if(hours>0){ text+=hours+' '+CLOCK_LANG[1]+' '; }
		 if(minutes>0){ text+=minutes+' '+CLOCK_LANG[2]+' '; }
		 if(secs>0){ text+=secs+' '+CLOCK_LANG[3]+''; }
	 }
	 else{
	 	 if(days>0){ text+='<strong>'+days+'</strong>'+' '+CLOCK_LANG[8]+' '; }
		 if(hours>0){ text+='<strong>'+hours+'</strong>'+' '+CLOCK_LANG[9]+' '; }
		 if(minutes>0){ text+='<strong>'+minutes+'</strong>'+' '+CLOCK_LANG[10]+' '; }
		 if(secs>0){ text+='<strong>'+secs+'</strong>'+' '+CLOCK_LANG[11]+''; }
	 }

	 
	 $('#'+timer+ID).removeClass('green');
	 $('#'+timer+ID).removeClass('orange');
	 $('#'+timer+ID).removeClass('red');
	 $('#'+timer+ID).removeClass('white');
	   if(!battle_on[ID]){
		 if(left>=60){
		 	$('#'+timer+ID).addClass('green');
		 }
		 if(left<60 && left>=10){
		 	$('#'+timer+ID).addClass('orange');
		 }
		 if(left<10){
		  $('#'+timer+ID).addClass('red');
		 }
		}
		else{
			if(typeof current_tab === 'undefined' || !current_tab){
				$('#'+timer+ID).addClass('red');
			}
			else{
				if(!$('#box'+ID).hasClass('bidbattle')){
					$('#'+timer+ID).addClass('white');
				}
			}
		}
	  
	  
	  if(!$('#box'+ID).hasClass('bidbattle') && $('#'+timer+ID).hasClass('white')){
		  $('#'+timer+ID).removeClass('white');
		  $('#'+timer+ID).addClass('green');
	  }
	  
	  if($('#box'+ID).hasClass('bidbattle') && !$('#'+timer+ID).hasClass('white')){
		  	 $('#'+timer+ID).removeClass('green');
			 $('#'+timer+ID).removeClass('orange');
			 $('#'+timer+ID).removeClass('red');
			 $('#'+timer+ID).removeClass('white');
			 $('#'+timer+ID).addClass('white');
	  }

	 if(left<0){
	 	text=CLOCK_LANG[4];
	 }

	 if(kohteet[ID].auction.status === 'undefined' || !kohteet[ID].auction.status) { return ".."; }
	 if(kohteet[ID].auction.status!="running"){
	   if(kohteet[ID].auction.status=="closed"){ text=CLOCK_LANG[5]; }
	   if(kohteet[ID].auction.status=="released"){ text=CLOCK_LANG[6]; }
	 }
	 
	 if(kohteet[ID].auction.starts === 'undefined'){ }
	 else{
	 	if(parseInt(kohteet[ID].auction.starts)==0 && $('#bidder'+ID).length==0){
	 		if(uid>0){
	 			reload_product_tabs();
	 		}
	 		else{
	 			text=CLOCK_LANG[12];
	 		}
	 	} 
	 }


	 if(kohteet[ID].auction.status!="halt"){
	 	if(left==1){
		 	//do_counter('counter');
		 	reset_interval();
	 	}

	 	if(left<=-1 && left>-2 && kohteet[ID].auction.status=="running"){
			CheckNew(ID);
	 	}
	 		$('#'+timer+ID).html(text);
	 		if($('#H'+timer+ID).length>0){ $('#H'+timer+ID).html(text); }
	 }
	 else{
	 		$('#'+timer+ID).removeClass('green');
		 	$('#'+timer+ID).removeClass('orange');
		 	$('#'+timer+ID).removeClass('red');
	 		$('#'+timer+ID).addClass('green');
	 		if(!product_page){
				 $('#'+timer+ID).html(CLOCK_LANG[7]+$('#continue'+ID).text());
				 if($('#H'+timer+ID).length>0){ $('#H'+timer+ID).html(CLOCK_LANG[7]+$('#continue'+ID).text()); }
			 }
			else{
			 	 $('#'+timer+ID).html(CLOCK_LANG[7]+'<strong>'+$('#continue'+ID).text()+'</strong>');
			 	 if($('#H'+timer+ID).length>0){ $('#H'+timer+ID).html(CLOCK_LANG[7]+'<strong>'+$('#continue'+ID).text()+'</strong>'); }
			}
	 }
}


function UpdateClock(){
	$.post("support/clock.php", { ID:'1' } , function(data){
  		$('#js_clock').text(data);
	});
}

function CheckNew(ID){
	var MTIME='mtime'+ID;
	var TIMERID='aika'+ID;
	var MTM=0;
	$.post("check_update.php", { ID:ID, MT:MTM, UID:uid, PART:md5_part, CNTRY:country } , function(data){
  		$('#update_field').html(data);
	});
	do_counter('counter');
}

function UpdateAuctions(){
}

function CometCheck(){

	var backup=update;
	var now = new Date();
	var t_t="1"+now.getMonth()+"2"+now.getDate()+"3"+now.getFullYear()+"4"+now.getHours()+"5"+now.getMinutes()+"6"+now.getSeconds();
	
		$.get("check_for_update.php", { IDt:update, LNG:'F2', IE:1, rand:t_t, UID:uid, PART:md5_part, CNTRY:country, pload:partial_load } , function(data){
  		$('#update_field').html(data);
  		//window.setTimeout('clocks_go=true',500);
		});
	
	partial_load++;
	if(partial_load==4){ partial_load=0; }
	do_counter('counter');
}

function do_counter(ID){
	if($('#'+ID).length>0){ }else{ return 0; }
	var cval=$('#'+ID).text(); cval++; $('#'+ID).text(cval);
}

function reset_interval(){
	clearInterval(Interval);
	CometCheck();
	Interval=window.setInterval('CometCheck()', base_clock_freq);
}

function decideBB(KID,bids,no_notifications){
	tmp=bids.slice(0,10);
	unq=unique(tmp);
	//console.log(unq);
	if(tmp.length>8){
		if(unq.length>2){
			StopBB(KID,unq,no_notifications);
		}
		else{
			StartBB(KID,unq,no_notifications);
		}
	}
}

function unique(a)
{
   var new_array=new Array(); 
   var i=0;
   var j=0;
   var found=false;
   for(i=0;i<a.length;i++){ 
	   found=false;
	   for(j=0;j<new_array.length;j++){
		   if(new_array[j].local_name==a[i].local_name){ found=true; }
	   }
	   if(!found){
		   new_array[new_array.length]=a[i];
	   }
   }
   return new_array;
}