﻿function emailToFriend(sender, recipient, contentID) {
    document.getElementById("sending_message").innerHTML = "Sending email, Please wait...";
    document.getElementById("email_error").innerHTML = "";

    var xmlHttp;
    try {  // Firefox, Opera 8.0+, Safari 
        xmlHttp = new XMLHttpRequest(); 
    }
    catch (e) {  // Internet Explorer 
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;   
            }
        } 
    }
    
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) { 
            if (xmlHttp.responseText == "success") {
                document.getElementById("email_to_friend_inner").innerHTML = "Your email has been successfully sent.<br /><br />Thank you!<br /><br /><br \><input type=\"button\" value=\"Close\" style=\"width: auto\" onclick=\"hideEmailDialog()\" />";
            }
            else {
                document.getElementById("sending_message").innerHTML = "";
                document.getElementById("email_error").innerHTML = "One or more email addresses is invalid.<br />Please verify your entries and try again.";    
            }
        }
    }
 
    xmlHttp.open("post", "email_to_friend.aspx", true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send("sender=" + sender + "&recipient=" + recipient + "&content_id=" + contentID); 
}

function processKeyPressForEmailForm(e)
{
     var key;

     if(window.event)
        key = window.event.keyCode;     //IE
     else
        key = e.which;     //firefox

     if(key == 13) {
        emailToFriend(document.getElementById('your_email').value, document.getElementById('friends_email').value, contentIDFromCollection)  
        return false;
     }
     else
        return true;
}

var transitionTime = 50;
var incrementTime = 7;

var endWidth = 300;
var endHeight = 200;
var endLeft = 170;
var endTop = 220;

var startWidth = 198;
var startHeight = 0;
var startLeft = 430;
var startTop = 525;

var mirrorLeft;
var mirrorTop;
var mirrorWidth;
var mirrorHeight;

var timeSegments = transitionTime / incrementTime;
var widthIncrement = (endWidth - startWidth) / timeSegments;
var heightIncrement = (endHeight - startHeight) / timeSegments;
var leftIncrement = (startLeft - endLeft) / timeSegments;
var topIncrement = (startTop - endTop) / timeSegments;

var dialog;
var intervalID;

function showEmailDialog() {
    dialog = document.getElementById("email_to_friend");    
    
    if (navigator.appName == "Microsoft Internet Explorer") {
        dialog.style.left = startLeft;  
        dialog.style.top = startTop; 
        dialog.style.width = startWidth;
        dialog.style.height = startHeight;
    }
          
    mirrorLeft = startLeft;  
    mirrorTop = startTop; 
    mirrorWidth = startWidth;
    mirrorHeight = startHeight;
        
    dialog.style.display = "inline";
    if (navigator.appName == "Microsoft Internet Explorer") {
        intervalID = setInterval("moveInEmailDialog()", incrementTime);
    }
}

function hideEmailDialog() {
    dialog = document.getElementById("email_to_friend"); 
    
    if (navigator.appName == "Microsoft Internet Explorer") {
        dialog.style.left = endLeft;  
        dialog.style.top = endTop; 
        dialog.style.width = endWidth;
        dialog.style.height = endHeight;
    }
    else
        dialog.style.display = "none";
          
    mirrorLeft = endLeft;  
    mirrorTop = endTop; 
    mirrorWidth = endWidth;
    mirrorHeight = endHeight;        
    
    if (navigator.appName == "Microsoft Internet Explorer") {
        intervalID = setInterval("moveOutEmailDialog()", incrementTime);  
    }
}

function moveInEmailDialog() {
    if ((mirrorLeft - leftIncrement) <= endLeft ||
        (mirrorTop - topIncrement) <= endTop ||
        (mirrorWidth + widthIncrement) >= endWidth ||
        (mirrorHeight + heightIncrement) >= endHeight) {

        dialog.style.left = endLeft;  
        dialog.style.top = endTop; 
        dialog.style.width = endWidth;
        dialog.style.height = endHeight;  
        
        clearInterval(intervalID);
    }
    else {        
        mirrorLeft -= leftIncrement;  
        mirrorTop -= topIncrement; 
        mirrorWidth += widthIncrement;
        mirrorHeight += heightIncrement;        
        
        dialog.style.left = mirrorLeft;  
        dialog.style.top = mirrorTop; 
        dialog.style.width = mirrorWidth;
        dialog.style.height = mirrorHeight;
    }
}

function moveOutEmailDialog() {
    if ((mirrorLeft + leftIncrement) >= startLeft ||
        (mirrorTop + topIncrement) >= startTop ||
        (mirrorWidth - widthIncrement) <= startWidth ||
        (mirrorHeight - heightIncrement) <= startHeight) {

        dialog.style.left = startLeft;  
        dialog.style.top = startTop; 
        dialog.style.width = startWidth;
        dialog.style.height = startHeight;  
        
        dialog.style.display = "none";      
        
        clearInterval(intervalID);
    }
    else {     
        mirrorLeft += leftIncrement;  
        mirrorTop += topIncrement; 
        mirrorWidth -= widthIncrement;
        mirrorHeight -= heightIncrement;        
        
        dialog.style.left = mirrorLeft;  
        dialog.style.top = mirrorTop; 
        dialog.style.width = mirrorWidth;
        dialog.style.height = mirrorHeight;
    }
}
