/*** BROWSER DETECT ***/
/** by www.quirksmode.org/js/detect.html **/
var BrowserDetect = { init: function () { this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version"; this.OS = this.searchString(this.dataOS) || "an unknown OS" }, searchString: function (data) { for (var i = 0; i < data.length; i++) { var dataString = data[i].string; var dataProp = data[i].prop; this.versionSearchString = data[i].versionSearch || data[i].identity; if (dataString) { if (dataString.indexOf(data[i].subString) != -1) return data[i].identity } else if (dataProp) return data[i].identity } }, searchVersion: function (dataString) { var index = dataString.indexOf(this.versionSearchString); if (index == -1) return; return parseFloat(dataString.substring(index + this.versionSearchString.length + 1)) }, dataBrowser: [{ string: navigator.userAgent, subString: "Chrome", identity: "Chrome" }, { string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb" }, { string: navigator.vendor, subString: "Apple", identity: "Safari", versionSearch: "Version" }, { prop: window.opera, identity: "Opera" }, { string: navigator.vendor, subString: "iCab", identity: "iCab" }, { string: navigator.vendor, subString: "KDE", identity: "Konqueror" }, { string: navigator.userAgent, subString: "Firefox", identity: "Firefox" }, { string: navigator.vendor, subString: "Camino", identity: "Camino" }, { string: navigator.userAgent, subString: "Netscape", identity: "Netscape" }, { string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE" }, { string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv" }, { string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla"}], dataOS: [{ string: navigator.platform, subString: "Win", identity: "Windows" }, { string: navigator.platform, subString: "Mac", identity: "Mac" }, { string: navigator.userAgent, subString: "iPhone", identity: "iPhone/iPod" }, { string: navigator.platform, subString: "Linux", identity: "Linux"}] }; BrowserDetect.init();
/* end BrowserDetect*/


$(document).ready(function () {
    //action padrão de botão TOPO
    $('.Topo').click(function () { tpPg(0, 'slow') });

    //action do menu
    if (document.URL.indexOf('EducacaoInfantil') < 0 && document.URL.indexOf('EnsinoFundamental') < 0 && document.URL.indexOf('EnsinoFundamental2') < 0 && document.URL.indexOf('EnsinoMedio') < 0) $('ul.nav').superfish();
});

//carrega slides para as páginas escolhidas
function loadSlides(t, _speed, _timeout) {

    if (_speed == "") _speed = 1000;
    if (_timeout == "") _timeout = 5000;

    if (t == "") {

        er("001 - Undefined destination page.");

    } else if (t == 60) {
        $.ajax({
            type: 'GET',
            async: true,
            processData: false,
            cache: false,
            url: 'loadImagesSlide.aspx?t=' + t + '&link=true',
            complete: function (html) {
                $('.loading').remove();
                if (html.responseText == "er001") {
                    er("Ajax connection problem (null ID reference).");
                    $('.SlideShow').append('Erro ao carregar imagens');
                } else {
                    $('.SlideShow').append(html.responseText),
                        $('.SlideShow').cycle({
                            speed: _speed,
                            timeout: _timeout,
                            pause: 1,
                            easing: 'easeInOutBounce',
                            pauseOnPagerHover: 1,
                            pager: '.pager',
                            slideExpr: 'a'
                        });
                }
            }
        });

    } else {
        $.ajax({
            type: 'GET',
            async: true,
            processData: false,
            cache: false,
            url: 'loadImagesSlide.aspx?t=' + t,
            complete: function (html) {
                $('.loading').remove();
                if (html.responseText == "er001") {
                    er("Ajax connection problem (null ID reference).");
                    $('.SlideShowPages').append('Erro ao carregar imagens');
                } else {
                    $('.SlideShowPages').append(html.responseText),
                        $('.SlideShowPages').cycle({
                            speed: _speed,
                            timeout: _timeout,
                            easing: 'easeInOutBounce',
                            slideExpr: 'img'
                        });
                }
            }
        });
    }
}

function loadSlideEstrutura(t, title, text) {
    $.ajax({
        type: 'GET',
        async: true,
        processData: false,
        cache: false,
        url: 'loadImagesSlide.aspx?t=' + t,
        beforeSend: function(){
            $('.Img').html('<div class="loading"><img src="images/ajax-loader.gif" alt="" /></div>');
        },
        complete: function (html) {
            if (html.responseText == "er001") {
                $('.BoxEstrutura .Desc .Title').html(title);
                $('.BoxEstrutura .Desc .Text').html(text);
                $('.Img').cycle('destroy');
                $('.Img').html('Erro ao carregar imagens');
                er("Ajax connection problem (null ID reference).");
            } else {
                $('.BoxEstrutura .Desc .Title').html(title);
                $('.BoxEstrutura .Desc .Text').html(text);
                $('.Img').html(html.responseText),
                        $('.Img').cycle({
                            speed: '1500',
                            timeout: 0,
                            easing: 'easeInOutBounce',
                            next: '.bRight',
                            prev: '.bLeft'
                        });
            }
        }
    });
}

//scroll page
function tpPg(_x, _speed) {
    $('html, body').animate({ scrollTop: _x }, _speed);
}


function er(_err) { var brw = BrowserDetect.browser; if (brw == 'Chrome' || brw == 'Firefox') console.error(_err); }

function loadMap() {
    var style = [
            {
                featureType: "all",
                elementType: "all",
                stylers: [
                    { hue: "#cfd4e3" },
                    { visibility: "on" },
                    { lightness: 50 },
                    { saturation: 2 }
                ]
            }];


    var myLatlng = new google.maps.LatLng(-22.973060, -47.00025);

    var image = 'images/marker.png';
    var contentString = '<div id="mapContent">' +
                '<p>Av. Onze de Agosto, 1.335. '+
                'Centro. Valinhos - SP.</p>' +
                '</div>';

    var myOptions = {
        zoom: 17,
        center: myLatlng,
        name: "FundMap",
        mapTypeControl: false,
        streetViewControl: false,
        zoomControl: false,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL
        },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        icon: image,
        title: "Colégio Fundamentum"
    });

    var infoBubble = new InfoBubble({
        map: map,
        content: contentString,
        shadowStyle: 0,
        padding: 15,
        backgroundClassName: 'MapBg',
        backgroundColor: 'transparent',
        borderRadius: 0,
        minWidth: 200,
        minHeight: 60,
        arrowSize: 10,
        borderWidth: 1,
        borderColor: '#ccc',
        disableAutoPan: true,
        arrowPosition: 30,
        arrowStyle: 0,
        arrowSize: 20
    });

    var MapType = new google.maps.StyledMapType(style, myOptions);
    map.mapTypes.set('FundMap', MapType);
    map.setMapTypeId('FundMap');

    google.maps.event.addListener(marker, 'click', function () {
        if (!infoBubble.isOpen()) {
            infoBubble.open(map, marker)
        }
    });
}

