﻿function pageScroll() {
    window.scrollBy(0, 350);
}

/*****************************************************
*** Search
*****************************************************/
function CallSearch(Keyword) {
    if (Keyword == '') {
        parent.location.href = '/busca/';
    } else if (Keyword.length < 3) {
        alert('A palavra-chave tem que ter pelo menos 3 caracteres.');
    } else {
        parent.location.href = '/busca/' + MyUrlEncode(Trim(Keyword));
    }
}

function SearchClick() {
    CallSearch(document.getElementById('ctl00_Header_txtSearchHeader').value);
}

function KeyPressEnterOnSearch() {
    keyPressed = event.keyCode;
    if (keyPressed == 13) {
        SearchClick();
        return false;
    }
}

function Trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

function MyUrlEncode(Keyword) {
    Keyword = Keyword.toLowerCase();
    Keyword = escape(Keyword);

    while (Keyword.indexOf('%C') >= 0) { Keyword = Keyword.replace('%C', '%c3%8'); }
    while (Keyword.indexOf('%D') >= 0) { Keyword = Keyword.replace('%D', '%c3%9'); }
    while (Keyword.indexOf('%E') >= 0) { Keyword = Keyword.replace('%E', '%c3%a'); }
    while (Keyword.indexOf('%F') >= 0) { Keyword = Keyword.replace('%F', '%c3%b'); }

    return Keyword;
}

function SearchTextBoxOnEnter() {
    document.getElementById('ctl00_Header_txtSearchHeader').className = 'HeaderSearchTextBoxBlack';
    if (document.getElementById('ctl00_Header_txtSearchHeader').value == 'Digite aqui o que procura ...') { document.getElementById('ctl00_Header_txtSearchHeader').value = ''; }
}

function SearchTextBoxOnExit() {
    if (document.getElementById('ctl00_Header_txtSearchHeader').value == '') {
        document.getElementById('ctl00_Header_txtSearchHeader').value = 'Digite aqui o que procura ...';
        document.getElementById('ctl00_Header_txtSearchHeader').className = 'HeaderSearchTextBoxGray';
    }
}

function Trim(str) { return str.replace(/^\s+|\s+$/g, ""); }
function MyUrlEncode(Keyword) {
    Keyword = Keyword.toLowerCase();
    Keyword = escape(Keyword);

    while (Keyword.indexOf('%C') >= 0) { Keyword = Keyword.replace('%C', '%c3%8'); }
    while (Keyword.indexOf('%D') >= 0) { Keyword = Keyword.replace('%D', '%c3%9'); }
    while (Keyword.indexOf('%E') >= 0) { Keyword = Keyword.replace('%E', '%c3%a'); }
    while (Keyword.indexOf('%F') >= 0) { Keyword = Keyword.replace('%F', '%c3%b'); }

    return Keyword;
}

/*****************************************************
*** Picture
*****************************************************/
function HiddenPhoto() {
    document.getElementById('ctl00_body_pnlPagePhoto').style.display = 'none';
}

function ShowPicture(src, label, source) {
    document.getElementById('ctl00_body_imgPictureAmp').src = '/App_Common/Pix.gif';
    document.getElementById('ctl00_body_imgPictureAmp').src = 'http://file.portofeliztenisclube.com.br/' + src;
    document.getElementById('ctl00_body_lblPictureAmp').innerHTML = label;
    if (source == '') {
        document.getElementById('ctl00_body_lblPictureSourceAmp').innerHTML = '&nbsp;' + source;
    } else {
        document.getElementById('ctl00_body_lblPictureSourceAmp').innerHTML = 'Foto: ' + source;
    }
}

function PreviousPicture() {
    CurrentPicture = CurrentPicture - 1;
    if (CurrentPicture < 0) { CurrentPicture = PictureCount; }
    ShowPicture(arrPictures[CurrentPicture], arrPicturesLabel[CurrentPicture], arrPicturesSourceLabel[CurrentPicture]);
}

function NextPicture() {
    CurrentPicture = CurrentPicture + 1;
    if (CurrentPicture > PictureCount) { CurrentPicture = 0; }
    ShowPicture(arrPictures[CurrentPicture], arrPicturesLabel[CurrentPicture], arrPicturesSourceLabel[CurrentPicture]);
}

