
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}



function autorizacaoPagina(url){
	if(!getCookie("autorizado")){		
        document.location = '/celular/disclaimer.shtml'+"?urlAuth="+url;
    }else{
		if(document.location  != url){
			document.location = url;
		}
	}
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable){
      return pair[1];
    }
  } 
  return "";
}

var selOp = 'vazio';
var mfunOp = '';
var idOp;
var mfun_param;
var marca;
var mfunCod;

function infoOperadora(op) {
	var divs,i,j;
	divs=document.getElementById('detalhe_produto').getElementsByTagName('div');
	var idDiv;
	if(mfunCod && mfunCod != '') {
		document.getElementById('mfun').style.display = 'block';
		document.getElementById('detalhe_produto').style.display = 'none';
		document.getElementById('produto_indisponivel').style.display = 'none';
		
		document.getElementById('centro').getElementsByTagName("blockquote")[0].style.display = "none";
		if(document.getElementById('box_exemplos')){
			document.getElementById('box_exemplos').style.display = "none";
		}
		
		if(getCookie('operadora')){
			carrier = getCookie('operadora').toUpperCase();
		}
		
		document.getElementById("iframe_mfun").src = "http://abrilweb.m4u.com.br/abril/web/mediaList.do?carrier="+carrier+"&"+mfunCod;
		
	} else {
		if(document.getElementById('mfun')) document.getElementById('mfun').style.display = 'none';
		
		if(dispOp.indexOf(op) != -1){
			document.getElementById('produto_indisponivel').style.display = 'none';
			document.getElementById('detalhe_produto').style.display = 'block';
		} else {
			//produto indisponível
			if(document.getElementById('produto_indisponivel').style.display == 'none') 
				document.getElementById('detalhe_produto').style.display = 'none';
				
			document.getElementById('produto_indisponivel').style.display = 'block';
			document.frmFaleConoscoHidden.Operadora.value = op;
			document.frmFaleConoscoHidden.Produto.value = requestProduto();
			document.frmFaleConoscoHidden.Revista.value = requestRevista();
		}
	}
	
	for(i in divs) {
		idDiv = String(divs[i].id);
		if(idDiv.indexOf(selOp) > -1) {
			divs[i].style.display = 'none';
		}
	}
	for(j in divs) {
		idDiv = String(divs[j].id);
		if(idDiv.indexOf(op) > -1) {
			divs[j].style.display = 'block';
		}
	}
	
	selOp = op;
	marca = mfun_param.split("|");
	
	if(marca[1] != "" && marca[1] != "ap"){
		document.getElementById('marca').innerHTML = "<b>Marca associada:</b><a href=/celular/revistas-abril.shtml?acao=rev&param="+marca[1]+"><img src=\"/celular/imagem/logo_"+marca[1]+".gif\"></a>";
	}
	
	if(document.getElementById("menu_operadoras")){
		document.getElementById("menu_operadoras").style.display = "none";
	}
}

function requestProduto(){
	produto = document.getElementById('centro').getElementsByTagName("blockquote")[0].getElementsByTagName("span")[0].innerHTML;
	produto = produto.split("&gt; ")
	return produto[1];
}

function requestRevista(){
	return document.getElementById('centro').getElementsByTagName("blockquote")[0].getElementsByTagName("span")[0].getElementsByTagName("b")[0].innerHTML
}

function mostraBanner(posicao) {
	var arrB;
	if(posicao == "banner01") {
		arrB = arrBanner01[b1];
		arrB = arrB.split('*|*');
		b1++;
		if(b1 > arrBanner01.length-1) b1 = 0;
	} else if(posicao == "banner02") {
		arrB = arrBanner02[b2];
		arrB = arrB.split('*|*');
		b2++;
		if(b2 > arrBanner02.length-1) b2 = 0;
	} else if(posicao == "box_aparelho") {
		arrB = arrBoxAp[ap];
		arrB = arrB.split('*|*');
		ap++;
		if(ap > arrBoxAp.length-1) ap = 0;
	}
	
	if(document.getElementById(posicao)) {
		document.getElementById(posicao).src = "/celular/" + arrB[0];
		document.getElementById(posicao).alt = arrB[3];
	}
	if(document.getElementById('lk_'+posicao)) document.getElementById('lk_'+posicao).href = arrB[1];
	if(document.getElementById('lk_'+posicao)) document.getElementById('lk_'+posicao).target = arrB[2];
	if(document.getElementById('ch_'+posicao)) document.getElementById('ch_'+posicao).innerHTML = arrB[4];
}


