﻿var sessionId = generateNewSessionId();
var submitted = false;

var chatEnabled = false;
var chatTimerID = false;
var detailsEnabled = true;
var leftBarEnabled = true;

var IsLive = 1;
var ItemID = 0;
var SwapID = 0;

function declineAll(swapId)
{
    var params = "swapId=" + swapId + "&action=declineall";
    
    new Ajax.Request('/ajax/swap/offer.aspx', {parameters:params, onComplete:function(){softRefreshOffers();}});	        
}

function initChat()
{
    if (!chatEnabled)
    {
        $('chatDiv').style.display = 'block';
        $('swapperDetails').style.display = 'none';
        $('liveChatTab').src = 'images/liveswap/tab_livechat.gif';
        $('swapDetailsTab').src = 'images/liveswap/tab_swapperdetails_off.gif';
        
        refreshMessages();               
        
        //var periodicalExecuter;
        
        //if (periodicalExecuter) periodicalExecuter.stop();
        
        //periodicalExecuter = new PeriodicalExecuter(refreshMessages, 10);
        
        chatEnabled = true;
        detailsEnabled = false;
    }
    else 
    {
        $('chatDiv').style.display = 'none';
        $('swapperDetails').style.display = 'block';
        $('liveChatTab').src = 'images/liveswap/tab_livechat_off.gif';
        $('swapDetailsTab').src = 'images/liveswap/tab_swapperdetails.gif';
        
        clearTimeout(chatTimerID);
        //if (periodicalExecuter) periodicalExecuter.stop();
        
        chatEnabled = false;
        detailsEnabled = true;
    }
}	  

function initDetails()
{
    if (!detailsEnabled)
    {
        chatEnabled = true;
        initChat();                
    }
} 

function showOffer(custId, swapId) 
{ 
    var params = "action=show&custID=" + custId + "&swapId=" + swapId;
    
    new Ajax.Updater('offerItems', '/ajax/swap/offer.aspx', { asynchronous:true, evalScripts:true, parameters: params});      
}

function clearOffer(swapId) 
{
    if ( confirm('Are you sure you want to retract your offer?') ) 
    {
        var params = "action=clear&swapId=" + swapId;
    
        new Ajax.Updater('offerItems', '/ajax/swap/offer.aspx', { asynchronous:true, evalScripts:true, parameters: params});
    }    
}

function clearOfferandJump(swapId)
{
    if ( confirm('Are you sure you want to retract your offer?') ) 
    {
        var params = "action=clearjump&swapId=" + swapId;
    
        new Ajax.Updater('offerItems', '/ajax/swap/offer.aspx', { asynchronous:true, evalScripts:true, parameters: params});
    }   
}

function acceptOffer(offerID)
{
    var params = "offerId=" + offerID + "&action=accept";
    
    new Ajax.Request('/ajax/swap/offer.aspx', {parameters:params, onComplete:function(){softRefreshOffers();}});	        
}

function addToOffer(itemId, swapId) 
{   
    var params = "action=add&itemId=" + itemId + "&sessionId=" + sessionId + "&swapId=" + swapId;
    
    new Ajax.Updater('offerItems', '/ajax/swap/offer.aspx', { asynchronous:true, evalScripts:true, parameters: params});
}

function declineOffer(offerID)
{
    var params = "offerId=" + offerID + "&action=decline";
    
    new Ajax.Request('/ajax/swap/offer.aspx', {parameters:params, onComplete:function(){softRefreshOffers();}});	        
}

function generateNewSessionId() 
{
    return Math.floor(Math.random() * 9999999);
}

function initOffers(swapId)
{
    SwapID = swapId;
    
    refreshOffers();
    
    //var offerExec = new PeriodicalExecuter(refreshOffers, 25);    
}

function newOffer(swapId) 
{
    var params = "action=newoffer&swapId=" + swapId;

    new Ajax.Updater('offerItems', '/ajax/swap/offer.aspx', { asynchronous:true, evalScripts:true, parameters: params});
}

