// JavaScript Document
if(!area_pessoal) var area_pessoal = false;
var pagesContents = "xml/pages.xml";
var areaPagesContents = "xml/area_pages.xml";
var commonContents = "xml/common.xml";
var imagesPath = "images/";
var header = new Spry.Data.XMLDataSet(commonContents, "common/header");
var footer = new Spry.Data.XMLDataSet(commonContents, "common/footer");
var pages = new Spry.Data.XMLDataSet(area_pessoal ? areaPagesContents : pagesContents, area_pessoal ? "area_pages/page" : "pages/page");
pages.setColumnType("pic", "image");
pages.setColumnType("texto",'html');
pages.loadData();
var obs = new Object();
obs.onPostLoad = function(notifier, data){
	pages.setCurrentRow(selectedPageIndex);
};
pages.addObserver(obs);

function getRandomPic(region, lookupFunc){
	var rowCount = parseInt(lookupFunc('{contentPics::ds_RowCount}'));
	switch(rowCount){
		case 0:
			return '';
			break;
		case 1:
			return imagesPath + lookupFunc('{contentPics::pic}');
			break;
		default:
			var randomPic = Math.round(Math.random()*(rowCount-1)) + 1;
			var pic = lookupFunc('{contentPics::pic}').split('.');
		//	alert(imagesPath + pic[0].substring(0,pic[0].length-1) + randomPic + "." + pic[1]);
			return imagesPath + pic[0].substring(0,pic[0].length-1) + randomPic + "." + pic[1];
	}
}
/*
function setPicObserver(picDataSet){
	var picsObs = new Object;
	picsObs.onPostLoad = function(notifier, data){
		var randomPic = Math.round(Math.random()*(picDataSet.getRowCount()-1));
		picDataSet.setCurrentRow(randomPic);
//		picDataSet.getCurrentRow()["pic"] = imagesPath + picDataSet.getCurrentRow()["pic"];
	};
	picDataSet.addObserver(picsObs);
}
*/
var dataDeHoje = new Date();

var allPages = area_pessoal ? ['inicio','perfil','campanhas','womcast','questionarios','informacao'] 
							: ['informacao','registo','campanhas','womcast','noticias','contactos'];
var currentPage = allPages[selectedPageIndex];
var menuCoord = 13 + 26*selectedPageIndex;

if(typeof(window.external) != 'undefined'){
	//yes, this is evil browser sniffing, but only IE has this bug
	document.getElementsByName = function(name, tag){
		if(!tag){
			tag = '*';
		}
		var elems = document.getElementsByTagName(tag);
		var res = []
		for(var i=0;i<elems.length;i++){
			att = elems[i].getAttribute('name');
			if(att == name) {
				res.push(elems[i]);
			}
		}
		return res;
	}
}

