﻿Type.registerNamespace("Dabs.Ajax");
Type.registerNamespace("Dabs.Ajax.Panels");


Dabs.Ajax.Panels.HotDeals = function(containerId)
{
    if(!compatible)
        return;
    if(!$get(containerId))
        return;
        
    this._container = $get(containerId);
    this._containerWidth = 0;
    this._containerMinWidth = 200;
    this._hotDealBoxes = getElementsByClass("midcolfl",this._container,"div");
    this._hotDealBoxClearingDivs = getElementsByClass("midcolflclr",this._container,"div");
	this._hotDealBoxMinWidth = 270;
	this._hotDealBoxNewWidth = 0;
	this._hotDealBoxMarginWidth = 10;
	this._hotDealBoxCount = this._hotDealBoxes.length;
	this._hotDealBoxesPerRow = 0;
	    
    this.initialise();

}


Dabs.Ajax.Panels.HotDeals.prototype = {
    initialise: function(){    
        this.resize();
        AddEvent(window,'resize',this.resize.closure(this));
    },
    
    resize: function(){

        // a DOM bug in Firefox versions prior to 1.5 gives us an incorrect offsetWidth value - this sorts it out!
        if(is_fx && (parseInt(is_minor)<1.5))
            this._container.style.position = "absolute";
            
        this._containerWidth = parseInt(this._container.offsetWidth);	
        
        if(is_fx && (parseInt(is_minor)<1.5))
            this._container.style.position = "relative";
        
        // compensate for a rendering bug in Opera by making the container width smaller
        if(navigator.userAgent.toLowerCase().indexOf('opera')!=-1){this._containerWidth -= 20}

	    // a safety check to make sure we don't have a smaller centre panel than we expect
	    if (this._containerWidth < this._containerMinWidth){this._containerWidth = this._containerMinWidth;}
    		
	    // Work out how many promo boxes we can fit on each row
	    this._hotDealBoxesPerRow = parseInt(this._containerWidth / this._hotDealBoxMinWidth);	
	    if (this._hotDealBoxesPerRow > 4){this._hotDealBoxesPerRow = 4;}	
	    if (this._hotDealBoxesPerRow > this._hotDealBoxCount){this._hotDealBoxesPerRow = this._hotDealBoxCount;}
	    // if we calculate 3 boxes per row, but end up with one box orphaned, revert to 2 boxes per row - looks neater
	    if (((this._hotDealBoxCount%this._hotDealBoxesPerRow)==1) && this._hotDealBoxesPerRow==3){this._hotDealBoxesPerRow = 2}
	    // if we calculate 4 boxes per row, but end up with 2 boxes orphaned, revert to 3 boxes per row - looks neater
	    if (((this._hotDealBoxesPerRow%this._hotDealBoxesPerRow)==2) && this._hotDealBoxesPerRow==4){this._hotDealBoxesPerRow = 3}
    	
    	
	    // figure out how wide each promo box should be
	    this._hotDealBoxNewWidth = parseInt((this._containerWidth - ((this._hotDealBoxesPerRow-1)*this._hotDealBoxMarginWidth)) / this._hotDealBoxesPerRow);
    	
	    // hack for FF rendering issue when there are only 2 boxes available and they both appear on one line
	    if((this._hotDealBoxCount == 2) && (this._hotDealBoxesPerRow == 2) && (is_fx)){this._hotDealBoxNewWidth -= 8}
    	
	    // iterate through the promo boxes applying width and margin to each
	    for (var i=0;i<=this._hotDealBoxes.length-1;i++){
    		
		    this._hotDealBoxClearingDivs[i].className = "midcolflclr"
    		
		    // the end box of each row should be handled differently (ie; 0 margin)
		    if (((i+1) / this._hotDealBoxesPerRow).toString().indexOf(".") == -1)
		    {
                this._hotDealBoxes[i].style.marginRight = 0;
                this._hotDealBoxClearingDivs[i].className += " clr midcolflclrmaxwidth";
		    }
		    else
		    {
		        this._hotDealBoxes[i].style.marginRight = this._hotDealBoxMarginWidth + "px";
		    }
		        this._hotDealBoxes[i].style.width = this._hotDealBoxNewWidth + "px";
    		    
	    }	
    },
    
    dispose: function(){
    
    }
}

Dabs.Ajax.Panels.HotDeals.registerClass('Dabs.Ajax.Panels.HotDeals',null,Sys.IDisposable)
