/*

 dlib layers extension library.

*/

/* config */
cfg_layers_plugins_path="js/dlib/layers/plugins/";
cfg_layers_img_path="js/dlib/layers/img/";

/* overide config by setting config variables in document object */
if(document.cfg_layers_plugins_path) cfg_layers_plugins_path=document.cfg_layers_plugins_path;
if(document.cfg_layers_img_path) cfg_layers_plugins_path=document.cfg_layers_img_path;
if(document.cfg_prefix_path) {
	cfg_layers_plugins_path=document.cfg_prefix_path+cfg_layers_plugins_path;
	cfg_layers_img_path=document.cfg_prefix_path+cfg_layers_img_path;
};

/* variables */

__EVENT_LAYER_MASK_LAST_ZINDEX__=0;

// loading needing plugin
load_required_lib (cfg_layers_plugins_path, __CURRENT_NAVIGATOR__+"layers.js");

// methods

/*  get and set visibility status of a layer.
    intValue must only have an interger or boolean value :
	true : show layer
	false : hide layer
*/
dlib.object.prototype.visible=function (intValue){
        if(dlib.debugLog(this.type+"s.visible", this.name, false,  (this.type=="layer")?true:false , 8, true)){
		if(arguments.length){
			dlib.setLayerVisibilityStatus(this.object, (intValue)?true:false);
		}
		return dlib.debugLog(this.type+"s.visible", this.name, dlib.getLayerVisibilityStatus(this.object),  true, false, false, true);
	}
	return false;
};

/* get and set value of layers top property */
dlib.object.prototype.top=function (intValue){
	if(arguments.length && dlib.debugLog(this.type+"s.top : "+intValue, this.name, false,  isInteger(intValue), 3, true)) {
		if(intValue.toString().indexOf('add')>-1) intValue=dlib.getLayerTop(this.object)+parseInt(intValue);
		dlib.setLayerTop(this.object, intValue);
	}
	return dlib.debugLog(this.type+"s.top", this.name, dlib.getLayerTop(this.object),  true, false, false, true);
};

/* get and set value of layers left property */
dlib.object.prototype.left=function (intValue){
	if(arguments.length && dlib.debugLog(this.type+"s.left : "+intValue, this.name, false,  isInteger(intValue), 3, true)) {
		if(intValue.toString().indexOf('add')>-1) intValue=dlib.getLayerLeft(this.object)+parseInt(intValue);
		dlib.setLayerLeft(this.object, intValue);
	}
	return dlib.debugLog(this.type+"s.left", this.name, dlib.getLayerLeft(this.object),  true, false, false, true);
};

/* get and set value of layers width  property */
dlib.object.prototype.width=function (intValue){
	if(dlib.debugLog(this.type+"s.width", this.name, false,  (this.type=="layer")?true:false , 8, true)){
		if(arguments.length && dlib.debugLog(this.type+"s.width : "+intValue, this.name, false,  isInteger(intValue), 3, true)) {
			if(intValue.toString().indexOf('add')>-1) intValue=dlib.getLayerWidth(this.object)+parseInt(intValue);
			dlib.setLayerWidth(this.object, intValue);
		}
		return dlib.debugLog(this.type+"s.width", this.name, dlib.getLayerWidth(this.object),  true, false, false, true);
	}
	return false;
};

/* get and set value of layers height property */
dlib.object.prototype.height=function (intValue){
	if(dlib.debugLog(this.type+"s.height", this.name, false,  (this.type=="layer")?true:false , 8, true)){
		if(arguments.length && dlib.debugLog(this.type+"s.height : "+intValue, this.name, false,  isInteger(intValue), 3, true)) {
			if(intValue.toString().indexOf('add')>-1) intValue=dlib.getLayerHeight(this.object)+parseInt(intValue);
			dlib.setLayerHeight(this.object, intValue);
		}
		return dlib.debugLog(this.type+"s.height", this.name, dlib.getLayerHeight(this.object),  true, false, false, true);
	}
	return false;
};

