// attach onresize event to the window to update the max left pixels we can float the
// menu so it doesent run off the right side of the page
$(window).resize(updateMaxLeft());

// menu vars
var menu = null;
var menucell = null;
var maxleft = null;


function changePerPage(osel) {
	var val = osel[osel.selectedIndex].value;
	url = new Url(location.href);
	url.setVar('perpage', val);
	url.go();
}

function changeOrder(osel) {
	var val = osel[osel.selectedIndex].value;
	url = new Url(location.href);
	url.setVar('order', val);
	url.go();
}


function toggleDirection(dir) {
	var set = (dir == 'ASC') ? 'DESC' : 'ASC';
	url = new Url(location.href);
	url.setVar('dir', set);
	url.go();
}



function updateMaxLeft() {
	start = getX(document.getElementById('submenu'));
	maxleft = start+955-770-22;
}


function showMenu(obj, menuid) {
	
	// menucell is the <td>
	menucell = obj;
  
	// adjust the style for rollover
	menucell.style.background = '#fff';
	menucell.firstChild.style.color  = '#75a71e';
	
	updateMaxLeft();
	
	if (menuid != '' && document.getElementById(menuid)) {
	    menu  = document.getElementById(menuid);
	    menu.style.top = (getY(menucell)+26) + 'px';
	    
	    var toleft = (getX(menucell));
	    if (toleft > maxleft){ 
	        toleft = maxleft;
	    }
	    
	    menu.style.left = toleft + 'px';
	    menu.style.display = ''; 
	} else {
		menu = null;
	}
	

  /*
  
  // work out max accross value
  if (maxleft == null) {
    start = getX(document.getElementById('submenu'));
    jQuery.each(jQuery.browser, function(i) {
    	  if($.browser.msie){
    		  
    		  maxleft = start+955-770-24;
    	  }else{
    		  maxleft = start+955-770-22;
    	  }
    	});

   // maxleft = start+955-770-22;
  }

  if (menuid != '' && document.getElementById(menuid)) {
    menu      = document.getElementById(menuid);
    ieoffset = (document.all)?0:0;
    menu.style.top = (getY(obj)+28) + 'px';
    
    var toleft = (getX(obj)-ieoffset);
    if (toleft > maxleft){ 
      toleft = maxleft;
	    jQuery.each(jQuery.browser, function(i) {
	  	  if($.browser.msie){
	  		  toleft = maxleft+2;
	  	  }
	  	});
    }
    //toleft = toleft + 1;
    
    menu.style.left = toleft + 'px';
    menu.style.display        = ''; 
  } else {
    menu = null;
  }
  
  menucell.style.background = '#fff';
  menucell.firstChild.style.color      = '#75a71e';
  //menucell.firstChild.style.fontWeight = 'bold';
  //menucell.firstChild.style.letterSpacing = '-1';
  
  //window.bork = STIMAGE_FlipImage;
  //window.STIMAGE_FlipImage = null;
  */
  
  
  

}


function hideMenu() {
  	
  if (menu != null) {
    menu.style.display        = 'none';
  }
  
  menucell.style.background = '';
  menucell.firstChild.style.color      = '#fff';
  //menucell.firstChild.style.fontWeight = 'normal';
  //menucell.firstChild.style.letterSpacing = '0';
  
  //window.STIMAGE_FlipImage = window.bork;
  //window.bork = null;
}


function overDiv(div) {
  div.style.display         = '';
  menucell.style.background = '#fff';
  menucell.firstChild.style.color      = '#75a71e';
  //menucell.firstChild.style.fontWeight = 'bold';
  //menucell.firstChild.style.letterSpacing = '-1'; 
}

function outDiv(div) {
  hideMenu();
}

