jQuery(function($) {


$('[name=offer]').attr('value','0.00');
$('[name=offer_by]').attr('value','0.00');
$('[name=offer_at]').attr('value','0.00');

$('[name=offer]').click(function(){$(this).val(''); });
$('[name=offer_by]').click(function(){$(this).val(''); });
$('[name=offer_at]').click(function(){$(this).val(''); });


$.get('jobs.php');

refresh_live_price();
});

function refresh(location){

if(location){
window.setTimeout("document.location='"+location+"'",2000);
}
}

function login_send(){

var fields = $("#login").serializeArray();

$.post('modules/login.php', fields,function(data){$('#result_login').html(data); $('#result_login').fadeOut();$('#result_login').fadeIn();$('#result_login').addClass('result_login');});


	
}



function discover_price(id){

$.get('modules/bid_special.php?act=info&id='+id,function(data){

var check_data= $.evalJSON(data).result;

if(check_data == 'ko'){
	
popup($.evalJSON(data).feedback);

refresh($.evalJSON(data).redirect);

return null;
}
	
if(confirm($.evalJSON(data).feedback)){	

$.get('modules/bid_special.php?act=discover&id='+id,function(data){
	
popup($.evalJSON(data).feedback);

$('#best_offer_'+id).html($.evalJSON(data).offer);
$('#best_offerent_'+id).html($.evalJSON(data).user);

refresh($.evalJSON(data).redirect);
});

}


});

$('.info_box').load('index.php .info_box p');


}




function send_offer(id){


var offer_val = $('#offer_'+id+' input[name=offer]').val();


$.get('modules/bid.php?act=info&id='+id+'&offer='+offer_val,function(data){

var check_data= $.evalJSON(data).result;

if(check_data == 'ko'){
	
alert($.evalJSON(data).feedback);

refresh($.evalJSON(data).redirect);

return null;
}
	
if(confirm($.evalJSON(data).feedback)){	

$.get('modules/bid.php?act=offer&id='+id+'&offer='+offer_val,function(data){
	
alert($.evalJSON(data).result);

refresh($.evalJSON(data).redirect);
});

}



});

$('.info_box').load('index.php .info_box p');


}

function lost_password(username){

if(username == undefined){
	
var username = prompt('Username');
	
	
}

$.get('modules/lost_password.php?username='+username,function(data){ $('#result_lost').html(data); $('#result_lost').fadeOut();$('#result_lost').fadeIn();$('#result_lost').addClass('result_login'); });
	
}


function send_offers(id){


var offer_val_by = $('#offer_'+id+' input[name=offer_by]').val();
var offer_val_at = $('#offer_'+id+' input[name=offer_at]').val();

$.get('modules/bid.php?act=info&id='+id+'&offer_by='+offer_val_by+'&offer_at='+offer_val_at,function(data){

var check_data= $.evalJSON(data).result;

if(check_data == 'ko'){
	
alert($.evalJSON(data).feedback);
	
return null;
}

if(confirm($.evalJSON(data).feedback)){
	
$.get('modules/bid.php?act=offer&id='+id+'&offer_by='+offer_val_by+'&offer_at='+offer_val_at,function(data){

alert($.evalJSON(data).result);

refresh('index.php?p=user_clicks');

});

}

});

$('.info_box').load('index.php .info_box p');
}


//Refresh prices

function refresh_live_price(){

$.get('modules/refresh_live_price.php',function(data){


var bids = $.evalJSON(data).bids;
var price = $.evalJSON(data).price;
var expired_time = $.evalJSON(data).expired_time;
var best_offerent = $.evalJSON(data).best_offerent;


for(var id in bids){

var best_offer = 	$('#best_offer_'+bids[id]).html();

/* Lampeggiamento */
if(price[id] != best_offer){
$('#best_offer_'+bids[id]).fadeOut('1.5');
$('#best_offer_'+bids[id]).fadeIn('1');
}


$('#best_offer_'+bids[id]).html(price[id]);
$('#best_offerent_'+bids[id]).html(best_offerent[id]);

$('#timer_'+bids[id]).countdown('change', 'until', expired_time[id]);

}



});

setTimeout('refresh_live_price()',5500);


}

function popup(data){

if(data){

alert(data);
}

}
