$(document).ready(function(){
	
	BuyItNow.Init();
	
});


var BuyItNow = function(){}
BuyItNow.BuyStatus = true;



/**********************************************************************
*						Buy it now 									  *
* 																	  *
**********************************************************************/

BuyItNow.Init = function(){
	
	BuyItNow.Events();
	if(!$(".logged-in-nav").hasClass('hideTag')){ BuyItNow.BuyStatus = false;}
	
}

BuyItNow.Events = function(){
	
	$(".buyitnow").click(BuyItNow.Buy);
	
}


BuyItNow.Buy = function(){
	
	//need to find a log in equivalent.
	if(!BuyItNow.BuyStatus){ main.toggleLoginPopup(); return false;}
	
	var itemCode = this.id;
	
	Data = {
		itemCode		: itemCode,
		actionDetails	: "buyitnow"
	};
	var AjaxVars = {
	        url        : Tools.Application.GetApplicationURL("BuyItNow") ,
	        data       : Data                                        , 
	        dataType   : 'json'                                      ,
	        error      : function(){console.log(obj); } ,
	        beforeSend : function(){}    ,
	        success    : BuyItNow.BuyItNowSuccess
	};

    Tools.Ajax.run(AjaxVars);    
    return false;
	
}

BuyItNow.BuyItNowSuccess = function(obj){
	if (obj.error == false && obj.saleId != null){
		window.location.href  = Tools.Application.GetApplicationURL('Purchase&AuctionId=' + obj.saleId);
	}else{
		if (obj.backorderId != null){
			Tools.Dialog.TextMessage(obj.errors.message);
			window.location.href  = Tools.Application.GetApplicationURL('UserAccountPage&tab=Backorders');
		}
		Tools.Dialog.ErrorMessage(obj.errors.message);
	}
}

/**********************************************************************
* 						End of buy it now
**********************************************************************/

