// JavaScript Document
// array used to hold all select boxes, populated by an onclick event attached to
// the specific select box
var sb_selectBoxArray = new Array();

// check all unique roll over objects to determine if whether list should be closed
function sb_checkSelectBoxOver(){
	for(var i=0; i<sb_selectBoxArray.length; i++){
		sb_selectBoxArray[i].over=false;
		for(var j=0; j<sb_selectBoxArray[i].optionOver.length; j++){
			if(sb_selectBoxArray[i].optionOver[j]==true){
				sb_selectBoxArray[i].over=true;
			}
		}
		if(sb_selectBoxArray[i].displayOver==true){
			sb_selectBoxArray[i].over=true;
		}
		if(sb_selectBoxArray[i].displayListOver==true){
			sb_selectBoxArray[i].over=true;
		}
		if(sb_selectBoxArray[i].over==false && sb_selectBoxArray[i].closing==false && sb_selectBoxArray[i].listElementContainer.style.display!="none"){
			sb_selectBoxArray[i].closing=true;
			sb_startCloseSelectBox(i);
		}
	}
}
// display list
function sb_openSelectBox(myElement){
	sb_selectBoxArray[myElement.id].listElementContainer.style.display="block";
	sb_selectBoxArray[myElement.id].element.style.zIndex="300";
	sb_positionDisplayList(myElement.id);
}
// delay closing list
function sb_startCloseSelectBox(myNumber){
	var t=setTimeout("sb_closeSelectBox("+myNumber+")",700);
}
// close list
function sb_closeSelectBox(myNumber){
	// check to make sure user didn't roll over menu again
	if(sb_selectBoxArray[myNumber].over==false){
		sb_selectBoxArray[myNumber].listElementContainer.style.display="none";
		sb_selectBoxArray[myNumber].element.style.zIndex="7";
	}
	sb_selectBoxArray[myNumber].closing=false;
}
// events to determine if user is over a list element
function sb_listElementOver(mySelectBox,myOption){
	sb_selectBoxArray[mySelectBox.id].optionOver[myOption.id]=true;
}
function sb_listElementOut(mySelectBox,myOption){
	sb_selectBoxArray[mySelectBox.id].optionOver[myOption.id]=false;
}
// events to determine if user is over the box or arrow
function sb_overSelectBox(mySelectBox){
	sb_selectBoxArray[mySelectBox.id].displayOver=true;
	sb_checkSelectBoxOver();
}
function sb_outSelectBox(mySelectBox){
	sb_selectBoxArray[mySelectBox.id].displayOver=false;
	sb_checkSelectBoxOver();
}
// events to determine if user is over list, necessary for long lists which generate scroll bar
function sb_overSelectBoxContainer(mySelectBox){
	sb_selectBoxArray[mySelectBox.id].displayListOver=true;
	sb_checkSelectBoxOver();
}
function sb_outSelectBoxContainer(mySelectBox){
	sb_selectBoxArray[mySelectBox.id].displayListOver=false;
	sb_checkSelectBoxOver();
}
// handles user selection of an option
function sb_listElementSelect(mySelectBox,myElement){
	mySelectBox.value=myElement.value;
	mySelectBox.displayValue=myElement.displayValue;
	mySelectBox.input.value=mySelectBox.value;
	mySelectBox.displayContentElement.innerHTML="<span class=\"refinementTextSel\">"+mySelectBox.displayValue+"</span>";
	mySelectBox.element.className="selectBoxSel";
	mySelectBox.listElementContainer.style.display="none";
	mySelectBox.element.style.zIndex="7";
}

// handle display of list based on position in page. If near bottom of page
// list will invert and go upwards from the select box
function sb_positionDisplayList(myNumber) {
    var myHeight = 0;
    var myScrollY = 0;
	sb_selectBoxArray[myNumber].listElementContainer.style.top="18px";
	// get window attributes
    if (typeof (window.innerWidth) == "number") {
		//Non-IE
        myHeight = window.innerHeight;
    } else {
        if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
        } else {
            if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            //IE 4 compatible
                myHeight = document.body.clientHeight;
            }
        }
    }
    
    // Find out how far the user has scrolled in order to position the list at the correct height
    if (window.pageYOffset) {
        myScrollY = window.pageYOffset;
    } else if (document.body.scrollTop && document.body.scrollTop > 0) {
        myScrollY = document.body.scrollTop;
    } else if (document.documentElement.scrollTop && document.documentElement.scrollTop > 0) {
        myScrollY = document.documentElement.scrollTop;
    }
	// position list
	if(sb_selectBoxArray[myNumber].listElementContainer.offsetTop+sb_selectBoxArray[myNumber].listElementContainer.parentNode.offsetTop+sb_selectBoxArray[myNumber].listElementContainer.offsetHeight>myScrollY+myHeight){
		sb_selectBoxArray[myNumber].listElementContainer.style.top="-"+(sb_selectBoxArray[myNumber].listElementContainer.offsetHeight-1)+"px";
	}
}