function CallPicture(numItem) {
    try {
        CurrentPicture = numItem;
        ShowPicture(arrPictures[CurrentPicture], arrPicturesLabel[CurrentPicture], arrPicturesSourceLabel[CurrentPicture]);
    } catch (err) { }
}

/*****************************************************
*** Font Size
*****************************************************/
var PageFontSize = 3;
function FontSizeMore() {
    if (PageFontSize < 6) {
        PageFontSize += 1;
        document.getElementById('ctl00_body_lblPageText').className = 'PageText' + PageFontSize;
    }
}

function FontSizeLess() {
    if (PageFontSize > 1) {
        PageFontSize -= 1;
        document.getElementById('ctl00_body_lblPageText').className = 'PageText' + PageFontSize;
    }
}

/*****************************************************
*** Zoom
*****************************************************/
function ZoomIn() {
    var PageWidth = document.body.offsetWidth;
    var PageHeight = document.getElementById('PageEnd').offsetTop + 20;

    document.getElementById('ctl00_body_pnlZoomPage').style.display = 'block';
    document.getElementById('ctl00_body_pnlZoomPage').style.width = PageWidth + 'px';
    document.getElementById('ctl00_body_pnlZoomPage').style.height = PageHeight + 'px';

    document.getElementById('ctl00_body_pnlZoomBox').style.display = 'block';
    BoxWidth = document.getElementById('ctl00_body_pnlZoomBox').offsetWidth;
    BoxHeight = document.getElementById('ctl00_body_pnlZoomBox').offsetHeight;
    document.getElementById('ctl00_body_pnlZoomBox').style.left = parseInt((PageWidth - BoxWidth) / 2) + 'px';
    document.getElementById('ctl00_body_pnlZoomBox').style.top = (parseInt((document.body.offsetHeight - BoxHeight) / 2) + document.documentElement.scrollTop) + 'px';
}

function ZoomClose() {
    document.getElementById('ctl00_body_pnlZoomPage').style.display = 'none';
    document.getElementById('ctl00_body_pnlZoomBox').style.display = 'none';
}

/*****************************************************
*** Mask
*****************************************************/
function MaskCEP(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '00.000-000', event);
}

function MaskPhone(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '(00) 0000-0000', event);
}

function MaskDate(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '00/00/0000', event);
}

function MaskHour(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '00.00', event);
}

function MaskCNPJ(cnpj) {
    if (MaskInteger(cnpj) == false) {
        event.returnValue = false;
    }
    return FormatField(cnpj, '00.000.000/0000-00', event);
}

function MaskCPF(cpf) {
    if (MaskInteger(cpf) == false) {
        event.returnValue = false;
    }
    return FormatField(cpf, '000.000.000-00', event);
}

function MaskInteger() {
    if (event.keyCode < 48 || event.keyCode > 57) {
        event.returnValue = false;
        return false;
    }
    return true;
}

function FormatField(campo, Mascara, evento) {
    var boleanoMascara;

    var Digitato = evento.keyCode;
    exp = /\-|\.|\/|\(|\)| /g
    campoSoNumeros = campo.value.toString().replace(exp, "");

    var posicaoCampo = 0;
    var NovoValorCampo = "";
    var TamanhoMascara = campoSoNumeros.length; ;

    if (Digitato != 8) { // backspace 
        for (i = 0; i <= TamanhoMascara; i++) {
            boleanoMascara = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                || (Mascara.charAt(i) == "/"))
            boleanoMascara = boleanoMascara || ((Mascara.charAt(i) == "(")
                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
            if (boleanoMascara) {
                NovoValorCampo += Mascara.charAt(i);
                TamanhoMascara++;
            } else {
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
                posicaoCampo++;
            }
        }
        campo.value = NovoValorCampo;
        return true;
    } else {
        return true;
    }
}

/*****************************************************
*** Picture
*****************************************************/
var PictureChangeTimeout;
$(document).ready(function () {
    StartPictureMini();
    StartPictureFull();
});

