var IE = !window.getComputedStyle && !!document.getElementById;
var IE6 = IE && navigator.appVersion.indexOf('MSIE 7') == -1 && navigator.appVersion.indexOf('MSIE 8') == -1
function $T(tName, parent, FIRST){
	var arr = $A((parent || document).getElementsByTagName(tName));
	return FIRST ? arr[0] : arr;
}	
var dur = 0.2;


window.onload = function(){
	init_overlay();
	init_tabs();
	init_ctabs();
	init_areas();
	//init_popup();
	init_panel();
	init_menu();
	if(IE6){
		fix_popup();
	}
	init_dom_search();
	init_dom_tabs();
	init_carousel();
};
function init_menu(){
	var root = $('menu');

	if(!root){
		return false;
	}
	var items = [];
	var divs = $T('DIV', root);
	divs.each(function(d){
		if(d.className.indexOf('item') != -1){
			items.push(d);
		}
	});
	var w = 0;
	items.each(function(item, ind){
		if(!ind){
			item.FIRST = true;;
		}
		w += item.offsetWidth;
		if(IE6){
			item.onmouseover = function(){
				if(this.FIRST){
					this.className += ' over-first';
				}
				this.className += ' over';
			};
			item.onmouseout = function(){
				if(this.FIRST){
					this.className = this.className.replace('over-first', '');
				}
				this.className = this.className.replace('over', '');
			};
		}
	});
	root.style.paddingLeft = Math.floor((root.offsetWidth - w) / 2) + 'px';
}
var overlay;
function init_overlay(){
	var root = $('overlay');
	if(!root){
		return false;
	}
	root.iframe = $T('IFRAME', root, 1);
	var divs = $T('DIV', root);
	root.button = divs.detect(function(d){return d.className.indexOf('b-close') != -1});
	Object.extend(root, overlay_funcs);
	root.button.onclick = root.hide.bind(root);

	var divs2 = $T('DIV', $('white-block'));
	divs2.each(function(d){
		if(d.className.indexOf('item2') != -1){
			var links = $T('A', d);
			d.link = links.detect(function(link){return link.className.indexOf('lm') != -1});
			d.link.onclick = function(){
				overlay.iframe.src = this.href;
				overlay.show();
				return false;
			};		
		}
	});
	overlay = root;
	window.onresize = function(){
		if(overlay.VISIBLE){
			overlay.place();
		}
	};
}
var overlay_funcs = {
	show: function(){
		this.VISIBLE = true;
		this.style.visibility = "visible";
		window.scrollTo(0, 0);
		this.place();
	},
	hide: function(){
		this.VISIBLE = false;
		this.style.visibility = 'hidden';
		this.style.left = -2000 + "px";
	},
	place: function(){
		var w = IE ? document.body.clientWidth : window.innerWidth;
		if(w > this.offsetWidth){
			this.style.left = Math.floor((w - this.offsetWidth) / 2) + 'px';				
		}else{
			this.style.left = 0;
		}
	}
};