/** 
:: Resize Functions ::
Data: 24.08.11
Author: Eduardo Matos
**/

function resizeViewPort() {

    _view = _getViewPosition();

    _w = parent.$('#fancybox-wrap').width();
    _h = parent.$('#fancybox-wrap').height() + 40;

    _top = parseInt(parseInt(
        (_view[0] * 0.5) - (_h * 0.5)
    ));

    _left = parseInt(Math.round(
        (_view[1] * 0.5) - (_w * 0.5)
    ));
    _left -= 20;

    if ($.browser.msie) {
        parent.$('#fancybox-wrap')
            .stop()
            .css({
                top: _top,
                left: _left
            }, 300);
    } else {
        parent.$('#fancybox-wrap')
            .animate({
                top: _top,
                left: _left
            }, 300);
    }

}


//scroll to Bottom
function scrollBottom() {
    //console.log($(window).scrollTop() + $(window).height());
    $(document).scrollTop(500);
}

function _getViewPosition() {

    var windowHeight, windowWidth;

    if ($.browser.msie) {
        windowHeight = parent.document.body.offsetHeight;
        windowWidth = parent.document.body.offsetWidth;
    } else {
        //windowHeight = parent.window.innerHeight;
        windowHeight = parent.$('#fancybox-overlay').height();
        //windowWidth = parent.window.innerWidth;
        windowWidth = parent.$('#fancybox-overlay').width();
    }

    return [
		        windowHeight,
		        windowWidth
	        ];
}

