document.onmousemove = follow_mouse;
var rollovers_arr = new Array();
var ro_z_index = 1000;
var dragObj = new Object();

function make_rollover(id,body,title,width,height,left,top,b_drag,z_index,margin,b_vis){
	rollovers_arr[id] = id;
	var ro_divs = new Array();
	
	if( browser.isIE ){
		winW = document.body.offsetWidth;
		winH = document.body.offsetHeight;
	}else{
		winW = window.innerWidth;
		winH = window.innerHeight;
	}
	if( width == null ) width = 350;
	if( height == null ) height = 200;
	if( left == null ) left = parseInt(winW/2) - parseInt(width/2);
	if( top == null ) top = parseInt(winH/2) - parseInt(height/2);
	if( title == null ) title = 'Alert!';
	if( b_drag == null ) b_drag = true;
	if( z_index == null ) z_index = 100;
	if( margin == null ) margin = '10px';
	else if( !isNaN(margin) ) margin += "px";
	
	if (browser.isIE || browser.isOpera) {
		scrollx = document.body.scrollLeft;
		scrolly = document.body.scrollTop;
	}
	else  {
		scrollx = window.scrollX;
		scrolly = window.scrollY;
	}
	foot = "<input type='button' value='Close' onclick='javascript:remove_rollover(\"" + id + "\");'>";
	left += scrollx;
	top += scrolly;
	container = document.createElement("div");
	container.id = id;
	if( b_drag ) container.onmousedown = function(event){( window.event ) ? dragStart(window.event,id) : dragStart(event,id)};
	body_div = document.createElement("div");
	html_div = document.createElement("div");
	foot_div = document.createElement("div");
	html_div.id = id + '_html';
	shadow_div = document.createElement("div");
	shadow_div.id = id+'_shadow';
	header_div = document.createElement("div");
	iframe_hack = document.createElement("iframe");
	iframe_hack.id = 'iframe_hack';
	header_div.className = "sidebarHeader";
	header_div.innerHTML = "<table id='header_table' style=\"width:"+ width +"px;\"><tr><td style='width:100%;'>"+title+"</td><td style=\"text-align:right;\"><a href=\"javascript:remove_rollover('"+id+"');\">X</a></td><td>&nbsp;</td></tr></table>";
	shadow_div.innerHTML = "<img id='shadow_img' src='http://images.neopets.com/ncmall/shadow.png'>";
	html_div.style.position = 'relative';
	header_div.style.position = 'relative';
	body_div.style.position = 'absolute';
	shadow_div.style.position = 'absolute';
	html_div.innerHTML = (typeof(body) == 'string') ? body : body.innerHTML;
	foot_div.innerHTML = foot;
	html_div.style.margin = margin;
	body_div.style.width = width + 'px';
	body_div.style.height = height + 'px';
	body_div.style.height = 'auto';
	body_div.style.zIndex = ro_z_index++;
	body_div.style.left = '0px';
	body_div.style.top = '0px';
	body_div.style.border = '2px solid lightgrey';
	body_div.style.backgroundColor = 'white';
	container.style.position = 'absolute';
	container.style.left = left + 'px';
	container.style.top = top + 'px';
	container.style.zIndex = ro_z_index;
	container.appendChild(shadow_div);
	foot_div.className = "sidebarFooter";
	foot_div.style.marginBottom = '10px';
	body_div.appendChild(header_div);
	var html_element = body_div.appendChild(html_div);
	body_div.appendChild(foot_div);
	var body_element = container.appendChild(body_div);
	container.style.zIndex = 100000;
	document.body.appendChild(container);
	if (browser.isIE) {
		iframe_hack.style.height = body_element.offsetHeight;
		iframe_hack.style.width = body_element.offsetWidth;
		container.appendChild(iframe_hack);
	}
	var shadow_img = document.getElementById('shadow_img');
	var shadow_width = parseInt(width * 1.03);
	var shadow_height = body_element.offsetHeight * 1.04;
	var shadow_left = parseInt(width*.05);
	var shadow_top = parseInt(height*.05);
	shadow_div.style.left = shadow_left + 'px';
	shadow_div.style.top = shadow_top + 'px';
	shadow_div.style.zIndex = 1;
	shadow_img.style.width = shadow_width + 'px';
	shadow_img.style.height = shadow_height + 'px';
	//document.getElementById('header_table').style.width = body_element.offsetWidth;
	//ro_divs['main'] = container;
	//ro_divs['shadow'] = shadow_img;
	//return ro_divs;
}


function remove_rollover(id){
	var code_1 = document.getElementById('code_1');
	var code_2 = document.getElementById('code_2');
	if(code_1) {
		code_1.value = '';				
	}
	if(code_2) {
		code_2.value = '';
	}

	if( id == 0 ){
		// remove all
		for( var i in rollovers_arr ){
			container = document.getElementById(rollovers_arr[i]);
			try{
				if (container.id != 'ext_prev_div') {
				container.parentNode.removeChild(container);
				
				}
			}catch(e){}
		}
		return;
	}
	container = document.getElementById(id);
	try{
		container.parentNode.removeChild(container);
	}catch(e){}
}

function in_array (needle, haystack) {
	var len = haystack.length;
	for (var x = 0; x <= len; x++) {
		if (haystack[x] == needle) return true;
	}
	return false;
}

function dragStart(event, id) {
  var el;
  var x, y;

  
  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    else
      dragObj.elNode = event.target;
    
    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

    if (browser.isIE || browser.isOpera) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  else  {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

    
  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;

  
  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

    if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  else {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {
  var x, y;

  
  if (browser.isIE || browser.isOpera) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  else {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

    dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  else 
    event.preventDefault();
}

function dragStop(event) {

    if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  else {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}
  
function get_ajax_data(script,args,func_str){
	var request = new Object;
	request.url = aserver + script;
	request.method = "POST";
	request.args = '';
	for( var i=0; i<args.length; i++ ){
		if( args.charAt(i) == '|' ) request.args += '&';
		else request.args += args.charAt(i);
	}
	request.onSuccess = function (response){
		response_str = response;
		if( func_str != null ) eval(func_str+"(response)");
		else return(response_str);
	}
	request.onFailure = function (response) {
		alert("Technical error. AJAX call failed!");
		if( 0 == 1 ) alert("script: " + script + "\nargs: " + request.args);
	}
	ajaxRequest(request);
}

function follow_mouse(evt){
	pop_x = parseInt(mouseX(evt));
	pop_y = parseInt(mouseY(evt));
}
                 
function mouseX(evt) {
	if (!evt) evt = window.event;
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX)
	   return evt.clientX + (document.documentElement.scrollLeft ?
	   document.documentElement.scrollLeft :
	   document.body.scrollLeft);
	else return null;
}

function mouseY(evt) {
	if (!evt) evt = window.event;
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
	   return evt.clientY + (document.documentElement.scrollTop ?
	   document.documentElement.scrollTop :
	   document.body.scrollTop);
	else return null;
}