function mostraEscondeDiv (nomeDiv){
		if(document.getElementById(nomeDiv).style.display == '')
			document.getElementById(nomeDiv).style.display = 'none';
		else
			if(document.getElementById(nomeDiv).style.display == 'none') document.getElementById(nomeDiv).style.display = '';
	}
	function mostraDiv (nomeDiv){
	document.getElementById(nomeDiv).style.display = '';
	}
	function escondeDiv (nomeDiv){
	document.getElementById(nomeDiv).style.display = 'none';
}
	
	
function enviar(frm) {
	if(frm.Email.value != '' && frm.DDD.value != '' && frm.Celular.value != ''){
		frm.Produto.value=document.getElementById('tit_produto').innerHTML;
		frm.Revista.value=bt;
		var i;
		for (i=0;i<document.frmOp.operadora.length;i++){ 
		   	if (document.frmOp.operadora[i].checked) {
		       	frm.Operadora.value = document.frmOp.operadora[i].value;
				break; 
			}
		} 
		return true;
	} else {
		alert('Email, DDD e Celular são campos obrigatórios. Por favor, preencha antes de enviar.');
		return false;
	}
}

var PWA = {
	isIE: function() {
		/*@cc_on return true; @*/
		return false;
	},
	isIE6: function() {
		/*@cc_on @if (@_jscript_version <= 5.6) return true; @end @*/
		return false;
	},
	isIE7: function() {
		/*@cc_on @if (@_jscript_version > 5.6) return true; @end @*/
		return false;
	}
};

PWA.Home = {
	
};

PWA.Home.Especiais = {

	setStyle: function(className){
		var miolo = document.getElementById("miolo");
		miolo.className = className;
		for(i=0;i<miolo.childNodes.length;i++){
			if(miolo.childNodes[i].id == "title"){
				miolo.childNodes[i].innerHTML = "<h1>"+PWA.Home.titles[className]+"</h1>";
			}
		}
		if(document.getElementById("stylecanal")){
			document.getElementById("stylecanal").href="/css/"+className+".css";
		}
	},
	setStyleFromUrl: function(){
		var miolo = document.getElementById("miolo");
		var url = window.location.pathname;
		var className = url.replace(/.*?\/(.*?)\/.*/,"$1");
		miolo.className = className;
		for(i=0;i<miolo.childNodes.length;i++){
			if(miolo.childNodes[i].id == "title"){
				miolo.childNodes[i].innerHTML = "<h1>"+PWA.Home.titles[className]+"</h1>";
			}
		}
		document.getElementById("stylecanal").href="/css/"+className+".css";
	}

};

PWA.Home.Rollover = {
	init: function(element) {
		var lis = element.parentNode.getElementsByTagName("a");
		var imgs = element.parentNode.getElementsByTagName("img");
		
		for(var i=0;i<lis.length;i++){
			lis[i].style.background = "#fff";
			imgs[i].style.display = "none";
			if(element == lis[i]){
				lis[i].style.background = "#eaf29c";
				imgs[i].style.display = "block";
			}
		}
	}
};

