/* $Id: jquery.ManheimProxyBids.js,v 1.12 2011-07-12 11:50:12 rshaw Exp $
 * Name:    Manheim proxy bids
 * Author:  M Ibbotson
 * Email:   Mark.Ibbotson@manheimeurope.com
 * Purpose: Provide ability to submit/remove a proxy bid
 * Requires: members/ajax/*, modules/MI/AjaxAPI/*
 * This is just a wrapper to access the javascript found above.
 * It simply passes through data and is of no use save for checking a check box.
 * 
 */
jQuery.manheimProxBids = {

    query_params: {
		ok: '',
		resultingBid: '',
		isWatched: '',
		isNotebook: '',
		error: ''
	},

    /*
     * Kick off 
     */ 
    initialize: function() {  },
 
    /*
     * Add proxy bid for this vehicle
     */     
    addProxyBid: function(sale_id, item_id){
    	var bid_value = jQuery('#proxy_bid-' + sale_id + "_" + item_id).val();
        var watched   = "#is_watch-" + sale_id + '_' + item_id;
		var result    = "#result-" + sale_id + '_' + item_id;
	    var _self     = this;
		
    	jQuery.ajax({ 
			type: "POST",
			url: "/members/ajax/api/PlaceProxyBid",
			data: { saleId: sale_id, itemId: item_id, bidAmount: bid_value }, 
			dataType: "application/xml",
			success: function(xml) {
                  
                if(jQuery(xml).find('ok').text() && 
                   jQuery(xml).find('error').text() == '' && 
                   jQuery(xml).find('isWatched').text() ) 
                {  
                    jQuery(watched).attr('checked','checked');
                    
				} else { 
					jQuery(result).html(jQuery(xml).find('error').text());
				}
			}	 
    	});    	
    },

    /*
     * Remove proxy bid for this vehicle
     */    
    removeProxyBid: function(sale_id, item_id){
    	
    	jQuery('#proxy_bid-' + sale_id + "_" + item_id).val('');
        var watched   = "#is_watch-" + sale_id + '_' + item_id;
        var removed   = "#remove-" + sale_id + '_' + item_id;
       
       // console.log(removed);
       
    	jQuery.ajax({
			type: "POST",
			url: "/members/ajax/api/RemoveProxyBid",
			data: { saleId: sale_id, itemId: item_id, bidAmount: 0 }, 
			dataType: "application/xml",
			success: function(xml) {

                jQuery(watched).removeAttr('checked');
				jQuery("#proxy-" +  sale_id + "_" + item_id).html('');
				jQuery(removed).fadeOut();
		
			}	 
    	});
    	
    },
    
    toggleWatch: function(sale_id, item_id) {
    
        var watched = "is_watch-" + sale_id + '_' + item_id;
        var url     = "/members/ajax/api/RemoveWatch";
 
        jQuery("input[type='checkbox']:checked").each(function(){
		   if (this.id == watched){ 
		       url = "/members/ajax/api/AddWatch"; 
		   }
        });          

        jQuery.ajax({
			type: "POST",
			url: url,
			data: { saleId: sale_id, itemId: item_id, bidAmount: 0}, 
			dataType: "application/xml",
			success: function(xml) {

			}	 
    	});
    },
    
    addRemoveWatch: function(key, sale_id, item_id) {
		
		var test = "";
		
		if(jQuery("#proxy-" +  sale_id + "_" + item_id).html()) {
		  test = jQuery("#proxy-" +  sale_id + "_" + item_id).html().trim();
		}
		
		if(test.length > 0) { 
		   alert("Remove proxy bid first");
		   return;	
		}
		
    	var watched      = "is_watch-" + sale_id + '_' + item_id;
    	var button       = "#watch-" + sale_id + '_' + item_id;
    	var watch_button = "#watchlist-button_" + key; 
    	var action       = "add_watch"; 
    	var toggle       = "#myWatchList-" + sale_id + '_' + item_id;
		var details      = "#details-" + sale_id + '_' + item_id;
		var link         = '<a href="/profile/myWatchlist" class="watchlist-button" title="Currently Watching"></a>'; 
		
    	if(!sale_id && !item_id) { 
			button  = "#watch-" + key;
			toggle  = "#myWatchList-" + key;
			details = "#details-" + key; 
		}
    	
    	switch(jQuery(button).html()){
    	    case 'Remove from Watchlist':
              action = 'remove_watch';
            break;	
    	}
    	
    	//console.log(watch_button);
    			
		jQuery.ajax({
			type: "POST",
			url: "/profile/myWatchlist/watch.mpl",
			data: { rm: action , unit_key: key }, 
			dataType: "text/html",
			success: function(data) {
                switch(action) {
                	
					case 'add_watch':
                	   jQuery(button).html("Remove from Watchlist");
                	   jQuery("#" + watched).attr('checked', true);
                	   jQuery(watch_button).html(link);
                	break;
					
                	default:
                	   jQuery(button).html("Add to Watchlist");
                	   jQuery("#" + watched).attr('checked', false);
                	   jQuery(watch_button).html('&nbsp;');
					   jQuery(toggle).fadeOut('slow', function() { jQuery(details).fadeOut('slow'); });
					break;					
                } 
			}	 
    	});
    },

    addRemoveWatchLite: function(key, sale_id, item_id) {
		
		var test = "";
		
		if(jQuery("#proxy_bid-" +  sale_id + "_" + item_id).val()) {
		  test = jQuery("#proxy_bid-" +  sale_id + "_" + item_id).val().trim();
        }
        else
        {
		  test = jQuery("#proxy_bid-" +  key).val().trim();
		}
		
		if(test.length > 0) { 
		   alert("Remove proxy bid first");
		   return;	
		}
		
    	var button       = "#watch-" + sale_id + '_' + item_id;
    	var action       = "add_watch"; 
		
    	if(!sale_id && !item_id) { 
			button  = "#watch-" + key;
		}
    	
    	switch(jQuery(button).attr('value')){
    	    case 'Remove from Watchlist':
              action = 'remove_watch';
            break;	
    	}
    	
		jQuery(button).attr('disabled','disabled');
		jQuery.ajax({
			type: "POST",
			url: "/profile/myWatchlist/watch.mpl",
			data: { rm: action , unit_key: key }, 
			dataType: "text/html",
			timeout:10000,
			success: function(data) {
				//console.log("ajax success");
				jQuery(button).attr('disabled','');
                switch(action) {
                	
					case 'add_watch':
						jQuery(button).fadeOut('fast',
							function()
							{
								jQuery(button).attr('value',"Remove from Watchlist");
								jQuery(button).fadeIn('fast');
							}
						);

                	break;
					
                	default:
						jQuery(button).fadeOut('fast',
							function()
							{
							   jQuery(button).attr('value',"Add to Watchlist");
								jQuery(button).fadeIn('fast');
							}
						);
					break;					
                } 
			},
			error: function(data,statusTxt,error)
			{
				//console.log("ajax failed ["+statusTxt+"]["+error+"]");
				jQuery(button).attr('disabled','');
			}	
    	});
		//console.log("ajax sent");
	},
	
    removeWatchLite: function(key) {
		
		var test = "";
		
    	var button       = "#watch-" + key;
    	var li           = "#watch-li-" + key;
    	var action       = "remove_watch"; 
		
    	
		jQuery(button).disable();
		jQuery.ajax({
			type: "POST",
			url: "/profile/myWatchlist/watch.mpl",
			data: { rm: action , unit_key: key }, 
			dataType: "text/html",
			success: function(data)
			{
				jQuery(button).fadeOut('fast',
					function()
					{
						jQuery(button).remove();
						jQuery(li).fadeOut('fast',
							function()
							{
								jQuery(li).remove();
							}
						);
					}

				);
			} 
		});
	}
}

