/* Javascript para funcionalidades do template do PWA */
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 = {
	titles: {
		generic: "<a href=\"/\">Geral</a>",
		diversao: "<a href=\"/diversao\">Diversão</a>",
		esportes: "<a href=\"/esportes\">Esportes</a>",
		mulher: "<a href=\"/mulher\">Mulher</a>",
		noticias: "<a href=\"/noticias\">Notícias</a>",
		revistas: "<a href=\"/revistas\">Revistas</a>",
		saude: "<a href=\"/saude\">Saúde</a>"
	},

	toggleAba: function(obj) {  
		var tab = obj.parentNode;
		var tabId = tab.id.substr((tab.id.length-1), 1);	
	  	var box = document.getElementById('box-mais-populares');
		var getTag = box.getElementsByTagName('h4');
		var quant = getTag.length;
		for(x=0;x<quant;x++) {
  			getTag[x].className = getTag[x].className.replace("selected", "");
			noc = document.getElementById("boxAba" + (x+1))
			noc.className = noc.className.replace("selected", "");  		
		}
		document.getElementById("boxAba" + tabId).className = document.getElementById("boxAba" + tabId).className + " selected"
		getTag[(tabId-1)].className = getTag[(tabId-1)].className + "selected" ;  
		    
		return false;
	}
};
PWA.Home.Especiais = {

	setStyle: function(className,a){
		var miolo = document.getElementById("miolo");
		miolo.className = className;
		if (a != 'ok') {
			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";
		}
	},
	list: function(pos,qtd){
		var porColuna = (qtd+1)/4;
		var valor = Math.ceil(porColuna);
		if((pos+1)%valor == 0){
			document.write('</div><div>');
		}
	},
	setStyleFromUrl: function(a){
		
		var miolo = document.getElementById("miolo");
		var url = window.location.pathname;
		var className = url.replace(/.*?\/(.*?)\/.*/,"$1");
		miolo.className = className;
		if (a != 'ok') {
			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.parentNode.getElementsByTagName("li");
		for(var i=0;i<lis.length;i++){
			lis[i].className = "off";
			if(!lis[i].onmouseover){
				lis[i].onmouseover = function(e) {
					var imgs = element.parentNode.parentNode.parentNode.getElementsByTagName("img");
					var lis = element.parentNode.parentNode.getElementsByTagName("li");
					var idimg = this.getElementsByTagName("a")[0].id.replace("btn","");

					for (var i = 0; i < lis.length; i++) {
						lis[i].className = "off";
						if(imgs[i].id == idimg){
							imgs[i].className = "on";
						} else {
							imgs[i].className = "off";
						}
					}
					this.className = "on";
				}
			}
		}
		element.className = "on";
	}
};

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);
	},

    hiddenObjectUno: function(){
        document.getElementById("btTV0").style.display="none";
        document.getElementById("bt-esq").style.display="none";
        document.getElementById("bt-dir").style.display="none";
    },
    
    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){
				if(document.getElementById("scrTV"+i).className == "oas"){
					document.getElementById("modTV").style.display="none";
					if(document.getElementById("modOAS")) document.getElementById("modOAS").style.display="block";
				} else {
					if(document.getElementById("modOAS")) document.getElementById("modOAS").style.display="none";
					document.getElementById("modTV").style.display="block";
				}
				document.getElementById("scrTV"+i).style.display="block";
				document.getElementById("chTV"+i).style.display="block";
				document.getElementById("btTV"+i).className="on";
				this.calculateOpacity("imgTV"+i, 0, 100);
			} else {
				document.getElementById("scrTV"+i).style.display="none";
				document.getElementById("chTV"+i).style.display="none";
				document.getElementById("btTV"+i).className="";
			}
		}
		return false;
	}

};
PWA.Home.fullDate = function(today) {
	return fullDate(today);
};

