﻿(function($) {
    $.linksToNotes = function(el) {
        // Kevin Clarke - a plugin to create an add to notes function from hyperlinks with an attribute of "_new"
        var base = this;
        base.$el = $(el);
        base.el = el;
        base.$el.data("linksToNotes", base);
        base.init = function() {
        };

        // Run initializer
        base.init();
    };


    $.fn.linksToNotes = function() {
        return this.each(function() {
            (new $.linksToNotes(this));
            //alert();
            var selector = "a";
            var selectedID = $(this).attr("id");
            if (selectedID) {
                selector = "#" + selectedID + " a";
            }


            $(selector).each(function() {
                var $link = $(this);
                var left = $link.offset().left;
                var top = $link.offset().top;
                //var scrollTop = $link.scrollTop();

                //top = (scrollTop * 1) + (top * 1);
                var hyperlink = $link.attr("href");
                var target = $link.attr("target");
                var ignoreWithRelPresent = $link.attr("rel");
                if (!ignoreWithRelPresent) {
                    if (target === "_new" || target === "_blank") {

                        var addToNotesHTML = "<a onclick='addLinkToNotes(\"" + hyperlink + "\", \"" + left + "\", \"" + top + "\" )' title='Add this link to your notes and clippings' style='cursor:pointer;'><div class='icoAddToNotesImg'><img src='/InvestAssets/Images/Icons/addToNotes.png' style='border:0;' alt='Add this link to your notes and clippings'></div></a>";
                        $link.before(addToNotesHTML);
                    }
                }
            });
        });
    };

})(jQuery);

$(document).ready(function() {
    //$("body").linksToNotes(); //2011/11/14 szarouski - no need for investor kit anymore
});

function addLinkToNotes(hyperlink, left, top) {

    var absoluteURL = "http://" + window.location.hostname + ":" + window.location.port;
    var fixedURL;
    if (hyperlink.indexOf("http://") > -1) {
        fixedURL = hyperlink;
    }
    else {
        if (hyperlink.charAt(0) == "/") {
            fixedURL = absoluteURL + hyperlink;
        }
        else {
            fixedURL = absoluteURL + "/" + hyperlink;
        }
    }
    var winscrollTop = $("#page-wrapper").scrollTop();

    top = parseInt(top, 10) - winscrollTop;
    top = top - 140;
    left = parseInt(left, 10);

    var isuserloggedin = "no";
    $.post("/InvestAjaxPages/ajax_IsUserLoggedIn.aspx", function(data) {
        isuserloggedin = data;
        var addHyperlinkForm = "";
        if (isuserloggedin === "yes") {
            addHyperlinkForm = "<div id='addHyperlinkForm' style='display:none; padding:10px; background-color:#ffffff; width:300px; height:135px;  border:2px solid #E70089; position:absolute; left:" + left + "px; top:" + top + "px;'><div class='addHyperlinkTitle'><h3 style='color:#E70089'>Add Link to Investor Toolkit</h3></div><div class='addHyperlinkLink'><input type='text' id='txtURLToAdd' value='" + fixedURL + "' style='font-size:10px; font-family:arial; width:294px;margin-top:10px;'/></div><div class='addHyperlinkNotes' style='margin-top:10px;'>Your Notes:<textarea id='txtURLToAddNotes' style='border:1px solid #333; width:289px; height:46px; margin-left:4px;' class='txtAddHyperlinkNotes'></textarea></div><div style='float:left; margin-left:8px;'><a href='javascript: cancelNotes();'>Cancel</a></div><div style='float:right; margin-right:8px;'><a href='javascript: saveNotes();'>Save</a></div></div>";
        }
        else {
            addHyperlinkForm = "<div id='addHyperlinkForm' style='display:none; padding:10px; background-color:#ffffff; width:300px; height:135px;  border:2px solid #E70089; position:absolute; left:" + left + "px; top:" + top + "px;'><div class='addHyperlinkTitle'><h3 style='color:#E70089'>Add Link to Investor Toolkit</h3></div><p style='font-weight:bold;'>To use this feature please <a href='/My-Account.aspx'><u>Login</u></a>.<br><br>If you have not registered with Invest Toronto you can do so by <a href='/My-Account.aspx'><u>going here</u></a>.</p><div style='float:left; margin-left:8px;'><br><a href='javascript: cancelNotes();'>Cancel</a></div></div>";
        }
        $("body").append(addHyperlinkForm);
        $("#addHyperlinkForm").slideDown(100);
    });



}

function cancelNotes() {

    $("#addHyperlinkForm").slideUp(100);

    setTimeout(function() {
        $("#addHyperlinkForm").remove();
    }, 300);

}

function saveNotes() {
    var data = new Object();
    data.link = $("#txtURLToAdd").val();
    data.notes = $("#txtURLToAddNotes").val();
    $("#addHyperlinkForm").html("<p style='margin-top:25px;margin-left:25px;margin-right:25px;'>This hyperlink has been added to your Notes and clippings.<br><br>You can find this in the Investor Toolkit.</p>");
    $.post('/InvestAjaxPages/ajax_AddLinkToNotes.aspx', data, function(data) {
        setTimeout(function() {
            $("#addHyperlinkForm").slideUp(100);
            setTimeout(function() {
                $("#addHyperlinkForm").remove();
            }, 300);

        }, 3000);
    });
}


