// JavaScript Document

var AJAX_LOADING_IOON = 'style/images/popup/loading.gif';

MessageController = function()
{
	this.total_received = 0;
	this.total_new = 0;
	
	this.remove = function(id)
	{
		$('messagelist_received_removeicon_' + id).hide();
		
		//page.loadScriptPage(60, '&del=' + id);
		Effect.Fade('messagelist_received_message_' + id)			
		
		if($('messagelist_new_message_' + id))
		{
			if($('messagelist_new_message_' + id).display == 'block')
			{
				Effect.Fade($('messagelist_new_message_' + id));
				this.setTotalNew(this.getTotalNew() - 1);
			}
		}
		
		this.setTotalReceived(this.getTotalReceived() - 1);
		
		page.loadScriptPage(58, '&del=' + id);
	}
	
	this.setRead = function(id)
	{
		if($('mesagelist_received_newicon_' + id))
			$('mesagelist_received_newicon_' + id).src="style/images/icons/mail_old.png"
		
		if($('messagelist_new_message_' + id))
		{
			Effect.Fade($('messagelist_new_message_' + id));
			this.setTotalNew(this.getTotalNew() - 1);
		}		
	}
	
	this.setTotalReceived = function(c)
	{
		this.total_received = c;
		if($('messagelist_received_count'))
			$('messagelist_received_count').innerHTML = c;
	}
	
	this.getTotalReceived = function()
	{
		return this.total_received;		
	}
	
	this.setTotalNew = function(c)
	{
		this.total_new = c;
		
		if($('messagelist_new_count'))
			$('messagelist_new_count').innerHTML = c;
	}
	
	this.getTotalNew = function()
	{
		return this.total_new;	
	}
}

var message_controller = new MessageController();

AddressBook = function()
{
		this.remove = function(id)
		{
				page.loadScriptPage(60, '&del=' + id);
				Effect.Fade('adressbook_line_' + id)			
				this.substractCount();
		}
		
		this.substractCount = function()
		{
			var current_count = parseInt($('addressbook_total_addresses').innerHTML);
			
			if(current_count - 1 < 0)	
				return;
				
			this.setCount(current_count - 1);
		}
		
		this.setCount = function(count)
		{
			$('addressbook_total_addresses').innerHTML = count
		}
		
		this.reload = function()
		{
			new Ajax.Updater('message_addressbook', 'ajax.php?page='+60, {evalScripts:true});
		}
}

var address_book = new AddressBook();



/*
Meun
*/

Menu = function()
{
	
	this.setActive = function(element)
	{
		var menu_links = $$('div.menuLink').concat($$('div.activeMenuLink'));
					
		for(i=0; i<menu_links.length; i++)
		{
			menu_links[i].removeClassName('activeMenuLink');
			menu_links[i].addClassName('menuLink');
		}

		element.removeClassName('menuLink')
		element.addClassName('activeMenuLink')
	}
}

menu = new Menu();


/*
Messages dropdown
*/
DropdownController = function()
{
	this.controls = Array();
	
	this.addControl = function(element_id, target, page, closed)	
	{
		if(closed == null)
			closed = true;
		
		var control = Object();
		control.target = target;
		control.page   = page;	
		control.closed = closed;
		control.loaded = false;
		
		this.controls[element_id] = control;
		
		//Load content
		if(closed)
			$(target).hide();
			
		new Ajax.Updater(control.target, 'ajax.php?page=' + control.page, {evalScripts:true});
	}
	
	this.toggle = function(element_id)
	{
		var control = this.controls[element_id];	
		
		if(control.closed)
		{
			Effect.BlindDown(control.target);
			this.controls[element_id].closed = false;
		}
		else
		{
			Effect.BlindUp(control.target);
			this.controls[element_id].closed = true;
		}
	}
	
	this.update = function(element_id, param)
	{
		var control = this.controls[element_id];

		new Ajax.Updater(control.target, 'ajax.php?page='+ control.page + param, {evalScripts:true})
	}
}

dropdown_controller = new DropdownController();



/*
Page
*/