function _getFrameSize() {
    return [parent.$("#fancybox-wrap").width(), parent.$("#fancybox-wrap").height()];
}

function _resizeFrame(h, scroll) {

    var w_wrap = _getFrameSize()[0];
    var h_wrap = _getFrameSize()[1];

    var window = _getViewPosition();

    if (h + 80 > window[0]) {
        h_wrap = parseInt(window[0] - 80);
        createScroll();
    } else {
        h_wrap = h;
    }

    if ($.browser.msie) {

        $('body').css('height', h_wrap + 'px');
        parent.$('#fancybox-wrap').css('height', h_wrap + 'px');
        parent.$('#fancybox-content').css('height', h_wrap + 'px');
        //$('body').css('height', h_wrap );
        resizeViewPort();
        //scrollBottom();

    } else {

        $('body').css('height', h_wrap);

        parent.$('#fancybox-content').css('height', h_wrap);

        parent.$('#fancybox-wrap').stop().animate({
            'height': h_wrap + 20
        }, 300, function () {
            resizeViewPort();
        });
    }

    if (scroll) {
        scrollBottom();
    }
}

function createScroll() {
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
    var ifrm = parent.$("iframe#fancybox-frame");

    if ($.browser.msie) {

        ifrm.attr('scrolling', 'yes');
        parent.$('#fancybox-content').css('overflow', 'auto');
        ifrm.css('height', $('form').height());

    } else if ($.browser.chrome) {

        ifrm.css('display', 'block');

        $('html').css({
            'overflow-y': 'auto',
            'overflow-x': 'hidden'
        }
        );

        $('body').css({
            'height': ifrm.height(),
            'overflow-y': 'auto',
            'overflow-x': 'hidden'
        });

    } else {

        ifrm.css({
            'overflow-y': 'auto'
        });
        $('body').css({
            'overflow-y': 'scroll',
            'overflow-x': 'no'
        }
        );

    }
}