var styleArray = ['#66cc00', '#000000', '#cc3333', '#0085ca', '#ff9900', '#993333', '#ff9933', '#564b3b'];
var ran;
//randomly choose a background and color
function setRandomStyle(sa){
	ran = Math.round(Math.random()*(sa.length-1));
	
	var leftBorderColorElements = document.getElementsByName('leftBorderColorElement');
	var borderColorElements = document.getElementsByName('borderColorElement');
	var backgroundColorElements = document.getElementsByName('backgroundColorElement');
	var colorElements = document.getElementsByName('colorElement');
	
	for(var i = 0; i < leftBorderColorElements.length; i++){
		leftBorderColorElements[i].style.borderLeftColor = sa[ran];
	}
	for(var i = 0; i < borderColorElements.length; i++){
		borderColorElements[i].style.borderColor = sa[ran];
	}
	for(var i = 0; i < backgroundColorElements.length; i++){
		backgroundColorElements[i].style.backgroundColor = sa[ran];
	}
	for(var i = 0; i < colorElements.length; i++){
		colorElements[i].style.color = sa[ran];
	}
	
	if(document.getElementById('barraMenu'))
		document.getElementById('barraMenu').style.backgroundImage = 'url(images/banner_lateral'+(ran+1)+'.jpg)';
	if(document.getElementById('mainHeaderLogin1')){
		document.getElementById('username').style.borderLeftColor = sa[ran];
		document.getElementById('password').style.borderLeftColor = sa[ran];
	}
	if(document.getElementById('headerEndPic'))
		document.getElementById('headerEndPic').style.backgroundImage = 'url(images/headerEnd'+(ran+1)+'.png)';
	if(document.getElementById('headerEndLogoutPic')){
		document.getElementById('headerEndLogoutPic').style.backgroundImage = 'url(images/headerEnd'+(ran+1)+'_long.png)';
		document.getElementById('headerEndLogoutPic').style.width = '84px';
	}
	if(document.getElementById('headerEndEnviarPic'))
		document.getElementById('headerEndEnviarPic').style.backgroundImage = 'url(images/headerEnd'+(ran+1)+'.png)';
	if(document.getElementById('mainHeaderEnviarAmigoBox')){
		document.getElementById('mainHeaderEnviarAmigoBox').style.borderLeftColor = sa[ran];
	}
	if(document.getElementById('mainDestaque'))
		document.getElementById('mainDestaque').style.borderLeftColor = sa[ran];
	if(document.getElementById('mainTitle'))
		document.getElementById('mainTitle').style.borderLeftColor = sa[ran];
	if(document.getElementById('mainTitleNumber1'))
		document.getElementById('mainTitleNumber1').style.color = sa[ran];
	if(document.getElementById('mainDestaqueButton'))
		document.getElementById('mainDestaqueButton').style.backgroundColor = sa[ran];
	if(document.getElementById('inicioTitle0')){
		document.getElementById('inicioTitle0').style.borderLeftColor = sa[ran];
		document.getElementById('inicioTitle1').style.borderLeftColor = sa[ran];
	}
	if(document.getElementById('nextLink'))
		document.getElementById('nextLink').style.color = sa[ran];
	if(document.getElementById('colorLink'))
		document.getElementById('colorLink').style.color = sa[ran];
	if(document.getElementById('enviar')){
		document.getElementById('name').style.borderLeftColor = sa[ran];
		document.getElementById('email').style.borderLeftColor = sa[ran];
//		document.getElementById('name2').style.borderLeftColor = sa[ran];
		document.getElementById('email2').style.borderLeftColor = sa[ran];
    }
	if(document.getElementById('registar')){
		document.getElementById('name').style.borderLeftColor = sa[ran];
		document.getElementById('surname').style.borderLeftColor = sa[ran];
		document.getElementById('email').style.borderLeftColor = sa[ran];
		document.getElementById('emailconfirm').style.borderLeftColor = sa[ran];
		document.getElementById('new_password').style.borderLeftColor = sa[ran];
		document.getElementById('passwordconfirm').style.borderLeftColor = sa[ran];
		document.getElementById('invite_code').style.borderLeftColor = sa[ran];
		document.getElementById('txtCaptcha').style.borderLeftColor = sa[ran];
	}
	if(document.getElementById('enviar_password')){
		document.getElementById('email').style.borderLeftColor = sa[ran];							   
	}
}
function getWindowHeight(){
	var myHeight = 0;
	if(typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	} else if(document.documentElement && (document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && (document.body.clientHeight)) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}
function updateArrow(evt){
	mouseY = evt.pageY?evt.pageY:evt.clientY;
	if(mouseY > document.getElementById('menu').offsetTop + 10 && 
	   mouseY < document.getElementById('menu').offsetTop + document.getElementById('menu').offsetHeight){
		document.getElementById('menuArrow').style.top = mouseY - document.getElementById('menu').offsetTop - 28 + "px";
		document.getElementById('menuBox').style.top = mouseY - document.getElementById('menu').offsetTop - 7 + "px";
	}
}
function isIE6(){
	var browser = '';
	var version = '';

	if (navigator.appName.indexOf('Microsoft') != -1)
		browser = 'IE'

	version = navigator.appVersion;
	var paren = version.indexOf('(');
	var whole_version = navigator.appVersion.substring(0,paren-1);
	version = parseInt(whole_version);

	return browser == 'IE' && version <= 6;
}
function setArrow(newY){
	moveObject('menuArrow', 246, newY - 28, 400, Spry.sinusoidalTransition);
	moveObject('menuBox', 202, newY - 7, 400, Spry.sinusoidalTransition);
}
function setInitArrow(newY){
	document.getElementById('menuArrow').style.top = newY - 28 + "px";
	document.getElementById('menuBox').style.top = newY - 7 + "px";
}
function moveObject(objID, fromX, toY, duration, transition){
	var obj = document.getElementById(objID);
	if(obj){
		var from = new Spry.Effect.Utils.Position();
		from.x = fromX;
		from.y = obj.offsetTop;
		var to = new Spry.Effect.Utils.Position();
		to.x = fromX;
		to.y = toY;
		Spry.Effect.makePositioned(obj);
		var objMove = new Spry.Effect.Move(objID, from, to, {duration:duration, transition: transition});
		objMove.start();
	}
}
var currentColor;
var currentElementID;
function MM_effectHighlightOver(e, targetElement, duration, startColor, endColor, restoreColor, toggle){
	document.body.style.cursor='pointer';
	var relTarg = e.relatedTarget || e.fromElement;

	if(relTarg.id != 'mainContentTitle' && targetElement.id != relTarg.id){
//		targetElement.style.backgroundColor = endColor;
		Spry.Effect.DoHighlight(targetElement, {duration: duration, from: startColor, to: endColor, restoreColor: restoreColor, toggle: toggle, transition: Spry.sinusoidalTransition});
	}
}
function MM_effectHighlightOut(e, targetElement, duration, startColor, endColor, restoreColor, toggle){
	document.body.style.cursor='default';
	var relTarg = e.relatedTarget || e.toElement;

	if(relTarg.id != 'mainContentTitle' && targetElement.id != relTarg.id){
//		targetElement.style.backgroundColor = endColor;
		Spry.Effect.DoHighlight(targetElement, {duration: duration, from: startColor, to: endColor, restoreColor: restoreColor, toggle: toggle, transition: Spry.sinusoidalTransition});
	}
}
function MM_effectAppearFade(targetID, duration, from, to, toggle){
	var obj = document.getElementById('mainContent');
	tp1.showPanel(Number(targetID.id));
	Spry.Effect.DoFade(obj, {duration: duration, from: from, to: to, toggle: toggle, finish: finish, transition: Spry.sinusoidalTransition});
}
function finish(element,effect){
//	SlidingPanels1.showNextPanel();
	var newHeight = getWindowHeight() - document.getElementById('hugeContent').offsetTop;
	for(var i=0; i < tp1.getTabbedPanelCount(); i++){
		var tempID = 'texto'+ i +'_0';
		var tempHeight = document.getElementById(tempID).offsetTop + document.getElementById(tempID).offsetHeight
		if(newHeight < tempHeight)
			newHeight = tempHeight;
	}
	document.getElementById('hugeContent').style.height = getWindowHeight() - document.getElementById('hugeContent').offsetTop + "px";
	document.getElementById('tabbedPanelsContent').style.height = newHeight + "px";
	document.getElementById('footer').style.top = newHeight + "px";	
}
function remove(element, effect){
	element.style.visibility = 'hidden';
	element.parentNode.removeChild(element);
}
function hide(element,effect){
	element.style.visibility = 'hidden';
}
function unhide(element,effect){
	element.style.visibility = 'visible';
}
function getNextLink(region, lookupFunc){
	var nextLink;
	if(lookupFunc('{ds_RowNumberPlus1}') < lookupFunc('{ds_RowCount}'))
		nextLink = subContents.getRowByID(parseInt(lookupFunc('{ds_RowNumberPlus1}')))['@title'];
	else
		nextLink = "<a href='"+pages.getCurrentRow()['@url']+"' class='linkClean' style='color:"+styleArray[ran]+"'>Voltar ao início</a>";
	return nextLink;
}
function nextLink(){
	var rowID = parseInt(subContents.getCurrentRowID())+1;
	subContents.setCurrentRow(rowID);
	checkSelected();
	if(document.getElementById('link'+rowID))
		document.getElementById('link'+rowID).className = 'subMenuItem subMenuItemSelected';
	scrollUp();
}
function checkSelected(){
	for(var i = 0; i < subContents.getRowCount(); i++){
		document.getElementById('link'+i).className = 'subMenuItem';;
	}
}
function scrollUp(){
	//scroll page up
	var cont = document.getElementById("mainContent") || document.getElementById("mainContentAlt");
	window.scrollBy(0,-cont.offsetHeight);
}
function eraseText(obj){
	if(obj.value == document.getElementById(obj.id).getAttribute('resettext'))
		obj.value = "";
}
function resetText(obj){
	if(obj.value == "")
		obj.value = document.getElementById(obj.id).getAttribute('resettext');
}
function popUpWindow(url) {
   mywindow = window.open(url, "mywindow","width=430,height=280");
   mywindow.moveTo(400,300);
} 
function popUpWindow2(url) {
   mywindow = window.open(url, "mywindow","width=430,height=460");
   mywindow.moveTo(400,300);
} 
function appendErro(str){
   throw new Error("DEBUG: "+str)
}

function li(str){
   setTimeout("appendErro('"+str+"')", 1)
}

function checkIfParent(node, parent){
	if(node == parent) return true;
	else if(node && node.parentNode) return checkIfParent(node.parentNode, parent);
	else return false;
}
function effectHighlightOver(e, targetElement, duration, startColor, endColor, restoreColor, toggle){
	document.body.style.cursor='pointer';
	var relTarg = e.relatedTarget || e.fromElement;
	if(!checkIfParent(relTarg, targetElement)){
		Spry.Effect.DoHighlight(targetElement, {duration: duration, from: startColor, to: endColor, restoreColor: restoreColor, toggle: toggle, transition: Spry.sinusoidalTransition});
	}
}
function effectHighlightOut(e, targetElement, duration, startColor, endColor, restoreColor, toggle){
	document.body.style.cursor='default';
	var relTarg = e.relatedTarget || e.toElement;
	if(!checkIfParent(relTarg, targetElement)){
		Spry.Effect.DoHighlight(targetElement, {duration: duration, from: startColor, to: endColor, restoreColor: restoreColor, toggle: toggle, transition: Spry.sinusoidalTransition});
	}
}
function shuffleArray(){
	return 0.5 - Math.random();
} 