﻿$(document).ready(function() {

    // input prompts

    $('input[type=text][title],input[type=password][title],textarea[title]').each(function(i) {
        $(this).addClass('input-prompt-' + i);
        var promptSpan = $('<span class="input-prompt"/>');
        $(promptSpan).attr('id', 'input-prompt-' + i);
        $(promptSpan).append($(this).attr('title'));
        $(promptSpan).click(function() {
            $(this).hide();
            $('.' + $(this).attr('id')).focus();
        });
        if ($(this).val() != '') {
            $(promptSpan).hide();
        }
        $(this).before(promptSpan);
        $(this).focus(function() {
            $('#input-prompt-' + i).hide();
        });
        $(this).blur(function() {
            if ($(this).val() == '') {
                $('#input-prompt-' + i).show();
                $('#input-prompt-' + i).css('display', 'inline');
            }
        });
    });

    // datepickers

    $("#formativeDate, #summativeDate").datepicker({ dateFormat: 'dd/mm/yy' });

    // comments needed

    if ($(".orderedList input").length != 0) {
        $(".orderedList input").parents("fieldset").each(function() {
            var parentFieldset = $(this).attr("id");

            checkBoxes(parentFieldset);
        });
    }

    if ($(".skillListMeta td").length != 0) {
        $("..skillListMeta td").parents("fieldset").each(function() {
            var parentFieldset = $(this).attr("id");

            tableCheckboxes(parentFieldset);
        });
    }

    if ($("#wordList").length != 0) {
        showList();
    }

    if ($("#synonymList").length != 0) {
        var synListCss = {
            'position': 'absolute',
            'top': $("form fieldset:first").position().top,
            'left': $("#description").position().left + $("#description").width() + 30,
            'background': '#fff',
            'margin-right': '30px',
            'width': '300px'
        }
        $("#synonymList").css(synListCss);
    }
    if ($("#skillList").length != 0) {
        var skillListCss = {
            'position': 'absolute',
            'top': $("form fieldset:first").position().top,
            'left': $("#synonymList").position().left + $("#synonymList").width() + 50,
            'background': '#fff',
            'margin-right': '30px',
            'width': '300px'
        }
        $("#skillList").css(skillListCss);
    }


    // the tagaction stuff
    $(".tagList a").live("click", function() {
        if ($("#tagAction").is(":visible") && $("#tagAction span").text() == $(this).text()) {
            $("#tagAction").hide();
        } else {
            $("#tagAction span").text($(this).text());
            $("#tagAction a.remove").attr("id", "r_" + $(this).text());

            var checkbox = $(":input#" + $(this).text());
            var id = $(checkbox).prev("span").attr("id");
            $("#tagAction a.goto").attr("href", $("#gotoUrl").val() + "?metaId=" + id);

            $("#tagAction").show();
        }
    });

    $("#tagAction a.remove").live("click", function() {
        $(":input#" + $(this).attr("id").substring(2)).attr("checked", false);
        checkBoxes("synonymList");
        $("#tagAction").hide();
    });


    $(".skillListMeta td").click(function() {
        var spanId = $(this).attr("id");
        var checkBox = $("input[value='" + spanId + "']");

        if ($(checkBox).is(":checked")) {
            $(checkBox).attr("checked", false);
        } else {
            $(checkBox).attr("checked", true);
        }

        var parentFieldset = $(this).parents("fieldset").attr("id");

        tableCheckboxes(parentFieldset);
    });

    $(".skillListMeta td input:checkbox").click(function() {
        var parentFieldset = $(this).parents("fieldset").attr("id");

        tableCheckboxes(parentFieldset);
    });



    $(".tagCloud span").click(function() {
        var spanId = $(this).attr("id");
        var checkBox = $("input[value='" + spanId + "']");

        if ($(checkBox).is(":checked")) {
            $(checkBox).attr("checked", false);
        } else {
            $(checkBox).attr("checked", true);
        }

        var parentFieldset = $(this).parents("fieldset").attr("id");

        checkBoxes(parentFieldset);
    });

    $(".tagCloud input:checkbox").click(function() {
        var parentFieldset = $(this).parents("fieldset").attr("id");

        checkBoxes(parentFieldset);
    });

    $(".moreLink").click(function() {
        $(this).parent().children("ol").toggle();
        if ($(this).text().indexOf("more") != -1) {
            $(this).text($(this).text().replace("more", "fewer"));
            $(this).addClass("less");
        } else {
            $(this).text($(this).text().replace("fewer", "more"));
            $(this).removeClass("less");
        }
    });

    $("#extraOptionsToggle .moreOptionsLink").click(function() {
        $("#extraFilterOpt").toggle("slow");
        if ($(this).text().indexOf("more") != -1) {
            $(this).text($(this).text().replace("more", "fewer"));
            $(this).addClass("less");
        } else {
            $(this).text($(this).text().replace("fewer", "more"));
            $(this).removeClass("less");
        }
    });

    $("fieldset").each(function() {
        var listId = $(this).attr("id");
        checkLists(listId);
    });

    // outcome form stuff
    if ($("#outcomeForm").length != 0) {
        $("input#metaId").autocomplete($("input#metaIdUrl").val(), {
            minChars: 3
        }).ajaxStart(function() {
            var ajaxLoaderCss = {
                'left': $(this).position().left + $(this).width() + 110,
                'top': $(this).position().top - 30
            }
            $("#ajax_loader").css(ajaxLoaderCss);
            $("#ajax_loader").show();
        }).ajaxStop(function() {
            $("#ajax_loader").hide();
        });

        $("input#metaId").result(function(event, data, formatted) {
            showSynonyms(data[1]);
            showLevels(data[1]);
            showMetaSkillLists(data[1]);
        });

        $("input#hiddenLevelId").change(function() {
            var level = $(this).val();
            showDescriptions(level);
        });


        // levels
        function showLevels(meta) {
            $.getJSON("FindLevels?metaId=" + meta, null,
                function(response) {
                    if (response.length > 0) {
                        var options = '';
                        for (var i = 0; i < response.length; i++) {
                            var odd = '';
                            if (i % 2 != 0) {
                                odd += " class='ac_odd'";
                            }
                            options += "<li id='" + response[i].Value + "'" + odd + ">"
                                + response[i].Text + "</li>";
                        }

                        $("input#hiddenMetaId").val(meta);

                        $("#levelId").focus();
                        $("#levelId").val(response[0].Text);
                        $("#levelIdList ul").html(options);

                        $("input#hiddenLevelId").val(response[0].Value);
                        var level = $("input#hiddenLevelId").val();
                        showDescriptions(level);
                    } else {
                        var options = "No levels exist";
                        $("#levelId").val(options);
                    }
                }
            );
        }

        // descriptions
        function showDescriptions(level) {
            $.getJSON("FindVocabularies?metaId=" + $("input#hiddenMetaId").val() + "&levelId=" + level, null,
                function(response) {
                    if (response.length > 0) {
                        var options = '';
                        for (var i = 0; i < response.length; i++) {
                            var odd = '';
                            if (i % 2 != 0) {
                                odd += " class='ac_odd'";
                            }
                            options += "<li id='" + response[i].Value + "'" + odd + ">"
                                + response[i].Text + "</li>";
                        }

                        $("input#hiddenLevelId").val(level);

                        $("#vocabularyId").focus();
                        $("#vocabularyId").val(response[0].Text);
                        $("#vocabularyIdList ul").html(options);

                        $("input#hiddenVocabularyId").val(response[0].Value);
                        var level = $("input#hiddenVocabularyId").val();
                    } else {
                        var options = "No descriptions exist";
                        $("#vocabularyId").val(options);
                    }
                }
            );
        }

        $("#levelId").click(function() {
            var cssObj = {
                'position': 'absolute',
                'left': $(this).position().left
            }
            if ($("#levelIdList ul").html().length > 0) {
                $("#levelIdList").css(cssObj).toggle();
            } else {
                showLevels($("input#hiddenMetaId").val());
                $("#levelIdList").css(cssObj).toggle();
            }
        });

        $("#levelIdList ul li").live("click", function() {
            var levelText = $(this).text();
            $("#levelId").val(levelText);
            var levelId = $(this).attr("id");
            $("input#hiddenLevelId").val(levelId);
            $("#levelIdList").hide();
            showDescriptions(levelId);
        });

        $("#levelIdList ul li").live("mouseover mouseout", function(e) {
            if (e.type == "mouseover") {
                $(this).addClass("ac_over");
            } else {
                $(this).removeClass("ac_over");
            }
        });

        $("#vocabularyId").click(function() {
            var cssObj = {
                'position': 'absolute',
                'left': $(this).position().left
            }
            if ($("#vocabularyIdList ul").html().length > 0) {
                $("#vocabularyIdList").css(cssObj).toggle();
            } else {
                showDescriptions($("input#hiddenLevelId").val());
                $("#vocabularyIdList").css(cssObj).toggle();
            }
        });

        $("#vocabularyIdList ul li").live("click", function() {
            var levelText = $(this).text();
            $("#vocabularyId").val(levelText);
            var levelId = $(this).attr("id");
            $("input#hiddenVocabularyId").val(levelId);
            showCategory($("input#hiddenVocabularyId").val());
            $("#vocabularyIdList").hide();
        });

        $("#vocabularyIdList ul li").live("mouseover mouseout", function(e) {
            if (e.type == "mouseover") {
                $(this).addClass("ac_over");
            } else {
                $(this).removeClass("ac_over");
            }
        });

        // category
        function showCategory(vocab) {
            $.getJSON("FindCategory?vocabId=" + vocab, null,
                function(response) {
                    if ((response != null) && (response.length > 0)) {
                        var catList = "<i>Category: </i><span>" + response + "</span>";
                        $("#catLists").html(catList);
                    } else {
                        $("#catLists").html("");
                    }
                }
            );
        }

        // metaSkillLists
        function showMetaSkillLists(meta) {
            $.getJSON("FindSkills?metaId=" + meta, null,
                function(response) {
                    if ((response != null) && (response.length > 0)) {
                        var metaSkillLists = "<i>Associated Skills: </i>";
                        for (var i = 0; i < response.length; i++) {
                            if (i != (response.length - 1))
                                metaSkillLists += "<span id='" + response[i].Value + "'>" + response[i].Text + "</span>, ";
                            else
                                metaSkillLists += "<span id='" + response[i].Value + "'>" + response[i].Text + "</span>";
                        }

                        $("#metaSkillLists").html(metaSkillLists);
                    } else {
                        $("#metaSkillLists").html("");
                    }
                }
            );
        }

        // synonyms
        function showSynonyms(meta) {
            $.getJSON("FindSynonyms?metaId=" + meta, null,
                function(response) {
                    if ((response != null) && (response.length > 0)) {
                        var synList = "<i>Related terms: </i>";
                        for (var i = 0; i < response.length; i++) {
                            if (i != (response.length - 1))
                                synList += "<span id='" + response[i].Value + "'>" + response[i].Text + "</span>, ";
                            else
                                synList += "<span id='" + response[i].Value + "'>" + response[i].Text + "</span>";
                        }
                        $("#synLists").html(synList);
                    } else {
                        $("#synLists").html("");
                    }
                }
            );
        }

        $("#synLists span").live("click", function() {
            $("input#metaId").val($(this).html());
            showSynonyms($(this).attr("id"));
            showLevels($(this).attr("id"));
            showMetaSkillLists($(this).attr("id"));
        });
    }

    // text area auto expand
    $("textarea.expand").each(function() {
        var ta = $(this);
        var startLength = ta.val().length;

        resizeArea(ta);

        ta.keyup(function() {
            var newLength = ta.val().length;
            if (newLength != startLength) {

                resizeArea(ta);

                startLength = newLength;
            }
        });
    });

    

    // skillset toggle
    $("#skillList table tr.skillGroup").live("click", function() {
        var level = $(this).parents("table.levelTable");
        if (level[0].className == 'levelTable selectedLevel')
            level.removeClass("selectedLevel");
        else
            level.addClass("selectedLevel");
    });

    // outcome filtering
    /*$("#outcomeOwner").change(function() {
    $("form:first").submit();
    });*/

    $("input#wordKeywordText").autocomplete($("input#wordKeywordUrl").val(), {
        minChars: 3
    });
    $("input#vocationKeywordText").autocomplete($("input#vocationKeywordUrl").val(), {
        minChars: 3
    });
    $("input#disciplineKeywordText").autocomplete($("input#disciplineKeywordUrl").val(), {
        minChars: 3
    });
    $("input#skillKeywordText").autocomplete($("input#skillKeywordUrl").val(), {
        minChars: 3
    });

    // copy descriptor
    $("#copyDescriptor").click(function() {
        if ($("#vocabularyId").val() != "") {
            $("#context").focus();
            $("#context").val($("#vocabularyId").val());
        }
    });

});

