//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;


function getHTML()
{

    //showActivity();
    
    var myAjax = new Ajax.Updater(
        dest,
        url, 
        {
            method: 'get',
            parameters: parms,
            asynchronous: true,
            onFailure: reportError,
            onComplete: showResponse
        });
}

function showResponse(request) 
{

//console.log('showResponse ' + request.responseText);
    
    positionDiv();
    
    var div = $(dest);
    
    div.innerHTML = request.responseText;
    //div.show();
   
    var eff = new Effect.Appear($(dest),{duration: .50});
}

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

    var div = $(dest);

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

    if((posY - windowScroll.top + pheight) > (pageSize.height - 10)) {
        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 = pwidth + 'px';
    div.style.height = pheight + 'px';
    div.style.top = posY + 'px';
    div.style.left = posX + 'px';

//console.log(div.style);

}

function reportError(request)
{
}

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();

    var div = $(dest);
    
    //div.innerHTML = '<div style="height: 32px; width: 32px;"><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;
        }
    }
});

function getPageSize(){
var xScroll, yScroll;

if (window.innerHeight && window.scrollMaxY) {	
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
}

var windowWidth, windowHeight;

if (self.innerHeight) {	// all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
}	
var pageHeight, pageWidth;

// for small pages with total height less then height of the viewport
if(yScroll < windowHeight){
    pageHeight = windowHeight;
} else { 
    pageHeight = yScroll;
}

// for small pages with total width less then width of the viewport
if(xScroll < windowWidth){	
    pageWidth = windowWidth;
} else {
    pageWidth = xScroll;
}

return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
}

function getWindowScroll() {
  // From script.aculo.us
var w = window;
  var T, L, W, H;
  with (w.document) {
    if (w.document.documentElement && documentElement.scrollTop) {
      T = documentElement.scrollTop;
      L = documentElement.scrollLeft;
    } else if (w.document.body) {
      T = body.scrollTop;
      L = body.scrollLeft;
    }
    if (w.innerWidth) {
      W = w.innerWidth;
      H = w.innerHeight;
    } else if (w.document.documentElement && documentElement.clientWidth) {
      W = documentElement.clientWidth;
      H = documentElement.clientHeight;
    } else {
      W = body.offsetWidth;
      H = body.offsetHeight
    }
  }
  return { top: T, left: L, width: W, height: H };

}



