var _bcUtil;

if (!_bcUtil) _bcUtil = {};



/**************************************************
			Mouse Tracker
**************************************************/

_bcUtil.MouseTracker = function(strEvent){
	//only cover mouse up right now
	this._x = 0;
	this._y = 0;

	if(strEvent == 'mouseup'){
		this.startTrackingMouseUp();
	}
};

_bcUtil.MouseTracker.prototype.trackMouseUp = function(e){
	  	if (!e) e= event;

	    var docX,docY;

	    if (e.pageX == null)
	    {
	       // IE case
	       var d= (document.documentElement &&
	       document.documentElement.scrollLeft != null) ?
	       document.documentElement : document.body;
	       docX= e.clientX + d.scrollLeft;
	       docY= e.clientY + d.scrollTop;
	    }
	    else
	    {
	       // all other browsers
	       docX= e.pageX;
	       docY= e.pageY;
	    }

	    gMouseStruct.setLoc({_x:docX-625, _y:docY-150});
	    //gMouseStruct.setLoc({_x:docX, _y:docY});
	   // return false;
};

_bcUtil.MouseTracker.prototype.startTrackingMouseUp = function(){


	if (document.addEventListener)
	{
	    document.addEventListener("mouseup",this.trackMouseUp, false);
	}
	else if (document.attachEvent)
	{
	    document.attachEvent("onmouseup",this.trackMouseUp);
	}

};

_bcUtil.MouseStruct = function(x,y){
	//only cover mouse up right now

        var theX = parseInt(x);
        var theY = parseInt(y);

        if(isNaN(theX))
            x = 0;

        if(isNaN(theY))
            y = 0;

	this._x = x;
	this._y = y;
};

_bcUtil.MouseStruct.prototype.setLoc = function(objLoc){
	this._x = objLoc._x;
	this._y = objLoc._y;
};

_bcUtil.MouseStruct.prototype.getLoc = function(objLoc){
	return {_x:this._x, _y:this._y};
};




/**************************************************
ImagePopper (pops open a div containing an image)
**************************************************/
_bcUtil.ImagePopper = function(imgID, offsetLoc){

                //offsetLoc is a mouseStruct object
                
		if(!imgID)
			return null;

                if(!offsetLoc)
                    offsetLoc = new _bcUtil.MouseStruct(0,0);
		
		this.state=0;
		this.currID=imgID;
		this.currImg = null;
                this.OFFSET = offsetLoc;
		
};

_bcUtil.ImagePopper.prototype.popImage = function(thisID, thisURL, elem){
	
	if(this.currImg)
	{
		this.currImg.hide();
		this.currImg = null;
	}

	var theRect = elem.getBoundingClientRect();
        var theWidth = theRect.right - theRect.left;

	var theTop = 0;
	var theLeft = 0;
	
	var totalOffsetTop = 0;
        var totalOffsetLeft = 0;

        var el = elem;

	while (el != null) {
		totalOffsetTop += el.offsetTop;
		el = el.offsetParent;
	}

        el = elem;
        while (el != null) {
		totalOffsetLeft += el.offsetLeft;
		el = el.offsetParent;
	}

	theTop = totalOffsetTop + this.OFFSET._x;
        theLeft = totalOffsetLeft + theWidth + this.OFFSET._y;
	
	var thisLoc = {_x:theLeft, _y:theTop};
	
	
	this.currImg = new _bcUtil.ImageDiv(this.currID, thisID, thisURL, thisLoc);
	if(this.currImg){
		this.currImg.show();
		this.state = 1;
	}
	else{
		this.currImg = null;
		this.state = 0;
	}
	
};

_bcUtil.ImagePopper.prototype.hideImage = function(thisID){
	
	if(this.currImg)
	{
		this.currImg.hide();
		this.currImg = null;
		this.state = 0;
	}
	

};

/**************************************************
		ImageDiv (display an image in a div)
**************************************************/

_bcUtil.ImageDiv = function(id, imgid, thisURL, thisLoc){
	if(!id || !imgid || !thisURL)
		return null;

	this.divID = id;	
	this.imgID = imgid;
	this.imgURL = thisURL;
	if(thisLoc)
		this.loc = thisLoc;
	
	
};

_bcUtil.ImageDiv.prototype.show = function(){
	
	
	var eImg=document.getElementById(this.imgID);
	eImg.src = this.imgURL;
	
	var eDiv=document.getElementById(this.divID);
	if(this.loc){
		eDiv.style.left = this.loc._x + 'px';
		eDiv.style.top = this.loc._y + 'px';
	}

	if(eDiv.style.visibility != 'visible')
		eDiv.style.visibility = 'visible';
	
};

_bcUtil.ImageDiv.prototype.hide = function(){
	
	var eDiv=document.getElementById(this.divID);
	if(eDiv.style.visibility != 'hidden')
		eDiv.style.visibility = 'hidden';
	
	var eImg=document.getElementById(this.imgID);
	eImg.src = '';
	
};


/*************************************
 *  ordinary functions
 *************************************/


function bcLoadCat(){

	var theCat = document.getElementById('catList').options[document.getElementById("catList").selectedIndex].value;
	//alert(theCat);
	var qStr = '';
	if(theCat)
		qStr = '?cat=' + theCat;

	var newLoc = '/categories.php' + qStr;
	//alert(newLoc);
	document.location = newLoc;

}

function getScrollXY() {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return [ scrOfX, scrOfY ];
	}

/*************************************
 * object instantiation
 *************************************/

var gImageUtil = new _bcUtil.ImagePopper('popImageWrapper', new _bcUtil.MouseStruct(-120,10));


