/***
 * Creates new filter box
 * For example for supplier or clinic filter
 * Needs functions.js and requests.js
 */
function box(boxId, boxImg, boxWidth, boxHeight, boxTop, boxLeft, withOverlay) {
	
	// Set box
	this.divbox = document.createElement('DIV');
	this.divbox.id = 'box' + boxId;
	this.divbox.name = 'box' + boxId;
	this.divbox.setAttribute('id', 'box' + boxId);
	this.divbox.setAttribute('name', 'box' + boxId);
	
	var bodies = document.getElementsByTagName('BODY');
	bodies[0].insertBefore(this.divbox, bodies[0].firstChild);

	if(withOverlay == '1' || withOverlay == true) {
	    var viewportSize = Client.getViewportSize();
		
		this.overlaybox = document.createElement('DIV');
	    this.overlaybox.id = 'boxOverlay' + boxId;
	    this.overlaybox.name = 'boxOverlay' + boxId;
	    this.overlaybox.setAttribute('id', 'boxOverlay' + boxId);
	    this.overlaybox.setAttribute('name', 'boxOverlay' + boxId);
	    this.overlaybox.className = 'boxOverlay';
	    
		var bodies = document.getElementsByTagName('BODY');
		bodies[0].insertBefore(this.overlaybox, bodies[0].firstChild);
		
		this.lastOverflowState = GetStyle(bodies[0], 'overflow');
	} else {
		this.overlaybox = null;
	}

	if(this.divbox == null) {
		alert('Box "'+boxId+'" not found!');
		return;
	}

	if(boxWidth == null || typeof boxWidth == 'undefined' || boxWidth == '') {
		boxWidth = '';
	}
	if(boxHeight == null || typeof boxHeight == 'undefined' || boxHeight == '') {
		boxHeight = '';
	}
	if(boxTop == null || typeof boxTop == 'undefined' || boxTop == '') {
		boxTop = 0;
	}
	if(boxLeft == null || typeof boxLeft == 'undefined' || boxLeft == '') {
		boxLeft = 0;
	}

	this.width	= boxWidth;
	this.height	= boxHeight;
	this.top	= boxTop;
	this.left	= boxLeft;
	this.closed = true;

	// Init open image
	this.openIcon	= GetImage(boxImg);

	var parent	= this;
	this.parent = parent;

	if(parent.overlaybox) {
		parent.overlaybox.onclick = function() { parent.closeBox(); };
	}

	if(this.openIcon != null) {
		this.openIcon.onmouseover	= function() { switchCursor(this, '1'); };
		this.openIcon.onmouseout	= function() { switchCursor(this, '0'); };
	}
}

// Set style
box.prototype.setBoxStyles= function (styles) {
	var parent = this.parent;
	
	styles = styles.replace(/: /g, ":");

	var arrStyles = styles.split(";");
	for(var s=0;s<arrStyles.length;s++) {
		var style = arrStyles[s];
		var arrStyle = style.split(":");
		if(arrStyle[0] && arrStyle[1] && arrStyle[0] != '' && arrStyle[1] != '') {
			arrStyle[0] = arrStyle[0].replace(/-q/g, "Q");
			arrStyle[0] = arrStyle[0].replace(/-w/g, "W");
			arrStyle[0] = arrStyle[0].replace(/-e/g, "E");
			arrStyle[0] = arrStyle[0].replace(/-r/g, "R");
			arrStyle[0] = arrStyle[0].replace(/-t/g, "T");
			arrStyle[0] = arrStyle[0].replace(/-z/g, "Z");
			arrStyle[0] = arrStyle[0].replace(/-u/g, "U");
			arrStyle[0] = arrStyle[0].replace(/-i/g, "I");
			arrStyle[0] = arrStyle[0].replace(/-o/g, "O");
			arrStyle[0] = arrStyle[0].replace(/-p/g, "P");
			arrStyle[0] = arrStyle[0].replace(/-a/g, "A");
			arrStyle[0] = arrStyle[0].replace(/-s/g, "S");
			arrStyle[0] = arrStyle[0].replace(/-d/g, "D");
			arrStyle[0] = arrStyle[0].replace(/-f/g, "F");
			arrStyle[0] = arrStyle[0].replace(/-g/g, "G");
			arrStyle[0] = arrStyle[0].replace(/-h/g, "H");
			arrStyle[0] = arrStyle[0].replace(/-j/g, "J");
			arrStyle[0] = arrStyle[0].replace(/-k/g, "K");
			arrStyle[0] = arrStyle[0].replace(/-l/g, "L");
			arrStyle[0] = arrStyle[0].replace(/-y/g, "Y");
			arrStyle[0] = arrStyle[0].replace(/-x/g, "X");
			arrStyle[0] = arrStyle[0].replace(/-c/g, "C");
			arrStyle[0] = arrStyle[0].replace(/-v/g, "V");
			arrStyle[0] = arrStyle[0].replace(/-b/g, "B");
			arrStyle[0] = arrStyle[0].replace(/-n/g, "N");
			arrStyle[0] = arrStyle[0].replace(/-m/g, "M");
			SetStyle(parent.divbox, arrStyle[0], arrStyle[1]);
		}
	}
};

