﻿/// <reference path="../Scripts/jquery-1.2.6.js" />


//Give all elements the click function
//This is used by some of the modal dialogs 
var browser = navigator.appName;
if (browser == "Netscape") {
    HTMLElement.prototype.click = function() {
        var evt =
    this.ownerDocument.createEvent('MouseEvents'); evt.initMouseEvent('click', true, true,
    this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0,
    null); this.dispatchEvent(evt);
    }
}    


//Activator Element Requirements: See SharingToExternal.ascx Comments at top.
//
//NOTE: the relative anchor box must not be within the repeater table but rather around it.
//divShareToExternal_ActivatorBase_BLOGID is a relatively positioned container where the module will be injected on hover.
$(document).ready(function() {
    STF_StartUp();
});

//This was moved into its own function so that it can easily be called from both the JQuery page load event and also from the update panel update event.
function STF_StartUp() {

    //Wire up hover events
    $('.AShareToExternal_Activator').each(
    function() {
        //move into position
        var shareLink = $('#' + this.id);
        shareLink.html($('#divSharingToExternal').html());

        var blog_id = $('#' + this.id).attr("href");
        blog_id = blog_id.replace('#', '');
        var url = shareLink.attr("stf_url");
        var title = shareLink.attr("stf_title");
        var cbPage = shareLink.attr("stf_cbPage");
        LoadShareToExternalWithBlog(blog_id, url, title, cbPage);
    }
    );


    //Only define these functions with the first control instance. They can be reused.
    if (typeof (Highlight) == "undefined") {
        Highlight = function(element) { element.className = 'NotFaded'; };
    }

    if (typeof (Faded) == "undefined") {
        Faded = function(element) { element.className = 'Faded'; };
    }
}

//When the cursor hovers over a given blogs "Share ..." link load the link collection with the current blog's title and url
function LoadShareToExternalWithBlog(blogid, url, title, cbPage) {
    //Load Url and Title into hyperlink
    title = STF_Website + ' - ' + title;
    url = encodeURIComponent(url);

    $('#AShareToExternal_Activator_' + blogid + ' #tblSharingToExternal a').each(function() {

        //set the current href using the template
        var loadedHref = this.getAttribute("href");
        loadedHref = loadedHref.toString().replace('<title>', title).replace('<url>', url);
        this.setAttribute("href", loadedHref);
        
    });
  
    //Set the STF icon click behavior and initialize related parts
    //This must be called here because every time the markup is moved around in the DOM the event handler wiring breaks and so must be reset
    SetupEmailBehaviors(blogid,url, title, cbPage);
}

/*---------- Send To A Friend Methods ------------*/

var Current_STF;//This is the current instance of the SendToFriend Object

function SetupEmailBehaviors(blogid,url, title, cbPage) {
    //When the STF icon is clicked...
    //#aSendToFriend or .divSTF_Activator
    $('#aSendToFriend_' + blogid ).click(function() {

        //Get the URL and TITLE and then setup the STF Form
        //var shareLink = $('#' + this.id).parent(); //get the parent that has the url and title settings
        //var url = shareLink.attr("stf_url");
        //var title = shareLink.attr("stf_title");
        Current_STF = new SendToFriend(title, url, cbPage);
        Current_STF.SetupForm();

        //Click the modal popup activation link (hidden server control)
        //Ideally the aSendToFriend link would be the server control for this but the click event in that control is not registering with the modal popup.
        //I suspect that this problem has something to do with the fact that the markup is being moved around in the DOM
        //As a result this click event from aSendToFriend is passed on to this activator server control which does not move around in the DOM.
        $('.divSTF_Activator').click(); //...is registered as the TargetControlID of the modal popup
    });

    //wire up success panel close event
    $('#divSuccess_STF').click(function() {
        $('#ibCancel_STF').click(); //close
    });
}

/*------------ SendToFriend Object definition -------------*/

function SendToFriend(title, url, callbackPage) {
    var self = this;
    this.Title = title;
    this.URL = url;
    this.CallbackPage = callbackPage;

    this.SetupForm = function() {
        //clear fields on load
        $('#txtSTF_ToEmail').val('');
        $('#txtSTF_ToName').val('');
        $('#txtSTF_FromEmail').val('');
        $('#txtSTF_FromName').val('');
        $('#divSubHeaderTools .rfStars').hide();
        $('#tbl_STF').show();
        $('#divSuccess_STF').hide();
        $('#bSTF_Title').html(self.Title);
        //Consider a delay on this focus until after the control loads
        $('#txtSTF_ToName').focus();
    }

    this.STF_Request = function() {
        var toName = $('#txtSTF_ToName').val();
        var toEmail = $('#txtSTF_ToEmail').val();
        var fromName = $('#txtSTF_FromName').val();
        var fromEmail = $('#txtSTF_FromEmail').val();
        var blogUrl = self.URL;
        var title = self.Title;

        if (toEmail.length > 0 && fromName.length > 0 && fromEmail.length > 0 && fromName.length > 0) {
            $.get(self.CallbackPage, { stf: 1, toName: toName, toEmail: toEmail, fromName: fromName, fromEmail: fromEmail, title: title, blogUrl: blogUrl }, function(data) { self.STF_Callback(data); });
        }
        else {
            self.STF_Callback('empty');
        }

        return false;
    }

    this.STF_Callback = function(data) {
        if (data == "success") {
            //$('#ibCancel_STF').click();

            //Show success panel
            $('#tbl_STF').hide();
            $('#divSuccess_STF').show();
        }
        else {

            if (data.toString().indexOf("error") > -1) {
                $('#txtSTF_ToEmail').val(data);
            }
            else {
                $('#RSTF_Box .rfStars').show();
            }
        }
    }
}

//public static function of SendToFriend
SendToFriend.ActivateSTF_Dialog = function() {
        $('.divSTF_Activator').click(); //...is registered as the TargetControlID of the modal popup
}


function FaceBookLink(url, title) { 
var u = url;
var t = title;
window.open('http://www.facebook.com/sharer.php?u=' + u + '&t=' + t, 'sharer', 'toolbar=0,status=0,width=626,height=436');
}



