var IE = !window.getComputedStyle && !!document.getElementById;
var IE6 = IE && navigator.appVersion.indexOf('MSIE 7') == -1 && navigator.appVersion.indexOf('MSIE 8') == -1
var selects = [];
window.onload = function(){
	init_overlay();
	init_tabs();
	init_ctabs();
	init_menu();
	init_areas();
	if(IE6){
		selects = $T('SELECT');
	}
};
function init_menu(){
	var root = $('menu');

	if(!root){
		return false;
	}
	var items = $C('item', root, 'DIV');
	var width = 0;

	items.each(function(item, ind){
		width += item.offsetWidth;
		if(IE6){
			if(!ind){
				item.FIRST = true;;
			}
			item.onmouseover = function(){
				if(this.FIRST){
					this.addClass('over-first');
				}
				this.addClass('over');
			};
			item.onmouseout = function(){
				if(this.FIRST){
					this.removeClass('over-first');
				}
				this.removeClass('over');
			};
		}
	});
	$('menu-margin').style.width = width + "px";
}
var overlay;
function init_overlay(){
	var root = $('overlay');
	if(!root){
		return false;
	}
	root.iframe = $T('IFRAME', root, 1);
	root.button = $C('b-close', root, 'DIV', 1);
	Object.extend(root, overlay_funcs);
	root.button.onclick = root.hide.bind(root);

	var items = $C('item2', $('white-block'), 'DIV');
	items.each(function(item){
		item.link = $C('lm', item, 'A', 1);
		item.link.onclick = function(){
			overlay.show();
			overlay.iframe.src = this.href;
			return false;
		};
	});
	root.zagl = $('zaglushka');
	root.resize_zagl();
	overlay = root;
	window.onresize = function(){
		if(overlay.VISIBLE){
			overlay.place();
			overlay.resize_zagl();
		}
	};
}
var overlay_funcs = {
	show: function(){
		var title = $('ov-title');
		var logo = $('ov-logo');

		var lnxt = $('lnxt');
		var wndt = $('wndt');
					
		var root = $('tabs');
		if(!root){
			return false;
		}
		var items = $C('tab', root, 'DIV');
		items.each(function(item){
		item.root = root;
			if(item.hasClass('here')){
				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';
				}
			}
		});
		
		this.VISIBLE = true;
		this.style.visibility = "visible";
		this.zagl.style.visibility = 'visible';
		window.scrollTo(0, 0);
		this.place();
		selects.each(function(sel){
			sel.style.visibility = 'hidden';
		});
	},
	hide: function(){
		this.VISIBLE = false;
		this.style.visibility = 'hidden';
		this.zagl.style.visibility = 'hidden';
		this.style.left = -2000 + "px";
		selects.each(function(sel){
			sel.style.visibility = 'visible';
		});
	},
	place: function(){
		var w = IE ? document.body.clientWidth : window.innerWidth;
		var h = IE ? document.body.clientHeight : window.innerHeight;
		if(w > this.offsetWidth){
			this.style.left = Math.floor((w - this.offsetWidth) / 2) + 'px';				
		}else{
			this.style.left = 0;
		}
		if(h > this.offsetHeight){
			this.style.top = Math.floor((h - this.offsetHeight) / 2) + 'px';						
		}else{
			this.style.top = 0;
		}
	},
	resize_zagl: function(){
		var h;
		if(IE){
			h = document.body.clientHeight;
		}else{
			var h2 = $('wrap').offsetHeight;
			h = Math.max(h2, window.innerHeight);
		}
		this.zagl.style.height = h + 'px';
	}
};

function init_tabs(){
	var root = $('tabs');
	if(!root){
		return false;
	}
	var items = $C('tab', root, 'DIV');
	var contents = $C('content-box', $('white-content'), 'DIV');
	items.each(function(item){
		item.root = root;
		if(item.hasClass('here')){
			item.HERE = true;
			item.root.here_item = item;
		}
		var type = item.className.split(' ').last();
		item.content = contents.detect(function(c){
			return c.hasClass(type);
		});
		item.onclick = function(){
			if(this.HERE){
				return false;
			}
			this.root.here_item.removeClass('here');
			this.root.here_item.content.style.display = "none";
			this.root.here_item.HERE = false;

			this.addClass('here');
			this.content.style.display = "block";
			this.HERE = true;

			this.root.here_item = this;
		};
		item.link = $T('A', item, 1);
		item.link.onclick = function(){
			return false;
		};
	});
}
function init_ctabs(){
	var root = $('c-tabs');
	if(!root){
		return false;
	}
	var items = $C('c-tab', root, 'DIV');
	items.each(function(item){
		item.link = $T('A', item, 1);
		item.link.onmouseover = function(){
			this.addClass('over');
		}.bind(item);
		item.link.onmouseout = function(){
			this.removeClass('over');
		}.bind(item);
	});
}

function init_areas(){
	var root = $('area-body');
	if(!root){
		return false;
	}
	var items = $C('area-item', root, 'DIV');
	items.each(function(item){
		item.link = $C('link', item, 'A', 1);
		item.link.style.width = item.offsetWidth + 'px';
		item.link.style.height = item.offsetHeight + 'px';
		item.link.onmouseover = function(){
			this.addClass('area-hover');
		}.bind(item);
		item.link.onmouseout = function(){
			this.removeClass('area-hover');		
		}.bind(item);
	});
}