Page = function()
{
	this.pages = new Array();
	this.title_update = '';
	this.last_id;

	this.showLoadingIcon = function(id)
	{
		$(id).innerHTML = '<div style="width:100%"><img src="' + AJAX_LOADING_IOON + '" /></div>';
	}
	
	this.logout = function()
	{
		new Ajax.Updater('page_content', 'ajax.php?page=26', {evalScripts:true});
	}
	
	
	/*
	Load basic elements of the page
	*/	
	this.loadUserSummary = function()
	{
		this.showLoadingIcon('user_summary');
			
		new Ajax.Updater('user_summary', 'ajax.php?block_id=41', {evalScripts:true});		
	}
	
	this.loadGMap = function()
	{
		this.showLoadingIcon('google_maps_page');
		
		new Ajax.Updater('google_maps_page', 'ajax.php?page=37', {evalScripts:true});
	}		
	
	/*
	Load a page
	*/	
	this.loadPage = function(id, params)
	{
		if(params == null)
			params = '';		
		
		this.showLoadingIcon('ajax_page');

		this.last_id  = id;
	
		new Ajax.Updater('ajax_page', 'ajax.php?title=1&page=' + id + params, {evalScripts:true});
		
		return false;
	}
	
	/*
	Reload the current page with different parameters
	*/
	this.reloadPage = function(params)
	{
		if(params == null)
			params = '';				
		
		this.loadPage(this.last_id, params);
	}
	
	/*
	Set the title for this page and for the callbacks
	*/	
	this.setTitle = function(id, title)
	{
		this.title_update = title;
		SWFAddress.setValue(title);	
	}	
	
	/*
	Send a form to a page
	*/
	this.sendForm = function(formname, id)
	{
		if(id == null)
			id = this.last_id;
			
		var params = Form.serialize(formname);	
		
		$('ajax_page').innerHTML = '<div style="width:100%"><img src="' + AJAX_LOADING_IOON + '" /></div>';

		new Ajax.Updater('ajax_page', 'ajax.php?title=1&page=' + id, {evalScripts: true, parameters: params, method: 'POST'});
	}	
	
	/*
	Backlink handling
	*/
	this.handleChange = function(event)
	{
		if(event.path != '/' || event.path == "")
		{	
			
			if(event.path == page.title_update || event.path == '/' + page.title_update)
			{
				page.title_update = '';				
			}
			else
			{
				$('ajax_page').innerHTML = '<div style="width:100%"><img src="' + AJAX_LOADING_IOON + '" /></div>';
	
				var params = '?title=1';
	
				for(var i =0; i < event.parameterNames.length; i++)
				{
					params += '&' + event.parameterNames[i] + '=' + event.parameters[event.parameterNames[i]];	
					
					if(event.parameterNames[i] == 'scid')
						show_call(event.parameters[event.parameterNames[i]]);
				}					
	
				new Ajax.Updater('ajax_page', 'ajax' + event.path + params, {evalScripts:true});
			}	
		}
	}
	
	/*
	Load a page in the jscript console
	*/
	this.loadScriptPage = function(id, params)
	{
		if(params == null)
			params = '';		
	
		new Ajax.Updater('jscript_console', 'ajax.php?page=' + id + params, {evalScripts:true});
	}	
	
	this.loadScriptBlock = function(id, params)
	{
		if(params == null)
			params = '';		
	
		new Ajax.Updater('jscript_console', 'ajax.php?block_id=' + id + params, {evalScripts:true});
	}		
	
	this.updateBlock = function(id, params)
	{			
		this.showLoadingIcon('block_' + id);
		new Ajax.Updater('block_' + id, 'ajax.php?block_id=' + id + params, {evalScripts:true});		
	}
	
}

var page = new Page();

/*
Functions for image uploading
*/
ImageUploader = function()
{
	this.start = function()
	{
		$('upload_avatar_form').hide();
		$('upload_avatar_form_waiting').show();				
	}
	
	this.complete = function(succes, msg)
	{
		$('upload_avatar_form').show();
		$('upload_avatar_form_waiting').hide();	
		
		if(succes)
		{
			popup.close();
			page.loadUserSummary();
		}
		else
		{
			if(msg == null)
				msg = '';
			
			$('upload_avatar_form_error').innerHTML = msg;
			$('upload_avatar_form_error').show();		
		}		
	}	
}

var image_uploader = new ImageUploader();


//SWF Adress callback
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, page.handleChange);

/*
Menu
*/
MainMenu = function()
{
	this.items = new Array();
	
	this.addItem = function(id)
	{
		alert('decrepated function: MainMenu.addItem');
		this.items.push(id);	
	}
	
	this.setItem = function(id)
	{		
		alert('decrepated function: MainMenu.setItem');
		
		for(var i = 0; i < this.items.length; i++)
		{					
			if(this.items[i] == id)
				$('menuitem_' + this.items[i]).className = 'tab_active';
			else
				$('menuitem_' + this.items[i]).className = 'tab_inactive';
		}	
	}	

}

var main_menu = new MainMenu();


