
function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;
  
  
  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

function yaay() {alert("!");}
// Global object to hold drag information.

var xOnly = 0; //Slide on X axis only
var dragObj = new Object();
dragObj.zIndex = 0;
//testDiv = document.getElementById("test");

function dragStart(event, id) {
  var el;
  var x, y;
  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id) {
    dragObj.elNode = document.getElementById(id);
	dragObj.id = id;
	d = dragObj.elNode;
  }
  else {
    if (browser.isIE)  dragObj.elNode = window.event.srcElement;
    if (browser.isNS)  dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3) dragObj.elNode = dragObj.elNode.parentNode;
  }

  
  // Get cursor position with respect to the page.

  
  if (typeof document.getElementById(id).slide == 'object') document.getElementById(id).slide.clear();
  if (browser.isIE) {

	    x = window.event.clientX + document.documentElement.scrollLeft
	      + document.body.scrollLeft;
		  
	    y = window.event.clientY + document.documentElement.scrollTop
	      + document.body.scrollTop;
	
  }	
  if (browser.isNS) {
//	test.innerHTML = "NS:"+event;
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
	if (typeof document.getElementById("bullshit") == 'object') document.getElementById("bullshit").style.display = "block";	
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

//  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

    
  if (browser.isIE) { // Capture mousemove and mouseup events on the page.
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
//    document.attachEvent("onmouseout",   dragStop);	
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {

    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
//    document.addEventListener("mouseout",   dragStop, true);	
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;
  // Get cursor position with respect to the page.
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  if (!xOnly) dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)  event.preventDefault();
}

function dragStop(event) {
	xOnly = 0;
	if (dragObj.id.substring(0,4) != 'pKey' && (dragObj.id != 'quickSearch')) {
		/* If it's NOT a drag object, it's a regular overlay, save location in preferences */
		
		if (cW) isDivOffScreen(d);
		
		tmpObj = document.getElementById(dragObj.id);
		if (typeof tmpObj.slide == 'object') {
			var bidSlide = new slide;	
//			bidSlide.setMyDiv(dragObj.id);	
//			tmpObj.slide.isOffScreen();
		}
		
//		alert(dragObj.elNode.style.left)

		//Bid_frameset uses the setpref in bid_search_new.js and refine_search_sajax.php
		setPref(dragObj.id+"_top",dragObj.elNode.style.top)
		setPref(dragObj.id+"_left",dragObj.elNode.style.left)		
	}else {
		cut = dragObj.id.substring(4)

		rowStarts = cut.search("M");
		rowId = cut.substring((rowStarts+1))
		pKey = cut.substring(0,rowStarts)
//		bidCopy(rowId,pKey);
	}
  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
	if (typeof document.getElementById("bullshit") == 'object') document.getElementById("bullshit").style.display = "none";	
  }
}

