﻿

var lastContentSection = null;
var currentMovie = null;

function ToggleLightBox(className, contentSectionName, useContentWidth)
{
    var divLightBox = GetObject('divLightBox');
    var divAreaContainer = GetObject('divAreaContainer');
    var divAreaContent = GetObject('divAreaContent');
    var divContentContainer = GetObject('divContentContainer');
    var lightbox_close_button = GetObject('lightbox_close_button');
    
    var contentSection = GetObject(contentSectionName);
    
    
    if (divLightBox)
    {
        if (divLightBox.className.indexOf('show') == -1)
        {
            divLightBox.style.height = document.body.clientHeight + 'px';
            divLightBox.style.width = document.body.clientWidth + 'px';
            divLightBox.style.zIndex = "10";
            
            divLightBox.className = divLightBox.className.replace('hide', 'show');
            
            if (parseInt(className.length) > 0)
            {
                divAreaContainer.className = className;
                divAreaContainer.style.padding = "25px";
            }
            
            if (contentSection)
            {
                if(useContentWidth != undefined && useContentWidth == true) {
                    divAreaContent.style.width = contentSection.width + "px";    // since it is still hidden, the offsetWidth is 0
                } else {
                    divAreaContent.style.width = divAreaContainer.offsetWidth * .8 + "px";
                }
                
                contentSection.style.position = "absolute";
                contentSection_pos = findPos(divContentContainer);
                
                contentSection.style.top = contentSection_pos[1] + "px";
                contentSection.style.left = contentSection_pos[0] + "px";
                contentSection.style.margin = "0 auto";
                contentSection.style.zIndex = "20";
                
                contentSection.className = contentSection.className.replace("hide", "show");
                
                lastContentSection = contentSection;
            }
        }
        else
        {
            divLightBox.className = divLightBox.className.replace('show', 'hide');
            divAreaContainer.className = ''
            
            if (lastContentSection) {
                if(currentMovie != null) {
                    lastContentSection.stopVideo();
                    currentMovie = null;
                }
                lastContentSection.className = lastContentSection.className.replace('show', 'hide');
            }
        }
    }
    
    return;
}

function ToggleLightBox_SharePage(className, contentSection)
{
    var divLightBox = GetObject('divLightBox');
    var divAreaContainer = GetObject('divAreaContainer');
    var divAreaContent = GetObject('divAreaContent');
    var divContentContainer = GetObject('divContentContainer');
    
    contentSection = GetObject(contentSection);
    
    
    if (divLightBox)
    {
        if (divLightBox.className.indexOf('show') == -1)
        {
            divLightBox.style.height = document.body.clientHeight + 'px';
            divLightBox.style.width = document.body.clientWidth + 'px';
            divLightBox.style.zIndex = "10";
            
            divLightBox.className = divLightBox.className.replace('hide', 'show');
            
            if (parseInt(className.length) > 0)
            {
                divAreaContainer.className = className;
                divAreaContainer.style.padding = "25px";
            }
            
            if (contentSection)
            {
                divAreaContainer.style.width = "500px";
                divAreaContent.style.width = "480px";
                
                divContentContainer.style.width = "400px";
              
                divContentContainer.style.margin = "0 auto";
                
                
                contentSection.style.position = "absolute";
                contentSection_pos = findPos(divContentContainer);
                
                contentSection.style.top = contentSection_pos[1] + "px";
                contentSection.style.left = contentSection_pos[0] + "px";
                
                contentSection.style.margin = "0 auto";
                contentSection.style.zIndex = "20";
                
                contentSection.className = contentSection.className.replace("hide", "show");
                
                lastContentSection = contentSection;
            }
        }
        else
        {
            divLightBox.className = divLightBox.className.replace('show', 'hide');
            divAreaContainer.className = ''
            
            if (lastContentSection)
                lastContentSection.className = lastContentSection.className.replace('show', 'hide');
        }
    }
    
    return;
}


function ToggleVideo(className, contentSectionName)
{
  var contentSection = GetObject(contentSectionName);
  currentMovie = contentSection;
  ToggleLightBox(className, contentSectionName, false);
}



// --- HELPER METHODS --- //
function GetObject(obj)
{
    return ((typeof(obj) != 'object') ? document.getElementById(obj) : obj);
}

function checkMOYNotify_Form(theForm)
{
    var success = true;
    var moyName_msg = GetObject("moyName_Required");
    moyName_msg.style.display = "none";
    if(theForm.moyName.value == "")
    {
        moyName_msg.style.display = "inline";
        
        success = false;
    }
    
    var moyEmail_reqmsg = GetObject("moyEmail_Required");
    moyEmail_reqmsg.style.display = "none";
    var moyEmail_invmsg = GetObject("moyEmail_Invalid");
    moyEmail_invmsg.style.display = "none";
    
    if(theForm.moyEmail.value == "")
    {
        moyEmail_reqmsg.style.display = "inline";
        success = false;
    }
    else if(!isEmailAddress(theForm.moyEmail.value))
    {
        moyEmail_invmsg.style.display = "inline";
        success = false;
    }
    
    return success;
}

function checkSharePage_Form(theForm)
{
    var success = true;
    var shareName_msg = GetObject("shareName_Required");
    shareName_msg.style.display = "none";
    if(theForm.shareNAME.value == "")
    {
        shareName_msg.style.display = "inline";
        
        success = false;
    }
    
    var recipientName_msg = GetObject("recipientName_Required");
    recipientName_msg.style.display = "none";
    if(theForm.recipiantNAME.value == "")
    {
        recipientName_msg.style.display = "inline";
        
        success = false;
    }
    
    var shareEmail_reqmsg = GetObject("shareEmail_Required");
    shareEmail_reqmsg.style.display = "none";
    var shareEmail_invmsg = GetObject("shareEmail_Invalid");
    shareEmail_invmsg.style.display = "none";
    
    if(theForm.shareEMAIL.value == "")
    {
        shareEmail_reqmsg.style.display = "inline";
        success = false;
    }
    else if(!isEmailAddress(theForm.shareEMAIL.value))
    {
        shareEmail_invmsg.style.display = "inline";
        success = false;
    }
    
    var recipientEmail_reqmsg = GetObject("recipientEmail_Required");
    recipientEmail_reqmsg.style.display = "none";
    var recipientEmail_invmsg = GetObject("recipientEmail_Invalid");
    recipientEmail_invmsg.style.display = "none";
    
    if(theForm.recipiantEMAIL.value == "")
    {
        recipientEmail_reqmsg.style.display = "inline";
        success = false;
    }
    else if(!isEmailAddress(theForm.recipiantEMAIL.value))
    {
        recipientEmail_invmsg.style.display = "inline";
        success = false;
    }
    
    return success;
}

function isEmailAddress(str)
{
    var regex = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
    
    return str.match(regex);
}

// reliably discover the position of an object
function findPos(obj) {
    var curleft = curtop = 0;
    
    if(obj.offsetParent)
    {
     do {
        curleft += obj.offsetLeft;
        curtop += obj.offsetTop;
     } while (obj = obj.offsetParent);
    }
    
    return [curleft, curtop];
}