var jslng={add:function(o) {for (attrname in o) { this[attrname] = o[attrname]; }}};
var _zwTagTooltipTimer=false;

// Preload Images
var _trPlus = new Image();
_trPlus.src = 'common/images/tree/plus.gif';
var _trMinus = new Image();
_trMinus.src = 'common/images/tree/minus.gif';

var _trMinussi = new Image();
_trMinussi.src = 'common/images/tree/minussi.gif';
var _trPlussi = new Image();
_trPlussi.src = 'common/images/tree/plussi.gif';

var _trMinusss = new Image();
_trMinusss.src = 'common/images/tree/minuss.gif';
var _trPlusss = new Image();
_trPlusss.src = 'common/images/tree/pluss.gif';

function $()
{
	var elements = new Array();
	for(var i = 0; i < arguments.length; i++)
	{
		var element=arguments[i];
		if(typeof element=='string')
			element = document.getElementById(element);
		if(arguments.length==1)
			return element;
		elements.push(element);
	}
	return elements;
}

// Events
function _addEventHandler(o, e, d)
{
	if(window.attachEvent)
	{
		return o.attachEvent(e, d);
	} else {
		return o.addEventListener(e.substr(2), d, false);
	}
}

function _removeEventHandler(o, e, d)
{
	if(window.attachEvent)
	{
		return o.detachEvent(e, d);
	} else {
		return o.removeEventListener(e.substr(2), d, false);
	}
}

function _stopEventPropagation(e)
{
	if(!e) e=window.event;
	e.cancelBubble=true;
	if(e.stopPropagation) e.stopPropagation();
	//e.returnValue = false; // <- Quitar cancela los clicks sobre checkboxes.
}

function _objFireEvent(o, e)
{
	if(o.fireEvent)
	{
		o.fireEvent(e);
	} else {
		var v = document.createEvent("Events");
		v.initEvent(e.substr(2), false, false);
		element.dispatchEvent(v);
	}
}


// Core Functions
function _setCoreText(grow)
{
	var oObj=document.getElementById('core');

	if(grow==true)
	{
		if(_textSize<3) _textSize++;
	} else {
		if(_textSize>1) _textSize--;
	}

	_setCoreTextValue(_textSize);
	return false;
}

function _setCoreTextValue(value)
{
	var oObj=document.getElementById('core');
	oObj.className='core'+value;

	var date = new Date();
	date.setDate(date.getDate()+30);
	document.cookie = 'zwcoresize='+value+'; expires=' + date.toGMTString();

	return false;
}

function _zwDoTreeAction(uid, s)
{
	var oContent=document.getElementById(uid+'_cnt');
	var oImg=document.getElementById(uid+'_img');
	var aName;
	var sName;

	if(oContent)
		oContent.style.display=(oContent.style.display=='block'?'none':'block');

	if(oImg)
	{
		aName=oImg.src.split('/');
		sName=aName[aName.length-1];
		switch(sName)
		{
			case 'minuss.gif':
				oImg.src=_trPlusss.src
				break;
			case 'pluss.gif':
				oImg.src=_trMinusss.src
				break;
			case 'minussi.gif':
				oImg.src=_trPlussi.src
				break;
			case 'plussi.gif':
				oImg.src=_trMinussi.src
				break;
			case 'minus.gif':
				oImg.src=_trPlus.src
				break;
			case 'plus.gif':
				oImg.src=_trMinus.src
				break;
		}
	}

	if(s) s.call(null, uid);
}

function _isMail(strEmail)
{
	validRegExp = /^([a-z0-9_\-]+\.)*([a-z0-9_\-]+@){1,1}([a-z0-9_\-]+\.)+[a-z]{2,4}$/i;
	return (strEmail.search(validRegExp)==-1?false:true);
}