var init_done = false;
var popup_x = 0;;
var popup_y = 0;
var popup_width = 0;
var popup_onstart_pagesize;

/*
Call functions
*/

var current_call_link;

function show_call(id)
{
	current_call_link = 'gmap/get_call.php?id=' + id;

	return popup.show(current_call_link, 'Viziertje bekijken', true) 
}

function pay_for_call(id)
{
	var url = current_call_link + '&p=1';

	return popup.show(url, 'Oproep bekijken') 
}
	
function bookmark_call(id)
{
	$('make_bookmark_button_' + id).hide();	
	
	new Ajax.Request('ajax.php?page=50&cid=' + id);
}


/*
Cluster parameters and functions
*/

var cluster_current_page  = 0;
var cluster_current_id    = '';
var cluster_current_count = '';

function show_cluster(count, id)
{
	cluster_current_page  = 0;
	cluster_current_id    = id;
	cluster_current_count = count;
	
	var url = 'gmap/get_cluster.php?id=' + id + '&page=' + cluster_current_page + '&cnt='+cluster_current_count+'&filter=' + lastFilter;
	
	popup.show(url, (count + ' viziertjes')) 
}



function cluster_next_page()
{
	cluster_current_page++;
	var url = 'gmap/get_cluster.php?id=' + cluster_current_id + '&page=' + cluster_current_page + '&cnt='+cluster_current_count+'&filter=' + lastFilter;
	
	popup.load(url)
}

function cluster_prev_page()
{
	cluster_current_page--;
	var url = 'gmap/get_cluster.php?id=' + cluster_current_id + '&page=' + cluster_current_page + '&cnt='+cluster_current_count+'&filter=' + lastFilter;
	
	popup.load(url)	
}

function cluster_linkback()
{
	var url = 'gmap/get_cluster.php?id=' + cluster_current_id + '&page=' + cluster_current_page + '&cnt='+cluster_current_count+'&filter=' + lastFilter;
	popup.setTitle(cluster_current_count + ' markers');
	popup.load(url);	
}

function cluster_show_call(id)
{
	current_call_link = 'gmap/get_call.php?id=' + id + '&bl=1';

	popup.setTitle('Oproep bekijken');
	popup.load(current_call_link);	
}


/*
Popup functions
*/

