//******************************************************** 
// Copyright (c) 2008 - Judison Oliveira Gil Filho <judison@gmail.com>
//********************************************************

//********************************************************
// Contextual Help
//********************************************************
var ContextualHelp = {
	absolutePos: function(elem) { 
		var offsetTrail = $(elem);
		var offsetLeft = 0;
		var offsetTop = 0;
		while (offsetTrail) {
			offsetLeft += offsetTrail.offsetLeft;
			offsetTop += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") {
			offsetLeft += document.body.leftMargin;
			offsetTop += document.body.topMargin;
		}
		return {left:offsetLeft, top:offsetTop};
	},

	toggle: function(div) {
		var img = div.getElementsByTagName("img")[0];
		var dl = div.getElementsByTagName("span")[0];
		
		if (JDF.hasClassName(dl, "hidden")) {
			var pos = ContextualHelp.absolutePos(img); //Arrumar aqui pra JDF.absolutePos apos dia 10;
			dl.style.top = (pos.top - 1) + "px";
			dl.style.left = (pos.left - 1) + "px";
			JDF.show(dl)
			JDF.addClassName(tc.activeTab.a, "block");
		} else {
			JDF.hide(dl);
			JDF.removeClassName(tc.activeTab.a, "block");
		}
	}
}

//********************************************************
// TabControl 
//********************************************************
var TabControl = {
	tabs: new Array(),
	
	setActiveTab: function(tab) {
		tc = tab.tc;
		if (tc.activeTab) {
			JDF.hide(tc.activeTab.div);
			JDF.removeClassName(tc.activeTab.a, "active");
		}
		JDF.addClassName(tab.a, "active");
		JDF.show(tab.div);
		tc.activeTab = tab;
	},
	
	aonclick: function(e) {
		if(typeof(e)=='undefined') var e=window.event;
		source = e.target ? e.target : e.srcElement;
		if (source.nodeType == 3) source = source.parentNode;
		//--
		if (source.tagName == "IMG" || source.tagName == "img")
			source = source.parentNode;
		//--
		id = source.getAttribute('href').split("#")[1];
		//--
		tab = TabControl.tabs[id];
		TabControl.setActiveTab(tab);
		source.blur();
		return false;
	},
	
	add: function(tab_ul) {
		tab_ul = $(tab_ul);
	
		var tc = new Object();
		tc.activeTab = false;
		tc.ul = tab_ul;
	
		lis = tab_ul.getElementsByTagName("li");
		for (var i=0; i < lis.length; i++) {
			li = lis[i];
			a = li.getElementsByTagName("a")[0];

			tab = new Object();
			tab.tc = tc;
			tab.a = a;
			tab.id = a.getAttribute('href').split("#")[1]; 
			tab.div = $(tab.id);

			TabControl.tabs[tab.id] = tab;

			//a.onclick = TabControl.aonclick;
			JDF.addEvent(a, "click", TabControl.aonclick);
			
			if (!tc.activeTab)
				TabControl.setActiveTab(tab);
			else
				JDF.hide(tab.div);
		}	
	},
	
	onload: function () {
		uls = document.getElementsByTagName("ul");
		for (var i=0; i < uls.length; i++) {
			ul = uls[i];
			if (JDF.hasClassName(ul, "tabs"))
				TabControl.add(ul);
		}
	}
}

JDF.addEvent(window, "load", TabControl.onload);

//********************************************************

getCategorias = function(o, search){
	c=0;
	if(search = search.toLowerCase())
		for(var i = -1, l = listCategorias.length; ++i < l;){
			for(var j = 0, indices = []; j = listCategorias[i].toLowerCase().indexOf(search, j) + 1; indices[indices.length] = j - 1);
			if(indices.length) {
				o.add(listCategorias[i], indices);
				c++;
			}
			if (c>5)
				break;
		};
	o.show();
};
getCredores = function(o, search){
	c=0;
	if(search = search.toLowerCase())
		for(var i = -1, l = listCredores.length; ++i < l;){
			for(var j = 0, indices = []; j = listCredores[i].toLowerCase().indexOf(search, j) + 1; indices[indices.length] = j - 1);
			if(indices.length) {
				o.add(listCredores[i], indices);
				c++;
			}
			if (c>5)
				break;
		};
	o.show();
};