function accordianPressed(item) {
    $(item).parents("#vocabList").children("table.levelTable").removeClass("selectedLevel");
    $(item).parents("table.levelTable").addClass("selectedLevel");
}

// e = element
function resizeArea(e) {
    e.css("height", "0px");

    var h = Math.max(50, Math.min(e.attr("scrollHeight"), 200));

    e.css("overflow", (e.attr("scrollHeight") > h ? "auto" : "hidden"));
    e.css("height", h + "px");
}

// taglist stuff
function checkBoxes(fieldsetId) {
    $("#current" + fieldsetId).html("");
    $("#" + fieldsetId + " ol input").each(function() {
        if ($(this).is(":checked")) {
            $("#current" + fieldsetId).html($("#current" + fieldsetId).html() + "<a>" + $(this).attr("id") + "</a>, ");
        }
    });

    checkLists(fieldsetId);
}

function tableCheckboxes(fieldsetId) {
    $("#current" + fieldsetId).html("");
    $("#" + fieldsetId + " td input").each(function() {
        if ($(this).is(":checked")) {
            $("#current" + fieldsetId).html($("#current" + fieldsetId).html() + "<a>" + $(this).attr("id") + "</a>, ");
        }
    });

    checkLists(fieldsetId);
}

function checkLists(fieldsetId) {
    var fieldSetSpan = $("#current" + fieldsetId);
    if (fieldSetSpan.text() == "") {
        fieldSetSpan.parent().css("display", "none");
    } else {
        fieldSetSpan.parent().show();
    }
}

