

// JavaScript Document
merlin={};
loco={};
loco.menu={};
Object.extend(merlin,{
	font: Object,
	forms: Object,
	effects: Object
});
merlin.effects=function(){};
merlin.effects.prototype={
	show:function(){},
	hide:function(){},
	doEffect:function(){},
	element:null
}
merlin.effects.baseShowHide=Class.create();
merlin.effects.baseShowHide.prototype=Object.extend(new merlin.effects,{
	initialize:function(ele){
		this.element=$(ele);
		return this;
	},
	show: function(){
		Element.show(this.element);
	},
	hide: function(){
		Element.hide(this.element);
	},
	doEffect: function(){
		if (Element.visible(this.element)) {
			this.hide();
		}
		else {
			this.show();
		}
	}
});

merlin.effects.moo={};

merlin.effects.moo.height=Class.create();
merlin.effects.moo.height.prototype=Object.extend(new merlin.effects,{
	initialize:function(ele){
		this.element=$(ele);
		this.effect=new fx.Height(this.element,{duration: 350, onComplete: this._callback.bind(this)});	
		this.effect.set(0);
		this.callbackFunction=null;
		return this;
	},
	show:function(){
		Element.show(this.element);
		this.callbackFunction=null;
		this.effect.custom(0, this.element.scrollHeight);
		this.visible=true;
	},
	hide:function(){
		this.callbackFunction = function(){
			Element.hide(this.element);
		}
		this.effect.custom(this.element.offsetHeight, 0);
		this.visible=false;
	},
	doEffect: function(){
		this.effect.toggle();
		this.visible=!this.visible;
	},
	_callback: function(){
		if(this.callbackFunction!=null){
			this.callbackFunction();
		}
	}
});
merlin.font.size=Class.create();
merlin.font.size.prototype = {
	initialize: function(conf){
		Object.extend(this,
			Object.extend({
				small	: "merlin-fontes-size-small",
				big		: "merlin-fontes-size-big",
				element	: "merlin-fontes-size",
				size	:12,
				sizeMin	:11,
				sizeMax	:14,
				unit	:'px'
			}, conf || {})
		);
		
		Event.observe(this.small, "click", this._small.bind(this));
		Event.observe(this.big, "click", this._big.bind(this));
		this.element = $(this.element);
		
		this.element.style.fontSize = this.size + "px";
		return this;
	},
	_small: function(){
		eln = this.element.style.fontSize.substr(0, 2);
		if (eln > this.sizeMin) {
			this.element.style.fontSize = --eln + this.unit;
		}
		return this;
	},
	_big: function(){
		eln = this.element.style.fontSize.substr(0, 2);
		if (eln < this.sizeMax) {
			this.element.style.fontSize = ++eln + this.unit;
		}
		return this;
	},
	getElement: function(){
		return this.element;
	}
}
loco.menu.subMenu=Class.create();
loco.menu.subMenu.prototype = {
	initialize: function(mn){
		this.menu=Object.extend({
			handler	: $(mn.handler),
			subMenu	: $(mn.subMenu),
			effect	: merlin.effects.baseShowHide,
			evtType	: 'click'
		}, mn || {});
		
		this.timer=null;
		this.menu.effect = new this.menu.effect(this.menu.subMenu);

		switch (mn.evtType){
			case "click":
				Event.observe(this.menu.handler, "click", this.menu.effect.doEffect.bind(this.menu.effect));
				break;
			case "mouseover":
				Event.observe(this.menu.handler, "mouseover", this.show.bind(this));
				Event.observe(this.menu.handler, "mouseout", this.hide.bind(this));
				Event.observe(this.menu.subMenu, "mouseover", this.show.bind(this));
				Event.observe(this.menu.subMenu, "mouseout", this.hide.bind(this));
				break;
		}
	},
	hide:function(evt){
		//if(!Element.hasClassName(Event.findElement(evt,"div"))
			this.timer=setTimeout(this.menu.effect.hide.bind(this.menu.effect),300);
	},
	show:function(evt){
		if(this.timer!=null){
			clearTimeout(this.timer);
			this.timer=null;
		}
		if(!this.menu.effect.visible)	this.menu.effect.show();
	}
}
merlin.forms.resetField=Class.create();
merlin.forms.resetField.prototype = {
	initialize: function(ele){
		this.ele = $(ele);
		this.ele.initValue = this.ele.value;
		Event.observe(this.ele, "focus", this.focus.bind(this));
		Event.observe(this.ele, "blur", this.blur.bind(this));
	},
	focus: function(){
		if (this.ele.value == this.ele.initValue) 
			this.ele.value = "";
	},
	blur: function(){
		//verifica se esta vazio
		if (/^\s*$/.test(this.ele.value)) 
			this.ele.value = this.ele.initValue;
	}
}
madeTelecom=Class.create();
madeTelecom.prototype = {
	isHome: Boolean,
	hasNews: Boolean,
	hasSearch: Boolean,
	hasTitleAbsolute: Boolean,
	subMenu: loco.menu.subMenu,
	initialize: function(){
		this.hasNews=!!$("merlin-fontes-size-small");
		this.hasTitleAbsolute=!!$("interno");
		this.hasSearch=!!$("busca");
		this.subMenu = new loco.menu.subMenu({
			handler: "outrosSites",
			subMenu: "subMenu",
			evtType: "mouseover",
			effect: merlin.effects.moo.height
		});
		if(this.hasNews) this.configureTexts();
		if (this.hasTitleAbsolute) {
			this.configureTitleAbsolute();
			Event.observe(window, "resize", this.configureTitleAbsolute);
			this.initSifr();
		}
		if(this.hasSearch) new merlin.forms.resetField($("busca"));
		return this;
	},
	configureTexts: function(){
		new merlin.font.size({
			size: 12
		});
		new merlin.font.size({
			size: 12,
			small: "merlin-fontes-size-small2",
			big: "merlin-fontes-size-big2"
		});
	},
	configureTitleAbsolute: function(){
		//Se for ie 5.5 ou 6
		if (/MSIE (5\.5|6\.)/.test(navigator.userAgent) && $("interno")) {
			if (document.documentElement.clientWidth < 1000) {
				$("interno").style.width = "1000px";
			}
			else {
				$("interno").style.width = "99.85%";
			}
		}
	},
	initSifr: function(){
		sIFR.replaceElement(named({
			sSelector: "h2.sifr",
			sFlashSrc: "css/sifr/HandelGotDBol.swf",
			sColor: "#fe4801",
			sBgColor: "#000000",
			sWmode: "transparent",
			sCase:"upper"
		}));
		sIFR.replaceElement(named({
			sSelector: "h3.sifr",
			sFlashSrc: "css/sifr/Arial80P.swf",
			sColor: "#656262",
			sBgColor: "#000000",
			sWmode: "transparent"
		}));
	}
}
Event.onDOMReady(function(){
	new madeTelecom();
});


/* Pesquisa nome da rua baseado no CEP
 * Autor: Vinicius Gallafrio - www.madeinweb.com.br
 *
 * Versão Inicial - 23/04/2008
 */
function pesquisarCep(sCEP){
	
	if ((obj=MM_findObj('msg'))!=null) obj.className = 'loading';
		
	if (sCEP.length != 9) return;
	sCEP = sCEP.replace(/-/g,'');
	
	try{ xmlhttp = new XMLHttpRequest(); }catch(ee){
		try{  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){
			try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");} catch(E) { xmlhttp = false;	}
		}
	}
	
	//Abre a url
    xmlhttp.open("GET", "pesquisar_cep.asp?cep="+sCEP,true);

    //Executada quando o navegador obtiver o código
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			eval(xmlhttp.responseText);
			if ((obj=MM_findObj('msg'))!=null) obj.className = 'msgAjax';
        }
    }
    xmlhttp.send(null)
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