function isDivOffScreen(obj) {
	/* Checkes to see if more than 1/2 the window is off screen, and moves back on screen if needed */
	
//	return;
	
//		alert(parseInt(obj.style.width))
//		alert(parseInt(document.getElementById("preferences").style.width))
	
	if(!obj) return;
	


	right = parseInt(obj.style.left) + parseInt(obj.style.width);
	bottom = parseInt(obj.style.top) + parseInt(obj.style.height);

	halfHeight = parseInt(obj.style.height) *  .60;
	halfWidth = parseInt(obj.style.width) * .65;		
	
	
//	if (parseInt(bottom) > (cH + halfHeight)) obj.style.top = (cH - (parseInt(obj.style.height) + 25))+"px";
	if (parseInt(obj.style.top) > (cH - 300)) obj.style.top = "150px";	
	if (parseInt(obj.style.top) < 0) obj.style.top = "3px";	
//	alert("cw:" + cH + "\nbottom:" +bottom + "\n(cH + halfHeight):" +(cH + halfHeight))	
// 	alert("halfWit:"+halfWidth+"\nright:"+ right + "\ncW+hW" + (cW + halfWidth));
//	alert("cw:" + cW + "\nright:" +right + "\n(cW - (right+100)):" +(cW + halfWidth))
	if (right > (cW + halfWidth)) obj.style.left = (cW - (parseInt(obj.style.width)+5))+"px";	
	if (parseInt(obj.style.left) < 0) obj.style.left = "5px";	
	
}


	/* Object oriented version of slide, motha fucka */

	var slide = function() {
		var myDiv; 
		var topButton;
		var bottomButton;
		this.delay = 10; //The current delay
		this.step = 20; //How many px to add or subtract on each move
		this.xy = true; //The axis we are sliding on - false = x, true = y
		this.plusminus = false; //true = add step pixels each iteration, false = subtract pixels
		this.distance = 180;
		var startPx; //Starting pixel on Y axis before scroll
		var endPx; //Ending pixel
		var pos; //Current pos on Y axis
		var outerThis;
	}
	
	slide.prototype.setMyDiv = function(div) {
			if (typeof div !== 'object') div = document.getElementById(div);		
			this.myDiv = div;
			div.slide = this;
			this.isOffScreen();
	}

	slide.prototype.isOffScreen = function() {
			t = parseInt(this.myDiv.style.top);
			h = this.myDiv.offsetHeight;
			w = this.myDiv.offsetWidth;
			if ((t+h) > (cH-25)/* && mU.style.display != 'block'*/) {
				this.displaySlideButton(2);
			}else{
				this.hideSlideButton(2)
			}
			if (t < 0) {
				this.displaySlideButton(1);
			}else{
				this.hideSlideButton(1)
			}
	}	
	
	slide.prototype.slide = function() {
			var MyObj = this.outerThis;	
			MyObj.plusminus = this.upDown;
			if (MyObj.xy) {MyObj.startPx = parseInt(MyObj.myDiv.style.top);}else{MyObj.startPx = parseInt(MyObj.myDiv.style.left);}		
			if (this.upDown === 1) {MyObj.endPx = MyObj.startPx + MyObj.distance;}else if(this.upDown === 2){MyObj.endPx = MyObj.startPx - MyObj.distance;}
			if (typeof document.getElementById("bullshit") == 'object') {document.getElementById("bullshit").style.display = "block";}
			MyObj.pos = MyObj.startPx;		
			MyObj.moveDiv();
	}
	
	slide.prototype.moveDiv = function() {
			var MyObj = this;
			this.isOffScreen();
			if (MyObj.plusminus == 1) {
				MyObj.pos = MyObj.pos + MyObj.step;
				if (MyObj.pos <= MyObj.endPx) {
					setTimeout(function(){MyObj.moveDiv();},MyObj.delay);			
				}else{
					if (typeof document.getElementById("bullshit") == 'object') document.getElementById("bullshit").style.display = "none";					
				}
			}else if (MyObj.plusminus == 2){
				MyObj.pos = MyObj.pos - MyObj.step;
				if (MyObj.pos >= MyObj.endPx) {
					setTimeout(function(){MyObj.moveDiv();},MyObj.delay);
				}else{
					if (typeof document.getElementById("bullshit") == 'object') document.getElementById("bullshit").style.display = "none";
				}
			}
			if (MyObj.xy) {MyObj.myDiv.style.top = MyObj.pos+"px";}else{MyObj.myDiv.style.left = MyObj.pos+"px";}					
	}
		
	slide.prototype.displaySlideButton = function(where) {
		if ((where == 1 && !this.topButton) || (where == 2 && !this.bottomButton)) {
		 	newDiv = document.createElement("div");
			newDiv.style.position = "absolute";
			newDiv.style.zIndex = "8";
			newDiv.style.display = "block";
			newDiv.style.textAlign = "center";
			newDiv.style.background = "#FFB221";
			newDiv.style.height = "20px";
			newDiv.style.color = "#000000";
			newDiv.style.cursor = "pointer";
			newDiv.style.cursor = "hand";	
			newDiv.style.width = this.myDiv.clientWidth+"px";
			newDiv.style.left = this.myDiv.style.left;
			newDiv.outerThis = this;
			newDiv.myDiv = this.myDiv;
			newDiv.onclick = this.slide;

				newClose = document.createElement("img");
				newClose.src = "../images/close_button.jpg";
//				newClose.style.width = "23px";
//				newClose.style.height = "23px";
//				newClose.addEventListener('click',yaay,false);
//				newClose.onclick = "document.getElementById('bid_details').style.display='none';document.getElementById('bid_details').slide.clear();bd=document.getElementById('bid_details');bd.slide.clear()";
//				newClose.style.float = "right";
				newClose.style.cursor = "pointer";
				newClose.style.cursor = "hand";
				newClose.style.position = "relative";
				newClose.style.left = "2px";
				newClose.style.zIndex = "9";


			
			if (where == 1) { /* 1 is top */
				
				newDiv.innerHTML = "Click to move down &nbsp; &nbsp;";
				newDiv.style.top = "0px";	
				newDiv.upDown = 1;
				newDiv.appendChild(newClose);			
				
				this.topButton = newDiv;
				this.myDiv.topButton = newDiv;				
			}else{ 			/* 2 is bottom */
				newDiv.innerHTML = "Click to move up";		
//				newDiv.innerHTML = "<img src='/images/move_button.gif height=19 width=100% \>";
				newDiv.style.bottom = "0px";		
				newDiv.upDown = 2;				
				this.bottomButton = newDiv;
				this.myDiv.bottomButton = newDiv;
			}
			document.body.appendChild(newDiv);

		}else{
			if (where == 1) {
				this.topButton.style.display = "block";
			}else{
				this.bottomButton.style.display = "block";				
			}
		}		
	}

	slide.prototype.hideSlideButton = function(where) {
		if (where == 1) {
			if (this.topButton) this.topButton.style.display = "none";
		}else if (where == 2) {
			if (this.bottomButton) this.bottomButton.style.display = "none";												
		}
	}
	
	slide.prototype.clear = function() {
		if (this.topButton) {document.body.removeChild(this.topButton);this.topButton = '';}
		if (this.bottomButton) {document.body.removeChild(this.bottomButton);this.bottomButton = '';}

	}




