var pricing = {
    list       : [],
    displayedEl: null,
    init: function() {
        if ($('pricingOverlay')|| $('page_product_start') ) {
            if ($('page_product_start')) {//create overlay for this page...
                var div = document.createElement('div');
                div.id  = 'pricingOverlay';
                div.style.display='none';
                $('page_product_start').appendChild(div);
            }
            //set overlay to appropriate height
            var info = this.getPageSize();
            var vinfo = document.viewport.getHeight();
            var height = vinfo > info.height ? vinfo : info[1];
            $('pricingOverlay').style.height = height + 'px';
            $('pricingOverlay').style.width = 100 + '%';
            document.body.appendChild($('pricingOverlay'));
            Event.observe($('pricingOverlay'), 'click', this.hideCurrent.bindAsEventListener(this), false);
            var snodes = $$('td.showPricing');
            var hnodes = $$('td.hidePricing');
            var length = snodes.length;
            if (length > 0) {
                for (var i = 0; i < length; i++) {
                    var std  = snodes[i];
                    var htd  = hnodes[i];
                    var name = std.getAttribute('axis');
                    this.list.push(name);
                    Event.observe(std, 'click', this.showAllPrices, false);
                    if (htd) {
                        Event.observe(htd, 'click', this.hideCurrent, false);
                    }
                    if ($('page_product_start')) {
                        $('page_product_start').appendChild($(name));
                    } else {
                        $('pricingPageContainer').appendChild($(name));
                    }
                }
            }
            Event.observe(document,'keydown',this.detectKeys,false);
        }
    },
    detectKeys: function(event) {
          if (pricing.displayedEl) {
              var curr = pricing.list.indexOf(pricing.displayedEl.id);
              var next = curr+1;
              var prev = curr-1;
              switch (event.keyCode) {
                  case 39://right
                      pricing.displayedEl.style.display='none';
                      if (curr != pricing.list.length-1) {
                          pricing.showAllPrices(pricing.list[next]);
                      } else {
                          pricing.showAllPrices(pricing.list.first());
                      }
                  break;
                  case 37://left
                      pricing.displayedEl.style.display='none';
                      if (curr != 0 ) {
                          pricing.showAllPrices(pricing.list[prev]);
                      } else {
                          pricing.showAllPrices(pricing.list.last());
                      }
                  break;
              }
          }
    },
    showAllPrices: function(event) {
        if (event.clientX) {
            var node = $(this.getAttribute('axis'));
        } else {
            var node = $(event);
        }
        var offsets = document.viewport.getScrollOffsets();
        var center  = Math.round(((document.viewport.getHeight() /2 ) - (node.getHeight()/2)));
        var newY     = offsets[1];
        if (center > 0) {
            newY += center;
        }
        node.style.top = newY+'px';
        node.style.left = ((document.viewport.getWidth() /2 ) - (node.getWidth()/2))+'px';
        node.style.display='block';
        pricing.showOverLay();
        pricing.displayedEl = node;
    },
    hideCurrent: function() {
        pricing.hideOverLay();
        pricing.displayedEl.style.display='none';
        pricing.displayedEl = null;
    },
    showOverLay: function() {
        $('pricingOverlay').style.display='block';
    },
    hideOverLay: function() {
        $('pricingOverlay').style.display='none';
    },
    //
    // getPageSize()
    // Returns array with page width, height and window width, height
    // Core code from - quirksmode.org
    // Edit for Firefox by pHaez
    //
    getPageSize: function(){
        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;
        }

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


        arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
        return arrayPageSize;
    }
};
Event.observe(window,'load',pricing.init.bindAsEventListener(pricing),false);