Phx.Application.Modules.PinboardEmoticons = new function()
{
    var emoticons = null;
    var emoticonLinks = {};
    var baseUrl = $('#platformUrl').val();
    
    function ReplaceAll(Source,stringToFind,stringToReplace){
        var temp = Source;
        var stringToFind = stringToFind.toLowerCase();
        
        var position = temp.toLowerCase().indexOf(stringToFind, 0);
        var searchStringLength = temp.length;
        var _patternLength = stringToFind.length;
        while(position != -1){            
            temp = temp.substring(0, position) + stringToReplace + temp.substring(_patternLength + position,  searchStringLength);
            searchStringLength = temp.length;
            position = temp.toLowerCase().indexOf(stringToFind, 0);
        }
        return temp;
    }

    var texts;
    var resetLinkify;

    var _linkify = function()
    {
        $.each(texts, function() {
           $(this).html(Phx.Util.linkify($(this).html(), {wrap: 52, embedActive: true}, resetLinkify));
           resetLinkify = false;
        });
    }
    
    this.init = function()
    {
        resetLinkify = true;
        texts = $('.pinboard-entry-text');
        
        _linkify();

        var maxWidth = 300;
        var maxHeight = 250;
        var pinboardEntriesObject = $('#Mod-Pinboard-Snipplet .obj-comment-list li');
        if (pinboardEntriesObject.length === 0) {
            pinboardEntriesObject = $('#Pinboard_List .obj-comment-list li');
            maxWidth = 510;
            maxHeight = 500;
        }

        if (pinboardEntriesObject.length > 0){
            this.prepareReplacement(pinboardEntriesObject);
            $('.pinboard-list').fadeIn();
        }

        Phx.Util.OEmbed(texts, maxWidth, maxHeight);        
    }


    /**
     * replace emoticon char with emoticon images
     */
    this.prepareReplacement = function(pinboardEntriesObject){
        var pinboardEntries = $('div.pinboard-entry-text', pinboardEntriesObject);
        var pinboardEntryWritersProfileLink = $('>a', pinboardEntriesObject);
        var pinboardEntriesLength = pinboardEntries.length;

        this.getEmoticonsAndLinks();
        
        for (var i = 0; i < pinboardEntriesLength; i++) {
            $(pinboardEntries[i]).html(this.realReplacement(
                $(pinboardEntries[i]).html(),                                                
                $(pinboardEntryWritersProfileLink[i]).attr('href')
            ));
        }
    }
    
    this.realReplacement = function(text, profileLink){
        for (var pattern in emoticons){
            var image = emoticons[pattern];
            var classStr = '';
            var link = emoticonLinks[pattern.toUpperCase()];
            
            if(link == '##GADGETLIST##'){
                text = ReplaceAll(text, pattern, '<a href="'+ baseUrl + profileLink +'#gadgets-list" title=""><img alt="" src="' + image + '" '+classStr+'/></a>');
                continue;
            }
            
            if(link.indexOf('http')!==-1) {
                link = link.replace('##SENDERLINK##', (baseUrl + profileLink));
                text = ReplaceAll(text, pattern, '<a href="'+link+'#gadgets-list" title=""><img alt="" src="' + image + '" '+classStr+'/></a>');
                continue;
            }
            
            text = ReplaceAll(text, pattern, '<img alt="" src="' + image + '" '+classStr+'/>');  
        };
        return text;
    }
    
    this.getEmoticonsAndLinks = function(){
        emoticonsObject = $('#emoticonArray');
        if (emoticonsObject.length == 0) {
            return
        }
        emoticons = JSON.parse(emoticonsObject.val());
        
        emoticonsLinksObject = $('#emoticonLinks');
        if (emoticonsLinksObject.length == 0 || emoticonsLinksObject.val().length == 0) {
            return;     
        }
        emoticonLinks = JSON.parse(emoticonsLinksObject.val());
    }
}();