/* get and set value of layers right property */
dlib.object.prototype.right=function (intValue){
	if(arguments.length && dlib.debugLog(this.type+"s.right : "+intValue, this.name, false,  isInteger(intValue), 3, true)) {
		if(intValue.toString().indexOf('add')>-1) intValue=dlib.getLayerLeft(this.object)+parseInt(intValue);
		this.left(parseInt(intValue)-this.width());
	}
	return dlib.debugLog(this.type+"s.right", this.name, this.left()+this.width(),  true, false, false, true);
};

/* get and set value of layers bottom property */
dlib.object.prototype.bottom=function (intValue){
	if(arguments.length && dlib.debugLog(this.type+"s.bottom : "+intValue, this.name, false,  isInteger(intValue), 3, true)) {
		if(intValue.toString().indexOf('add')>-1) intValue=dlib.getLayerTop(this.object)+parseInt(intValue);
		this.top(parseInt(intValue)-this.height());
	}
	return dlib.debugLog(this.type+"s.bottom", this.name, this.top()+this.height(),  true, false, false, true);
};

/* get and set values of layer clip property */
dlib.object.prototype.clip=function(intTop, intRight, intBottom, intLeft){
	if(dlib.debugLog(this.type+"s.clip", this.name, false,  (this.type=="layer")?true:false , 8, true)){
		if(arguments.length==4 && dlib.debugLog(this.type+"s.clip : "+intTop+", "+intRight+", "+intBottom+", "+intLeft, this.name, false,  (!isNaN(parseInt(intTop)) && !isNaN(parseInt(intRight)) && !isNaN(parseInt(intBottom)) && !isNaN(parseInt(intLeft)))?true:false, 3, true)) {
			intTop=(intTop=="1k")?dlib.getClip(this.object).top:(intTop.toString().indexOf("add")>-1)?dlib.getClip(this.object).top+parseInt(intTop):parseInt(intTop);
			intRight=(intRight=="1k")?dlib.getClip(this.object).right:(intRight.toString().indexOf("add")>-1)?dlib.getClip(this.object).right+parseInt(intRight):parseInt(intRight);
			intBottom=(intBottom=="1k")?dlib.getClip(this.object).bottom:(intBottom.toString().indexOf("add")>-1)?dlib.getClip(this.object).bottom+parseInt(intBottom):parseInt(intBottom);
			intLeft=(intLeft=="1k")?dlib.getClip(this.object).left:(intLeft.toString().indexOf("add")>-1)?dlib.getClip(this.object).left+parseInt(intLeft):parseInt(intLeft);
			dlib.setClip(this.object, intTop, intRight, intBottom, intLeft);
		}
        	arrClip=dlib.getClip(this.object);
		dlib.debugLog(this.type+"s.clip", this.name, arrClip.top+", "+arrClip.right+", "+arrClip.bottom+", "+arrClip.left,  true, false, false);
		return  arrClip;
	}
	return false;
};

/* create a layers */
dlib.object.prototype.create=function (strParentName){
	if(!this.exists && dlib.debugLog(this.type+"s.create", this.name, false,  (this.type=="layer")?true:false , 6, true)) {
		if(arguments.length && dlib.getElement(strParentName, "layer"))  dlib.createLayer(this.name, dlib.getElement(strParentName, "layer"));
		else dlib.createLayer(this.name);
		dlib.debugLog(this.type+"s.create", this.name, "layer made",  true, false, false);
	}
	else dlib.debugLog(this.type+"s.create", this.name, false,  false, 7,  false);
	return true;
};

/* get and set HTML content of a layer object
    Warning : HTML content must set fisrt to be get, for NS 4. */
dlib.object.prototype.content=function (strContent){
	if(dlib.debugLog(this.type+"s.content", this.name, false,  (this.type=="layer")?true:false , 8, true)){
		if(arguments.length) {
			dlib.debugLog(this.type+"s.content", this.name, "content updated",  true, false, false);
			dlib.setInnerHTML(this.object, strContent);
		}
		return dlib.getInnerHTML(this.object);
	}
	return false;
};

