﻿function isEncHTML(str) {
    if (str.search(/&amp;/g) != -1 || str.search(/&lt;/g) != -1 || str.search(/&gt;/g) != -1)
        return true;
    else
        return false;
};

function decHTMLifEnc(str) {
    if (isEncHTML(str))
        return str.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
    return str;
}

var trophyInit = false;

function TrophyCheck() {
    if (!trophyInit) {
        $().bind('cbox_close', function() { $('.popup-trophy-container').hide(); });
        $().bind('cbox_open', function() { $('.popup-trophy-container').show(); });
               
        if ($(".popup-trophy-container").size() == 0) {
            $('body').append(
                	'<div style="display: none;" class="popup-trophy-container">' +
                	'Congratulations! You earned a new trophy! Booya!!<br /><br />' +
                	'<div class="ajaxtrophy"></div>	<br />' +
		            'Visit your profile to see all your trophies and check out the ones you can still earn.' +
		            '</div>'
		            );
        }

        trophyInit = true;
    }

    $.ajax({
        type: "POST",
        url: "/ws/TrophyService.asmx/TrophyCheck",
        dataType: "html",
        error: function(msg) {
            alert('Whoops. : ' + msg);
        },
        success: function(data, textStatus) {
            if (data.indexOf("#false#") == -1) {
                $(".ajaxtrophy").html(decHTMLifEnc(data));
                $.fn.colorbox({ width: '701px', open: true, inline: true, scrolling: false, href: ".popup-trophy-container" });
            }
        }
    });
}