function init_tabs(){
	var title = $('ov-title');
	var logo = $('ov-logo');

	var lnxt = $('lnxt');
	var wndt = $('wndt');

	var root = $('tabs');
	if(!root){
		return false;
	}
	var divs = $T('DIV', root);
	var items = [];
	divs.each(function(d){
		if(d.className.indexOf('tab') != -1 && d.className != 'tabs'){
			items.push(d);
		}
	});

	var contents = [];
	var divs2 = $T('DIV', $('white-content'));
	divs2.each(function(d){
		if(d.className.indexOf('content-box') != -1){
			contents.push(d);
		}
	});
//	var contents = $C('content-box', , 'DIV');

	items.each(function(item){
		item.root = root;
		if(item.className.indexOf('here') != -1){
			item.HERE = true;
			item.root.here_item = item;
		}
		var type = item.className.split(' ').last();
		item.content = contents.detect(function(c){
			return c.className.indexOf(type) != -1;
		});
		item.onclick = function(){
			if(this.HERE){
				return false;
			}
			this.root.here_item.className = this.root.here_item.className.replace('here', '');
			this.root.here_item.content.style.display = "none";
			this.root.here_item.HERE = false;

			this.className += " here";
			this.content.style.display = "block";
			this.HERE = true;

			this.root.here_item = this;

			if (item.id == 'lnx')
			{	
				wndt.style.display = 'none';
				
				lnxt.style.display = ''; 
				lnxt.className = 'header'; 
				
				title.innerHTML = 'Informacoes dos Planos de Hospedagem Linux';
				logo.className = 'logo linux';
			}
			if (item.id == 'wnd')
			{
				lnxt.style.display = 'none';
				
				wndt.style.display = ''; 
				wndt.className = 'header'; 
				
				title.innerHTML = 'Informacoes dos Planos de Hospedagem Windows';
				logo.className = 'logo windows';
			}
		};
		item.link = $T('A', item, 1);
		item.link.onclick = function(){
			return false;
		};
	});
}
function init_ctabs(){
	var root = $('c-tabs');
	if(!root){
		return false;
	}
	var divs = $T('DIV', root);
	var items = [];
	divs.each(function(d){
		if(d.className.indexOf('c-tab') != -1){
			items.push(d);
		}
	});
	items.each(function(item){
		item.link = $T('A', item, 1);
		item.link.onmouseover = function(){
			this.className += ' over';
		}.bind(item);
		item.link.onmouseout = function(){
			this.className = this.className.replace('over', '');
		}.bind(item);
	});
}
function init_areas(){
	var root = $('area-body');
	if(!root){
		return false;
	}
	var items = [];
	var divs = $T('DIV', root);
	divs.each(function(d){
		if(d.className.indexOf('area-item') != -1){
			items.push(d);
		}
	});
	items.each(function(item){
		item.img = $T('IMG', item, 1);
		item.img.onmouseover = function(){
			this.className += ' area-hover';
		}.bind(item);
		item.img.onmouseout = function(){
			this.className = this.className.replace('area-hover', '');		
		}.bind(item);
	});
}
function init_popup(){
	var root = $('popup');
	if(!root){
		return false;
	}
	var divs = $T('DIV', root);
	root.b_close = divs.detect(function(d){return d.className.indexOf('close') != -1});
	root.b_close.onclick = function(){
		this.style.display = 'none';
	}.bind(root);
}
function init_panel(){
	var root = $('registre');
	if(!root){
		return false;
	}
	var divs = $T('DIV', root);
	root.bgr_div = divs.detect(function(d){return d.className.indexOf('bgr') != -1});
	if(root.bgr_div.className.indexOf('logged-in') != -1){
		return false;
	}
	
	
		root.button = $T('SPAN', root).detect(function(s){return s.className == 'button'});
		root.HIDDEN = true;
		root.button.onclick = function(){
			if(this.HIDDEN){
				this.bgr_div.className += ' bgr-open';
				this.HIDDEN = false;
			}else{
				this.bgr_div.className = this.bgr_div.className.replace('bgr-open', '');
				this.HIDDEN = true;
			}
		}.bind(root);
}
function fix_popup(){
	var popup = $('popup');
	if(!popup){
		return false;
	}
	var root = $('ie6-body-bgr');
	root.style.height = root.parentNode.offsetHeight + 'px';
}

