//support scripts for sales history
var searchurl = '/prod_rollover/';
var itemcode;
var posX;
var posY;
var timeid;
var dest = 'hlj_popup2';
var parms;
var default_pheight = 210;
var default_pwidth = 350;
var pheight;
var pwidth;
var winW = 640;
var winH = 480;
var cache = new Array();

function getHTML()
{

    //check if we have this one already in the cache
    if(cache[url] === undefined) {
    
        var myAjax = new Ajax.Updater(
            dest,
            url, 
            {
                method: 'get',
                parameters: parms,
                asynchronous: true,
                onFailure: reportError,
                onComplete: function(request) { cache[url] = request.responseText; showResponse( cache[url] );}
            });
    }
    else { showResponse( cache[url] ); }
    
}

function showResponse(content) 
{

//console.log('showResponse ' + request.responseText);

//    cache[itemcode] = request.responseText;
    
    positionDiv( {height: pheight, width: pwidth} );
    
    var div = $(dest);
    
    div.innerHTML = content;
    div.show();
   
    //var eff = new Effect.Appear($(dest),{duration: .50});
}

function positionDiv(divsize) {
    var pageSize = document.viewport.getDimensions();    
    var windowScroll = document.viewport.getScrollOffsets();    

    var width = divsize.width;
    var height = divsize.height;

    if (width === undefined) width = pwidth;
    if (height === undefined) height = pheight;
    
//console.log(height + ', ' + width);
    
    var div = $(dest);

//console.log(posX + ', ' +  posY + ', ' + windowScroll.left + ', ' + pageSize.width + ', ' + pwidth);
    
    if((posX  - windowScroll.left + width) > (pageSize.width - 20) ) {
        posX = posX - pwidth - 5;
        }
    else
        {
        posX = posX + 5;
    }

    if((posY - windowScroll.top + height) > (pageSize.height - 20)) {
        posY = posY - pheight - 5;
        }
    else
        {
        posY = posY - 5;
    }
  
    if(posX < 0) {
        posX = 0;
        }
        
    if(posY < 0) {
        posY = 0;
        }

//console.log('final position: ' + posX + ', ' + posY);    

    div.style.zIndex = '10000';  
    div.style.width = width + 'px';
    div.style.height = height + 'px';
    div.style.top = posY + 'px';
    div.style.left = posX + 'px';

//console.log(div.style);

}

function reportError(request)
{
}

//adds clickability to divs and other things IE chokes on
function prod_click(e) 
{

    e.stop();
    
//    console.log('got one');
    var elt = $(Event.element(e));

    //are we already on the container?    
    if(! elt.hasClassName('rollover')) {
        elt = elt.up('.rollover'); 
    }
    
    var productInfo = $w(elt.className).grep(/^productId/)[0].split("\:")

    if(! productInfo[1]) {
        return false;
        }


    if ((event.metaKey) || (event.isRightClick()) ) {
        window.open('/product/' +  productInfo[1]);
        }
    else
        {
        parent.location = '/product/' + productInfo[1]; 
    }
        
//    parent.location = '/product/' + productInfo[1]; 
    return false; 
}    

function prod_rollover(e) 
{
//    console.log('got one');
    var elt = $(Event.element(e));

    //are we already on the container?    
    if(! elt.hasClassName('rollover')) {
        elt = elt.up('.rollover'); 
    }
    
    var productInfo = $w(elt.className).grep(/^productId/)[0].split("\:")
    //console.log( elt.className + ' - ' + ((productInfo) ? productInfo[1] : "Ain't nothing here") );

    if(! productInfo[1]) {
        return false;
        }
        
    itemcode = productInfo[1]; 

    if(! $(dest)) {

//console.log('created div');

        var rollDiv = new Element('div', {id: dest} );
        rollDiv.hide();
        rollDiv.setStyle( {position: 'absolute', backgroundColor: 'transparent'} );
        document.body.appendChild(rollDiv);
    }

        //window.console.log('search ' + icode);

        //default widt/height
        pheight = default_pheight;
        pwidth = default_pwidth;
        
        //current mouse coordinates
        posX = Event.pointerX(e);
        posY = Event.pointerY(e);
        Event.stop(e);

//console.log(posX + ' ' + posY);
        
        clearTimeout(timeid);    
    
        timeid = setTimeout('loadXMLdoc()', 750);
}

function loadXMLdoc() 
{
    url = searchurl + itemcode;
    parms = '';
    
    getHTML();
}

function showActivity()
{

    
    //positionDiv( {width: 32, height: 32} );

    var div = $(dest);
    
    div.innerHTML = '<div style="height: 32px; width: 32px; border: 1px solid #D3D3D3"><img src="/graphics/bigrotation2.gif" height="32" width="32"></div>';
    div.show();

    //var e = new Effect.Appear($(dest),{duration: 0.5});
}

function stockStatus(e,stockstatus,height,width)
{
//    return false;

//default widt/height
    pheight = height || default_pheight;
    pwidth = width || default_pwidth;
    
//current mouse coordinates
    posX = Event.pointerX(e);
    posY = Event.pointerY(e);
    Event.stop(e);
    
    clearTimeout(timeid);    
    
    url = 'http://www.hlj.com/info/' + stockstatus + '.html'
    
    timeid = setTimeout("getHTML()",1500);

}

function clearpop(e) 
{
    var TO;
    
    Event.stop(e);
    
    clearTimeout(timeid);                

    var div = $(dest);
  
    TO = setTimeout("$(dest).hide();",100);
}

Ajax.currentRequests = {};

Ajax.Responders.register({
  onCreate: function(request) {
    if (request.options.onlyLatestOfClass && Ajax.currentRequests[request.options.onlyLatestOfClass]) {
            // if a request of this class is already in progress, attempt to abort it before launching this new request
           try { Ajax.currentRequests[request.options.onlyLatestOfClass].transport.abort(); } catch(e) {}
        }
        // keep note of this request object so we can cancel it if superceded
        Ajax.currentRequests[request.options.onlyLatestOfClass] = request;
  },
    onComplete: function(request) {
    if (request.options.onlyLatestOfClass) {
            // remove the request from our cache once completed so it can be garbage collected
             Ajax.currentRequests[request.options.onlyLatestOfClass] = null;
        }
    }
});