/** REFATORAR **/
function socialBookmark(svc,t,u){
	var title;
	var url;

	if(!t) title = document.title;
	else title = t;
	if(!u) url = document.location.href;
	else url = u;

	switch (svc){
		case 'delicious':
			window.open('http://del.icio.us/post?url=' + url + '&title=' + title,'wSocialBM');
			break;
		case 'blinklist':
			window.open('http://www.blinklist.com/index.php?Action=Blink/addblink.php&Url=' + url + '&Title=' + title,'wSocialBM');
			break;
		case 'feedmelinks':
			window.open('http://feedmelinks.com/categorize?from=toolbar&op=submit&name=' + title + '&url=' + url,'wSocialBM');
			break;
		case 'google':
			window.open('http://www.google.com/bookmarks/mark?op=edit&bkmk=' + url + '&title=' + title,'wSocialBM');
			break;
		case 'newsvine':
			window.open('src="/js/scripts.js"http://www.newsvine.com/_wine/save?popoff=0&u=' + url + '&h=' + title,'wSocialBM');
			break;
		case 'reddit':
			window.open('http://reddit.com/submit?url=' + url + '&title=' + title,'wSocialBM');
			break;
		case 'yahoo':
			window.open('http://myweb2.search.yahoo.com/myresults/bookmarklet?t=' + title + '&u=' + url,'wSocialBM');
			break;
		case 'facebook':
			window.open('http://www.facebook.com/sharer.php?u=' + url + '&t=' + title,'wSocialBM');
			break;
		case 'technorati':
			window.open('http://technorati.com/faves?add=' + url + '&title=' + title,'wSocialBM');
			break;
		case 'digg':
			window.open('http://digg.com/submit?phase=2&url=' + url + '&title=' + title,'wSocialBM');
			break;
		case 'rec6':
			window.open('http://rec6.via6.com/link.php?url=' + url + '&title=' + title,'wSocialBM');
			break;
		case 'twitter':
			window.open('http://twitter.com/home?status=' + title + ':%20' + url,'wSocialBM');
			break;
		case 'windowslive':
			window.open('https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=' + url + '&title=' + title,'_self');
			break;
		case 'netvibes':
			window.open('http://www.netvibes.com/subscribe.php?type=rss&url=http://www.abril.com.br/rss/home.xml','wSocialBM');
			break;
	}
}

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 = '';
		}
	}
}
/** REFATORAR **/

/*muda img do horoscopo*/
sig = function(obj){
	this.signJS = {
		"signos": [
			{"sgn": "Capricórnio", "dia": "22",	"img": "capricornio"}, 
			{"sgn": "Aquário",	"dia": "18","img": "aquario"}, 
			{"sgn": "Peixes","dia": "19","img": "peixes"}, 
			{"sgn": "Aries","dia": "21","img": "aries"}, 
			{"sgn": "Touro","dia": "21","img": "touro"}, 
			{"sgn": "Gêmeos","dia": "21","img": "gemeos"},
			{"sgn": "Cancer","dia": "21","img": "cancer"}, 
			{"sgn": "Leão","dia": "23","img": "leao"}, 
			{"sgn": "Virgem","dia": "23","img": "virgem"}, 
			{"sgn": "libra","dia": "23","img": "libra"}, 
			{"sgn": "Escorpião","dia": "23","img": "escorpiao"}, 
			{"sgn": "Sagitário","dia": "22",	"img": "sagitario"}, 
		]
	};
//	this.serv = 'http://homolog.abril.com.br'
//	this.serv = 'http://www.abril.com.br'
	this.serv = ''
	this.ox = new Date();
	this.ms = ox.getMonth();
	this.hj = ox.getDate();
	(this.hj < signJS.signos[this.ms].dia) ? this.ms : this.ms = (this.ms + 1);
	(this.ms > 11)?this.ms = 0 : this.ms;
	this.nm = document.getElementById(obj);
	this.nm.src = this.serv + "/css/img/ico-" + signJS.signos[ms].img + '.png';
	this.nm.title = signJS.signos[ms].sgn;
	this.nm.alt = signJS.signos[ms].sgn; 
}
/*funções para formatar texto do twitter*/
    
    function relative_time(C){
        var B = C.split(" ");
        C = B[1] + " " + B[2] + ", " + B[5] + " " + B[3];
        var A = Date.parse(C);
        var D = new Date();
		tw = (D.getTimezoneOffset()/60)
        TX = B[3].split(':');
        return (TX[0]-tw) + ":" + TX[1]
    };

function parseNameOrIngredientHome(frm, field) {
	if (/busca\/.+$/.test(frm.action)) return false;
	var v = frm[field].value.toLowerCase();
	if (v == 'BUSCA') {
		return false;
	}
	try {
		v = v.replace(/[\s\-]+/g, "-").replace(/[\,\.\;\!\/]/g, '');
		var a = frm.action + '/' + v;
		var f = document.createElement('form');
		f.style.display = 'none';
		frm.parentNode.appendChild(f);
		f.method = 'POST';
		f.action = a;
	/*	var s = document.createElement('input');
		s.setAttribute('type', 'hidden');
		s.setAttribute('name', 'authenticity_token');
		s.setAttribute('value', frm.authenticity_token.value);
		f.appendChild(s);	// */
		f.submit();
		return false;
	} catch (e) {
		alert(e);
	}
	return false;
}
function muda(nome) {
	document.body.className=nome;
//	document.getElementById('oas_visagofans').src = "http://oasadmin.abril.com.br/RealMedia/ads/adstream_nx.ads/abril/home@x70!x70";
}

