var openDiv = null;
var hiddenObjects = new Array();
var currentContentDiv = null;
var currentMarkerDiv = null;

function resizeContents(delta){
    var height = 0;

    if (window.innerWidth) {//if browser supports window.innerWidth
        height = window.innerHeight;
    }
    else if (document.all) {//else if browser supports document.all (IE 4+)
       	height = document.body.clientHeight;
	}
    height = height - delta;

	//alert(height + " > " + currentMarkerDiv.offsetTop + " = " + currentContentDiv.style.height);
    if (height > currentMarkerDiv.offsetTop) {
        currentContentDiv.style.height = height + 'px';
        //currentContentContainer.style.height = (height - 1) + 'px';
    }
	else {
		var currentHeight = parseInt(currentContentDiv.style.height.replace(/px/, ''));
		if (currentMarkerDiv.offsetTop < currentHeight){
			currentContentDiv.style.height = currentMarkerDiv.offsetTop + 'px';
		}
	}
}

function showDiv(object, width, height){
    window.scrollTo(0, 0);

    var objects = document.getElementsByTagName('object');
    for (var i = 0; i != objects.length; i++) {
        if (objects[i].style.visibility != 'hidden') {
            objects[i].style.visibility = 'hidden';
            hiddenObjects[hiddenObjects.length] = objects[i];
        }
    }

    //centrar o popup na horizontal
    if (window.innerWidth) {
        document.getElementById(object).style.left = ((window.innerWidth / 2) - (width / 2)) + 'px';
    }
    else {
        document.getElementById(object).style.left = ((document.body.offsetWidth / 2) - (width / 2)) + 'px';
    }

    document.getElementById(object).style.top = '150px';

    document.getElementById('overlay_popup').style.display = 'block';
    document.getElementById(object).style.display = 'block';

    openDiv = object;
}


function showDivNoOverlay(object, width, height){
    window.scrollTo(0, 0);

    var objects = document.getElementsByTagName('object');
    for (var i = 0; i != objects.length; i++) {
        if (objects[i].style.visibility != 'hidden') {
            objects[i].style.visibility = 'hidden';
            hiddenObjects[hiddenObjects.length] = objects[i];
        }
    }

	if (width != null) {

		//centrar o popup na horizontal
		if (window.innerWidth) {
			document.getElementById(object).style.left = ((window.innerWidth / 2) - (width / 2)) + 'px';
		}
		else {
			document.getElementById(object).style.left = ((document.body.offsetWidth / 2) - (width / 2)) + 'px';
		}
	}

	if (height != null) {

		//centrar o popup na horizontal
		if (window.innerHeight) {
			document.getElementById(object).style.top = ((window.innerHeight / 2) - (height / 2)) + 'px';
		}
		else {
			document.getElementById(object).style.top = ((document.body.offsetHeight / 2) - (height / 2)) + 'px';
		}
	} else {

    	//document.getElementById(object).style.top = '150px';

	}


    document.getElementById(object).style.display = 'block';

    openDiv = object;
}

function hideDiv(object){
    document.getElementById(object).style.display = 'none';

    if (document.getElementById('overlay_popup')) {
        document.getElementById('overlay_popup').style.display = 'none';
    }
    for (var i = 0; i != hiddenObjects.length; i++) {
        hiddenObjects[i].style.visibility = 'visible';
    }
    openDiv = null;
}

var page = new Array();
var lastpage = new Array();
function abre(div){
    var prefixDiv = div.substring(0, div.indexOf('_', 0));
    var sufixDiv = div.substring(div.indexOf('_', 0) + 1, div.length);

    if (document.getElementById(div) != null) {
        if (document.getElementById(div).style.display == 'block') {
            document.getElementById(div).style.display = 'none';
        }
        else {
            document.getElementById(div).style.display = 'block';
        }
    }

    //fechar todas as divs do mesmo nivel
    if (prefixDiv != 'lasterpage') {
        if (prefixDiv == 'page') {
            for (i in page) {
                if (sufixDiv != page[i]) {
                    element = prefixDiv + '_' + page[i];
                    if (document.getElementById(element) != null) {
                        document.getElementById(element).style.display = 'none';
                    }

                }
            }
        }
        else
            if (prefixDiv == 'lastpage') {
                for (i in lastpage) {
                    if (sufixDiv != lastpage[i]) {
                        element = prefixDiv + '_' + lastpage[i];
                        if (document.getElementById(element) != null) {
                            document.getElementById(element).style.display = 'none';
                        }
                    }
                }
            }
    }
}

