﻿$(document).ready(function () {
    SetHeader();
    SetSearch();
    SetFooter();
    SetPageList();
    SetPageReport();
    SetGuiaGessulli();
});

/*****************************************************
*** Page List
*****************************************************/
var headerTimeout;
var isHoverBox = false;
function SetHeader() {
    var src = '#ctl00_Header_imgDigitalButton';

    $(src).click(function (event) {
        // OpenDigitalBox(src);
    });

    $(src).mouseenter(function () {
        clearTimeout(headerTimeout);
        isHoverBox = true;
        headerTimeout = setTimeout(function () {
            OpenDigitalBox(src);
        }, 1000);
    });

    $(src).mouseleave(function () {
        clearTimeout(headerTimeout);
        isHoverBox = false;
        headerTimeout = setTimeout(function () {
            CloseDigitalBox();
        }, 500);
    });

    $('.DigitalBox').mouseenter(function () {
        clearTimeout(headerTimeout);
        isHoverBox = true;
    });

    $('.DigitalBox').mouseleave(function () {
        isHoverBox = false;
    });

    $(document).click(function (event) {
        if (!isHoverBox) { CloseDigitalBox(); }
    });
}

function OpenDigitalBox(src) {
    $('.DigitalBox').css({ display: 'block', left: $(src).offset().left - 73, top: 78 });
}

function CloseDigitalBox() {
    $('.DigitalBox').css({ display: 'none' });
}

/*****************************************************
*** Page List
*****************************************************/
function SetPageList() {
    $('.PageList .Navegate .PageNumber').keypress(function (event) {
        if ((event.which < 48) || (event.which > 57)) {
            event.preventDefault();
        }

        if (event.which == 13) {
            var Url = $(this).attr('url');
            var PageNumber = $(this).val();
            var PageTotal = parseInt($(this).attr('pageTotal'));

            if (Url != '') {
                if (IsNumeric(PageNumber)) {
                    PageNumber = parseInt(PageNumber);
                } else {
                    PageNumber = 1;
                }
                if (PageNumber < 1) { PageNumber = 1; }
                if (PageNumber > PageTotal) { PageNumber = PageTotal; }

                if (PageNumber != 1) {
                    Url = Url + '/' + PageNumber
                }
                location.href = Url;
            }
        }
    });
}

/*****************************************************
*** Page Report
*****************************************************/
var fontSizeIndex = 3;
function SetPageReport() {
    arrFontSize = new Array(5);
    arrFontSize[0] = '9px';
    arrFontSize[1] = '11px';
    arrFontSize[2] = '13px';
    arrFontSize[3] = '15px';
    arrFontSize[4] = '17px';
    arrFontSize[5] = '23px';
    arrFontSize[6] = '30px';

    $('.PageTitle .FontSizeDown').click(function (event) {
        fontSizeIndex -= 1;
        if (fontSizeIndex < 0) { fontSizeIndex = 0; }
        $('.PageReport').css({ fontSize: arrFontSize[fontSizeIndex] });
    });

    $('.PageTitle .FontSizeUp').click(function (event) {
        fontSizeIndex += 1;
        if (fontSizeIndex > 6) { fontSizeIndex = 6; }
        $('.PageReport').css({ fontSize: arrFontSize[fontSizeIndex] });
    });

    $('.PageReport .SocialMedia .CommentsNumber').click(function (event) {
        var destination = $('.PageReport .CommentsTitle').offset().top;
        $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination - 20 }, 1000);
    });

    $('.PageReport .OptionBar .Link .TextBox').focus(function () {
        // Select input field contents    
        this.select();
    });
}

/*****************************************************
*** Search
*****************************************************/
var searchDefaultText;
function SetSearch() {
    var searchObj = '#txtSearchMain';
    searchDefaultText = 'O que você procura?';

    $(searchObj).val(searchDefaultText);
    $(searchObj).addClass('TextBoxDisabled');

    $(searchObj).focus(function () {
        $(this).removeClass('TextBoxDisabled');
        $(this).addClass('TextBoxEnabled');

        if ($(this).val() == searchDefaultText) {
            $(this).val('');
        }
    });

    $(searchObj).focusout(function () {
        if ($(this).val() == '') {
            $(this).removeClass('TextBoxEnabled');
            $(this).addClass('TextBoxDisabled');
            $(this).val(searchDefaultText);
        }
    });

    $(searchObj).keypress(function (event) {
        if (event.which == 13) {
            event.preventDefault();
            DoSearch(this);
        }
    });

    $('.Header .Search .Button').click(function (e) {
        DoSearch(searchObj);
    });
}

function DoSearch(searchObj) {
    if ($(searchObj).val() == searchDefaultText) { $(searchObj).val(''); }

    $.post('/Search/SetSession.ashx', {
        keyword: $(searchObj).val()

    }, function (data) {
        location.href = '/busca';

    });
}

