﻿TwentyQuestions.Common = {};

TwentyQuestions.Common.ShowMessageBar = function (message, time) {
    if (!time) {
        time = 4000;
    }

    $("<div />", { 'class': 'messageBar', text: message }).hide().prependTo("body")
      .slideDown('fast').delay(time).slideUp(function () { $(this).remove(); });
}

TwentyQuestions.Common.SetupTabs = function (context) {
    if (!context)
        context = '';
    else
        context = context + ' ';

    //When page loads...
    $(context + ".tab_content").hide(); //Hide all content
    $(context + "ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(context + ".tab_content:first").show(); //Show first tab content

    //On Click Event
    $(context + "ul.tabs li").click(function () {

        $(context + "ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(context + ".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
        $(context + activeTab).show(); //Fade in the active ID content

        return false;
    });

}

TwentyQuestions.Common.SetupMenu = function () {
    $('#nav li').hover(
        function () {
            //show its submenu  
            $('ul', this).stop(false,true).slideDown(100); 


        },
        function () {
            //hide its submenu  
            $('ul', this).stop(false, true).slideUp(100); 
        }
    );
}

TwentyQuestions.Common.AddTokenizerInput = function (textboxId, url, prePopulateItemsJson, hintText, resultsFormatter, tokenFormatter) {
    $("#" + textboxId).tokenInput(url, {
        classes: {
            tokenList: "token-input-list-facebook",
            token: "token-input-token-facebook",
            tokenDelete: "token-input-delete-token-facebook",
            selectedToken: "token-input-selected-token-facebook",
            highlightedToken: "token-input-highlighted-token-facebook",
            dropdown: "token-input-dropdown-facebook",
            dropdownItem: "token-input-dropdown-item-facebook",
            dropdownItem2: "token-input-dropdown-item2-facebook",
            selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
            inputToken: "token-input-input-token-facebook"
        },
        prePopulate: [prePopulateItemsJson],
        hintText: hintText,
        resultsFormatter: resultsFormatter,
        tokenFormatter: tokenFormatter,
        preventDuplicates: true,
        allowNewItems: true,
        onDelete: function (item) {
            if (item.id <= 0) { //only if it's a prepopulated item
                var currentHint = $("#" + textboxId).tokenInput("getHintText");
                var js = "javascript:TwentyQuestions.Common.AddToken(this, '" + textboxId + "'," + item.id + ",'" + item.name + "');return false;";
                var ul = $(currentHint).find("ul").append('<li><a href="javascript://" onclick="' + js + '">' + item.name + '</a></li>');

                //update dropdown suggestions
                var newHint = $(currentHint).outerHtml().replace($(currentHint).find("ul").html(), ul.html());
                $("#" + textboxId).tokenInput("updateHintText", newHint);
                //$("#" + textboxId).tokenInput("showDropdownHint");

                //hide post to facebook option if public is removed
                if (item.id == 0) {
                    $('#GamePostOptions').hide();
                }
            }
        }
    });
}

TwentyQuestions.Common.AddToken = function (sender, textboxId, id, name) {
    $("#" + textboxId).tokenInput("add", { id: id, name: name });
    var div = $(sender).parent().parent().parent();

    if (div.find('ul').children().length > 1) {
        $(sender).parent().remove();
    } else {
        $(sender).parent().html('');
    }

    //update dropdown suggestions
    $("#" + textboxId).tokenInput("updateHintText", div.outerHtml());
    //$("#" + textboxId).tokenInput("showDropdownHint");

    //hide post to facebook option if public is added
    if (id == 0) {
        $('#GamePostOptions').show();
    }

    return false;
}

TwentyQuestions.Common.RemoveQueryString = function (url, param) {
    var urlparts = url.split('?');
    if (urlparts.length >= 2) {
        var prefix = encodeURIComponent(param) + '=';
        var pars = urlparts[1].split(/[&;]/g);
        for (var i = pars.length; i-- > 0; )
            if (pars[i].indexOf(prefix, 0) == 0)
                pars.splice(i, 1);
        if (pars.length > 0)
            return urlparts[0] + '?' + pars.join('&');
        else
            return urlparts[0];
    }
    else
        return url;
}

TwentyQuestions.Common.GetQueryStringByName = function (name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}


jQuery.fn.outerHTML = function (s) {
    return (s) ? this.before(s).remove() : jQuery("&lt;p&gt;").append(this.eq(0).clone()).html();
}