function toggle(objecto){
    if (document.getElementById(objecto).style.display != 'block') {
        document.getElementById(objecto).style.display = 'block';
        lastChapter = objecto;
    }
    else {
        document.getElementById(objecto).style.display = 'none';
    }
}

function toggleQuestion(id){
    var image = document.getElementById(id + '_img');
    var question = document.getElementById(id + '_question');
    var answer = document.getElementById(id + '_answer');

    if (answer.style.display == null || answer.style.display == 'none') {
        answer.style.display = 'block';
        image.src = 'images/bt_close_question.gif';
        question.style.color = '#e0003b';
    }
    else {
        answer.style.display = 'none';
        image.src = 'images/bt_open_question.gif';
        question.style.color = '#6e6e6e';
    }
}

function Centrar(pagina, titulo, w, h, scrollu, resize){
    LeftPosition = (screen.width) ? (screen.width - w) / 2 : 0;
    TopPosition = (screen.height) ? (screen.height - h) / 2 : 0;
    settings = 'height=' + h + ',width=' + w + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scrollu + ',resizable=' + resize + '';
    win = window.open(pagina, titulo, settings)
}

function recoverPass(){
    document.getElementById('recoverPassForm').style.display = 'block';
    document.getElementById('loginBox').style.display = 'none';
}

function getXMLHttpRequest(){
    var httpRequest;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
    }
    else
        if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                }
            }
        }

    return httpRequest;
}

function openPageHighlight(pageHighlightID){
    var httpRequest = getXMLHttpRequest();

    httpRequest.open('GET', 'stats.php?contentType=PageHighlight&contentID=' + pageHighlightID, true);
    httpRequest.send(null);
}

function openSearch(searchString){
    var httpRequest = getXMLHttpRequest();

    httpRequest.open('GET', 'stats.php?contentType=Search&contentID=' + escape(searchString), true);
    httpRequest.send(null);
}

function openBannerPage(bannerID, pageID, baseURL){
    if (baseURL == null)
        baseURL = '';

    var httpRequest = getXMLHttpRequest();

    httpRequest.open('GET', baseURL + 'stats.php?contentType=BannerPage&contentID=' + bannerID + '&pageID=' + pageID, true);
    httpRequest.send(null);
}

var navigate_to = null;
function showLogin(destino){
    navigate_to = destino;

    document.getElementById('login_form').style.display = 'block';
    document.getElementById('registo_form').style.display = 'none';
    document.getElementById('recuperar_form').style.display = 'none';

    showDiv('divLogin', 298, 177);
}

function submitForm(toSubmit){
    var httpRequest = getXMLHttpRequest();
    httpRequest.onreadystatechange = function(){
        receiveResponse(httpRequest, toSubmit);
    };

    var requestParameters = '';
    requestParameters += escape('__submited_action') + '=' + escape(toSubmit.__submited_action.value) + '&';
    if (toSubmit.username != null)
        requestParameters += escape('username') + '=' + escape(toSubmit.username.value) + '&';
    if (toSubmit.email != null)
        requestParameters += escape('email') + '=' + escape(toSubmit.email.value) + '&';
    if (toSubmit.password != null)
        requestParameters += escape('password') + '=' + escape(toSubmit.password.value) + '&';
    if (toSubmit.confpassword != null)
        requestParameters += escape('confpassword') + '=' + escape(toSubmit.confpassword.value) + '&';
    if (toSubmit.aceitar != null && toSubmit.aceitar.checked)
        requestParameters += escape('aceitar') + '=' + escape(toSubmit.aceitar.value) + '&';
    if (toSubmit.pageID != null)
        requestParameters += escape('pageID') + '=' + escape(toSubmit.pageID.value) + '&';

    httpRequest.open('POST', 'index.php?returnFormat=json', true);
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    httpRequest.send(requestParameters);

    return false;
}