function _isWeb(strWeb)
{
	validRegExp = /^(([a-z0-9_\-]+\.)+[a-z]{2,4}(\/[a-z0-9\.&#-=?]*)*|#[a-z0-9_\-]+\.*)$/i;
	return (strWeb.search(validRegExp)==-1?false:true);
}

function hex(dec)
{
	var hexs='0123456789ABCDEF';
	var r;
	var low=dec%16;
	var high=(dec-low)/16;
	r=hexs.charAt(high)+hexs.charAt(low);
	while(r.length<2) r='0'+r;
	return r;
}

function _zwInsertEditorImage(e, id, w, h, f)
{
	var t, c, ed;

	if(w>=800 || h>=600)
	{
		if(w>h)
		{
			t=w; w=300;
			h=h*w/t;
		} else {
			t=h; h=300;
			w=w*h/t;
		}
	}

	ed=tinyMCE.get(e);
	switch(f)
	{
		case 'flv':
			c='<img id=\"img_'+id+'\" class=\"cmsflvdummy\" border=\"0\" width=\"'+w+'\" height=\"'+h+'\" src=\"common/images/dummy.gif\" />';
			ed.execCommand('mceInsertContent', false, c);
			break;
		case 'swf':
			c='<img id=\"img_'+id+'\" class=\"cmsswfdummy\" border=\"0\" width=\"'+w+'\" height=\"'+h+'\" src=\"common/images/dummy.gif\" />';
			ed.execCommand('mceInsertContent', false, c);
			break;
		case 'img':
		default:
			c='<img class=\"photo\" border=\"0\" width=\"'+w+'\" height=\"'+h+'\" src=\"image.php?zwimg='+id+'&zww='+w+'&zwh='+h+'&zwnc=1\" />';
			ed.execCommand('mceInsertContent', false, c);
			break;
	}

	ed.resizeToContent();
	ed.onChange.dispatch();
}

function zwGetCookie(check_name)
{
	var a_all_cookies=document.cookie.split(';');
	var a_temp_cookie='';
	var cookie_name='';
	var cookie_value='';
	var b_cookie_found=false;

	for(var i=0;i<a_all_cookies.length;i++)
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		if(cookie_name==check_name)
		{
			b_cookie_found=true;
			if (a_temp_cookie.length>1)
				cookie_value=unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}

	if(!b_cookie_found)
		return null;
}

function zwSetCookie(name, value, expires, path, domain, secure)
{
	var today = new Date();
	today.setTime(today.getTime());
	if(expires) expires = expires * 1000 * 60 * 60 * 24;

	var expires_date=new Date(today.getTime()+(expires));

	document.cookie=name+'='+escape(value) +
		((expires)?';expires='+expires_date.toGMTString():'') +
		((path)?';path='+path:'') +
		((domain)?';domain='+domain:'') +
		((secure)?';secure':'');
}

function zwDeleteCookie(name, path, domain)
{
	if (zwGetCookie(name))
		document.cookie = name + '=' +
		((path)?';path='+path:'')+
		((domain)?';domain='+domain:'')+
		';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function zwGetAttributes(attribute_string)
{
	var attributeName = "";
	var attributeValue = "";
	var withInName;
	var withInValue;
	var attributes = new Array();
	var whiteSpaceRegExp = new RegExp('^[ \n\r\t]+', 'g');

	if(attribute_string == null || attribute_string.length < 2)	return attributes;
	withInName = withInValue = false;

	for(var i=0;i<attribute_string.length;i++)
	{
		var chr=attribute_string.charAt(i);
		if((chr=='"'||chr=="'") && !withInValue)
		{
			withInValue = true;
		} else if((chr=='"'||chr=="'") && withInValue) {
			withInValue=false;
			var pos=attributeName.lastIndexOf(' ');
			if(pos!=-1) attributeName=attributeName.substring(pos+1);
			attributes[attributeName.toLowerCase()]=attributeValue.substring(1);
			attributeName="";
			attributeValue="";
		} else if(!whiteSpaceRegExp.test(chr)&&!withInName&&!withInValue) {
			withInName=true;
		}

		if(chr=='='&&withInName) withInName=false;
		if(withInName) attributeName+=chr;
		if(withInValue) attributeValue+=chr;
	}
	return attributes;
}

var zwBrowser={
	init:function()
	{
		this.browser = this.searchString(this.dataBrowser) || "an unknown browser";
		this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},

	searchString:function(data)
	{
		for (var i=0;i<data.length;i++)
		{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if(dataString)
			{
				if(dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			} else if (dataProp) {
				return data[i].identity;
			}
		}
	},

	searchVersion:function(dataString)
	{
		var index = dataString.indexOf(this.versionSearchString);
		if(index==-1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},

	dataBrowser:[
	{ string: navigator.userAgent, subString: "Chrome", identity: "Chrome" },
	{ string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb" },
	{ string: navigator.vendor, subString: "Apple", identity: "Safari", versionSearch: "Version" },
	{ prop: window.opera, identity: "Opera" },
	{ string: navigator.vendor, subString: "iCab", identity: "iCab" },
	{ string: navigator.vendor, subString: "KDE", identity: "Konqueror" },
	{ string: navigator.userAgent, subString: "Firefox", identity: "Firefox" },
	{ string: navigator.vendor, subString: "Camino", identity: "Camino" },
	{ string: navigator.userAgent, subString: "Netscape", identity: "Netscape" },
	{ string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE" },
	{ string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv" },
	{ string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla" }],

	dataOS:[
	{ string: navigator.platform, subString: "Win", identity: "Windows" },
	{ string: navigator.platform, subString: "Mac", identity: "Mac" },
	{ string: navigator.userAgent, subString: "iPhone", identity: "iPhone/iPod" },
	{ string: navigator.platform, subString: "Linux", identity: "Linux"} ]
}
zwBrowser.init();

var zwKeyMap={
	keys:[],
	isEvt:false,
	registerKey:function(k, ct, alt, cb)
	{
		if(isNaN(parseInt(k))) k=k.toUpperCase().charCodeAt(0);
		this.keys.push({key:k, ctrl:ct, alt:alt, callback:cb});
		if(!this.isEvt)
		{
			var _self=this;
			_addEventHandler(document, 'onkeydown', function() { _self.eventHandler(event); } );
			this.isEvt=true;
		}
	},
	getKey:function(e)
	{
		var i, c, k, alt, ct;

		c=(window.event?e.keyCode:e.charCode);
		alt=e.altKey;
		ct=e.ctrlKey;

		for(i=0;i<this.keys.length;i++)
		{
			k=this.keys[i];
			//alert(c+' - '+alt+' - '+ct+'\n'+k.key+' - '+k.alt+' - '+k.ctrl);
			if(k.key==c && k.alt==alt && k.ctrl==ct)
				return k;
		}

		return false;
	},
	eventHandler:function(e)
	{
		var e=(window.event?window.event:e);
		var o=(e.srcElement?e.srcElement:e.target);
		var k=this.getKey(e);
		var b=true;

		if(k)
		{
			if((o.tagName=='INPUT' || o.tagName=='TEXTAREA') && k.ctrl==false && k.alt==false) b=false;
			if(b && k.callback) k.callback.call(o, e);
		}
	}
}


function zwAnimation(o, w)
{
	this.state=0;
	this.showWidth=w;
	this.obj=o;
	this.onswitch=false;
	this.speedratio=4;
	this.speed=25;

	this.obj.style.width='0px';
	this.obj.style.overflow='hidden';
	this.obj.style.display='block';

	this.show=function()
	{
		if(this.state!=2) this.state=1;
		this.raiseonswitch();
		this.animate();
	}

	this.hide=function()
	{
		if(this.state!=0) this.state=3;
		this.raiseonswitch();
		this.animate();
	}

	this.raiseonswitch=function()
	{
		if(this.onswitch)	this.onswitch.call(this);
	}

	this.toggle=function()
	{
		switch(this.state)
		{
			case 0: // Oculto
				this.state=1;
				break;
			case 1: // Mostrando
				this.state=3;
				break;
			case 2: // Visible
				this.state=3;
				break;
			case 3: // Ocultando
				this.state=1;
				break;
		}
		this.raiseonswitch();
		this.animate();
	}

	this.animate=function()
	{
		var m=0;
		var w=parseInt(this.obj.style.width);
		var s;

		switch(this.state)
		{
			case 1:
				m=Math.round((this.showWidth-w)/this.speedratio);
				break;
			case 3:
				m=-Math.round(w/4);
				break;
		}

		if(m!=0)
		{
			this.obj.style.width=w+m;
			s=this;
			setTimeout(function(){ s.animate(); }, this.speed);
		} else {
			switch(this.state)
			{
				case 1:
					this.state=2;
					break;
				case 3:
					this.state=0;
					break;
			}
		}
	}

}

var zwToolTip={
	state:0,
	timer:null,
	trans:0,
	speed:10,
	showTime:5000,
	doTimer:function()
	{
		switch(this.state)
		{
			case 0: // Hidden
				break;
			case 1: // Showing
				this.trans+=10;
				if(this.trans>100) this.trans=100;
				this.setTrans();
				if(this.trans>=100)
				{
					this.state=2;
					this.doTimer();
				} else {
					this.setTimer();
				}
				break;
			case 2: // Visible
				var _self=this;
				this.timer=setTimeout(function (){ _self.hide();}, this.showTime);
				break;
			case 3: // Hidding
				this.trans-=10;
				if(this.trans<0) this.trans=0;
				this.setTrans();
				if(this.trans<=0)
				{
					this.state=0;
					this.doTimer();
				} else {
					this.setTimer();
				}
				break;
		}

	},
	setTrans:function()
	{
		var o=document.getElementById('_zwtooltip');
		window.status=zwBrowser.browser;
		if(zwBrowser.browser=='Explorer')
			o.style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity='+this.trans+', Style=0)';
		else
			o.style.opacity=this.trans/100;
	},
	setTimer:function()
	{
		if(this.timer!=false) clearTimeout(this.timer);
		var _self=this;
		this.timer=setTimeout(function() { _self.doTimer(); }, this.speed);
	},
	show:function(s, x, y, w, h)
	{
		var o=document.getElementById('_zwtooltip');
		var c=document.getElementById('_zwtooltip_text');
		if(this.timer) clearTimeout(this.timer);

		if(!w) w='250px';
		if(!h) h='auto';

		if(o && c)
		{
			c.innerHTML=s;
			o.style.left=x;
			o.style.top=y;
			o.style.width=w;
			o.style.height=h;
			o.style.zIndex=150;
			this.setTrans();
			this.state=1;
			this.setTimer();
			o.style.display='block';
		}
	},
	hide:function()
	{
		if(this.state==1 || this.state==2)
		{
			this.state=3;
			this.setTimer();
		}
	}
};

var zwLightBox={
	baseBox:null,
	iframe:null,
	currentBox:null,
	isModal:true,
	fadeObj:null,
	shadow:null,
	initialized:false,
	width:350,
	height:250,
	ahah:null,
	iframeCnt:0,
	initialize:function()
	{
		var o;
		var _self=this;

		o=document.createElement('DIV');
		o.className='cmslightboxbg';
		_addEventHandler(o, 'onclick', function() { _self.modalHide(); return false; } );
		document.body.insertBefore(o, document.body.childNodes[0]);
		this.fadeObj=o;

		o=document.createElement('DIV');
		o.className='cmslightboxsh';
		document.body.insertBefore(o, this.fadeObj);
		this.shadow=o;

		o=document.createElement('DIV');
		o.className='cmslightbox';
		document.body.insertBefore(o, this.shadow);
		this.baseBox=o;

		this.initialized=true;
	},
	showContent:function(o, w, h, m)
	{
		var _self=this;
		var p;

		if(o)
		{
			if(!this.initialized) this.initialize();
			p=o.parentNode;
			if(p!=document.body)
			{
				p.removeChild(o);
				document.body.insertBefore(o, document.body.childNodes[0]);
			}

			if(w) this.width=w; else this.width=350;
			if(h) this.height=h; else this.height=250;
			if(m) this.isModal=m; else this.isModal=false;

			o.style.width=this.width;
			o.style.height=this.height;
			o.className='cmslightbox';

			this.shadow.style.width=this.width;
			this.shadow.style.height=this.height;
			this.currentBox=o;

			this.setPosition()
			this.currentBox.style.display='block';
			this.shadow.style.display='block';
			this.fadeObj.style.display='block';

			_addEventHandler(document, 'onkeydown', this.keyHandler);
			_addEventHandler(window, 'onscroll', this.scrollHandler);
		}
	},
	getIframe:function()
	{
		var n='zwLightboxFrame'+this.iframeCnt++;

		this.destroyIframe();
		o=document.createElement('DIV');
		o.innerHTML='<iframe src="about:blank" frameborder="0" scrolling="auto" id="'+n+'" class="cmslightboxfr" name="'+n+'"></iframe>';
		this.baseBox.appendChild(o);
		this.iframe=document.getElementById(n);
	},
	destroyIframe:function()
	{
		if(this.iframe)
			this.iframe.parentNode.removeChild(this.iframe);
	},
	showLoad:function(u, w, h, m)
	{
		if(!this.initialized) this.initialize();
		this.getIframe();
		this.iframe.src=u;
		this.iframe.style.height=h;
		this.iframe.style.width=w;
		this.showContent(this.baseBox, w, h, m);
	},
	showObject:function(s, w, h, m)
	{
		var o=document.getElementById(s);
		if(!this.initialized) this.initialize();
		this.destroyIframe();
		this.showContent(o, w, h, m);
	},
	keyHandler:function(e)
	{
		var c=(window.event?e.keyCode:e.charCode);
		if(c==27) zwLightBox.hide();
	},
	scrollHandler:function()
	{
		zwLightBox.setPosition();
	},
	hide:function()
	{
		if(this.currentBox)
		{
			this.currentBox.style.display='none';
			this.shadow.style.display='none';
			this.fadeObj.style.display='none';
			_removeEventHandler(document, 'onkeydown', this.keyHandler);
		}
	},
	modalHide:function()
	{
		if(!this.isModal) this.hide();
	},
	setPosition:function()
	{
		var p,b,x,y,v;

		p=_getClientScrollOffset();
		v=zwGetViewportSize();

		y=p.y+Math.round((v.h-this.height)/2);
		x=p.x+Math.round((v.w-this.width)/2);
		this.fadeObj.style.top=p.y;
		this.currentBox.style.top=y;
		this.currentBox.style.left=x;
		this.shadow.style.top=y+5;
		this.shadow.style.left=x+5;
	}
}

// WTags
function _zwShowTagTooltip(id, oEvt)
{
	oEvt=(oEvt?oEvt:window.event);
	var oTrg=(oEvt.srcElement?oEvt.srcElement:oEvt.target);

	var oPos=_getObjRectangle(oTrg);
	var oTip=document.getElementById('_zwwtag');
	var oCnt=document.getElementById('_zwwtag_text');
	var oTxt=document.getElementById('_zwtagtip_'+id);

	if(_zwTagTooltipTimer) clearTimeout(_zwTagTooltipTimer);
	if(oTip && oCnt && oTxt)
	{
		oCnt.innerHTML=oTxt.innerHTML;
		oTip.style.left=oPos.left-((parseInt(oTip.style.width)-oPos.width)/2);
		oTip.style.top=oPos.top+oPos.height;
		oTip.style.display='block';
	}
}

function _zwStopTimerTagToolTip()
{
	if(_zwTagTooltipTimer) clearTimeout(_zwTagTooltipTimer);
}

function _zwTimerTagTooltip()
{
	if(_zwTagTooltipTimer) clearTimeout(_zwTagTooltipTimer);
	_zwTagTooltipTimer=window.setTimeout('_zwHideTagTooltip();', 1500);
}

function _zwHideTagTooltip()
{
	oTip=document.getElementById('_zwwtag');
	if(oTip) oTip.style.display='none';
	_zwTagTooltipTimer=false;
}


function zwTextArea(s)
{
	this.obj=document.getElementById(s);
	this.timer=false;
	this.dummy;
	this.initialized=false;
	this.height;
	this.lineHeight=15;
	this.minHeight=false;
	this.maxHeight=250;

	var self=this;
	var v;

	if(this.obj)
	{
		v=this.obj.value;
		this.obj.style.overflow='hidden';
		_addEventHandler(this.obj, 'onfocus', function(){ self.startGrow(); });
		_addEventHandler(this.obj, 'onblur', function(){ self.stopGrow(); });
	}

	this.startGrow=function()
	{
		var self=this;
		this.timer=window.setInterval(function() {self.checkSize()}, 100);
	}

	this.stopGrow=function()
	{
		window.clearInterval(this.timer);
	}

	this.init=function()
	{
		var s=_getObjectSize(this.obj);

		this.dummy=document.createElement('DIV');

//	this.dummy.style.fontFamily=this.obj.style.fontFamily;
//	'width'      : this.textarea.css('width'),
//    'padding'    : this.textarea.css('padding'),
//	'line-height': this.line_height + 'px',
//	'overflow-x' : 'hidden',
//	'position'   : 'absolute',
//	'top'        : 0,
		this.obj.style.overflowX='hidden';

		this.dummy.style.position='absolute';
//		this.dummy.style.fontSize=this.obj.fontSize;
		this.dummy.style.overflowX='hidden';
		this.dummy.style.whiteSpace='normal';
		this.dummy.style.top=0;
		this.dummy.style.left=-9999;
		this.dummy.style.width=s.width+'px';
		document.body.insertBefore(this.dummy, document.body.childNodes[0]);

		this.height=s.height;
		if(!this.minHeight) this.minHeight=this.height;
		this.initialized=true;
	}

	this.checkSize=function()
	{
		var v, s, d;

		if(!this.initialized) this.init();

		if(this.v!=this.obj.value)
		{
			v=this.obj.value;
			v=v.replace(/\n/g, '<br>new');
			this.dummy.innerHTML=v;

			s=_getObjectSize(this.dummy);
			this.height=s.height+this.lineHeight;
			if(this.height<this.minHeight) this.height=this.minHeight;
			if(this.height>this.maxHeight)
			{
				this.height=this.maxHeight;
				this.obj.style.overflowY='auto';
			} else {
				this.obj.style.overflowY='hidden';
			}
			this.v=this.obj.value;
		}

		s=_getObjectSize(this.obj);
		if(this.height!=s.height)
		{
			d=(this.height-s.height)/2;
			this.obj.style.height=(s.height+d)+'px';
			this.dummy.style.width=s.width;
		} else {
			this.obj.focus(); // Mozilla tip.
		}
	}
}