// Close box
box.prototype.isClosed= function () {
	var parent = this.parent;
	
	return parent.closed;
};

// Close box
box.prototype.closeBox= function () {
	var parent = this.parent;
	
	SwitchLayer(parent.divbox, 0);
	parent.closed = true;

	if(parent.overlaybox) {
		SwitchLayer(parent.overlaybox, 0);

		var bodies = document.getElementsByTagName('BODY');
		SetStyle(bodies[0], 'overflow', this.lastOverflowState);
	}

	//showSelect();

	if(parent.openIcon != null) {
		parent.openIcon.src = parent.openIcon.src.replace(/open/gi, 'close');
	}
};

box.prototype.switchBox = function (e) {
	var parent = this.parent;

	if(parent.divbox.closed) {
		parent.showBox();
	} else {
		parent.closeBox();
	}
};

box.prototype.showBox = function (e) {
	var parent = this.parent;

	var left = parseInt(parent.left);
	var top = parseInt(parent.top);

	var viewportSize = Client.getViewportSize();

	var body = document.getElementsByTagName("body")[0];
	if(body && top == 0 && left == 0) {
		left = (viewportSize[0] / 2 - parent.width / 2);
		top = (viewportSize[1] / 2 - parent.height / 2 + Client.getPageOffsetY());
	}

	if(parent.openIcon != null) {
		left = left + parseInt(GetX(parent.openIcon));
		top = top + parseInt(GetY(parent.openIcon));
	
		parent.openIcon.src = parent.openIcon.src.replace(/close/gi, 'open');
	}

	SetStyle(parent.divbox, 'left', left + 'px');
	SetStyle(parent.divbox, 'top', top + 'px');

	if(parent.width != '')
		SetStyle(parent.divbox, 'width', parseInt(parent.width) + 'px');
	if(parent.height != '')
		SetStyle(parent.divbox, 'height', parseInt(parent.height) + 'px');
		
	SwitchLayer(parent.divbox, 1);
	parent.closed = false;

	if(parent.overlaybox) {
	    SetStyle(parent.overlaybox, 'position', "absolute");
	    SetStyle(parent.overlaybox, 'width', "100%");
	    SetStyle(parent.overlaybox, 'height', "100%");
	    SetStyle(parent.overlaybox, 'left', "0px");
	    SetStyle(parent.overlaybox, 'top', Client.getPageOffsetY() + "px");
		SetStyle(parent.overlaybox, 'zIndex', parseInt(GetStyle(parent.divbox, 'zIndex')) -1);
		Util.SetOpacity(parent.overlaybox, 50);

		SetStyle(body, 'overflow', "hidden");
		
		SwitchLayer(parent.overlaybox, 1);
	}

	//hideSelect();
};

box.prototype.getBoxContent = function (e) {
	var parent = this.parent;

	return GetContent(parent.divbox);
};