function logout(){
    navigate_to = null;
    var httpRequest = getXMLHttpRequest();
    httpRequest.onreadystatechange = function(){
        receiveResponse(httpRequest, null);
    };

    var requestParameters = escape('__submited_action') + '=' + escape('Entity:Logout');

    httpRequest.open('POST', 'index.php?returnFormat=json', true);
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    httpRequest.send(requestParameters);
}

function receiveResponse(httpRequest, submitedForm){
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            if (httpRequest.responseText == 'ok') {
                if (navigate_to == null || navigate_to == '') {
                    window.location.reload(true);
                }
                else {
                    window.location.href = navigate_to;
                }
            }
            else {
                if (submitedForm != null) {
                    var htmlElement = document.getElementById(submitedForm.id + '_error');
                    if (htmlElement) {
                        htmlElement.innerHTML = httpRequest.responseText;
                        htmlElement.style.display = 'block';

                        var x = htmlElement.getElementsByTagName("script");
                        for (var i = 0; i < x.length; i++) {
                            eval(x[i].innerHTML);
                        }
                    }
                    else {
                        alert(httpRequest.responseText);
                    }
                }
                else {
                    alert(httpRequest.responseText);
                }
            }
        }
        else {
            alert(httpRequest.responseText);
            alert('There was a problem with the request.');
        }
    }
}

function sinopse(scheduleRecordLink){
    var httpRequest = getXMLHttpRequest();
    httpRequest.onreadystatechange = function(){
        receiveSinopse(httpRequest);
    };

    httpRequest.open('GET', scheduleRecordLink + '&version_id=1', true);
    httpRequest.send(null);
}

function receiveSinopse(httpRequest){
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            var htmlElement = document.getElementById('detalheProgramacao');
            if (htmlElement) {
                htmlElement.innerHTML = httpRequest.responseText;
            }

            showDiv('detalheProgramacao', 637, 300);
        }
        else {
            alert(httpRequest.responseText);
        }
    }
    else {
    }
}

/* função que altera a source de uma imagem */
function changeImgSource(target, newSource){
    document.getElementById(target).src = newSource;
}

/* função que altera o valor de um elemento */
function setFilePath(target, value){
    document.getElementById(target).value = value;
}

function setHeader(header, section){

    targetHeader = document.getElementById(header);

    if (screen.width <= 1024) {
        targetHeader.style.backgroundImage = 'url(images/header/1024/' + section + '/1.jpg)';
    }
    else
        if (screen.width <= 1152) {
            targetHeader.style.backgroundImage = 'url(images/header/1152/' + section + '/1.jpg)';
        }
        else
            if (screen.width <= 1280) {
                targetHeader.style.backgroundImage = 'url(images/header/1280/' + section + '/1.jpg)';
            }
            else
                if (screen.width <= 1440) {
                    targetHeader.style.backgroundImage = 'url(images/header/1440/' + section + '/1.jpg)';
                }


}

function getResolution(){
    var today = new Date();
    var the_date = new Date("December 31, 2023");
    var the_cookie_date = the_date.toGMTString();
    var the_cookie = "users_resolution=" + screen.width + "x" + screen.height;
    var the_cookie = the_cookie + ";expires=" + the_cookie_date;
    document.cookie = the_cookie
	location.reload(true);
}


function setWidth(all, center, left, right,  bottom) {
	var site = window.document.getElementById(all);
	var centerBar = window.document.getElementById(center);
	var leftBar = window.document.getElementById(left);
	var rightBar = window.document.getElementById(right);
	var bottomBar = window.document.getElementById(bottom)
	var clientWidth = window.document.body.clientWidth >= 1440 ? 1440 : window.document.body.clientWidth;
	var clientHeight = window.document.body.clientHeight;

	if (clientHeight <= bottomBar.offsetTop) {
		clientHeight = bottomBar.offsetTop;
    }
	if (clientWidth > 1024) {
		site.style.width = (clientWidth - 30) + 'px';
		centerBar.style.width = (clientWidth - 60) + 'px';
		leftBar.style.display= 'inline';
		rightBar.style.display= 'inline';
		leftBar.style.width = '15px';
		rightBar.style.width = '15px';
		leftBar.style.height = clientHeight + 'px';
		rightBar.style.height = clientHeight + 'px';
	}
	else {
		window.document.body.style.backgroundImage = 'url(\'images/site_background_1024.jpg\')';
		window.document.body.style.backgroundRepeat = 'no-repeat';
		window.document.body.style.backgroundPosition = 'top';
		site.style.width = '960px';
		centerBar.style.width = '960px';
		leftBar.style.display= 'none';
		rightBar.style.display= 'none';
	}
}