/* create a layer that should be used for Event mask */
__ARR_LAYERS_MASK_NAME__=[];

dlib.createEventLayerMask=function(intZIndex, intDrag, strLayerName){
        if(!arguments.length || dlib.debugLog("createEventLayerMask", "dlib", false,  isInteger(intZIndex), 3, true)) intZIndex=__EVENT_LAYER_MASK_LAST_ZINDEX__++;
	if(arguments.length<2) intDrag=false;
	if(arguments.length<3){
		 __ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]="dlibEventMaskLayer"+intZIndex;
        	dlib.layers("dlibEventMaskLayer"+intZIndex).create();
	}
	else __ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]=strLayerName;
	dlib.layers(__ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]).object.isLayerMask=true;
	dlib.layers(__ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]).left(0);
	dlib.layers(__ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]).top(0);
	dlib.layers(__ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]).height(dlib.windowHeight());
	dlib.layers(__ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]).width(dlib.windowWidth());
	if(!intDrag) dlib.layers(__ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]).content(dlib.EventLayerMaskContent(intZIndex));
	dlib.layers(__ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]).style.zIndex=intZIndex;
	return dlib.layers(__ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]);
};

/* return Event mask Layer object */
dlib.eventLayerMask=function(intZIndex){
        if(!arguments.length || dlib.debugLog("eventLayerMask", "dlib", false,  isInteger(intZIndex), 3, true)) intZIndex=0;
	if(!dlib.layers(__ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]).exists) dlib.createEventLayerMask(intZIndex);
	return dlib.layers(__ARR_LAYERS_MASK_NAME__["dlibEventMaskLayer"+intZIndex]);
};

/* scroll a layer object verticaly. Return true if layer has scrolled */
dlib.object.prototype.scrollV=function (intValue, intDoNotCheckScroll){
	if(dlib.debugLog(this.type+"s.scrollV", this.name, false,  (this.type=="layer")?true:false , 8, true)){
		if(arguments.length && dlib.debugLog(this.type+"s.scrollV : "+intValue, this.name, false,  isInteger(intValue), 3, true)){
        		if(arguments.length<2 || (arguments.length>1 &&  !intDoNotCheckScroll)){
	                	if(intValue<0){
        	            		if(this.clip().top+intValue<0) intValue=-this.clip().top;
					if(!this.clip().top) return false;
				}
				else {
					if(this.clip().bottom+intValue>this.height()) intValue=this.height()-this.clip().bottom;
					if(this.clip().bottom==this.height()) return false;
				}
			}
			this.top(add(-parseInt(intValue)));
			this.clip(add(parseInt(intValue)),keep(),add(parseInt(intValue)),keep());
		}
		return true;
	}
	return false;
};

/* scroll a layer object horizontaly. Return true if layer has scrolled*/
dlib.object.prototype.scrollH=function (intValue, intDoNotCheckScroll){
	if(dlib.debugLog(this.type+"s.scrollH", this.name, false,  (this.type=="layer")?true:false , 8, true)){
		if(arguments.length && dlib.debugLog(this.type+"s.scrollV : "+intValue, this.name, false,  isInteger(intValue), 3, true)){
        		if(arguments.length<2 || (arguments.length>1 &&  !intDoNotCheckScroll)){
	                	if(intValue<0){
        	            		if(this.clip().left+intValue<0) intValue=-this.clip().left;
					if(!this.clip().left) return false;
				}
				else {
					if(this.clip().right+intValue>this.width()) intValue=this.width()-this.clip().right;
					if(this.clip().right==this.width()) return false;
				}
			}
			this.left(add(-parseInt(intValue)));
			this.clip(keep(), add(parseInt(intValue)),keep(),add(parseInt(intValue)));
		}
		return true;
	}
	return false;
};