function init_dom_search(){
	var root = $('dom-search');
	if(!root){
		return false;
	}
	var divs = $T('DIV', root);
	var d_more = divs.detect(function(d){return d.className == 'more'});
	d_more.d_displ = divs.detect(function(d){return d.className == 'display'});
//	d_more.d_dd = divs.detect(function(d){return d.className == 'dropdown'});
	d_more.OPEN = false;
	d_more.onmouseover = function(){
		if(this.OPEN){
			return false;
		}
		this.className += ' more-open';	
		this.OPEN = true;
	};
	d_more.onmouseout = function(){
		if(!this.OPEN){
			return false;
		}
		this.className = this.className.replace('more-open', '').replace(/\s$/, '');
		this.OPEN = false;
	};
	d_more._input = $T('INPUT', d_more, 1);
	divs.each(function(div){
		if(div.className.indexOf('d-item') != -1){
			div.root = d_more;
			div.onclick = function(){	
				this.root._input.value = this.innerHTML;
				this.root.d_displ.innerHTML = this.innerHTML;
				this.root.d_displ.onclick();
			};
			if(IE6){
				div.onmouseover = function(){
					if(this.className.indexOf('d-over') == -1){
						this.className += " d-over";
					}
				}
				div.onmouseout = function(){
					this.className = this.className.replace('d-over', '');
				}
			}
		}
	});
	var ips = $T('INPUT', root);
	var ip_text = ips.detect(function(ip){return ip.type == 'text'});
	ip_text.onfocus = function(){
		if(this.value == 'Dominio: www.'){
			this.value = '';
		}
	};
	ip_text.onblur = function(){
		if(this.value == ''){
			this.value = 'Dominio: www.';
		}
	};
}
function init_dom_tabs(){
	var root = $('dom-tabs');
	if(!root){
		return false;
	}
	var divs = $T('DIV', root);
	var tabs = [];
	var contents = [];
	divs.each(function(div){
		if(div.className.indexOf('tab ') != -1){
			tabs.push(div);
			div.root = root;
			div._type = div.className.split(' ').last().split('-').last();
			if(div.className.indexOf('tab-here') != -1){
				root.here_tab = div;
				div.HERE = true;
			}
		}
		if(div.className.indexOf('content ') != -1){
			contents.push(div);
			div._type = div.className.split(' ').last().split('-').last();
		}
	});
	tabs.each(function(tab){
		tab._content = contents.detect(function(cont){return cont._type == tab._type});
		tab.onclick = function(){
			if(this.HERE){
				return false;
			}
			this._content.className += " cont-here";
			this.className += ' tab-here';
			this.HERE = true;
			this.root.here_tab.className = this.root.here_tab.className.replace('tab-here', '');
			this.root.here_tab._content.className = this.root.here_tab._content.className.replace('cont-here', '');
			this.root.here_tab.HERE = false;
			this.root.here_tab = this;
		};
		if(tab.HERE){
			tab._content.className += ' cont-here';
		}
	});

}
function init_carousel(){
	var root = $('carousel');
	if(!root){
		return false;
	}
	var divs = $T('DIV', root);
	var pic_block = divs.detect(function(d){
		return d.className == 'pic-block';
	});
	var buttons = [];
	var blocks = [];
	root.d_triangle = divs.detect(function(d){return d.className == 'triangle'});

	divs.each(function(d){
		if(d.className == 'block'){
			blocks.push(d);
		}
		if(d.className.indexOf('button ') != -1){
			buttons.push(d);
		}
	});
	buttons.each(function(b, index){
		b.root = root;
		b._block = blocks[index];
		b._type = Number(b.className.split(' ').last().split('-').last());
		if(b.className.indexOf('b-here') != -1){
			b.HERE = true;
			b.root.here_button = b;
			b._block.style.visibility = 'visible';
			new Effect.Appear(b._block, {afterFinish: function(){
				this._block.style.filter = 'opacity(alpha=100)';
				this._block.style.opacity = '1';
			}.bind(b), duration: dur});
			b.root.here_num = b._type;
		}
		b.onclick = function(e, no_timeout){
			if(this.HERE){
				return false;
			}
			Effect.Fade(this.root.here_button._block, {duration: dur, afterFinish: function(){
				this._block.style.visibility = 'visible';
				new Effect.Appear(this._block, {afterFinish: function(){
					this._block.style.filter = 'opacity(alpha=100)';
					this._block.style.opacity = '1';
				}.bind(this), duration: dur});
			}.bind(this)});
			Effect.BlindLeft(this.root.here_button._block, {duration: dur, afterFinish: function(){
				Effect.BlindRight(this._block, {duration: dur});
			}.bind(this)});
			this.root.here_button.className = this.root.here_button.className.replace('b-here', '');
			this.root.here_button.HERE = false;

			this.className += " b-here";
			this.root.here_button = this;
			this.root.d_triangle.style.left = this.offsetLeft + 8 + 'px';
			this.root.here_num = this._type;
			this.HERE = true;
			if(!no_timeout){
				clearTimeout(this.root.timeout);
				this.root.set_animation();
			}
		}
	});

	root.buttons = buttons;
	root.set_animation = function(){
		this.timeout = setTimeout(function(){
			var button = this.buttons[this.here_num] || this.buttons[0];
			button.onclick(true, true);
			this.set_animation();
		}.bind(this), 6000);	
	};
	root.set_animation();
	
}