/*
	function isOffScreen(val) {
		t = parseInt(val.style.top);
		h = val.offsetHeight;
		w = val.offsetWidth;
		if ((t+h) > (cH-25) && mU.style.display != 'block') {
			mU.style.display = "block";
			mU.style.width = w+"px";
			mU.style.bottom = "0px";
			mU.style.left = val.style.left;
			createSlideButton(2,val);
		}else{
			mU.style.display = "none";
		}
		if (t < 0) {
			mD.style.display = "block";
			mD.style.width = w+"px";
			mD.style.top = "0px";
			mD.style.left = val.style.left;		
			createSlideButton(1,val);	
		}else{
			mD.style.display = "none";		
		}
		howfar = cH - (t+h);
	
	}
	
	

	function createSlideButton(where,what) {
	 	newDiv = document.createElement("div");
//		newDiv.style = "position:absolute;z-Index:8;display:block;text-align:center;background-color:#FFF000;height:25px;font-family:arial;font-size:25px;color:#000000;cursor:pointer;cursor:hand;";
		newDiv.style.position = "absolute";
		newDiv.style.zIndex = "8";
		newDiv.style.display = "block";
		newDiv.style.textAlign = "center";
		newDiv.style.background = "#FF0000";
		newDiv.style.height = "30px";
		newDiv.style.color = "#000000";
		newDiv.style.cursor = "pointer";
		newDiv.style.cursor = "hand";	
		newDiv.style.width = what.style.width;
		newDiv.style.left = what.style.left;
		if (where == 1) { // 1 is top 
			newDiv.innerHTML = "Move Down";		
			newDiv.style.top = "0px";	
			newDiv.id = what.id + "_slideTop";
		}else{ 			// is bottom 
			newDiv.innerHTML = "Move Up";		
			newDiv.style.bottom = "0px";		
			newDiv.id = what.id + "_slideBottom";				
		}
		document.body.appendChild(newDiv);
		return newDiv;
		if (document.all) {
			
		}else{
		
		}
	}	

	
	function slide(obj,px,slideObj) {
		this.delay = 15; //The current delay
		this.step = 22; //How many px to add or subtract on each move
		this.xy = true; //The axis we are sliding on - false = x, true = y
		this.plusminus = false; //true = add step pixels each iteration, false = subtract pixels
		this.obj = document.getElementById(obj);
		this.slideObj = slideObj;
//		this.newDiv = createElement("div");
//		this.newDiv.innerHTML = 
		if (this.xy) {this.startPx = parseInt(this.obj.style.top);}else{this.startPx = parseInt(this.obj.style.left);}		
		if (this.plusminus) {this.endPx = this.startPx + px;}else{this.endPx = this.startPx - px;}
		this.pos = this.startPx;
				
	}
	
	slide.prototype.moveDiv = function() {
		isOffScreen(this.obj);
		if (this.plusminus) {
			this.pos = this.pos + this.step;
			if (this.pos <= this.endPx) setTimeout(this.slideObj+".moveDiv()",this.delay);			
		}else{
			this.pos = this.pos - this.step;
			if (this.pos >= this.endPx) setTimeout(this.slideObj+".moveDiv()",this.delay);				
		}
		if (this.xy) {this.obj.style.top = this.pos+"px";}else{this.obj.style.left = this.pos+"px";}
	}
*/




