/**
 * @Class Zadig
 */
Zadig = {
'ie': (navigator.userAgent.indexOf("MSIE")!= -1),	
'ie6': (navigator.userAgent.indexOf("MSIE 6")!= -1),
'firefox': (navigator.userAgent.indexOf("ecko")!= -1),
'bind': function(fn, obj){return function(){fn.apply(obj, arguments)};},
'inherit' : function(from, to)
{
	var z= function(){}
	z.prototype = from.prototype;
	for(var i in z.prototype)
	{
		if(to.prototype[i]==null)to.prototype[i] = z.prototype[i];
	}
	to.prototype.superClass = from;
	to.superClass = from;
},
'addListener':function(type, target, listener)
{
	target.addEventListener ? target.addEventListener(type, listener, false) : target.attachEvent('on' + type, listener);
},
'removeListener':function(type, target, listener)
{
	target.addEventListener ? target.removeEventListener(type, listener, false) : target.detachEvent('on' + type, listener);
},
'stopEvent' : function(e)
{
	e.stopPropagation ?	e.stopPropagation() : e.cancelBubble = true;
  e.preventDefault ? e.preventDefault() : e.returnValue = false;
},
'getTarget' :  function(e)
{
	return e.srcElement ? e.srcElement : e.target;
},
'pageRect': function()
{
	return {'x':document.documentElement.offsetWidth, 'y': Math.max(document.documentElement.scrollHeight, document.documentElement.offsetHeight - document.documentElement.scrollTop)};
},
'viewPort' : function()
{
	var x =  self.pageXOffset ? self.pageXOffset : document.documentElement.scrollLeft;	
	var width = self.innerWidth ? self.innerWidth : document.documentElement.clientWidth;
	var y = self.pageYOffset ? self.pageYOffset : document.documentElement.scrollTop;
	var height = self.innerHeight ? self.innerHeight : document.documentElement.clientHeight;
	return {'x':x, 'y':y, 'width':width, 'height':height};
},
'getRect' : function(e)
{
	var width = e.offsetWidth;
	var height = e.offsetHeight;
	
	var left = 0;
	var top = 0;
	
	if (e.offsetParent)
	{
		left = e.offsetLeft
		top = e.offsetTop
		
		while (e = e.offsetParent) 
		{
			left += e.offsetLeft
			top += e.offsetTop
		}
	}
	
	return {"x": left, "y": top, 'width':width, 'height':height}
},
'posAt' : function(obj) 
{
  var pos = arguments[1] ? arguments[1] : 'cm';
	var rect = arguments[2] ? this.getRect(arguments[2]) : Zadig.viewPort();
	var off = arguments[3] ? arguments[3] : {x:0,y:0};
	var orect = Zadig.getRect(obj);
	
	obj.style.position = 'absolute';
	var x = rect.x;
	var y = rect.y;
  //calc x
  if(pos.match(/c/)){x += Math.round((rect.width - orect.width) /2);}
  else if(pos.match(/r/)) {x += rect.width-2;}
  //calc y
  if(pos.match(/m/)){y += Math.round((rect.height - orect.height) /2);}
  else if(pos.match(/b/)) {y += rect.height;} 
  else if(pos.match(/t/)) {y -= rect.height;} 
	obj.style.top = y+off.y + "px";
	obj.style.left = x+off.x + "px";
},
'mousePos' : function(e)
{
	var x = 0;
	var y = 0;
	if (e.pageX || e.pageY)
	{
		x = e.pageX;
		y = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	if(arguments[1])
	{
		var r = Zadig.getRect(arguments[1]);
		x -= r.x;
		y -= r.y;
	}
	return {'x':x, 'y': y};
},

'setOpacity' : function (element, opacity) 
{
	if(Zadig.ie)
	{
		element.style.filter = "alpha(opacity:"+opacity+");"; 
		return;
	}
	if(element.style.opacity != undefined){element.style.opacity = opacity/100; return;}
	if(element.style.KHTMLopacity) {element.style.KHTMLopacity = opacity/100; return;} 
	if(element.style.Mozopacity) {element.style.Mozopacity = opacity/100; return;}
	
},
'getStyle' : function (element, prop)
{
	if (element.currentStyle)
		return element.currentStyle[prop];
	if (window.getComputedStyle)
		return document.defaultView.getComputedStyle(x,null).getPropertyValue(prop);
	return '';
},

'bindComponents' : function(e)
{
	var elements = arguments[1]==null ? document.getElementsByTagName("*") : arguments[1];
	var i;
	for(i in elements)
	{
		try{
		if((elements[i].attributes == null) || (elements[i].attributes.flow == null))continue;
		var element = elements[i];	
		eval('var args =' + elements[i].attributes.flow.nodeValue.replace(/&apos;/g,'\'')+';');
		var comp;
		var ex;
		for(comp in args){
			new args[comp].code(elements[i], args[comp]);}
		}catch(ex){}
	}
},


'dump' : function(obj, to)
{
	if(to == undefined)return; 
	if((typeof obj).match(/string|number/)) {typeof to == 'function'  ? to(obj) : to.innerHTML = obj; return;}
	var s = "";
	for(var p in obj)
		s +=  "<div>" + p + ": " + obj[p] + "</div>\n";
	if(arguments[2])
	{
		to.innerHTML += s;
		return;
	}
	typeof to == 'function' ? to(s) : to.innerHTML = s;
},
'trace' : function(obj)
{
	document.getElementById('crap').innerHTML +=  obj + '\n';
},
'hasClass': function (element, classname) 
{
	var pattern = new RegExp("(^|\\s)" + classname + "(\\s|$)");
	if (element.className.match(pattern)) return 1;
	return 0;
},


'addClass': function (element, classname) 
{
	var pattern = new RegExp("(^|\\s)" + classname + "(\\s|$)");
	if (element.className.match(pattern)) return;
	element.className += " " + classname;
	return;
},
'removeClass': function (element, classname) 
{
	var pattern = new RegExp("(^|)" + classname + "(\\s|$)");
	element.className = element.className.replace(pattern, '');
}

}







Zadig.addListener('load',window,Zadig.bindComponents);

Zadig.Hover = function(element, args) 
{
	this.element = element;
	this.classOver = args.classOver;
	this.removeRegex = new RegExp("(^| )"+args.classOver+"(|$)");
	Zadig.addListener('mouseover',element,Zadig.bind(this.over, this));
	Zadig.addListener('mouseout',element,Zadig.bind(this.out, this)); 
}
Zadig.Hover.prototype.over = function (e){this.element.className += " "+this.classOver;}
Zadig.Hover.prototype.out = function (e){this.element.className = this.element.className.replace(this.removeRegex,'');}

Zadig.showfig = function(v)
{
	div = document.createElement("DIV");
	div.style.backgroundColor = "black";
	Zadig.setOpacity(div, 85);
	div.style.width = Zadig.viewPort().width + 'px';
	div.style.height = Zadig.viewPort().height + 'px';
	
	document.body.appendChild(div);
	Zadig.posAt(div);
	var c = document.createElement('SPAN');
	c.innerHTML = v.innerHTML; 
	c.className = "enlarged";
	
	document.body.appendChild(c);
	Zadig.posAt(c);
	c.onclick = function (e) {document.body.removeChild(c);document.body.removeChild(div);}
	c.getElementsByTagName('IMG')[0].onload = function (e) {
	
Zadig.posAt(c);
	}
	c.getElementsByTagName('IMG')[0].src = c.getElementsByTagName('IMG')[0].src.replace('small/','');
	
	
}
Zadig.SubMenu = function(element, args) 
{
	this.element = element;
	this.flags = args.flags;
	this.level = args.level ? args.level : 0 ;
	this.subMenu = element.nextSibling.nodeName=="#text" ? element.nextSibling.nextSibling : element.nextSibling;
	if(this.subMenu.nodeName != 'DIV') return;
	
	Zadig.addListener('mouseover', element, Zadig.bind(this.over, this));
	Zadig.addListener('mouseout', element, Zadig.bind(this.out, this)); 
	Zadig.addListener('mouseover', this.subMenu, Zadig.bind(this.over, this));
	Zadig.addListener('mouseout', this.subMenu, Zadig.bind(this.out, this));
	
}
Zadig.SubMenu.prototype.over = function (e){
	Zadig.trace('in' + Zadig.getTarget(e).id );
	
 this.subMenu.style.display = 'block';
 var r = Zadig.getRect(this.element).x
 
 if(this.element.parentNode.style.position=='absolute')
 {
 	   
 	 Zadig.posAt(this.subMenu, 'r', this.element, {'x': -r, y:0 });  
 }
 else
 {
 	Zadig.posAt(this.subMenu, 'r', this.element);
 }
 }
Zadig.SubMenu.prototype.out = function (e){
	 
Zadig.trace('out' + Zadig.getTarget(e).id );
	this.subMenu.style.display = 'none';
	}


Zadig.NewSubMenu = function(element, args) 
{
	this.element = element;
	
	Zadig.addListener('mouseover', element, Zadig.bind(this.over, this));
	Zadig.addListener('mouseout', element, Zadig.bind(this.out, this)); 
}
Zadig.NewSubMenu.prototype.over = function (e){
	e = Zadig.getTarget(e);
	Zadig.trace('in ' +e.className);
	if(e.className == 'submenu') return this.showMenu(e);
	if(e.className == 'submenuBox') return  e.style.display = 'block';	
}
Zadig.NewSubMenu.prototype.out = function (e){
	var p =Zadig.mousePos(e);
	e = Zadig.getTarget(e);
	Zadig.trace('out ' +e.className);
	if(e.className == 'submenu') this.hideMenu(e);	
	if(e.className == 'submenuBox') this.hideAll(e,p);
	if(e.className == ''){this.hideAll(e.parentNode, p);}
}
Zadig.NewSubMenu.prototype.showMenu = function (e){
	var menu = e.nextSibling.nextSibling;
	menu.style.display = 'block';
	var r = Zadig.getRect(e).x;
	Zadig.posAt(menu, 'r', e, {'x': -r, 'y':0 });
	
}
Zadig.NewSubMenu.prototype.hideMenu = function (e){
	var menu = e.nextSibling.nextSibling;
	menu.style.display = 'none';
}
Zadig.NewSubMenu.prototype.hideAll = function (e, p){
	
	var r = Zadig.getRect(e);
	if((p.x>r.x) && (p.x< r.x + r.width)&&(p.y>r.y) && (p.y< r.y + r.height)) return;
	var s = this.element.getElementsByTagName('div');
	for(var i in s)
	{
		if(s[i].style) s[i].style.display='none';
	}
}



Zadig.Animate = function(args)
{
	this.duration = args.duration;
	this.start = new Date().getTime();
	this.callback = args.callback;
	this.ref = Zadig.bind(this.go, this);
	setTimeout(this.ref,33);
	this.backwards = false;
	
}
Zadig.Animate.prototype = {
'go':function()
{
	this.current = new Date().getTime();
	var cur = this.current - this.start;
	this.callback(cur/this.duration);
	if(cur > this.duration) {return;}
	setTimeout(this.ref,33); 
}
}

ZedGallery = function(element, args)
{
	this.bg = null;
	this.element = element;
	this.imgList = element.getElementsByTagName("IMG");
	this.position = 0;
	var i;
	for(i=0; i<this.imgList.length; i++)
	{
		this.imgList[i].showPic = this;
		this.imgList[i].i = i;
		this.imgList[i].onclick =  function(){this.showPic.showPicture(this.i);};
	}
	Zadig.addListener('click',this.element.nextSibling,Zadig.bind(this.click, this)); 
	Zadig.addListener('keydown',document,Zadig.bind(this.keydown, this));
}
ZedGallery.prototype = {
'showPicture':function(i)
{
	if(i >= this.imgList.length) return;
	if(i < 0) return;
	if(!this.bg) this.bg = darken();
	this.position = i;
	this.element.style.zIndex = 5;
	this.element.nextSibling.innerHTML = '<img style=\'display:none;\' onload="if(this.parentNode.style.display!=\'block\') return; this.style.display=\'block\'; this.parentNode.style.display=\'block\'; Zadig.posAt(this.parentNode);" src="' + this.imgList[i].src.replace(/(small|thumbs2?)\//,'') + '" /><br />' +
			this.imgList[i].nextSibling.innerHTML +
			'<div> <div class="links"><a class="' + (i >0 ? 'prev' : 'disabled')+'" title="anterior (seta para esquerda)">anterior</a> | <a class="' + (i != this.imgList.length - 1 ? 'next':'disabled')+'" title="próximo (seta para direita)">próximo</a> | <a class="close" title="fechar (Esc)">fechar</a></div></div>';
	this.element.nextSibling.style.display='block';	
	Zadig.posAt(this.element.nextSibling);
},
'click':function(e)
{
	if(Zadig.getTarget(e).className=='close'){document.body.removeChild(this.bg); this.bg = null; this.element.nextSibling.style.display='none';};
	if(Zadig.getTarget(e).className=='prev')this.showPicture(this.position - 1);
	if(Zadig.getTarget(e).className=='next')this.showPicture(this.position + 1);
},
'keydown':function(e)
{
	if(this.element.nextSibling.style.display != 'block') return;
	if(e.keyCode == 27) {document.body.removeChild(this.bg); this.bg = null; this.element.nextSibling.style.display='none';};
	if(e.keyCode == 37)this.showPicture(this.position - 1);
	if(e.keyCode == 39)this.showPicture(this.position + 1);
}
}

function darken()
{
	var div = document.createElement('div');
	var d = Zadig.pageRect();
	var d2 = Zadig.viewPort();
	//alert(d.width);
	div.style.top = '0';
	div.style.left = '0';
	div.style.width = d.x + "px";
	div.style.height = Math.max(d.y, d2.height) + "px";
	div.style.backgroundColor = "black";
	div.style.position = 'absolute';
	Zadig.setOpacity(div, 80);
	document.body.appendChild(div);
	return div;
}

function YoutubeVideo(title, id)
{
	this.bg = darken();
	this.element = document.createElement("div");
	this.element.className = "youtubePlayer"
	document.body.appendChild(this.element);
	this.element.innerHTML = "<h2><b>X</b>"+title+"</h2><embed src='http://www.youtube.com/v/"+id+"&hl=en&fs=1&color1=0x3a3a3a&color2=0x999999' type='application/x-shockwave-flash' allowfullscreen='true' width='425' height='344' ></embed>";
	Zadig.posAt(this.element);
	Zadig.addListener('click',this.element, Zadig.bind(this.click, this));
}
YoutubeVideo.prototype = {
'click' : function (e)
{
	document.body.removeChild(this.bg); document.body.removeChild(this.element);
}

}




//---------------------------------------------------------------------------------------//
//DivSlider
//---------------------------------------------------------------------------------------//
DivSlider = function(element, args)
{
	this.divs = new Array();
	this.current = 0;
	var buttons='';
	var mh = 0;
	for(var i=element.firstChild; i; i = i.nextSibling)
	{
		if(i.nodeName == 'DIV')
		{
		var h = Zadig.getRect(i);
		i.style.display = 'none';
		if (mh < h.height) mh = h.height; 
		this.divs.push(i);
		buttons += '<span>'+ this.divs.length +'</span>';
		}
	}
	for(var i=0; i < this.divs.length; i++) this.divs[i].style.height = mh + "px";
	this.divs[this.current].style.display = 'block';
	this.controls = document.createElement("DIV");
	this.controls.innerHTML = buttons;
	element.appendChild(this.controls);
	this.controls.firstChild.className = 'selected';
	this.controls.className = "controls";
	Zadig.addListener('click', this.controls, Zadig.bind(this.click,  this));
	if(args.delay)setInterval(Zadig.bind(this.change, this), args.delay);
}

DivSlider.prototype.click = function(e)
{
	var t = Zadig.getTarget(e);
	if((t.nodeName == 'SPAN')  )
	{
		this.divs[this.current].style.display = 'none';
		this.current=parseInt(t.innerHTML)-1;
		this.divs[this.current].style.display = 'block';
		var buttons = this.controls.getElementsByTagName('*');
		for(var i in buttons)
		{
			var select = (this.current == i) ? 'selected' : '';
			buttons[i].className = select;
		}
	}
}

DivSlider.prototype.change = function()
{
	this.divs[this.current].style.display = 'none';
	if((++this.current)== this.divs.length) this.current = 0;
	this.divs[this.current].style.display = 'block';
	var buttons = this.controls.getElementsByTagName('*');
	for(var i in buttons)
	{
		var select = (this.current == i) ? 'selected' : '';
		buttons[i].className = select;
	}

}
//-----------------------------
Zadig.Container = function (element, args)
{
	this.element = element;
	element.flow = null;
	element.jsClass = this;
	if(args.url) this.load(args.url);
}

Zadig.Container.prototype.onLoad = function (response)
{
	this.element.innerHTML = response; 
	Zadig.bindComponents(0,this.element.getElementsByTagName("*"));
}
Zadig.Container.prototype.load = function (url)
{
	new Zadig.Request(url, true, null, Zadig.bind(this.onLoad, this ) ); 
}
//------------------------

Zadig.NoSubmit = function (element, args)
{
	return;
	if(element.nosub) return;
	element.nosub= 1;
	var r= element;
	r.addEventListener('focus', function(e){r.form.addEventListener('submit', noSub, false);}, false);
	r.addEventListener('blur', function(e){r.form.removeEventListener('submit', noSub, false);}, false);
}
function noSub(e) {e.stopPropagation ?	e.stopPropagation() : e.cancelBubble = true;
e.preventDefault ? e.preventDefault() : e.returnValue = false;	return false;}




