var productAddToCartForm;

var debug_store;

Event.observe(window, 'load', function() 
{
   hookAvailability(); 
});

function hookAvailability()
{
    $$('.request-order a img').invoke('observe','mouseover',function(e){ 
        tooltip = e.srcElement.next();
        tooltip.show();
    });
    
    $$('.request-order a img').invoke('observe','mouseout',function(e){
        tooltip = e.srcElement.next();
        tooltip.hide();
    });
}

function getPricingInfo(item){
        //bring in Prototye
        Element.extend(item);
        
        //store this row for data gathering
        var row = item.up().siblings();
        
        //get the itemno
        //  -------- row -> select the td holding item#  (end enum) select strong (end enum)  data
        var itemno = row.grep(new Selector("td.itemno")).first().childElements().first().innerHTML;
        //get the site
        var site   = row.grep(new Selector("td.site")).first().childElements().first().innerHTML;
        //get the id (for passing to data)
        var nameid = row.grep(new Selector("td.itemno")).first().childElements().first().next().innerHTML;
        //get the size (for cart adding)
        var size   = row.grep(new Selector("td.size")).first().childElements().first().innerHTML;
        //get the form (for cart adding)
        var form   = row.grep(new Selector("td.size")).first().childElements().first().next().innerHTML;
        
        showBox(nameid,itemno,site,size,form,item.up());
}

function getHotpicsPricingInfo(item){
        //bring in Prototye
        Element.extend(item);
        
        //store this row for data gathering
        //HOTPICS has to go up TWO block levels.
        var row = item.up(2).siblings();
        
        //get the itemno
        //  -------- row -> select the td holding item#  (end enum) select strong (end enum)  data
        var itemno = row.grep(new Selector("span.itemno")).first().innerHTML;
        //get the site
        var site   = row.grep(new Selector("span.site")).first().innerHTML;
        //get the id (for passing to data)
        var nameid = row.grep(new Selector("span.nameid")).first().innerHTML;
        //get the size (for cart adding)
        var size   = row.grep(new Selector("span.size")).first().innerHTML;
        //get the form (for cart adding)
        var form   = row.grep(new Selector("span.form")).first().innerHTML;

        showBox(nameid,itemno,site,size,form,item.up());
}

function showBox(nameid,itemno,site,size,form,context)
{
        //get the height
        var viewHeight = window.innerHeight;
        
        //set the overlay height
        var overlayHeight = Math.min(475, viewHeight-35);
            
        //now load the page (parameters will get price info)
        //page URL: availability/product/view?itemno=[x]&site=[y]
        Modalbox.show('/availability/product/view',
                        {
                            method: 'POST',
                            params: 
                                'nameid='+nameid+
                                '&itemno='+itemno+
                                '&site='+site+
                                '&size='+size+
                                '&form='+form,
                            width: 600, 
                            height: overlayHeight,
                            loadingString: "Loading",
                            overlayOpacity: 0.8,
                            overlayClose: true,
                            slideDownDuration: 0,
                            slideUpDuration: 0,
                            closeValue: "CLOSE",
                            autoFocusing: false
                        }
        );
        
        $('MB_window').addClassName('overlay').addClassName('cr-x');
        
        productAddToCartForm = new VarienForm('product_addtocart_form');

        productAddToCartForm.submit = function(){
                //This will add to the cart - but redirect.
                //    product_addtocart_form.submit();
                
                //So lets write a version that calls it via ajax,
                //and then updates the page.
                
                //create an overlay for the cart load...
                var overlay = new Element('div');
                overlay.setAttribute('id','addtocart_overlay');
                
                var loadtxt = new Element('div');
                
                var loadimg = new Element('div');
                loadimg.setAttribute('id','addtocart_loader');
                
                //set the content
                loadtxt.innerHTML = 'Adding Product to your Request...';
                loadimg.innerHTML = '&nbsp;'
                
                overlay.appendChild(loadtxt);
                overlay.appendChild(loadimg);
                
                //get the qty
                
                $('MB_window').appendChild(overlay);
                //the ajax request
                $('product_addtocart_form').request(
                    {
                        method: "POST",
                        parameters: $('product_addtocart_form').serialize(),
                        onSuccess: function(r)
                        {
                            var qty = $('product_addtocart_form').getInputs('text','qty').first().getValue()
                            //hide the overlay
                            overlay.remove();
                            //hide the box
                            Modalbox.hide({afterHide: function() { updateAvailabilityPage(context,qty) }});
                            return false;
                        },
                        onFailure: function(r)
                        {
                            alert('The product was not added to the request due to an error.  Please try again later.');
                            
                            //hide the overlay
                            overlay.remove();
                            //hide the box
                            Modalbox.hide();
                            return false;
                        }
                    }
                );
        }.bind(productAddToCartForm);
        
        overrideSubmit();
}

function overrideSubmit()
{
    if($('product_addtocart_form') == undefined)
    {
        setTimeout(overrideSubmit,250);
        return;
    }
    $('product_addtocart_form').observe('submit',function(event) { Event.stop(event); });
}

function loadStaticPage(page)
{
    Modalbox.show(page,
                        {
                            method: 'POST',
                            width: 600, 
                            height: 475,
                            loadingString: "Loading",
                            overlayOpacity: 0.8,
                            overlayClose: true,
                            slideDownDuration: 0,
                            slideUpDuration: 0,
                            closeValue: "CLOSE",
                            autoFocusing: false,
                            title: ' '
                        }
        );
}

function updateAvailabilityPage(context,qty)
{
    new Ajax.Request(
        '/availability/product/cartCount', 
        {
            method: 'post',
            onSuccess: function(r) 
            {
                $$('.header .links .my-cart a span').first().update(r.responseText);
            }
        }
    );
    
    //update the count in the bag
    var bag = context.childElements().last();
    
    //reach the span:
    // CHILD of the last CHILD div.
    var qspan = bag.childElements().last().childElements().first();
    
    //convert to int
    var quantity = qspan.innerHTML * 1;
    qty = qty*1;
    
    //add more
    quantity = quantity + qty;
    
    //update the span
    qspan.update(quantity);
    
    //make the bag icon appear
    bag.show();
    
}

function loadPricingView(context)
{
    $('tab-description').hide();
    $('tab-pricing').show();
    
    $$('.active').first().removeClassName('active');
    context.addClassName('active');
}

function loadDescriptionView(context)
{
    $('tab-pricing').hide();
    $('tab-description').show();
    
    $$('.active').first().removeClassName('active');
    context.addClassName('active');
}

/*
//THIS Code is for the dynamics forms - not complete.

new Form.Element.Observer('configurable_location',.5,updateForm());
	
var formJson = array();

function updateForm()
{   
    var location = $('configurable_location');
    
    var request = new Ajax.Request(
        '/dynamics/getjsonupdate?loc='+location.getValue(),
        {
            onSuccess: function(response)
            {
                updateJson(response);
            }
        }
    );
}

function updateJson(newJsonData)
{
    formJson = newJsonData;
}
*/