function setBackground(container, image){
	targetContainer = document.getElementById(container);

	targetContainer.style.backgroundImage = 'url(' + image + ')';
}

function createPrintableContent(container, baseURL, stylesheet){
	var $printableDirections=window.open();
    self.focus();
    $printableDirections.document.open();

	$printableDirections.document.write('<html><head>');
	$printableDirections.document.write('<base href="'+baseURL+'" />');
	if (stylesheet != null) {
		$printableDirections.document.write('<link rel="stylesheet" href="css/'+stylesheet+'" type="text/css" media="screen" />');
	}
	$printableDirections.document.write('<style>body, td { font-family: Verdana; font-size: 10pt;}</style>');
	$printableDirections.document.write('</head><body>');
    $printableDirections.document.write(document.getElementById(container).innerHTML);
	$printableDirections.document.write('</body></html>');

    $printableDirections.document.close();
    //$printableDirections.print();
	//$printableDirections.close();

}

function normalizePixelValue(value) {
	return parseFloat(value.replace(/px/g, ''));
}

var totalHeight;
var containerHeight;

function slide(id, amount) {

	var clip = window.document.getElementById(id);
	var top = normalizePixelValue(clip.style.top);

	var temp = containerHeight + Math.abs(top);

	if ( (temp > totalHeight && amount < 0 )  || (clip.style.top == '0px' && amount > 0)) {
		return;
	}
	clip.style.top = (top + amount) + 'px';
}

function setCookie(name, value) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + 1);
	document.cookie = name + "=" + escape(value);
}

