/**
 * minimums (width and height) for ie6
 *
 * @version		1.0
 *
 * @license		Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
 * @author		Curtis C. Chung <keycon [at] curtischung.com>
 * @copyright	Author
 */
 
/**
 * notice
 * 
 * your code sould look like this in the head section of a html file
 *
 * <!--[if IE 6]>
 * <script type-="text/javascript" src="[your directory]/ie6minimums.js"></script>
 * <script type="text/javascript">
 * <!--
 * var list = new itemsToWatch; //1. an array to keep all dom object to be checked
 *
 * var item1 = new minimumObject; //2. register a object to be checked;
 * item1.domId = "domtocheck"; //3. id of the dom object
 * item1.minHeight = 400; //4. required if there should be a min-height (the value)
 * item1.heightClass = 'minh'; //5. required if there should be a min-height (the class to be added)
 * item1.minWidth = 800; //4. required if there should be a min-width (the value)
 * item1.widthClass = 'minw'; //5. required if there should be a min-width (the class to be added)
 * item1.relId = "wrapper"; //6. id to compare which has the same relative measures, usually parent element
 *
 * item1.addTo(list); //7. add a item to the list (if needed repeat 2 to 6)
 *
 * list.watch(); //8. start to watch and activate the minimums
 *
 * //-->
 * </script>
 * <![endif]-->
 *
 * heightClass or/and widthClass class style should be in css for ie6
 * put the script section and js link inside an ie6 conditional comment
 *
 */

itemsToWatch.prototype = Array.prototype;

function itemsToWatch() {  
    this.set = verifyAndSet;
    this.watch = watch;
    this.start = startListener;
    this.count = 0;
    var items = this;
    var objectList = new Array;
    var domList = new Array;
    
    function watch() {
        items.set();
        items.start();
    }
    
    function verifyAndSet() {
        for (var i=0; i<items.count; i++) {
            var objectDom = document.getElementById(items[i].domId);
            
            if(objectDom != null) {
                domList[i] = objectDom;
                objectList[i] = items[i];
            }
        }
    }
    
    function startListener() {
        var t = setTimeout(startListener,100);
        items.set();
        minimumCheck(objectList, domList);
    }
}


function minimumObject() {
    this.hasminWidth = false;
    this.hasminHeight = false;
    this.setOk = false;
    this.set = verifyObject;
    this.addTo = toItemsToWatch;
    var theobj = this;
    
    function verifyObject() {
        var hasId = verifyId();
        theobj.hasminWidth = verifyWidth();
        theobj.hasminHeight = verifyHeight();
        if (hasId) {
            if (theobj.hasminWidth || theobj.hasminHeight) theobj.setOk = true;
            else theobj.setOk = false;            
        }
        else theobj.setOk = false;
    }
    
    function verifyId() {
        if((typeof theobj.domId != 'undefined') && (typeof theobj.relId != 'undefined')) return true;
        else return false;
    }
    
    function verifyWidth() {
        if((typeof theobj.minWidth != 'undefined') && (typeof theobj.widthClass != 'undefined')) return true;
        else return false;
    }

    function verifyHeight() {
        if((typeof theobj.minHeight != 'undefined') && (typeof theobj.heightClass != 'undefined')) return true;
        else return false;
    }
    
    function toItemsToWatch(itemsArray) {
        theobj.set();
        if(theobj.setOk) {
            itemsArray[itemsArray.count] = theobj;
            itemsArray.count += 1;
        }
    }
    
}


function minimumCheck(objectList, domList) {
    var itemlength = objectList.length;
    
    for (var i=0; i<itemlength; i++) {
        var nobject = objectList[i];
        var ndom = domList[i];
        
        if(nobject.hasminWidth) {
            minWidthCheck(nobject, ndom);
        }
        if(nobject.hasminHeight) {
            minHeightCheck(nobject, ndom);
        }
    }
}    

function minWidthCheck(nobject, ndom) {
    var nobject = nobject;
    var ndom = ndom;
    var reldom = document.getElementById(nobject.relId);
    var classFlag = hasClass(ndom, nobject.widthClass);
    var shortFlag = isShort(reldom.offsetWidth, nobject.minWidth);
    
    if (shortFlag) {
        if(!classFlag) addClass(ndom, nobject.widthClass);
    }
    else {
        if(classFlag) removeClass(ndom, nobject.widthClass);
    }
}

function minHeightCheck(nobject, ndom) {
    var nobject = nobject;
    var ndom = ndom;
    var reldom = document.getElementById(nobject.relId);
    var classFlag = hasClass(ndom, nobject.heightClass);
    var shortFlag = isShort(reldom.offsetHeight, nobject.minHeight);
    if (shortFlag) {
        if(!classFlag) addClass(ndom, nobject.heightClass);
    }
    else {
        if(classFlag) removeClass(ndom, nobject.heightClass);
    }
}

function hasClass(domObject, className) {
    var classArray = parseClass(domObject);
    returnFlag = false;
    for (var i=0; i < classArray.length; i++) {
        if(classArray[i] == className) {
            returnFlag = true;
            break;
        }
    }
    return(returnFlag);
}

function isShort(relNum, minNum) {
    if (relNum > minNum) return(false);
    else return(true);
}        
    
function parseClass(domObject) {
    var classString = domObject.className;
    var returnArray = classString.split(' ');
    return(returnArray);
}    
                
function addClass(domObject, classToAdd) {
    var classArray = parseClass(domObject);
    classArray.push(classToAdd);
    domObject.className = classArray.join(' ');
}
    
function removeClass(domObject, classToRemove) {
    var classArray = parseClass(domObject);
    classArray.pop(classToRemove);
    domObject.className = classArray.join(' ');
}
        

