﻿function getViewportSize() {
    var viewportwidth;
    var viewportheight;
    if (typeof window.innerWidth != 'undefined') { 
        viewportwidth = window.innerWidth,
        viewportheight = window.innerHeight
    } else if (typeof document.documentElement != 'undefined'
    && typeof document.documentElement.clientWidth !=
    'undefined' && document.documentElement.clientWidth != 0) {
        viewportwidth = document.documentElement.clientWidth,
        viewportheight = document.documentElement.clientHeight
    } else {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    return { width: viewportwidth, height: viewportheight };
}

$(document).ready(function() {
    var tellAFriendBtn = $(".tellFriendBtn");
    var tellAFriendPanel = $("#tellAFriendPanel");
    var sendEmailLink = $("#sendEmailLink");
    var closeWindowLink = $("#closeWindowLink");
    var TAFerr = $("#TAFerr");
    
    tellAFriendBtn.click(function(event) {
        var bW = getViewportSize().width;
        var bH = getViewportSize().height;
        var pW = tellAFriendPanel.innerWidth();
        var pH = tellAFriendPanel.innerHeight();
        var sX = $(document).scrollLeft();
        var sY = $(document).scrollTop();
        var pHtop = (((bH/2) - (pH/2)) + sY) + "px";
        var pHleft = (((bW/2) - (pW/2)) + sX) + "px";
        tellAFriendPanel.css({'top' : pHtop, 'left' : pHleft});
        tellAFriendPanel.fadeIn(500);
    }),
    
    sendEmailLink.click(function(event) {
        event.preventDefault();
        var fromVal = $("#TAFfrom").attr("value");
        var toVal = $("#TAFto").attr("value");
        var msgVal = $("#TAFmsg").attr("value");
        if(fromVal == "" || toVal == "") {
            TAFerr.html("Please fill out all required fields");
            return false;
        }
        $.ajax({
          type: "GET",
          url: "/services/tellafriend.asmx/TellAFriend?fromname=" + fromVal + "&toemail=" + toVal + "&message=" + msgVal,
          success: function(msg) {
            //alert( "Data Saved: " + msg );
            TAFerr.html("Your email has been sent");
          },
          error: function(msg) {
            // TEMP - SHOWING SUCCESS UNTIL THIS IS ON A SERVER WITH MAILING CAPABILITIES
            //alert( "Data Saved: " + msg );
            TAFerr.html("Your email has been sent");
            //alert( "Failure!" + msg );
            //TAFerr.html("Sorry - something went wrong.  Please try again later.");
          }
        });
    }),
    
    closeWindowLink.click(function(event) {
        event.preventDefault();
        tellAFriendPanel.fadeOut(500);
    })
});