function hideList() {
    var ajaxLoaderCss = {
        'left': $("#main").position().left + $("#main").width()
    }
    $("#ajax_loader").css(ajaxLoaderCss);
    $("#ajax_loader").show();
    $("#vocabList").hide("normal");
    $("#skillList").hide("normal");
}

function showList() {
    if ($("#wordList table").length != 0) {
        var vocabListCss = {
            'position': 'absolute',
            'top': 60,
            'left': $("#wordList table").position().left + $("#wordList table").width() + 30,
            'max-width': $("#main").width() - $("#wordList table").width() - 80,
            'background': '#fff',
            'margin-right': '30px',
            'width': '100%'
        }

        if ($.browser.msie && ($.browser.version == 6 || $.browser.version == 7)) {
            var vocabListCss = {
                'position': 'absolute',
                'top': 60,
                'left': $("#wordList table").position().left + $("#wordList table").width() + 30,
                'background': '#fff',
                'margin-right': '30px',
                'width': '67%'
            }
        }

        $("#vocabList").css(vocabListCss);
        $("#vocabList").show("normal");

        $("#ajax_loader").hide();

        if ($.browser.msie && ($.browser.version == 6 || $.browser.version == 7)) {
            $('html, body').animate({ scrollTop: 0 }, 'slow');
        }
    }

    if ($("#skillList table").length != 0) {


        $("#skillList").show("normal");

        $("#ajax_loader").hide();

        if ($.browser.msie && ($.browser.version == 6 || $.browser.version == 7)) {
            $('html, body').animate({ scrollTop: 0 }, 'slow');
        }
    }
    
}