function effects(action) {
    $(document).ready(function () {

        var _linkOp = 0.65;

        switch (action) {

            case "home":

                loadSlides(60, 1000, 4000);

                $('.CalendarGrid').jScrollPane();

                $(".VisitaVirtual").hover(function () {
                    //$(this).fadeTo('200','0.7');
                    $(this).css({
                        'border': '1px solid #ccc',
                        'width': '128px',
                        'height': '100px',
                        'background-position': '-3px -250px'
                    });
                },
                function () {
                    $(this).css({
                        'border': 'none',
                        'width': '130px',
                        'height': '102px',
                        'background-position': '-2px -249px'
                    });
                });
                break;

            case "colegio":

                loadSlides(40, 1000, 5000);

                $('#btHistoria').click(function () { tpPg(540, 'slow') });
                $('#btMissao').click(function () { tpPg(1000, 'slow') });
                $('#btFilosofia').click(function () { tpPg(1190, 'slow') });

                break;

            case "cursos":

                loadSlides(63, 1000, 5000);

                $('.Topo').click(function () { tpPg(0, 'slow') });

                $('.KernelCursos, .KernelExtra').toggle();

                $('.Curriculares').stop().click(function (event) {

                    event.preventDefault();

                    $('.KernelExtra').animate(
                    { opacity: ['hide', 'easeInElastic'] }, 300, 'linear',
                    function () {
                        $('.KernelCursos').animate(
                            { opacity: ['show', 'easeOutBounce'] }, 1500, 'linear');
                        tpPg(510, 'slow');
                    });

                    $('.Curriculares').css('borderBottom', '4px solid #999');
                    $('.ExtraCurriculares').css('borderBottom', 'none');
                });

                $('.ExtraCurriculares').stop().click(function () {

                    $('.KernelCursos').animate(
                    { opacity: ['hide', 'easeInElastic'] }, 300, 'linear',
                    function () {
                        $('.KernelExtra').animate(
                            { opacity: ['show', 'easeOutBounce'] }, 1500, 'linear');
                        tpPg(510, 'slow');
                    });

                    $('.ExtraCurriculares').css('borderBottom', '4px solid #999');
                    $('.Curriculares').css('borderBottom', 'none');

                });

                $('.BottomC .Button *, .BottomCExtra, .Menu .Link').hover(function () {
                    $(this).stop().fadeTo(200, 0.65);
                }, function () {
                    $(this).stop().fadeTo(400, 1);
                });

                $('.Curriculares, .ExtraCurriculares').hover(function () {
                    $(this).stop().fadeTo(600, 0.25);
                }, function () {
                    $(this).stop().fadeTo(200, 1);
                });

                break;

            case "estrutura":

                var idBox;
                var effect = "easeInOutBounce";

                loadSlides(41, 1000, 5000);

                $('.Spacer, .Spacer *, .BoxEstrutura, .BoxEstrutura *').hide();

                $('.Left div a, .Right div a').click(function () {
                    var idAux = this.id;
                    var title = $(this).html();
                    var text = $(this).next('div').html();

                    if (idAux != idBox) {
                        $('.Img').html('');
                        if (!idBox) {
                            $('.Spacer, .Spacer *').fadeIn('800', effect);
                            $('.BoxEstrutura, .BoxEstrutura *').fadeIn('800', effect);
                            loadSlideEstrutura(idAux, title, text);
                            tpPg(550, '200');
                        } else {
                            $('.Spacer, .Spacer *').fadeOut('800', effect);
                            $('.BoxEstrutura, .BoxEstrutura *').fadeOut('800', effect);
                            $('.Spacer, .Spacer *').fadeIn('800', effect),
                                    $('.BoxEstrutura, .BoxEstrutura *').fadeIn('800', effect),
                                        loadSlideEstrutura(idAux, title, text);
                        }
                        idBox = idAux;
                        tpPg(550, '200');

                    } else {
                        $('.BoxEstrutura').effect("shake", { times: '2', direction: 'left', distance: '6' }, 100);
                    }

                });

                $('.Fechar').click(function () {
                    $('.Spacer, .Spacer *').fadeOut('400', effect),
                        $('.BoxEstrutura, .BoxEstrutura *').fadeOut('400', effect);
                    idBox = '';
                });

                break;

            case "perguntas":

                loadSlides(53, 1000, 5000);

                $('.PerguntaTitle').click(function () {
                    $(this).next().fadeToggle('300', 'easeInBounce');
                });
                $('.Resposta').bind('mouseover', function () {
                    $(this).addClass('Bg', 'fast');
                })
                .bind('mouseout', function () {
                    $(this).removeClass('Bg', 'fast');
                });

                break;

            case "tesouraria":

                loadSlides(55, 1000, 5000);

                var _a, _b, _c;
                $('table tr td').hide();
                $('td.HeaderTable').show();
                _a = 1, _b = 1, _c = 1;
                $('#Mensalidades11 td.HeaderTable').click(function () {
                    if (_a) {
                        $(this).parent().parent().find('td').fadeIn('slow');
                        _a = null;
                    } else {
                        $(this).parent().parent().find('td').not('.HeaderTable').fadeOut('fast');
                        _a = 1;
                    }
                });

                $('#Mensalidades12 td.HeaderTable').click(function () {
                    //$(this).parent().parent('table tr td').toggle();
                    if (_b) {
                        $(this).parent().parent().find('td').fadeIn('slow');
                        _b = null;
                    } else {
                        $(this).parent().parent().find('td').not('.HeaderTable').fadeOut('fast');
                        _b = 1;
                    }
                });

                $('#Material td.HeaderTable').click(function () {
                    //$(this).parent().parent('table tr td').toggle();
                    if (_c) {
                        $(this).parent().parent().find('td').fadeIn('slow');
                        _c = null;
                    } else {
                        $(this).parent().parent().find('td').not('.HeaderTable').fadeOut('fast');
                        _c = 1;
                    }
                });

                break;

            case "infantil":

                loadSlides(46, 1000, 5000);

                $('.Topo').click(function () { tpPg(0, 'slow') });
                $('.Menu .Link').click(function () { window.location = "EducacaoInfantil" });
                $('.Menu .Link').hover(function () {
                    $(this).stop().fadeTo(200, _linkOp);
                }, function () {
                    $(this).stop().fadeTo(400, 1);
                });
                $('.Sobre .Contato').click(function () { window.location = "Contato.aspx?r=Infantil" });

                $('a.buttonShow').click(function () {
                    var _id = $(this).attr('id');
                    $('.BoxHidden.' + _id).slideToggle('slow');
                });

                break;

            case "fundamental1":

                loadSlides(47, 1000, 5000);

                $('.Topo').click(function () { tpPg(0, 'slow') });
                $('.Menu .Link').click(function () { window.location = "EnsinoFundamental" });
                $('.Menu .Link').hover(function () {
                    $(this).stop().fadeTo(200, _linkOp);
                }, function () {
                    $(this).stop().fadeTo(400, 1);
                });
                $('.Sobre .Contato').click(function () { window.location = "Contato.aspx?rf=Fundamental" });

                $('a.buttonShow').click(function () {
                    var _id = $(this).attr('id');
                    $('.BoxHidden.' + _id).slideToggle('slow');
                });

                break;

            case "fundamental2":

                loadSlides(48, 1000, 5000);

                $('.Topo').click(function () { tpPg(0, 'slow') });
                $('.Menu .Link').click(function () { window.location = "EnsinoFundamental2" });
                $('.Menu .Link').hover(function () {
                    $(this).stop().fadeTo(200, _linkOp);
                }, function () {
                    $(this).stop().fadeTo(400, 1);
                });
                $('.Sobre .Contato').click(function () { window.location = "Contato.aspx?rf=Fundamental2" });

                $('a.buttonShow').click(function () {
                    var _id = $(this).attr('id');
                    $('.BoxHidden.' + _id).slideToggle('slow');
                });

                break;

            case "medio":

                loadSlides(49, 1000, 5000);

                $('.Topo').click(function () { tpPg(0, 'slow') });
                $('.Menu .Link').click(function () { window.location = "EnsinoMedio" });
                $('.Menu .Link').hover(function () {
                    $(this).stop().fadeTo(200, _linkOp);
                }, function () {
                    $(this).stop().fadeTo(400, 1);
                });

                $('.Sobre .Contato').click(function () { window.location = "Contato.aspx?rf=Medio" });

                $('a.buttonShow').click(function () {
                    var _id = $(this).attr('id');
                    $('.BoxHidden.' + _id).slideToggle('slow');
                });

                break;

            case "cronograma":

                loadSlides(51, 1000, 5000);

                $('.CalendarGrid').jScrollPane();
                $('.Back a, .Next a').click(function () {
                    $('.CalendarioLista .loader').fadeIn('10');
                    setTimeout(function () {
                        $('.CalendarioLista .loader').fadeOut('100');
                        return true;
                    }, 1500);
                });

                break;

            case "equipe":

                loadSlides(42, 1000, 5000);

                break;

            case "exaluno":

                loadSlides(44, 1000, 5000);

                $(".Enviar").formValidate();

                break;

            case "trabalhe":

                loadSlides(45, 1000, 5000);

                $(".Enviar").formValidate({
                    fileUpload: {
                        value: $('.FileUploadAnexo'),
                        required: true,
                        name: "Currículo"
                    },
                    extensoesOk: '.doc,.docx,.pdf'
                });

                break;

            case "vagas":

                loadSlides(54, 1000, 5000);

                $(".chzn-select").chosen();

                $(".Enviar").formValidate();

                break;

            case "uniforme":

                loadSlides(56, 1000, 5000);

                $(".chzn-select").chosen();

                break;

            case "jornal":

                loadSlides(56, 1000, 5000);

                $(".chzn-select").chosen();

                break;

            case "comunicados":

                loadSlides(50, 1000, 5000);

                break;

            case "secretaria":

                loadSlides(43, 1000, 5000);

                break;

            case "contato":

                loadSlides(52, 1000, 5000);
                
                $(".chzn-select").chosen();

                $(".Enviar").formValidate({
                    form: $('form')
                });

                break;
        }

    });
}