/*****************************************************
*** Publicity
*****************************************************/
function PublicityClick(IdPub, Url, Target) {
    if (IdPub != '') {

        $.post('/App_Structure/Banner/Click.ashx', {
            idItem: IdPub

        }, function (data) {
            if (Url != '') {
                if (Target == 'B') {
                    $(window).open(Url);
                } else {
                    location.href = Url;
                }
            }
        }, 'json');
    }
}

function PublicityHover(size) {
    document.getElementById('ctl00_Banner_pnlBanner').style.height = size + 'px';
}

function PublicityHoverOut() {
    document.getElementById('ctl00_Banner_pnlBanner').style.height = '90px';
}

/*****************************************************
*** Footer
*****************************************************/
var footerStartMarginLeft;
var footerScroolTime;
var footerTransitionTime;
var footerTimer;
function SetFooter() {
    footerScroolTime = 15000;
    footerTransitionTime = 5000;

    /*** Social Media ***/
    $('.Footer .SocialMedia a').css({ opacity: 0 });

    $('.Footer .SocialMedia a').mouseenter(function () {
        $(this).animate({ opacity: 1 }, { queue: false, duration: 300 });
    });

    $('.Footer .SocialMedia a').mouseleave(function () {
        $(this).animate({ opacity: 0 }, { queue: false, duration: 300 });
    });

    /*** Logos ***/
    var animeteWidth = $('.Footer .Logo .Box table:first').width() - 1020;

    if ($(document).width() >= 1028) {
        $('.Footer .Logo .Box').width(1028);
        footerStartMarginLeft = 19;
    } else {
        footerStartMarginLeft = 0;
    }
    $('.Footer .Logo .Box table').css({ 'margin-left': footerStartMarginLeft + 'px' });

    $('.Footer .Logo a').css({ opacity: 0 });

    $('.Footer .Logo a').mouseenter(function () {
        $(this).animate({ opacity: 1 }, { queue: false, duration: 300 });
    });

    $('.Footer .Logo a').mouseleave(function () {
        $(this).animate({ opacity: 0 }, { queue: false, duration: 300 });
    });

    FooterScrolling(animeteWidth);
    footerTimer = setTimeout(function () { FooterStartScroll(animeteWidth); }, footerTransitionTime);
}

function FooterScrolling(animeteWidth) {
    var div = $('.Footer .Logo .Box');
    var divWidth = div.width();

    div.mousemove(function (e) {
        var ulWidth = animeteWidth - footerStartMarginLeft;
        var left = (e.pageX - div.offset().left) * animeteWidth / divWidth;
        left = left - (left * 2) + footerStartMarginLeft;

        clearTimeout(footerTimer);
        $('.Footer .Logo .Box table').stop();

        $('.Footer .Logo .Box table').css({ 'margin-left': left + 'px' });
    });

    div.mouseleave(function (e) {
        footerTimer = setTimeout(function () {
            FooterStartScroll(animeteWidth);
        }, 30000);
    });
}

function FooterStartScroll(animeteWidth) {
    $('.Footer .Logo .Box table').css({ 'margin-left': footerStartMarginLeft + 'px' });
    $('.Footer .Logo .Box table').animate({ 'margin-left': '-' + (animeteWidth - footerStartMarginLeft) + 'px' }, { queue: false, duration: footerScroolTime, easing: 'linear', complete: function () {
        footerTimer = setTimeout(function () {
            $('.Footer .Logo .Box table').animate({ opacity: 0 }, { queue: false, duration: 1000, easing: 'linear', complete: function () {
                footerTimer = setTimeout(function () {
                    $('.Footer .Logo .Box table').css({ 'margin-left': footerStartMarginLeft + 'px' });
                    $('.Footer .Logo .Box table').animate({ opacity: 1 }, { queue: false, duration: 1000, easing: 'linear', complete: function () {
                        footerTimer = setTimeout(function () {
                            FooterStartScroll(animeteWidth);
                        }, footerTransitionTime);
                    }});
                }, 1000);
            }});
        }, footerTransitionTime);
    }});
}

function SetGuiaGessulli() {
    $('.SideBar .GuiaGessulli .Button').click(function (event) {
        var Company = $('#txtGuiaCompany').val();
        var Service = $('#txtGuiaService').val();
        var Theme = $('#ctl00_SideBar_lnkGuia').attr('rel');

        window.open('http://www.guiagessulli.com.br/GuiaGessulli/WebSite/Search/Default.aspx?NewTheme=' + Theme + '&name=' + Company + '&service=' + Service + '&category=');
    });

    $('#btnHomeGuiaSearch').click(function (event) {
        var Company = $('#txtHomeGuiaCompany').val();
        var Service = $('#txtHomeGuiaService').val();
        var Theme = $('#ctl00_SideBar_lnkGuia').attr('rel');

        if ((Company == '') && (Service == '')) {
            window.open('http://www.guiagessulli.com.br/GuiaGessulli/WebSite/Home/Default.aspx?NewTheme=' + Theme);
        } else {
            window.open('http://www.guiagessulli.com.br/GuiaGessulli/WebSite/Search/Default.aspx?NewTheme=' + Theme + '&name=' + Company + '&service=' + Service + '&category=');
        }
    });
}