PWA.Home.TV = {
	totalChannels: null,
	currentChannel: null,
	currentOpacity: null,
	opacityIncrement: null,
	rotatePaused: false,
	opacitySteps: 10,


	init: function(total, start) {
		jslog.info("total: "+total+", start: "+start);
		this.totalChannels = total;
		this.currentChannel = start;
		this.changeChannel(start);
	},

	changeOpacity: function(element, opacity) {
		try {
			if (PWA.isIE6()) {
				element.style.filter = "alpha(opacity="+opacity+")";
			} else if (PWA.isIE7()) {
				element.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+opacity+")";
			} else {
				var floatOpacity = (opacity/100);
				element.style.opacity = floatOpacity;
				element.style.MozOpacity = floatOpacity;
				element.style.KhtmlOpacity = floatOpacity;
			}
		} catch (e) {
		}
	},

	calculateOpacity: function(id, start, end, opacity) {
		try {
			if (opacity == undefined) {
				PWA.Home.TV.currentOpacity = start;
			} else {
				PWA.Home.TV.currentOpacity = opacity;
			}
			if (PWA.Home.TV.opacityIncrement == null) {
				PWA.Home.TV.opacityIncrement = Math.round((end-start)/PWA.Home.TV.opacitySteps);
			}
			PWA.Home.TV.changeOpacity(document.getElementById(id), PWA.Home.TV.currentOpacity);
			PWA.Home.TV.currentOpacity += PWA.Home.TV.opacityIncrement;
			if (PWA.Home.TV.currentOpacity <= end) {
				setTimeout("PWA.Home.TV.calculateOpacity('"+id+"', "+start+", "+end+", "+PWA.Home.TV.currentOpacity+")", 50);
			}
		} catch (e) {
		}
	},

	rotateChannel: function(time) {
		tTV = window.setInterval("PWA.Home.TV.changeChannel('rotate')", time * 1000);
	},

	pauseRotate: function(pause) {
		if (pause == true) {
			PWA.Home.TV.rotatePaused = true;
		} else {
			PWA.Home.TV.rotatePaused = false;
		}
	},

	changeChannel: function(ch) {
		if(!isNaN(ch)){
			this.currentChannel = ch;
		} else {
			if(ch == "previous") {
				jslog.info("user clicked on number");
				this.currentChannel--;
				if(this.currentChannel < 0) this.currentChannel = (this.totalChannels-1);
			} else if(ch == "next") {
				jslog.info("user clicked next");
				this.currentChannel++;
				if(this.currentChannel >= this.totalChannels) this.currentChannel = 0;
			} else if(ch == "rotate" && !this.rotatePaused) {
				jslog.info("rotate mode");
				this.currentChannel++;
				if(this.currentChannel >= this.totalChannels) this.currentChannel = 0;
			} else {
				return false;
			}
		}

		jslog.info("changing channel to "+this.currentChannel);

		for(i=0;i<this.totalChannels;i++){
			if(i == this.currentChannel){
				document.getElementById("rotativo"+(i+1)).style.display ="block";
				document.getElementById("bt_"+(i+1)).style.background = "transparent url('/celular/css/img/bt_paginacao.gif') repeat-y 2px 0";
				this.calculateOpacity("rotativo"+(i+1), 0, 100);
			} else {
				document.getElementById("rotativo"+(i+1)).style.display = "none";
				document.getElementById("bt_"+(i+1)).style.background = "none";
			}
		}
		return false;
	}
};

function shL(layer){
	if(document.getElementById(layer).style.display == 'block' || document.getElementById(layer).style.display == ''){
		document.getElementById(layer).style.display = 'none';	
	}
	else{
		document.getElementById(layer).style.display = 'block';
	}
}

function escreveBanners(position){
	var elemDivOas = document.getElementById(position + "_X");
	var elemDivOasDisplay = document.getElementById(position);
		
	if(elemDivOasDisplay != null){
		elemDivOasDisplay.appendChild(elemDivOas);
		elemDivOas.style.display='block';
	}
}

function escreveIframe(){
	if(document.getElementById("detalhe_produto_trf")){
		document.getElementById("detalhe_produto").innerHTML = document.getElementById("detalhe_produto_trf").innerHTML;
		document.getElementById("detalhe_produto_trf").innerHTML = "";
	}
}
