// adv-CMS - Copyright (c) Pete Calvert 2006
// Filename : lib/resize.js
// Function : Resizable Areas (horizontal only)

var drag_objects = [];
var drag_current;
var container_width;

// Mozilla Event Fixes
document.addEventListener('mousedown', function(info) {window.event = info;}, true);
document.addEventListener('mousemove', function(info) {window.event = info;}, true);
document.addEventListener('mouseup', function(info) {window.event = info;}, true);

// Drag functions
function drag_init(id, element, formdata)
{	
	this.element	= element;
	this.formdata	= formdata;
	this.min_width	= 10;
	this.max_width	= 100;
	this.st_width;
	this.st_mouse;
	
	this.constructor = function() {
		this.id = drag_objects.length;
		drag_objects[this.id] = this;
	}
	
	this.constructor();
}

function drag_start(id)
{
	drag_current = id;
	
	container_width = document.getElementById('content').offsetWidth;
	
	drag_objects[drag_current].st_width = drag_objects[drag_current].element.offsetWidth;
	drag_objects[drag_current].st_mouse = event.clientX;
	
	// Internet Explorer
	if(document.all)
	{
		document.body.attachEvent('onmousemove', drag_resize);
		document.body.attachEvent('onmouseup', drag_end);
	}
	// Mozilla etc.
	else
	{
		document.addEventListener('mousemove', drag_resize, false);
		document.addEventListener('mouseup', drag_end, false);
	}
}

function drag_resize()
{
	var new_width = drag_objects[drag_current].st_width + (event.clientX - drag_objects[drag_current].st_mouse);
	
	new_width = Math.round((new_width * 100) / container_width);
	
	if(new_width < drag_objects[drag_current].min_width)
	{
		new_width = drag_objects[drag_current].min_width;
	}
	
	if(new_width > drag_objects[drag_current].max_width)
	{
		new_width = drag_objects[drag_current].max_width;
	}
	
	drag_objects[drag_current].element.style.width = new_width + '%';
}
	
function drag_end()
{
	drag_objects[drag_current].formdata.setAttribute('value', drag_objects[drag_current].element.style.width);
	
	// Internet Explorer
	if(document.all)
	{
		document.body.detachEvent('onmousemove', drag_resize);
		document.body.detachEvent('onmouseup', drag_end);
	// Mozilla etc.
	} else {
		document.removeEventListener('mousemove', drag_resize, false);
		document.removeEventListener('mouseup', drag_end, false);
	}
}