﻿var currentEditor;

$(document).ready(function () {

    $('#form1').attr('action', formAction);

    //Rotating background header images
    //var numInSet = 2;
    //var imgNum = Math.floor(Math.random() * numInSet) + 1;
    //$('.topBar').css('background-image', 'url(images/header_top_bg_' + imgNum + '.jpg)');
    //$('.lowerBar').css('background-image', 'url(images/header_lower_bg_' + imgNum + '.jpg)');

    var hours = new Date().getHours();
    if ((hours >= 18) || (hours <= 6)) {
        $('.page').addClass('night');
    }

    var menuID, height, width;
    $('.menuElements').children('div').each(function () {
        menuID = $(this).attr('id').replace('element', '');
        if ($(this).children('img').length > 0) {
            $(this).children('img').css('height', $(this).children('img').height() + 'px');
            width = $(this).children('img').width();
            $(this).children('img').css('width', $(this).children('img').width() + 'px');
            $(this).children('img').detach().appendTo('#menuImage' + menuID);
        }
        height = 100;
        $(this).children('ul').each(function () {
            if ($(this).height() + 10 > height)
                height = $(this).height() + 10;
            width = width + $(this).width();
            $(this).detach().appendTo('#menuLinks' + menuID);
        });
        $('#menuLinks' + menuID).css('height', height + 'px');
        $('#menuImageBorder' + menuID).css('height', height + 'px');
        $('#menuLinks' + menuID).parent().css('height', (height + 20) + 'px');
        $('#menuLinks' + menuID).parent().css('width', (width + 90) + 'px');
    });

    //Menus
    $('.menu').children(':nth-child(1)').children().hover(
        function () {
            if ($(this).find('.subMenu').css('display') == 'none') {
                $(this).children(':nth-child(1)').addClass('hover');
                if ($(this).find('.subMenu').css('left') == '0px') {
                    if (($(this).position().left + $(this).find('.subMenu').width()) > $(this).parent().parent().width()) {
                        $(this).find('.subMenu').css('left', $(this).position().left - ($(this).find('.subMenu').width() - 110) + 'px');
                    } else {
                        $(this).find('.subMenu').css('left', $(this).position().left + 'px');
                    }
                }
                $(this).find('.subMenu').slideDown('fast');
            }
        }, function () {
            if ($(this).find('.subMenu').css('display') == 'block')
                $(this).find('.subMenu').slideUp('fast');
            $(this).children(':nth-child(1)').removeClass('hover');
        }
    );

    //Content editors
    $(this).find('.content').find('.editLink').children(':nth-child(1)').click(function (event) {
        event.preventDefault();
        var textBox = $(this).parent().parent().children(':nth-child(3)').children(':nth-child(1)').children(':nth-child(1)').children(':nth-child(2)').children(':nth-child(1)').children(':nth-child(1)');

        $(this).parent().parent().children(':nth-child(3)').modal({ modal: true, opacity: 70, overlayCss: { backgroundColor: "#232323"} });
        currentEditor = textBox.cleditor({ width: 596, height: 350 })[0];
    });

    $(this).find('.contentModal').find('.contentButtons').find('.update').click(function (event) {
        event.preventDefault();

        var content;

        if (currentEditor.sourceMode()) {
            content = currentEditor.$area.text();
        } else {
            content = currentEditor.$frame.contents().find('body').html();
        }

        var id = $(this).parent().parent().attr('id').replace('sect', '').replace('Content', '');

        $.ajax({
            type: "POST",
            url: url + "/Service.aspx/UpdateSectionContent",
            data: "{'sectionID':'" + id + "','content':'" + content.replace(/'/g, '{sq}') + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () {
                $('#sect' + id).html(content);
                $('#sect' + id + 'Content').children(':nth-child(1)').children(':nth-child(1)').val(content);
            },
            error: function (xhr, err) {
                alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                alert("responseText: " + xhr.responseText);
            }
        });

        $.modal.close();
    });

    $(this).find('.contentModal').find('.contentButtons').find('.cancel').click(function (event) {
        event.preventDefault();
        $.modal.close();
    });

    $('#searchText').blur(function () {
        if ($.trim($('#searchText').val()).length > 0 && $.trim($('#searchText').val()).toLowerCase() != 'search...')
            $('#searchLink').attr('href', 'http://www.atlantaga.gov/sbsearch.aspx?pagesize=25&query=' + $.trim($('#searchText').val()));
    });
});