function getX( oElement ) {
  var iReturnValue = 0;
  while( oElement != null ) {
    iReturnValue += oElement.offsetLeft;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}

function getY( oElement ) {
  var iReturnValue = 0;
  while( oElement != null ) {
    iReturnValue += oElement.offsetTop;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}






/* $Id: url.class.js 17635 2008-04-01 23:50:46Z pvandijk $ */


function Url(uri) {

    this.uri_array = new Array();

    //if no URI is passed, use the current URI.
    if (typeof(uri) == 'undefined') {
        uri = window.location.href;
    }

    // Change to support HTTPS
    if( ! uri.match(/http(s)*:\/\//g)) {
        this.page_parts = new Array('', uri);
    } else {
        if(uri.indexOf('?') > 0) {
            this.page_parts = uri.split('?');
        } else {
            this.page_parts = new Array(uri, '');
        }
    }

    //split the URI into its key/var pairs
    this.uri_tmp_array = this.page_parts[1].split('&');


    //loop through the pairs splitting on equals
    for(i=0; i < this.uri_tmp_array.length; i++) {
        pair_array = this.uri_tmp_array[i].split('=');

        //automatic array splitting removed to provide ability to address simplebrowse variables
        if(typeof(pair_array[1])=='undefined'){
            pair_array[1] = '';
        }
        this.uri_array[pair_array[0]] = pair_array[1].trim('#');
    }

    delete(this.uri_array['msg']);

    this.isVar = function (key) {
        if(typeof(this.uri_array[key]) != 'undefined') {
            return true;
        } else {
            return false;
        }
    }


    this.go = function()
    {
        try {
            location.href = this.getUrl();
        } catch (e) {
        }
    }


    this.getUrl = function() {
        uri = '';
        for (key in this.uri_array) {
            if (typeof(this.uri_array[key]) == 'function') continue;
            if(key.length > 0) {
                if(uri.length > 0) {
                    uri = uri + '&';
                }
                if(typeof(this.uri_array[key]) == 'object') {
                    for (sub_key in this.uri_array[key]) {
                        if (typeof(this.uri_array[key][sub_key]) == 'function') continue;
                        if((uri.substr(uri.length-1,uri.length) != '&') && (uri.length > 1)) {
                            uri = uri + '&';
                        }
                        // this was removed about when .replace() below was added, but is still needed for when an
                        // array is passed as a val to this.setVar(). for an example, see simplebrowse.js sbNavigate().
                        // this relies on automatic conversion of an array to a comma delimited string during string
                        // concatenation. bit of a gotcha - any reason why we can't use a .join(',') for clarity?
                        if (typeof(this.uri_array[key][sub_key]) == 'object') {
                            uri += key + '[' + sub_key + ']=' + this.uri_array[key][sub_key];
                        }
                        // only replace # symbols when val is a string and therefore has a .replace() method.
                        if (typeof(this.uri_array[key][sub_key]) == 'string') {
                            uri += key + '[' + sub_key + ']=' + this.uri_array[key][sub_key].replace('#', '');
                        }
                    }
                } else {
                    uri += key + '=' + new String(this.uri_array[key]).replace('#', '');
                }
            }
        }

        if(uri.length > 0) {
            uri = '?' + uri;
        }

        return this.page_parts[0] + uri;
    }


    this.getVar = function (key) {
        if (this.isVar(key)) {
            // make sure variables don't contain the trailing hash from anchors
            return new String(this.uri_array[key]).replace('#', '');
        } else {
            return false;
        }
    }



    this.setVar = function(key, val) {
        if(typeof(key) == 'object') {
            if(typeof(this.uri_array[key[0]]) != 'object') {
                this.uri_array[key[0]] = new Array();
            }
            this.uri_array[key[0]][key[1]] = val;
        } else {
            this.uri_array[key] = val;
        }
    }


    this.delVar = function(key) {
        if(typeof(key) == 'object') {
            delete this.uri_array[key[0]];
        } else {
            delete this.uri_array[key];
        }
    }

    this.delVarsMatching = function(regex) {
        for (var key in this.uri_array) {
            if (typeof this.uri_array[key] == 'function') {
                continue;
            }
            if (key.match(regex)) {
                delete this.uri_array[key];
            }
        }
    }

    this.delVarArray = function(key) {
        if(typeof(key) == 'object') {
            this.delVarsMatching(new RegExp('^' + key[0] + '(\\[.*\\])?$'));
        } else {
            this.delVarsMatching(new RegExp('^' + key + '(\\[.*\\])?$'));
        }
    }

}













