// Wrapper to retrieve a DOM object.  This function (should) contain browser 
// checks and extra convienence functionality. 
function g_getObj(objId) { 
    if (document.getElementById) { 
        return document.getElementById(objId); 
    } 
  
    return null; 
}

// Will find any HTML elements that have a name attribute, including those
// that IE normally doesn't support...
function g_getObjectsByName(tag, name) {
    var tags = document.getElementsByTagName(tag);
    var obj = new Array();
    if (tags != null) {
        for (var i = 0, n = tags.length; i < n; i++) {
            if (tags[i].getAttribute("name") == name) {
                obj.push(tags[i]);
            }
        }
    }
    
    return obj;
}

function g_hideObj(objId) {
    var obj = (typeof(objId) == "string" ? g_getObj(objId) : objId);
    if (obj && obj.style) {
        obj.style.display = "none";
    }
}

function g_showObj(objId) {
    var obj = (typeof(objId) == "string" ? g_getObj(objId) : objId);
    if (obj && obj.style) {
        // Certain objects will not get displayed if their display style is set to blank.
        // For these objects, you will need to explicitly pass in an extra parameter for them
        // to display, try "inline" or "block" - Todd Hansberger
        obj.style.display = (arguments[1] != null ? arguments[1] : "");
    }
}

function g_getTrueOffsetLeft(obj) {
    var trueOffset = 0;
    if (obj != null) {
        while (obj.offsetParent) {
            trueOffset += obj.offsetLeft;
            obj = obj.offsetParent; 
        }
    }
    return trueOffset;
}

function g_getTrueOffsetTop(obj) {
    var trueOffset = 0;
    if (obj != null) {
        while (obj.offsetParent) {
            trueOffset += obj.offsetTop;
            obj = obj.offsetParent; 
        }
    }
    return trueOffset;
}

function ObjectDimension(left, top, width, height) {
    
    // private members
    var _this = this;
    this.left = left;
    this.top = top;
    this.right = left + width;
    this.bottom = top + height;
    
    // private methods
    function isCoordinateOverlap(x, y) {
      return (_this.left <= x && _this.right >= x && _this.top <= y && _this.bottom >= y);
    }

    // privileged methods
    this.isCornersOverlap = function (other) {
      return (isCoordinateOverlap(other.left, other.top)
              || isCoordinateOverlap(other.right, other.top)
              || isCoordinateOverlap(other.left, other.bottom)
              || isCoordinateOverlap(other.right, other.bottom));
    }
    
    this.isOverlap = function (other) {
      return (this.isCornersOverlap(other) || other.isCornersOverlap(this));
    }
}

function getObjectDimension(obj) {
    if (obj == null) {
      return null;
    }
    
    // Requires global_js.jsp 
    return new ObjectDimension(g_getTrueOffsetLeft(obj), g_getTrueOffsetTop(obj), obj.offsetWidth, obj.offsetHeight);
}

function applyParametricSort(f) {
    
    var sort = g_getObj("s");
    var sortValue = "";
    
    if (sort != null) {
        sortValue = sort[sort.selectedIndex].value;
        f.action += "&s=" + sortValue;
    }

    self.location.href = f.action; 
    
    return false;
}

function hideAllExpandMore() {
    var expandMoreElems = g_getObjectsByName("div", "expandMore");
    for (var i = 0, n = expandMoreElems.length; i < n; i++) {
        g_hideObj(expandMoreElems[i]);
        expandMoreElems[i].style.zIndex = -100;
        toggleDropDowns(expandMoreElems[i], false);
    }
}

function showParametricMoreValues(attrName) {
    g_hideObj("more_" + attrName);
    
    var moreContent = g_getObjectsByName("p", "moreContent_" + attrName);
    if (moreContent != null) {
        for (var i = 0; i < moreContent.length; i++) {
            g_showObj(moreContent[i], "block");            
        }
    }
}

function showParametricExpandMoreValuesWithAjax(categoryId,filterUrl,attrName){
        if(g_getObj("expandMoreContent_" + attrName)==null){
                //get the extra module data via ajax
		var url = '/include/parametricModulesAjax.jsp';
		var pars = 'categoryId=' + categoryId + '&parametricFilterBaseUrl=' + filterUrl + '&moduleName=' + attrName;
		new Ajax.Request(url,{method: 'post',postBody: pars,onSuccess:function(transport){var holder= document.createElement("div");holder.innerHTML=transport.responseText;$("leftnav_container").appendChild(holder);showParametricExpandMoreValues(attrName);}});
	}else{
		showParametricExpandMoreValues(attrName);
	}
}

function showParametricExpandMoreValues(attrName) {
    
    hideAllExpandMore();

    var module = g_getObj("module_" + attrName);
    var expandMore = g_getObj("expandMore_" + attrName);
    var expandMoreContent = g_getObj("expandMoreContent_" + attrName);
    var positioner = null;
    var heightAdjuster = null;
    
    if (module.style.cssFloat == "left") { // horizontal alignment
        positioner = g_getObj("parametricFilters");
        heightAdjuster = g_getObj("modules");
    } else { // vertical alignment
        positioner = module;
        heightAdjuster = module;
    }

    expandMore.style.zIndex = 100;
    expandMore.style.position = "absolute";
    expandMore.style.top = g_getTrueOffsetTop(positioner) + "px";
    expandMore.style.left = g_getTrueOffsetLeft(positioner) + "px";

    g_showObj(expandMore, "block");
    
    if (expandMoreContent.offsetHeight < heightAdjuster.offsetHeight) {
        expandMoreContent.style.height = "auto";
        expandMoreContent.style.height = heightAdjuster.offsetHeight + "px";
    }

    toggleDropDowns(expandMore, true);
}

function toggleDropDowns(expandMore, isHiding) {
    var allDropDowns = document.getElementsByTagName("select");
    
    if (allDropDowns != null) {

        var expandMoreDimension = getObjectDimension(expandMore);
        var dropDownDimension;
        
        for (var i = 0, n = allDropDowns.length; i < n; i++) {
            if (isHiding) {
                dropDownDimension = getObjectDimension(allDropDowns[i]);
            
                if (dropDownDimension.isOverlap(expandMoreDimension)) {
                    g_hideObj(allDropDowns[i]);
                } 
            } else {
                g_showObj(allDropDowns[i]);
            }
        }
    }
}



