﻿$(document).ready(function() {
    
    /* JavaScript is enabled, initiate page with the fields collapsed */
    $("a.toggleCollapse").next().hide();
    $("a.toggleCollapse").addClass("collapsed");
    $("a.toggleCollapse").removeClass("expanded");

    /* Function used to expand and collapse fields in the profile form control */
    $("a.toggleCollapse").click(function() {
        if ($(this).next().is(":hidden")) {
            $(this).next().show();
            $(this).addClass("expanded");       //[-] image next to link
            $(this).removeClass("collapsed");   //[+] image next to link
        }
        else {
            $(this).next().hide();
            $(this).addClass("collapsed");
            $(this).removeClass("expanded");
        }
        return false;   //Avoid page reload
    });
});

// INTRA functions
function initAddCalenderEvent() {
    $("#AddCalendarEventArea").hide();
    $("#AddEventButtonsArea a.closeBody").hide();

    $("#AddEventButtonsArea a.openBody").click(function() {
        $("#AddCalendarEventArea").slideToggle(150);
        $(this).hide();
        $(this).next("#AddEventButtonsArea a.closeBody").show();
    });


    $("#AddEventButtonsArea a.closeBody").click(function() {
        $("#AddCalendarEventArea").slideToggle(150);
        $(this).hide();
        $(this).prev("#AddEventButtonsArea a.openBody").show();
    });
}
function initAddCalenderVisible() {
    $("#AddCalendarEventArea").show();
}
function initCollapsablePageList() {
    $(".collapsingMainBody").hide();
    $("ul.pageList a.closeBody").hide();

    $("ul.pageList a.openBody").click(function() {
        $(this).nextAll(".collapsingMainBody").slideToggle(150);
        $(this).hide();
        $(this).next("ul.pageList a.closeBody").show();
    });


    $("ul.pageList a.closeBody").click(function() {
        $(this).next(".collapsingMainBody").slideToggle(150);
        $(this).hide();
        $(this).prev("ul.pageList a.openBody").show();
    });
}

function initVisibleOnChecked(checkbox, objectToActOn, collapsedClassName, expandedClassName) {
    function SetClassName(visible) {
        if (visible) {  
            $(objectToActOn).removeClass(collapsedClassName);
            $(objectToActOn).addClass(expandedClassName);
            $(objectToActOn).show();

        } else {
            $(objectToActOn).removeClass(expandedClassName);
            $(objectToActOn).addClass(collapsedClassName);
            $(objectToActOn).hide();
        }

    };

    var visible = $(checkbox).attr('checked') || $(checkbox).children('input').attr('checked');
    SetClassName(visible);
    
    $(checkbox).click(function() {
        SetClassName($(objectToActOn).is(':hidden'));
    });
   
}
