// JavaScript Document
function $( name ){
	return document.getElementById( name );
}

function confirmAction(){
	if( confirm( "Are you sure you want to do this?" ) ) return true;
	else return false;
}

function delegate( that, thatMethod ){
    return function() { return thatMethod.call(that, arguments ); }
    //return function() { return thatMethod.apply(that, arguments ); }
}

function registerClearDefault( inpt, defTxt ){
	inpt.onfocus = function(){ inptClearDefault( inpt, true, defTxt ); }
	inpt.onblur = function(){ inptClearDefault( inpt, false, defTxt ); }
}

function inptClearDefault( inpt, isfocus, txt ){
	if( isfocus ){
		if( inpt.value == txt ) inpt.value = "";
	}else{
		if( inpt.value == "" ) inpt.value = txt;
	}
}

function refreshSubCats( catid ){
	$( "subcatdiv" ).className = "hidden";
	$( "subcatselect" ).options.length = 0;	
	
	var obj = {
		processXMLResponse:function( xml ){
			var cats =  xml.childNodes;
			if( cats.length > 0 ){
				var catOptions = $( "subcatselect" ).options;
				catOptions[catOptions.length] = new Option("-- Select -- ", 0 );
				for( var i = 0; i < cats.length; i++ ){
					var node = cats[i];
					catOptions[catOptions.length] = new Option(node.getAttribute( "name" ),node.getAttribute( "id" ));
				}
				$( "subcatdiv" ).className = "visible";
			}else{
			}
		}
	};
	
	var conn = new ajaxConnection( obj, "/ajax/categories.php" );
	conn.initRequestObject();
	conn.addVariable( "catid", catid );
	conn.execute();
}

function showCategoryMenu(){
	var div = $( "catdiv" );
	var lnk = $( "browseMenuLink" );
	//alert( getXY( lnk ).x );
	var pos = getXY( lnk );
	var containerPos = getXY( $( "siteHeader" ) );
	
	div.className = "catdiv visible";
	div.style.left = ( pos.x - containerPos.x - 5 ) + "px";
	div.style.top = ( pos.y - 62 ) + "px";
	//alert( div.style.top );
}

function hideCategoryMenu(){
	var div = $( "catdiv" );
	div.className = "catdiv hidden";
}

/**
 * Returns the absolute X and Y positions of an object.
 * @param {HTMLObject} obj HTML Object.
 * @return {Object} Returns an accessor with .x and .y values.
 */
function getXY(obj)
{
  var curleft = 0;
  var curtop = obj.offsetHeight + 5;
  var border;
  if (obj.offsetParent)
  {
    do
    {
      // XXX: If the element is position: relative we have to add borderWidth
      if (getStyle(obj, 'position') == 'relative')
      {
        if (border = getStyle(obj, 'border-top-width')) curtop += parseInt(border);
        if (border = getStyle(obj, 'border-left-width')) curleft += parseInt(border);
      }
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
    while (obj = obj.offsetParent)
  }
  else if (obj.x)
  {
    curleft += obj.x;
    curtop += obj.y;
  }
  return {'x': curleft, 'y': curtop};
}
/**
 * Returns the specified computed style on an object.
 * @param {HTMLObject} obj HTML Object
 * @param {String} styleProp Property name.
 * @return {Mixed} Computed style on object.
 */
function getStyle(obj, styleProp)
{
  if (obj.currentStyle)
    return obj.currentStyle[styleProp];
  else if (window.getComputedStyle)
    return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
}


