/*

	Manipulate with popup layers



	Usage:

		onclick = "javascript: Fire_PopupLayer('layer_id_2open', event, 'click' [, 400, 200, 100, someFunction, false/true])"

	Requires:

		1. utils.js



	// author: Alexey Karpinskiy

	// karpinskiy@kppublications.com



*/



var servicesPopupObjArray = new Array();



function Fire_PopupDocLayer(id, event, action, docLeftPos, docTopPos, zIndex, extraCloseEventCallback, stopAutoClose) {



	var servicesPopup = null;

	zIndex = (zIndex) ? parseInt(zIndex) : parseInt(Get_ElementCurrentStyle(document.getElementById(id), 'z-index'));



	event = (event) ? event : window.event;

	var element = (event.target) ? event.target : event.srcElement;



	var defaultLeftDeviation = 0;

	var defaultTopDeviation = 15;

	

	var elementCoords_X = (docLeftPos) ? parseInt(docLeftPos) : (Find_AbsoluteDocObjCoord_X(element) + defaultLeftDeviation);

	var elementCoords_Y = (docTopPos) ? parseInt(docTopPos) : (Find_AbsoluteDocObjCoord_Y(element) + defaultTopDeviation);



	for (var i=0; i < servicesPopupObjArray.length; i++) {

		var obj = servicesPopupObjArray[i];

		if (obj.id == id) {

			servicesPopup = obj;

			//break;

		}

		else if(action == obj.action) {

			obj.Close_AllServicesPopup();

		}

	}

	if (servicesPopup) {

		servicesPopup.Update_Properties(id, event, action, elementCoords_X, elementCoords_Y, zIndex, extraCloseEventCallback, stopAutoClose);

	}

	else {

		servicesPopup = new AbsLayerPopup(id, event, action, elementCoords_X, elementCoords_Y, zIndex, extraCloseEventCallback, stopAutoClose);

		servicesPopup.Create_AlphaBgLayer();

		servicesPopupObjArray[servicesPopupObjArray.length] = servicesPopup;

	}

	servicesPopup.Control_LayerPopup();

	return false;

}



function AbsLayerPopup(id, event, action, docLeftPosition, docTopPosition, zIndex, extraCloseEventCallback, stopAutoClose) {



	this.id = id;

	this.alphaBgPopupLayer = 'alphaBg_'+Math.round(Math.random()*1000000);

	this.event = (event) ? event : window.event;

	this.action = action;

	this.topPos = (docTopPosition) ? parseInt(docTopPosition) : 0;

	this.leftPos = (docLeftPosition) ? parseInt(docLeftPosition) : 0;

	this.zIndex = parseInt(zIndex);

	this.callback = extraCloseEventCallback;

	this.stopAutoClose = (stopAutoClose) ? true : false;



	var this_closure = this;



	this.Update_Properties = function(id, event, action, docLeftPosition, docTopPosition, zIndex, extraCloseEventCallback, stopAutoClose) {

		this.id = id;

		this.event = (event) ? event : window.event;

		this.action = action;

		this.topPos = (docTopPosition) ? parseInt(docTopPosition) : 150;

		this.leftPos = (docLeftPosition) ? parseInt(docLeftPosition) : 150;

		this.zIndex = parseInt(zIndex);

		this.callback = extraCloseEventCallback;

		this.stopAutoClose = (stopAutoClose) ? true : false;

		this_closure = this;

	}

	/*

		Should be called after object creation

		Intent: adds opacity layer with lower z-index of existing popup layer to prevent lundertow layer's events

	*/

	this.Create_AlphaBgLayer = function() {

		var ELEMENT_NODE = 1;

		var styleAlphaBg = new Array(

			'position:absolute',

			'background-color: #FFF',

			'filter:alpha(opacity=0)', 'moz-opacity:0', 'opacity:0',

			'display:none', 'visibility:hidden'

		);

		if (document.getElementById(this.id).nodeType == ELEMENT_NODE) {

			var parentNode = document.getElementById(this.id).parentNode;

			if(parentNode != null) {

				var alphaLayer = document.createElement('DIV');

				alphaLayer.id = this.alphaBgPopupLayer;

				alphaLayer.style.cssText = styleAlphaBg.join(';');

				parentNode.appendChild(alphaLayer);

			}

		}

	}

	this.Control_LayerPopup = function () {

		var layer = document.getElementById(this.id); if (!layer) return false;

		Cancel_EventPropagation(null, this.event);

		var element = (this.event.target) ? this.event.target : this.event.srcElement;

		if (element.blur) {element.blur();}	// fix: remove annoying link border



		layer.style.top = this.topPos+'px';

		layer.style.left = this.leftPos+'px';

		layer.style.zIndex = this.zIndex;

		Change_LayerVisibility(this.id);



		// Background layer with lower z-index value to make undertow layers not active

		var alphaLayer = document.getElementById(this.alphaBgPopupLayer);

		if (alphaLayer) {

			alphaLayer.style.left = parseInt(this.leftPos)+'px';

			alphaLayer.style.top = parseInt(this.topPos)+'px';

			alphaLayer.style.width = parseInt(layer.offsetWidth)+'px';

			alphaLayer.style.height = parseInt(layer.offsetHeight)+'px';

			alphaLayer.style.zIndex = parseInt(layer.style.zIndex)-1;

			Change_LayerVisibility(this.alphaBgPopupLayer);

		}

		if (this.stopAutoClose == false) {

			Attach_EventListener(document, this.action, this.Close_AllServicesPopup, false);

		}

		Attach_EventListener(layer, this.action, this.Cancel_Close_AllServicesPopup, false);

		

	}

	this.Close_AllServicesPopup = function(event) {

		Change_LayerVisibility(this_closure.id, false, true);

		if (document.getElementById(this_closure.alphaBgPopupLayer)) Change_LayerVisibility(this_closure.alphaBgPopupLayer, false, true);

		Detach_EventListener(document, this_closure.action, this_closure.Close_AllServicesPopup, false);

		Detach_EventListener(document.getElementById(this_closure.id), this_closure.action, this_closure.Cancel_Close_AllServicesPopup, false);

		if (typeof(this_closure.callback) == 'function') {this_closure.callback(event);}

	}

	this.Cancel_Close_AllServicesPopup = function(event) {

		event = (event) ? event : window.event;

		var element = (event.target) ? event.target : event.srcElement;

		if (element != document.getElementById(this_closure.id)) {

			Cancel_EventPropagation(document, event, (cancelDefault=false));

		}

	}

}

