/* 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: "Geral",
		diversao: "Diversão",
		esportes: "Esportes",
		mulher: "Mulher",
		noticias: "Notícias",
		revistas: "Revistas",
		saude: "Saúde"
	},

	toggleAba: function(obj) {
		var tab = obj.parentNode;
		var tabId = tab.id.substr((tab.id.length-1), 1);
		var box = document.getElementById("boxAba"+tabId);

		if (tab && tab.className.indexOf("selected") == -1) {
			tab.className = tab.className + " selected";
		}

		if (box && box.className.indexOf("selected") == -1) {
			box.className = box.className + " selected";
		}

		var otherTab = null;
		var otherTabId = null;
		var otherBox = null;

		if (tabId == 1) {
			otherTabId = 2;
		} else {
			otherTabId = 1;
		}

		otherTab = document.getElementById("aba"+otherTabId);
		otherBox = document.getElementById("boxAba"+otherTabId);

		otherTab.className = otherTab.className.replace("selected", "");
		otherBox.className = otherBox.className.replace("selected", "");

		return false;
	}
};
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";
		}
	},
	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(){
		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.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);
	},

	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("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) {
	var splitDate = today.split("-");
	var months = new Array("Janeiro","Fevereiro","Mar&ccedil;o","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro");
	var weekdays = new Array("Domingo","Segunda-feira","Terça-feira","Quarta-feira","Quinta-feira","Sexta-feira","Sábado");
	todayDate = new Date() ;
	todayDate.setFullYear(splitDate[0], (splitDate[1]-1), splitDate[2]);
	return (weekdays[todayDate.getDay()] + ", " + todayDate.getDate() + " de " + months[todayDate.getMonth()].toLowerCase() + " de " + todayDate.getFullYear());
};



/** 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 **/