function StartPictureMini() {
    var div = $('div.PageSlidePicture');
	var	ul = $('ul.PageSlidePicture');

    var divWidth = div.width();
    var lastLi = ul.find('li:last-child');

    div.css({ overflow: 'hidden' });

    div.mousemove(function (e) {
        var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth();
        var left = (e.pageX - div.offset().left) * (ulWidth - divWidth) / divWidth;
        div.scrollLeft(left);
    });
}

function StartPictureFull() {
    $('.PageSlidePictureLi').click(function (e) {
        e.preventDefault();
        ShowSlideImage($(this).index());
    });

    $('.PageSlideFullPicturePrevious').click(function (e) {
        ShowSlideImage($('.PageSlidePictureLi:last').index());
    });

    $('.PageSlideFullPictureNext').click(function (e) {
        ShowSlideImage(2);
    });

    /*** Start Picture Change ***/
    if ($('.PageSlideFullPictureInner').length > 0) {
        $(".PageSlideFullPictureDivisor").css('left', '0');
        $(".PageSlideFullPictureInner").css('width', '4');

        $(".PageSlideFullPicture").mousemove(function (e) {
            ChangeImage(e.pageX);
        });
        $(".PageSlideFullPictureInner").mousemove(function (e) {
            ChangeImage(e.pageX);
        });
    }
}

function ShowSlideImage(index) {
    clearTimeout(PictureChangeTimeout);
    $('.PageSlideFullPictureInner').stop(false, false);
    $('.PageSlideFullPictureDivisor').stop(false, false);

    var id = '.PageSlideFullPicture';
    var image = '.PageSlidePictureLi:eq(' + index + ')'
    var html = '';
    var FullPicture, TodayPicture;
    var lastImage = $('.PageSlidePictureLi:last').index();
    var Description = $(image).attr('rel_desc');

    // Mount the content
    if ($(image).attr('rel').indexOf('|') > 0) {
        var arrPicture = $(image).attr('rel').split('|');
        FullPicture = arrPicture[0];
        TodayPicture = arrPicture[1];
    } else {
        FullPicture = $(image).attr('rel');
        TodayPicture = '';
    }

    html += '<img src="' + FullPicture + '" alt="' + Description + '" title="' + Description + '" border="0" />';
    if (TodayPicture != '') {
        html += '<div class="PageSlideFullPictureInner"><img src="' + TodayPicture + '" alt="' + Description + '" title="' + Description + '" border="0" /><div class="PageSlideFullPictureDivisor"></div></div>';
    }

    html += '<div class="PageSlideFullPicturePrevious"></div>';
    html += '<div class="PageSlideFullPictureNext"></div>';
    $(id).html(html);
    $('.PageSlideFullPictureDescription').html(Description);

    // Functions
    $('.PageSlideFullPicturePrevious').click(function (e) {
        var prevIndex = index - 1;
        if (prevIndex < 0) { prevIndex = lastImage; }

        ShowSlideImage(prevIndex);
    });

    $('.PageSlideFullPictureNext').click(function (e) {
        var nextIndex = index + 1;
        if (nextIndex > lastImage) { nextIndex = 0; }

        ShowSlideImage(nextIndex);
    });

    // Animate Changeble Pictures
    if (TodayPicture != '') {
        $(".PageSlideFullPictureInner").css('width', $(".PageSlideFullPicture").width());
        $(".PageSlideFullPictureDivisor").css('left', $(".PageSlideFullPicture").width() - 4);

        PictureChangeTimeout = setTimeout(function () {
            $('.PageSlideFullPictureInner').animate({
                'width': 4
            }, { queue: false, duration: 2000 });

            $('.PageSlideFullPictureDivisor').animate({
                'left': 0
            }, { queue: false, duration: 2000 });
        }, 1000);
    }
}

function ChangeImage(width) {
    clearTimeout(PictureChangeTimeout);
    $('.PageSlideFullPictureInner').stop(false, false);
    $('.PageSlideFullPictureDivisor').stop(false, false);

    var newWidth = width - $(".PageSlideFullPicture").position().left + 1;
    if (newWidth < 4) { newWidth = 4; }
    if (newWidth > 900) { newWidth = 900; }

    $(".PageSlideFullPictureInner").width(newWidth);
    $(".PageSlideFullPictureDivisor").css('left', (newWidth - 4) + 'px');
}
