﻿var alert_t = 0;

$(document).ready(function() {

    /* for keeping track of what's "open" */
    var activeClass = 'dropdown-active', showingDropdown, showingMenu, showingParent;
    /* hides the current menu */
    var hideMenu = function() {
        if (showingDropdown) {
            showingDropdown.removeClass(activeClass);
            showingMenu.hide();
        }
    };

    /* recurse through dropdown menus */
    $('.dropdown').each(function() {
        /* track elements: menu, parent */
        var dropdown = $(this);
        var menu = dropdown.next('div.dropdown-menu'), parent = dropdown.parent();
        /* function that shows THIS menu */
        var showMenu = function() {
            hideMenu();
            showingDropdown = dropdown.addClass('dropdown-active');
            showingMenu = menu.show();
            showingParent = parent;
        };
        /* function to show menu when clicked */
        dropdown.bind('click', function(e) {
            if (e) e.stopPropagation();
            if (e) e.preventDefault();
            showMenu();
        });
        /* function to show menu when someone tabs to the box */
        dropdown.bind('focus', function() {
            showMenu();
        });
    });

    /* hide when clicked outside */
    $(document.body).bind('click', function(e) {
        if (showingParent) {
            var parentElement = showingParent[0];
            if (!$.contains(parentElement, e.target) || !parentElement == e.target) {
                hideMenu();
            }
        }
    });

    getNewAlert();
    alert_t = window.setInterval('getNewAlert();', 15000);
});


function getNewAlert() {
    $.ajax({
        type: 'POST',
        url: 'http://' + document.domain + '/WService/SocialGraph.asmx/getUserNewAlert',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: "{}",
        success: function(res) 
        {
            var data = eval('(' + res.d + ')');
            if (data != 'error' && data != 'unauthorize') {
                var displayMsg = '';
                if (data.NumOfNewFollowers != 0) {
                    displayMsg = displayMsg + '<a href="http://' + document.domain + '/Followship/Followers.aspx"><img src="http://' + document.domain + '/images/icons/user_add.png" alt="Users" align="absmiddle" /> ' + data.NumOfNewFollowers + ' new followers</a><br/>';
                }
                if (data.NumNewOfNotic != 0) {
                    displayMsg = displayMsg + '<a href="http://' + document.domain + '/Account/Notification.aspx"><img src="http://' + document.domain + '/images/icons/bell.png" alt="Users" align="absmiddle" /> ' + data.NumNewOfNotic + ' new notifications</a>';
                }

                if (displayMsg != '') {
                    $('#NewAlert').css("display", "block");
                    $('#NewAlert').html(displayMsg);
                    $("#NewAlert").fadeTo('slow', 0.2);
                    $("#NewAlert").fadeTo('slow', 1.0);                
                }
            }
        },
        error: function() {
            $('#NewAlert').css("display", "none");
            clearInterval(alert_t);
        }
    });
}


function getTipByType(type) {
    $.ajax({
        type: 'POST',
        url: 'http://' + document.domain + '/WService/User_Service.asmx/getRandomTipByType',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: "{'type':'" + type + "'}",
        success: function(res) {
            var data = eval('(' + res.d + ')');
            if (data != '') 
            {
                $('#rightcol').prepend('<div id="tip_msg" class="tip_box"><div class="incontent"><div class="topbar"><a title="close" href="javascript:void(); return false;" onclick="javascript:closeTip(); return false;">X</a><span class="title"><img src="http://' + document.domain + '/images/icons/lightbulb.png" align="absmiddle" /> Tip</span></div><div id="tip_c" style="width:260px; padding: 10px;">' + data + '</div></div></div>');
                $("#tip_msg").fadeTo('slow', 0.2);
                $("#tip_msg").fadeTo('slow', 1.0);
            }
        },
        error: function() {
        }
    });
}

function closeTip() {
    $('#tip_msg').fadeOut(500, function() { $('#tip_msg').remove(); });
}

function enterHere(e) {
    e = e || window.event;
    var code = e.keyCode || e.which;
    if (code == 13) {
        document.getElementById(btn).click();
    }
}