function refreshOffers()
{
    var params = "swapId=" + SwapID;
    
    new Ajax.Updater('offersDiv', '/ajax/swap/swap-offers.aspx', { onComplete:function(){ setTimeout('refreshOffers()', 15000); }, asynchronous:true, evalScripts:true, parameters: params});            
}

function softRefreshOffers()
{
    if (IsLive == 0)
        showOfflineOffers(ItemID);
    else
    {
        var params = "swapId=" + SwapID;
    
        new Ajax.Updater('offersDiv', '/ajax/swap/swap-offers.aspx', { asynchronous:true, evalScripts:true, parameters: params});            
    }
}

function showGroups(iType, custID)
{
    var params = "iType=" + iType + "&CustID=" + custID;
    
    new Ajax.Updater('groupsContainer', '/ajax/swap/groups.aspx', { asynchronous:true, evalScripts:true, parameters: params});        
}

function showItems(iType, custID)
{
    var params = "iType=" + iType + "&CustID=" + custID;
    
    new Ajax.Updater('itemsContainer', '/ajax/swap/items.aspx', { asynchronous:true, evalScripts:true, parameters: params});        
}

function showDetailItems(iType, custID, itemID)
{
    var params = "iType=" + iType + "&CustID=" + custID + "&itemID=" + itemID;
    
    new Ajax.Updater('itemsContainer', '/ajax/swap/items.aspx', { asynchronous:true, evalScripts:true, parameters: params});        
}


function showNextOfferPage()
{
    if ($('offersDiv').scrollLeft + 250 <= $('offersDiv').scrollWidth)
        $('offersDiv').scrollLeft += 260;
}

function showOfflineOffers(itemID)
{
    ItemID = itemID;
    IsLive = 0;
    
    var params = "itemID=" + itemID + "&isLive=0";
    
    new Ajax.Updater('offersDiv', '/ajax/swap/swap-offers.aspx', { asynchronous:true, evalScripts:true, parameters: params});            
}

function showPrevOfferPage()
{
    if ($('offersDiv').scrollLeft - 260 < 0)
        $('offersDiv').scrollLeft = 0;
    else    
        $('offersDiv').scrollLeft = $('offersDiv').scrollLeft - 260;
}

function showQuestion(msgID)
{
    var params = "msgID=" + msgID + "&opt=txt";
    
    new Ajax.Updater('question' + msgID, '/ajax/mail/content.aspx', { asynchronous:true, evalScripts:true, parameters: params});        
}

function submitOffer(btn, swapId) 
{   
    if (btn.value == 'Submit Offer') 
    {
        var params = "action=send&sessionId=" + sessionId + "&swapId=" + swapId;
        
        new Ajax.Updater('offerItems', '/ajax/swap/offer.aspx', { asynchronous:true, evalScripts:true, parameters: params});
                
        // disable the drop region until we want to accept offers again
        Droppables.remove('offer');
                      
        //btn.value = 'Cancel';
        btn.style.visibiliy = 'hidden';
    } 
    else 
    {
        clearOffer(swapId);
        
        btn.value = 'Submit Offer';
        
        Droppables.add('offer', { hoverclass: 'fredhover',  onDrop:function(element) { addToOffer(element.id, swapId); }})                           
    }
        
}

function toggleLeftBar()
{        
    if (leftBarEnabled)
    {
        $('itemInfoBox').style.display = 'none';
        $('tabs').style.display = 'none';
        $('leftBarCell').style.width = '20px';
        $('showHidePalettesTab').style.left = '20px';
        $('leftTabImg').src = '/images/liveswap/tab_leftclose.png';
        
        leftBarEnabled = false;
        
        softRefreshOffers();
    }
    else 
    {
        $('itemInfoBox').style.display = 'block';
        $('tabs').style.display = 'block';
        $('leftBarCell').style.width = '27%';
        $('showHidePalettesTab').style.left = '27%';
        $('leftTabImg').src = '/images/liveswap/tab_leftopen.png';
        
        leftBarEnabled = true;
    }
}