function htmlEntitiesDecode(data){
    data = data.replace(/&(\w+);/g, function(wholematch, match){
        switch (match) {
            case 'nbsp':
                return String.fromCharCode(32);

            case 'iexcl':
                return String.fromCharCode(161);

            case 'cent':
                return String.fromCharCode(162);

            case 'pound':
                return String.fromCharCode(163);

            case 'curren':
                return String.fromCharCode(164);

            case 'yen':
                return String.fromCharCode(165);

            case 'brvbar':
                return String.fromCharCode(166);

            case 'sect':
                return String.fromCharCode(167);

            case 'uml':
                return String.fromCharCode(168);

            case 'copy':
                return String.fromCharCode(168);

            case 'ordf':
                return String.fromCharCode(170);

            case 'laquo':
                return String.fromCharCode(171);

            case 'not':
                return String.fromCharCode(172);

            case 'shy':
                return String.fromCharCode(173);

            case 'reg':
                return String.fromCharCode(174);

            case 'macr':
                return String.fromCharCode(175);

            case 'deg':
                return String.fromCharCode(176);

            case 'plusmn':
                return String.fromCharCode(177);

            case 'sup2':
                return String.fromCharCode(178);

            case 'sup3':
                return String.fromCharCode(179);

            case 'acute':
                return String.fromCharCode(180);

            case 'micro':
                return String.fromCharCode(181);

            case 'para':
                return String.fromCharCode(182);

            case 'middot':
                return String.fromCharCode(183);

            case 'cedil':
                return String.fromCharCode(184);

            case 'sup1':
                return String.fromCharCode(185);

            case 'ordm':
                return String.fromCharCode(186);

            case 'raquo':
                return String.fromCharCode(187);

            case 'frac14':
                return String.fromCharCode(188);

            case 'frac12':
                return String.fromCharCode(189);

            case 'frac34':
                return String.fromCharCode(190);

            case 'iquest':
                return String.fromCharCode(191);

            case 'Agrave':
                return String.fromCharCode(192);

            case 'Aacute':
                return String.fromCharCode(193);

            case 'Acirc':
                return String.fromCharCode(194);

            case 'Atilde':
                return String.fromCharCode(195);

            case 'Auml':
                return String.fromCharCode(196);

            case 'Aring':
                return String.fromCharCode(197);

            case 'AElig':
                return String.fromCharCode(198);

            case 'Ccedil':
                return String.fromCharCode(199);

            case 'Egrave':
                return String.fromCharCode(200);

            case 'Eacute':
                return String.fromCharCode(201);

            case 'Ecirc':
                return String.fromCharCode(202);

            case 'Euml':
                return String.fromCharCode(203);

            case 'Igrave':
                return String.fromCharCode(204);

            case 'Iacute':
                return String.fromCharCode(205);

            case 'Icirc':
                return String.fromCharCode(206);

            case 'Iuml':
                return String.fromCharCode(207);

            case 'ETH':
                return String.fromCharCode(208);

            case 'Ntilde':
                return String.fromCharCode(209);

            case 'Ograve':
                return String.fromCharCode(210);

            case 'Oacute':
                return String.fromCharCode(211);

            case 'Ocirc':
                return String.fromCharCode(212);

            case 'Otilde':
                return String.fromCharCode(213);

            case 'Ouml':
                return String.fromCharCode(214);

            case 'times':
                return String.fromCharCode(215);

            case 'Oslash':
                return String.fromCharCode(216);

            case 'Ugrave':
                return String.fromCharCode(217);

            case 'Uacute':
                return String.fromCharCode(218);

            case 'Ucirc':
                return String.fromCharCode(219);

            case 'Uuml':
                return String.fromCharCode(220);

            case 'Yacute':
                return String.fromCharCode(221);

            case 'THORN':
                return String.fromCharCode(222);

            case 'szlig':
                return String.fromCharCode(223);

            case 'agrave':
                return String.fromCharCode(224);

            case 'aacute':
                return String.fromCharCode(225);

            case 'acirc':
                return String.fromCharCode(226);

            case 'atilde':
                return String.fromCharCode(227);

            case 'auml':
                return String.fromCharCode(228);

            case 'aring':
                return String.fromCharCode(229);

            case 'aelig':
                return String.fromCharCode(230);

            case 'ccedil':
                return String.fromCharCode(231);

            case 'egrave':
                return String.fromCharCode(232);

            case 'eacute':
                return String.fromCharCode(233);

            case 'ecirc':
                return String.fromCharCode(234);

            case 'euml':
                return String.fromCharCode(235);

            case 'igrave':
                return String.fromCharCode(236);

            case 'iacute':
                return String.fromCharCode(237);

            case 'icirc':
                return String.fromCharCode(238);

            case 'iuml':
                return String.fromCharCode(239);

            case 'eth':
                return String.fromCharCode(240);

            case 'ntilde':
                return String.fromCharCode(241);

            case 'ograve':
                return String.fromCharCode(242);

            case 'oacute':
                return String.fromCharCode(243);

            case 'ocirc':
                return String.fromCharCode(244);

            case 'otilde':
                return String.fromCharCode(245);

            case 'ouml':
                return String.fromCharCode(246);

            case 'divide':
                return String.fromCharCode(247);

            case 'oslash':
                return String.fromCharCode(248);

            case 'ugrave':
                return String.fromCharCode(249);

            case 'uacute':
                return String.fromCharCode(250);

            case 'ucirc':
                return String.fromCharCode(251);

            case 'uuml':
                return String.fromCharCode(252);

            case 'yacute':
                return String.fromCharCode(253);

            case 'thorn':
                return String.fromCharCode(254);

            case 'yuml':
                return String.fromCharCode(255);

            case 'quot':
                return String.fromCharCode(34);

            case 'lt':
                return String.fromCharCode(60);

            case 'gt':
                return String.fromCharCode(62);

            case 'amp':
                return String.fromCharCode(38);

        }
        return "";
    });
    return data;
}

/* função que mostra a informação
 * dos pontos de interesse / campos de golfe
 * junto do mapa do google */

function showInfo(points, titleContainer, textContainer, indexToShow) {
	var titleContainer = document.getElementById(titleContainer);
	var textContainer = document.getElementById(textContainer);

	var titleToShow = htmlEntitiesDecode(points[indexToShow].name);
	var textToShow = htmlEntitiesDecode(points[indexToShow].description);

	if (textToShow.length == 0) {
		var textToShow = htmlEntitiesDecode(points[indexToShow].shortDescription);
	}

	//alert(titleToShow);

	titleContainer.innerHTML = '';
	textContainer.innerHTML = '';

	titleContainer.innerHTML = titleToShow;
	textContainer.innerHTML = textToShow;
}