//********************************************************
// Incremental Search
//********************************************************
IncrementalSearch = function(input, c, callback){
	var i;
	this.input = input;
	this.c = c;
	this.callback = callback || function(){};
	this.hide();
	this.visible = 0;
	for(i in {keydown: 0, focus: 0, blur: 0, keyup: 0, keypress: 0})
		JDF.addEvent(input, i, this._handler, this);
};
with({p: IncrementalSearch.prototype}){
	p.show = function(){
		for(s = this.c.style, o = this.input, x = o.offsetLeft,	y = o.offsetTop + o.offsetHeight; o = o.offsetParent; x += o.offsetLeft, y += o.offsetTop);
		s.left = x + "px";
		s.top = y + "px";
		if (this.l.length) {
			JDF.show(this.c); 
			if (!this.visible) {
				this._callEvent("onshow");
				this.visible = true;
			}
			this.highlite(0);
		} else {
			JDF.hide(this.c);
		}
	};
	p.hide = function(){
		this.l = [];
		this.i = -1;
		JDF.hide(this.c);
		this._old = null;
		if (this.visible) {
			this._callEvent("onhide");
			this.visible = false;
		}
		JDF.removeContent(this.c);
	};
	p.add = function(s, x, data){
		var $ = this, l = 0, d = document, i = $.l.length, v = $.input.value.length,
			o = ($.l[i] = [s, data, $.c.appendChild(JDF.createElement("div"))])[2];
		if(x instanceof Array || (x = [x]), o.i = i, !isNaN(x[0]))
			for(var j = -1, k = x.length; ++j < k; o.appendChild(d.createTextNode(
				s.substring(l, x[j]))).parentNode.appendChild(JDF.createElement("b")).appendChild(d.createTextNode(s.substring(x[j],	l = x[j] + v))));
		for(x in o.appendChild(d.createTextNode(s.substr(l))), {click: 0, mouseover: 0})
			JDF.addEvent(o, x, $._handler, $);
	};
	p.highlite = function(i){
		var $ = this;
		$._invalid(i) || ($._invalid($.i) || ($.l[$.i][2].className = ""),
		$.l[$.i = i][2].className = "selected", $._callEvent("onhighlite", $.l[i][0], $.l[i][1]));
	};
	p.select = function(i){
		var $ = this;
		$._invalid(i = isNaN(i) ? $.i : i) || ($._callEvent("onselect",
			$.input.value = $.l[$.i][0], $.l[i][1]), $.hide());
	};
	p.next = function(){
		var $ = ($ = this, $.highlite(($.i + 1) % $.l.length));
	};
	p.previous = function(){
		var $ = ($ = this, $.highlite((!$.i ? $.l.length : $.i) - 1));
	};
	p._fadeOut = function(){
		var f = (f = function(){arguments.callee.x.hide();}, f.x = this, setTimeout(f, 200));
	};
	p._handler = function(e){
		var $ = this, t = e.type, k = e.key;
		t == "focus" || t == "keyup" ? k != 40 && k != 38 && k != 13 && $._old != $.input.value && ($.hide(), $.callback($, $.input.value))
		: t == "keydown" ? k == 40 ? $.next() : k == 38 ? $.previous() : $._old = $.input.value
		: t == "keypress" ? k == 13 && (e.preventDefault(), $.select())
		: t == "blur" ? $._fadeOut() : t == "click" ? $.select()
		: $.highlite((/span/i.test((e = e.target).tagName) ? e.parentNode : e).i);
	};
	p._invalid = function(i){
		return isNaN(i) || i < 0 || i >= this.l.length;
	}
	p._callEvent = function(e){
		var $ = this;
		return $[e] instanceof Function ? $[e].apply($, [].slice.call(arguments, 1)) : undefined;
	};
}