﻿// Sharing properties
var setArticleId = 0;
var setLeadArticleId = 0;
var setArticleHtml = '';
var setLeadArticleHtml = '';
var setArticleCellId = 0;
var setArticleForm = '';
var setLeadArticleForm = '';

function LoadHomepageEvents() {
    // Put share form in a var, delete the div contents so the ids are unique on the form
    setArticleForm = $("#shareArticle").html();
    setLeadArticleForm = $("#shareTopStory").html();
    $("#shareArticle").html('');
    $("#shareTopStory").html('');

    // Set events
    $(".module").hover
    (
        function() {

            $(this).children('.article').children('.overlay').hide();

            if ($.browser.msie) {
                $(this).children('.article').css({ opacity: "0.16" });
                //$(this).children('.article').hide();
            }
            else {
                $(this).children('.article').fadeTo(1, 0.16, function() {
                    $(this).parent('.module').children('.module-select').fadeIn(200);
                    $(this).parent('.module').children('.module-select').css({ opacity: "1" });
                });
            }

            // Confirm select shows
            $(this).children('.module-select').show();
        },
        function() {

            if ($.browser.msie) {
                $(this).children('.article').css({ opacity: "1.0" });
                //$(this).children('.article').show();
            }
            else {
                $(this).children('.module-select').fadeOut(1, function() {
                    $(this).parent('.module').children('.article').fadeTo(50, 1.0);
                    $(this).parent('.module').children('.module-select').css({ opacity: "0" });

                });
            }

            $(this).children('.article').children('.overlay').show();

            // Confirm select is removed
            $(this).children('.module-select').hide();
        }
    );

    $("#top-story").hover
    (
        function() {
            $(this).children('.overlay-hover').show();
        },
        function() {
            $(this).children('.overlay-hover').hide();
        }
    )

    // Set ad code
    //var rnd = Math.round(Math.random()*10000000);
    //$('#ad-unit').html('<IFR' + 'AME FRAMEBORDER=0 MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING=NO WIDTH=300 HEIGHT=250 SRC=http://demr.opt.fimserve.com/adopt/?r=h&l=c485c73a-9511-4b79-8140-5ff6456a530e&sz=300x250&rnd=' + rnd + '></IFR' + 'AME>');
}

function InsertAd() {
    var adUnitHtml = document.getElementById('temp-ad-unit').innerHTML;
    document.getElementById('temp-ad-unit').innerHTML = "";
    document.getElementById('ad-unit').innerHTML = adUnitHtml;
}

// Sharing methods
function SetShareArticle(articleId, cellId) 
{ 
    // Cancel any existing share
    CancelShare();
    setArticleId = articleId;
    setArticleCellId = cellId;
    setArticleHtml = $("#place-" + cellId).html();
    $("#place-" + cellId).html(setArticleForm);   
}

function SetShareLeadArticle(leadArticleId) 
{
    setLeadArticleId = leadArticleId;
    setLeadArticleHtml = $("#top-story").html();
    $("#top-story").html(setLeadArticleForm);
}

function ShareArticle() {
    SendArticle(setArticleId, "ToEmail", "FromEmail", "ShareComment");
    CancelShare();
}

function ShareLeadArticle(articleId) {
    SendArticle(setLeadArticleId, "ToEmailLead", "FromEmailLead", "ShareCommentLead");
    CancelLeadShare();
}

function SendArticle(articleId, toEmailId, fromEmailId, commentId) 
{
    if ($("#" + toEmailId).val().length > 6 && $("#" + fromEmailId).val().length > 6) {
        sUrl = "/services.asmx/EmailArticle";
        var jsonpostdata = "{";
        jsonpostdata += "HomepageItemId:'" + articleId + "', ";
        jsonpostdata += "ToEmail:'" + $("#" + toEmailId).val() + "', ";
        jsonpostdata += "FromEmail:'" + $("#" + fromEmailId).val() + "', ";
        jsonpostdata += "Comment:'" + EscapeString($("#" + commentId).val()) + "'";
        jsonpostdata += "}";
        $.ajax({
            url: sUrl,
            type: 'POST',
            data: jsonpostdata,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            timeout: 20000,
            error: function() {
            },
            success: function(json) {
            }
        });
    }
    else {
        alert('Please enter a To Email and From Email before submitting.');
    }
}

function EscapeString(OriginalString) {
    if (OriginalString.length > 0) {
        return OriginalString.replace(/'/g, "&#39");
    }

    return OriginalString;
}

function CancelShare() {
    if (setArticleCellId > 0) {
        $("#place-" + setArticleCellId).html(setArticleHtml);
        setArticleHtml = '';
        setArticleId = 0;
        setArticleCellId = 0;
    }
}

function CancelLeadShare() {
    $("#top-story").html(setLeadArticleHtml);
}

function HpClick(id, position)
{    
    $.ajax
    (
        {
            type: "POST",
            url: "/ajax/hpClick.ashx",
            data: "id=" + id + "&pos=" + position
        }
     );
}