Popup = function()
{
	this.last_url;
	this.current_layer = 0;
	
	//var for the reload function
	this.last_page_id;
	this.last_title;
	
	this.init = new Array();
	
	/*
	Create a popup layer
	*/	
	this.createLayer = function(layer)
	{  
		var objBody = document.getElementsByTagName('body')[0];
		
		//Create background fader
		var div = document.createElement('div');
		div.id = 'background_overlay_' + layer;	
		div.className = 'background_overlay';	
		div.style.zIndex = 90 + (100 * layer);
		
		objBody.appendChild(div);	
	
		//Create popup
		var popup = document.createElement('div');
		popup.id = 'popup_window_' + layer;
		popup.className = 'popup_window';
		popup.style.zIndex = 100 + (100 * layer);;
	
		var html  = '<table width="100%" cellspacing="0" cellpadding="0">';
		html += '<tr> <td class="lt"></td> <td class="t" valign="bottom"> <div onmousedown="dragStart(event, \'popup_window_'+layer+'\')"  class="title_bar"><div class="popup_title" id="popup_title_'+layer+'"> </div> <div onclick="popup.close('+layer+')" class="close">  </div></div>  </td> <td class="tr"></td> </tr>';
		html +=	'<tr> <td class="l"></td>  <td class="m"><div class="popup_body" id="popup_body_'+layer+'"></div></td>  <td class="r"></td> </tr>';
		html +=	'<tr> <td class="bl"></td> <td class="b"></td> <td class="br"></td> </tr>';
		html +=	'</table>';
	
		popup.innerHTML = html;
	
		objBody.appendChild(popup);	
	
		this.init[layer] = true;		
	}
	
	/*
	Show a popup
	*/
	this.show = function(url, title, newLayer, width)
	{
		this.last_url = url;
					
		if(title == null)
			title = '';

		if(newLayer == null)
			newLayer = false;		
		
		if(width == null)
			width = 500;
		
		if(newLayer || this.current_layer == 0)
			this.current_layer++;

		layer = this.current_layer;
		
		//Get page size
		var sizes = getPageSize()
	
		//Generate popup
		if(popup.init[layer] == null)
		{
			popup.createLayer(layer);
		}
		
		//Set position
		popup_x = ((sizes[0] - width) / 2);
	
	    if (browser.isIE) 
	    { 
		 popup_y = document.documentElement.scrollTop + document.body.scrollTop;
	    }
	    else if (browser.isNS) {
		 	popup_y =  window.scrollY;
	    }
	  
	    popup_y += 50;
	
		popup_width = width;
		popup_onstart_pagesize = sizes;
	
	
		//Set background fader	
		var elem  = $('background_overlay_' + layer);
	
		elem.style.display = 'block';
		elem.style.height  = sizes[1] + 'px';
	
		var opacity = 90;

		elem.style.filter = "alpha(opacity:"+opacity+")"; // IE/Win
		elem.style.KHTMLOpacity = opacity/100; // Safari<1.2, Konqueror
		elem.style.MozOpacity = opacity/100; // Older Mozilla and Firefox
		elem.style.opacity = opacity/100; // Safari 1.2, newer Firefox and Mozilla, CSS3	
   
   
		//Popup Title
		this.setTitle(title, layer);

		//Popup Body
		popup.load(url, layer);

		//Show popup
		var elem  = $('popup_window_' + layer);
		elem.style.display = 'block';
		elem.style.width   = width + 'px';
		elem.style.left    = popup_x + 'px';
		elem.style.top     = popup_y + 'px';   

		return false;		
	}
	
	/*
	Set a title for a popup
	*/
	this.setTitle = function(title, layer)
	{
		if(layer == null)
			layer = this.current_layer;		
	
		var elem  = $('popup_title_' + layer);
		elem.innerHTML = title;		
	}
	
	
	/*
	Load a url into an existing popup
	*/
	this.load = function(url, layer)
	{
		if(layer == null)
			layer = this.current_layer;				

		this.last_url = url;

		var elem  = $('popup_body_' + layer);
		elem.innerHTML = '<div style="width:100%; text-align:center"><img src="' + AJAX_LOADING_IOON + '" /></div>';

		new Ajax.Updater('popup_body_' + layer, url, {evalScripts: true});	
		
		return false;
	}	


	/*
	Load a page in a popup
	*/
	this.loadPage = function(id, title, newLayer, params, width)
	{
		if(params == null)
			params = '';
			
		if(newLayer == null)
			newLayer = false;			
		
		this.last_page_id = id;
		this.last_title   = title;
		
		return this.show('ajax.php?page=' + id + params, title, newLayer, width);		
	}
	
	this.reloadPage = function(params)
	{
		this.loadPage(this.last_page_id, this.last_title, false, params)
	}
	
	/*
	Send a form
	*/
	this.sendForm = function(formname, url)
	{
		layer = this.current_layer;	
	
		if(url == null)
			url = this.last_url;
			
		var params = Form.serialize(formname);	
		
		var elem  = $('popup_body_' + layer);
		elem.innerHTML = '<div style="width:100%; text-align:center"><img src="' + AJAX_LOADING_IOON + '" /></div>';

		new Ajax.Updater('popup_body_' + layer, url, {evalScripts: true, parameters: params, method: 'POST'});			
	}
	
	/*
	Close a popup
	*/
	this.close = function(layer)
	{
		if(layer == null)
			layer = this.current_layer;		
		
	   //Hide background fader	
	   var elem  = $('background_overlay_' + layer);
	   elem.style.display = 'none';
		
	   //Hide popup
	   var elem  = $('popup_window_' + layer);
	   elem.style.display = 'none';   
	   
	   //Clear popup body
		$('popup_body_' + layer).innerHTML = '';
	   
	   this.current_layer--;
	}
	
	/*
	Close all popups
	*/
	this.closeAll = function()
	{
		for(var i = this.current_layer; i > 0; i--)
		{
			this.close(i);
		}
	}
}

var popup = new Popup();

