﻿tagrootpath = '/resources/';
_orderbytag = false;
_orderbydesc = false;
_randomize = false;
_itemcount = 50;
_activeTab = '#tab1';
_CurrentViewMode = '';

$(function () {



    tabcontent = $(".tab_content");
    letterbutton = $(".explore_tags_searchbar_inner_link");

    _randomize = true;


    tabcontent.hide();
    $("ul.tabs li:first").addClass("active").show();
    $(".tab_content:first").show();

    $("ul.tabs li").click(function () {

        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content

        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content

        _activeTab = activeTab;
        if (_activeTab == '#tab2')
            _randomize = false;
        else
            _randomize = true;

        getclouddata(letter, _itemcount, _orderbytag, _orderbydesc, _randomize);

        $(activeTab).show();
        return false;
    });

    $("#alpha").click(function () {
        if (_CurrentViewMode == 'alpha') {
            if (_orderbydesc)
                _orderbydesc = false;
            else
                _orderbydesc = true;
        }
        _CurrentViewMode = 'alpha';
        _orderbytag = true;
        _randomize = false;
        getclouddata(letter, _itemcount, _orderbytag, _orderbydesc, _randomize);
    });

    $("#pop").click(function () {
        if (_CurrentViewMode == 'pop') {
            if (_orderbydesc)
                _orderbydesc = false;
            else
                _orderbydesc = true;
        }
        _CurrentViewMode = 'pop';
        _orderbytag = false;
        _randomize = false;
        getclouddata(letter, _itemcount, _orderbytag, _orderbydesc, _randomize);
    });

    $("#25tags").click(function () {
        _CurrentViewMode = '25tags';
        _itemcount = 25;
        getclouddata(letter, _itemcount, _orderbytag, _orderbydesc, _randomize);
    });

    $("#50tags").click(function () {
        _CurrentViewMode = '50tags';
        _itemcount = 50;
        getclouddata(letter, _itemcount, _orderbytag, _orderbydesc, _randomize);
    });

    letterbutton.click(function () {

        // get the current letter
        letterbutton_selected = $(".explore_tags_searchbar_inner_link_selected");
        letterbutton_selected.removeClass("explore_tags_searchbar_inner_link_selected");
        letterbutton_selected.addClass("explore_tags_searchbar_inner_link");

        if ($(this)[0].innerHTML == letter) {
            letter = '';
        }
        else {
            letter = $(this)[0].innerHTML;
            $(this).addClass("explore_tags_searchbar_inner_link_selected");
        }

        $(".explore_tags_searchbar_label")[0].innerText = letter.toUpperCase();

        getclouddata(letter, _itemcount, _orderbytag, _orderbydesc, _randomize);
    });

    // set getclouddata to all tags on page load.  
    letter = '';
    getclouddata(letter, _itemcount, _orderbytag, _orderbydesc, _randomize);
});

function getclouddata(letter, tagcount, orderbytag, orderbydesc, randomize) {

    defaultParameters = "{letter:'" + letter + "',tagcount:" + tagcount + ",orderbytag:" + orderbytag + ",orderbydesc:" + orderbydesc + ",randomize:" + randomize + "}";

    if (_activeTab == '#tab1') {
        $(".explore_tags_tagcloud").empty();
        $(".explore_tags_tagcloud").append('<ul id="ResultsTable">')
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/models/CareersTagged_local.asmx/GetPopularNTagsByLetter",
            data: defaultParameters,
            dataType: "json",
            timeout: 4000,
            success: function (msg) {
                obj = jQuery.parseJSON(msg.d);
                if (obj != null) {
                    $.each(obj, function (index, value) {
                        $("#ResultsTable").append("<li><a href='" + tagrootpath + value.Tag + "' title='" + value.Tag + "'>" + value.Tag + "</a>" + "<span dir=ltr>" + value.ItemsCount + "</span></li>");
                    });
                    StyleTags();
                }

            },
            error: function (msg) {
                alert(msg.statusText);
            }
        });
        $(".explore_tags_tagcloud").append('</ul>')
    }
    else {
        $(".explore_tags_taglist").empty();
        $(".explore_tags_taglist").append('<ul id="ResultsTableList">')
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/models/CareersTagged_local.asmx/GetPopularNTagsByLetter",
            data: defaultParameters,
            dataType: "json",
            timeout: 4000,
            success: function (msg) {
                obj = jQuery.parseJSON(msg.d);
                if (obj != null) {
                    $.each(obj, function (index, value) {
                        $("#ResultsTableList").append("<li><a href='" + tagrootpath + value.Tag + "' title='" + value.Tag + "'>" + value.Tag + "</a>" + " (" + value.ItemsCount + ")</li>");
                    });
                }

            },
            error: function (msg) {
                alert(msg.statusText);
            }
        });
        $(".explore_tags_taglist").append('</ul>')
    }
}



function StyleTags() {

    var cloudMarkup = '';
    var maxPercent = 150, minPercent = 100;
    var max = 1, min = 999, count = 0;
    var catContainer = $('.explore_tags_tagcloud');
    var categories = catContainer.find('ul li');

    var diff = 0;
    var distribution = 0;

    categories.each(function (i) {
        count = parseInt($(this).find('span').text());
        max = (count > max ? count : max);
        min = (min > count ? count : min);
    });

    diff = max - min;
    distribution = diff;

    var sizeclass = "";
    categories.each(function (i) {

        link = $(this).find('a');
        count = parseInt($(this).find('span').text());

        if (count >= 3500)
            sizeclass = "tag10";
        else if (count >= 3000 && count < 3500)
            sizeclass = "tag9";
        else if (count >= 2500 && count < 3000)
            sizeclass = "tag8";
        else if (count >= 2000 && count < 2500)
            sizeclass = "tag7";
        else if (count >= 1500 && count < 2000)
            sizeclass = "tag6";
        else if (count >= 1000 && count < 1500)
            sizeclass = "tag5";
        else if (count >= 800 && count < 1000)
            sizeclass = "tag4";
        else if (count >= 600 && count < 800)
            sizeclass = "tag3";
        else if (count >= 400 && count < 600)
            sizeclass = "tag2";
        else if (count < 400)
            sizeclass = "tag1";

        cloudMarkup += "<a class='" + sizeclass + "'" + " href='" + link.attr('href') + "'>" + link.text() + "</a>";

    });

    catContainer.html(cloudMarkup);
} 

