/*
    Author: Matthew Juffs
*/

$(document).ready(function () {
    SetupScroll();
    SetupDropdowns();
    SetupMobileMenu();
    SetupLightBox();
    SetupResponsiveSlides();
    SetupEvents();
    SetupCKEditor();
    SetupDates();
});

function SetupCalendar() {
    $("#calendar").fullCalendar({
        // setup header
        header: {
            left: "prev,next today",
            center: "title",
            right: "month,basicWeek"
            //right: "month,basicWeek,basicDay"
        },

        editable: false, // disable drag/drop of events

        firstDay: 1, // week starting on a Monday

        weekMode: "fixed", // "fixed" ensures calendar is always 6 weeks high

        columnFormat: {
            month: 'ddd',    // Mon
            week: 'ddd d/M', // Mon 20/2
            day: 'dddd d/M'  // Monday 20/2
        },

        events: {
            url: '/Json.ashx?cmd=events',
            error: function () {
                //alert('There was an error while fetching events!');
                $("#calendar").hide();
            },
            success: function () {
                //alert('loaded');
            },
            cache: true
        },

        loading: function (bool) {
            if (bool) {
                $("#loading").show();
                logMessage("loading");
            }
            else {
                $("#loading").hide();
                logMessage("loaded");
            }
        }
    });
}

function logMessage(msg) {
    try {
        console.log(msg)
    } catch (e) {
        alert(msg)
    }
}

function SetupDates() {
    try {
        $(function () {
            $("#txtStartDate, #txtEndDate").datepicker({ dateFormat: 'dd-mm-yy' });
        });
    } catch (e) {

    }
}

function SetupCKEditor() {
    console.log("before");
    if (window.CKEDITOR) {
        if ($("#txtContent").length > 0) {
            CKEDITOR.replace('txtContent');
        }
    }
    console.log("after");
}

function SetupEvents() {
    $("#aGenerateUrl").click(function () {
        GenerateURL();
    });

    $("#aInsertImage, #aCloseImages").click(function () {
        Toggle('#imageList');
    });
}

function SetupLightBox() {
    $(function () {
        $('ul.images a').lightBox();
        $('a.lightbox').lightBox();
    });
}

function SetupResponsiveSlides() {
    $("#banners").responsiveSlides({
        speed: 4000, //Integer: How long one image shows before fading to next, in milliseconds
        fade: 1000, //Integer: Crossfade, in milliseconds
        auto: true, //Boolean: Animate automatically? if 'false', pagination is created automatically
        maxwidth: 960 //Integer: Max-width of the Slideshow + images, in pixels
    });
}

function SetupScroll() {
    $(".scroll").click(function (event) {
        // prevent the default action for the click event
        event.preventDefault();

        // get the full url - like mysitecom/index.htm#home
        var full_url = this.href;

        // split the url by # and get the anchor target name - home in mysitecom/index.htm#home
        var parts = full_url.split("#");
        var trgt = parts[1];

        // get the top offset of the target anchor
        var target_offset = $("#" + trgt).offset();
        var target_top = 0; //target_offset.top;//use to determine how high to scroll, but in this use we're going to the top

        // goto that anchor by setting the body scroll top to anchor top
        $('html, body').animate({ scrollTop: target_top }, 500);
    });
}

function SetupMobileMenu() {
    // TODO: use current location to pre-select the mobile menu
    //alert(window.location.href);
    $("#mobileMenu").change(function () {
        //alert($(this).val());
        window.location.href = $(this).val();
    });
}

function SetupDropdowns() {
    var $menulis = $('#menu li.root');// root menu items
    var $dropdowns = $('#menu li ul');// dropdown menus

    // trigger dropdowns if available
    $menulis.hover(function () {
        try {
            // determine the dropdown's id from the li's id
            var liId = $(this).attr('id');
            var ddId = '#' + liId.replace('li', 'dd');

            if ($(ddId).length > 0) {
                ShowMenu(ddId);
            }
            else {
                // hide if there's no dropdown
                HideMenus();
            }
        } catch (e) {
            // hide if there's any problems
            HideMenus();
        }
    });

    // trigger showmenu when on the dropdown itself
    $dropdowns.hover(function () {
        ShowMenu($(this).attr('id'));
    });

    // hide dropdowns if moving down the page
    $('#title, #main').hover(function () {
        // fade them out when moving down, for effect
        HideMenus();
    });
}

function ShowMenu(strObj) {
    // hide all dropdowns initially
    HideMenus();

    // show this specific dropdown
    $(strObj).show();
}

function HideMenus() {
    $('.dropdown').hide();
    //$('.dropdown').fadeOut('fast');
    //$('.dropdown').fadeOut('slow');
}

// used as a helper to generate a URL automatically
function GenerateURL() {
    var title = document.getElementById("txtTitle").value;

    title = title.toLowerCase();
    title = title.replace('/', '');
    title = title.replace(new RegExp(/\\/g), '');
    title = title.replace(/ /g, '-');
    title = title.replace('&', 'and');
    title = title.replace('\'', '');
    title = title.replace('--', '-');
    title = title.replace(/\./g, '');

    document.getElementById("txtUrl").value = title;
}

function Toggle(id) {
    $(id).toggle();
}

/*function Email() {
// buils up an email address to prevent spam
var user = 'courthouse';
var domain = 'rbwm';
var extension = '.org';
var subject = 'Mail from website';
var email = user + '@' + domain + extension
document.write('<a href=\"mailto:' + email + '?subject=' + subject + '\">' + email + '</a>');
}*/

/*function CheckFormInput(obj) {
    // clears form input values for user to enter their own values
    if (obj.value == "Name" || obj.value == "Phone" || obj.value == "Email" || obj.value == "Your Message") {
        obj.value = "";
    }
}*/

/*function RequestViewing() {
    //preselects the Request a viewing option in the Subject dropdown
    //var obj = document.getElementById("ContactForm_ddlSubject");
    //obj[1].selected = "1";

    //1) show the form
    document.getElementById("divContactForm").style.display = 'block';

    //2) set the subject and hide
    try {
        var obj = document.getElementById("MainContent_ContactForm_ddlSubject");
        obj[1].selected = "1";
        obj.style.display = 'none';
    } catch (e) {

    }
}*/

/*function ValidateComment() {
//validates a visitor's comment
var valid = true;
var error = '';
var commentError = document.getElementById('comments-error');

// required fields
if (document.getElementById('name').value == '') {
error += '<em>Name</em><br />';
valid = false;
}

if (document.getElementById('email').value == '') {
error += '<em>Email</em><br />';
valid = false;
}

if (document.getElementById('comment').value == '') {
error += '<em>Comment</em><br />';
valid = false;
}
    
if (valid == false){
error = 'Please ensure all of the following are filled in:<br />' + error;
commentError.style.display = 'block';
commentError.innerHTML = error;
} else {
commentError.style.display = 'none';
}

return valid;
}*/