/*
function init()
{
	alert('decrepated function: init')
	
	var objBody = document.getElementsByTagName('body')[0];
	
	//Create background fader
	var div = document.createElement('div');
	div.id = 'background_overlay';	
	
    objBody.appendChild(div);	

	//Create popup
	var popup = document.createElement('div');
	popup.id = 'popup_window';

	var html  = '<table width="100%" cellspacing="0" cellpadding="0">';
	html += '<tr> <td class="lt"></td> <td class="t" valign="bottom"> <div onmousedown="dragStart(event, \'popup_window\')"  class="title_bar"><div id="popup_title"> </div> <div onclick="close_popup()" class="close">  </div></div>  </td> <td class="tr"></td> </tr>';
	html +=	'<tr> <td class="l"></td>  <td class="m"><div id="popup_body"></div></td>  <td class="r"></td> </tr>';
	html +=	'<tr> <td class="bl"></td> <td class="b"></td> <td class="br"></td> </tr>';
	html +=	'</table>';

	popup.innerHTML = html;

    objBody.appendChild(popup);	

	init_done = true;	
}



function close_popup(layer)
{
	alert('decrepated function: close_popup');
	
	if(layer == null)
		layer = 1;		
	
   //Hide background fader	
   var elem  = $('background_overlay_' + layer);
   elem.style.display = 'none';
	
   //Hide popup
   var elem  = $('popup_window_' + layer);
   elem.style.display = 'none';   
}

function popup_set_title(title, layer)
{
	alert('decrepated function: popup_set_title');	
	
	if(layer == null)
		layer = 1;		

	var elem  = $('popup_title_' + layer);
	elem.innerHTML = title;
}

function show_popup(title, url, layer, width)
{
	alert('decrepated function: show_popup');	

	if(layer == null)
		layer = 1;	
	
	var sizes = getPageSize()
	
	if(popup.init[layer] == null)
	{
		popup.createLayer(layer);
		
		popup_x = ((sizes[0] - width) / 2);
		popup_y = 50;
	}
	
	popup_width = width;
	popup_onstart_pagesize = sizes;
	
	
	//Set background fader	
 	var elem  = $('background_overlay_' + layer);
	
	elem.style.display = 'block';
	elem.style.height  = sizes[1] + 'px';
	
	var opacity = 90;

	elem.style.filter = "alpha(opacity:"+opacity+")"; // IE/Win
    elem.style.KHTMLOpacity = opacity/100; // Safari<1.2, Konqueror
    elem.style.MozOpacity = opacity/100; // Older Mozilla and Firefox
    elem.style.opacity = opacity/100; // Safari 1.2, newer Firefox and Mozilla, CSS3	
   
   
	//Popup Title
	popup_set_title(title, layer);

	//Popup Body
	popup_load(url, layer);

	//Show popup
	var elem  = $('popup_window_' + layer);
	elem.style.display = 'block';
	elem.style.width   = width + 'px';
	elem.style.left    = popup_x + 'px';
	elem.style.top     = popup_y + 'px';   


   
   return false;
}


function popup_load(url, layer)
{
	alert('decrepated function: popup_load');	
	
	if(layer == null)
		layer = 1;		
	
	var elem  = $('popup_body_' + layer);
	elem.innerHTML = '<div style="width:100%; text-align:center"><img src="' + AJAX_LOADING_IOON + '" /></div>';

	new Ajax.Updater('popup_body_' + layer, url, {evalScripts: true});	
	
	return false;
}


function fadeIn2(opacity, layer) 
{   
	alert('decrepated function: fadeIn2');
   
   if (opacity <= 100) 
   {
        boks = $('background_overlay_' + layer);
		boks.style.filter = "alpha(opacity:"+opacity+")"; // IE/Win
        boks.style.KHTMLOpacity = opacity/100; // Safari<1.2, Konqueror
        boks.style.MozOpacity = opacity/100; // Older Mozilla and Firefox
        boks.style.opacity = opacity/100; // Safari 1.2, newer Firefox and Mozilla, CSS3
        opacity += 10;
        window.setTimeout("fadeIn2("+opacity+", "+layer+")");
    }
}

*/


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize()
{
		
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	// for small pages with total height less then height of the viewport
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}



/*
Drag functions:
*/

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();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

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);
  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.

  var mp = getMousePosition(event);
  var x = mp[0];
  var y = mp[1];

  // 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;

  // Capture mousemove and mouseup events on the page.

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


function getMousePosition(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;
  }
	
  return [x,y]	
}

function dragGo(event) {

  var mp = getMousePosition(event);
  var x = mp[0];
  var y = mp[1];	

  // Move drag element by the same amount the cursor has moved.

	var sizes = popup_onstart_pagesize; 
	
	var newX = dragObj.elStartLeft + x - dragObj.cursorStartX;
	var newY = dragObj.elStartTop  + y - dragObj.cursorStartY;

	if((newX + 14) > 0 && (newX + popup_width) < sizes[0] && (newY + 13) > 0 && (newY + 42) < sizes[1])
	{
		popup_x = newX;
		popup_y = newY;

		dragObj.elNode.style.left = newX + "px";
		dragObj.elNode.style.top  = newY + "px";
	}


  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // 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);
  }
}

function toConsole(text)
{
	if($('jscript_console'))
		$('jscript_console').innerHTML = text + '<br/>' + $('jscript_console').innerHTML;
}