﻿jQuery.textExpander = {
    showAll: false,
    init: function() {
        $(".expandoBody").hide();
        $(".expandoHeader").click(function() {
            var bodyId = $(this).attr("title");
            var expandoBody = $("#" + bodyId);
            if (!jQuery.textExpander.showAll) {
                $(".expandoHeader").removeClass("expandoHeaderSelected");
                if (!expandoBody.is(':visible')) {
                    $(".expandoHeader > img").attr("src", "../content/images/plus.gif");
                    $(".expandoBodySelected").removeClass("expandoBodySelected").addClass("expandoBody");
                    expandoBody.removeClass("expandoBody").addClass("expandoBodySelected").slideDown("slow"); 
                    $(this).find("img").attr("src", "../content/images/minus.gif");
                    $(this).addClass("expandoHeaderSelected");

                }
                else {
                    expandoBody.removeClass("expandoBodySelected").addClass("expandoBody");
                    $(this).find("img").attr("src", "../content/images/plus.gif");

                }
                $(".expandoBody").slideUp("slow");
            }
            else {
                if (!expandoBody.is(':visible')) {
                    expandoBody.slideDown("slow");
                    $(this).find("img").attr("src", "../content/images/minus.gif");
                    $(this).addClass("expandoHeaderSelected");
                }
                else {
                    $(this).find("img").attr("src", "../content/images/plus.gif");
                    $(this).removeClass("expandoHeaderSelected");
                    expandoBody.slideUp("slow");
                }
            }
        });
        $(".expandoMenuItem").click(function() {
            var action = $(this).attr("title");
            $(".expandoMenuItem").removeClass("ui-state-active");
            $(this).addClass("ui-state-active");
            $(".expandoBodySelected").removeClass("expandoBodySelected").addClass("expandoBody");
            switch (action) {
                case "showAll":
                    $(".expandoHeader > img").attr("src", "../content/images/minus.gif");
                    $(".expandoBody").slideDown("slow");
                    $(".expandoHeader").addClass("expandoHeaderSelected");
                    jQuery.textExpander.showAll = true;
                    break;
                case "hideAll":
                    $(".expandoHeader > img").attr("src", "../content/images/plus.gif");
                    $(".expandoHeader").removeClass("expandoHeaderSelected");
                    $(".expandoBody").slideUp("slow");
                    var questionSelector = ($(".expandoBody:visible:first").size() == 0) ? ".expandoBody:first" : ".expandoBody:visible:first";
                    $(questionSelector).slideDown("slow");
                    var headerTitle = $(questionSelector).attr("id");
                    $(".expandoHeader").each(function() {
                        if (headerTitle == $(this).attr("title")) {
                            $(this).find("img").attr("src", "../content/images/minus.gif");
                            $(this).addClass("expandoHeaderSelected");
                        }

                    });
                    jQuery.textExpander.showAll = false;
                    break;
            }
        });
        /*$(".expandoMenuItem").hover(function() {
        $(this).addClass("ui-state-hover");
        },
        function() {
        $(this).removeClass("ui-state-hover");
        });*/
    }
}


function limitChars(textid, limit, infodiv){
    var text = $('#'+textid).val(); 
    var textlength = text.length;
    if(textlength > limit){
        $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
        $('#'+textid).val(text.substr(0,limit));
        return false;
    }
    else{
        $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
        return true;
        }
    }