﻿/*
    compose_global.js
    Copyright 2007, Poetry Picture
    
    Constants and functions shared across compose_*.aspx pages
*/

// Global variables

var g_poemTemplate = { "id": -1,
                       "noun": "", "adjective": "", "adverb": "", "verb": "",
                       "image": "",
                       "fontFamily": "",
                       "textColor": "",
                       "backgroundOpacityPercent": 0
                     };

var g_poem = { "wordsInPoem": { },  // array of word objects (see below)
               "alternates": { "_null": [] },  // array of arrays of alternates, indexed by root word
               "universal": [] ,  // copied from constant
               "noun": [], "adjective": [], "adverb": [], "verb": [],  // copied from poem template
               "fontFamily": "",
               "textColor": "",
               "poemTemplateId": 0
             } ;

var g_infoBannerIntervalTimer = null;
var g_infoBanner = null;
var g_infoBannerAlpha = 0; // [0..1]
var g_infoBannerAlphaChangePerFrame = 0.0;

// Constants

var UNIVERSAL_WORDS = [ "a", "above", "across", "although", "am", "among", "an", "and", "are", "as", "at", "be", 
    "been", "being", "below", "but", "by", "can", "did", "do", "does", "down", "for", "from", "had", "has", 
    "have", "he", "her", "hers", "him", "his", "I", "in", "into", "is", "it", "its", "like", "may", "me", "mine", 
    "must", "my", "near", "no", "nor", "not", "of", "off", "on", "or", "ours", "out", "she", "should", "so", 
    "some", "that", "the", "their", "them", "there", "these", "they", "this", "those", "through", "to", "under",
    "up", "us", "was", "we", "were", "what", "when", "where", "while", "will", "with", "would", "you", 
    "your", "yours",
    ".", ",", "!", "?", "“", "”", "’", ";", ":" ];    // character after quotation marks is an em dash
var PARTS_OF_SPEECH = [ "universal", "noun", "adjective", "adverb", "verb" ];

// all in milliseconds
var INFOBANNER_DISPLAY_TIME = 3500;     // time before fade starts
var INFOBANNER_FADE_TIME = 700;        // duration of fading out animation
var INFOBANNER_FRAME_TIME = 50;         // duration of one frame of fading animation

// Functions

function pageLoadHandler()
{
    loadInspirationGallery();
    if ( window.pageLoadHandler_dragdrop ) pageLoadHandler_dragdrop();
    if ( window.pageLoadHandler_freestyle ) pageLoadHandler_freestyle();
    
    if ( document.getElementById( "divStructuredPoem" ).innerHTML != "" )
    {
        loadPoemTemplate( document.getElementById( "divPoemTemplateId" ).innerHTML );
        deserializePoem( document.getElementById( "divStructuredPoem" ).innerHTML );
        // redirect to appropriate compose page if on the wrong one somehow
        if ( document.location.href.indexOf( g_poem.poemStyle ) == -1 ) 
            document.location.href = document.location.href.replace( /compose_[a-z]+/, "compose_" + g_poem.poemStyle );
        renderPoemAfterPoemTemplateLoaded();
    }
    else
    {
        var args = getQueryStringArgs();
        if ( args.PoemTemplateId )
        {
            loadPoemTemplate( parseInt( args.PoemTemplateId ) );
        }
        else
        {
            loadPoemTemplate( document.getElementById( "divDefaultTemplateId" ).innerHTML );
        }
    }
    
    // Align poem picture message box with poem picture -- must do in code for x-browser compat
    var messageTop = absTop( document.getElementById( "divPoemPicture" ) ) + "px";
    document.getElementById( "divPoemPictureMessage" ).style.top = messageTop;
    document.getElementById( "divPoemPictureMessageOutlines" ).style.top = messageTop;
    
    // Hide transparent PNGs in IE<7 and all Safari versions
    if ( ( navigator.userAgent.search( /MSIE [456]/ ) != -1 ) || ( navigator.userAgent.search( "Safari" ) != -1 ) )
    {
        document.getElementById( "fadeLeft" ).style.visibility = "hidden";
        document.getElementById( "fadeRight" ).style.visibility = "hidden";
    }
    
    var infoBannerMessage = document.getElementById( "divInfoBannerMessage" ).innerHTML;
    if ( infoBannerMessage != "" )
    {
        g_infoBannerIntervalTimer = setInterval( fadeInfoBanner, INFOBANNER_DISPLAY_TIME );
        document.getElementById( "infoBannerText" ).innerHTML = infoBannerMessage;
        g_infoBanner = document.getElementById( "infoBanner" );
        g_infoBanner.style.visibility = "visible";
        g_infoBannerAlpha = 1.0;
        g_infoBannerAlphaChangePerFrame = INFOBANNER_FRAME_TIME / INFOBANNER_FADE_TIME;
    }
}

// Every 50ms, checks to see if poem template is loaded yet and it's safe to draw the poem
// This is a bit of a hack, but far less destabilizing than rewriting the template loading sequence to break
// out the part about clearing out the poem currently on the page when loading the template

function renderPoemAfterPoemTemplateLoaded()
{
    var renderTimer = setInterval(renderNow, 50);
    
    function renderNow()
    {
        if (!g_waitingOnAsyncLoadPoemTemplate)
        {
            clearInterval( renderTimer );
            renderPoemOnPicture();
            showMessageOnPicture( false );
        }
    }
}

// Takes a string and displays it on the picture using the font color of the poem template.
// If s == false or s == null, erases any previously shown message.
function showMessageOnPicture( s )
{
    if ( document.getElementById( "divPoemPictureMessage" ) )
    {
        var poemPictureMessage = document.getElementById( "divPoemPictureMessage" );
        var childDivs = document.getElementById( "divPoemPictureMessageOutlines" ).getElementsByTagName( "div" );
        if ( ( s == false ) || ( s == "" ) )
        {
            poemPictureMessage.style["visibility"] = "hidden";
            for ( var i = 0; i < childDivs.length; i++ )
            {
                childDivs[i].style.visibility = "hidden";
            }
        } else {
            poemPictureMessage.innerHTML = s;
            poemPictureMessage.style["visibility"] = "visible";
            
            for ( var i = 0; i < childDivs.length; i++ )
            {
                childDivs[i].innerHTML = s;
                childDivs[i].style.visibility = "visible";
            }
        }
    }
}

function showTooltip( text, x, y )
{
    var tail = document.getElementById( "resizableTooltipTail" );
    var balloon = document.getElementById( "resizableTooltip" );
    
    balloon.style.visibility = "visible";
    
    document.getElementById( "resizableTooltipPlaceholder" ).innerHTML = text;
    document.getElementById( "resizableTooltipText" ).innerHTML = text;
    if ( document.getElementById( "resizableTooltipPlaceholder" ).offsetWidth < 250 )
        balloon.style.width = document.getElementById( "resizableTooltipPlaceholder" ).offsetWidth + "px";
        
    balloon.style.left = x - 12 + "px";
    balloon.style.top = y - balloon.offsetHeight - tail.offsetHeight + "px";
    
    document.getElementById( "resizableTooltipBackground" ).style.height = balloon.offsetHeight - 2 + "px";
    document.getElementById( "resizableTooltipBackground" ).style.width = balloon.offsetWidth - 2 + "px";
    tail.style.top = balloon.offsetHeight - 2 + "px";
}

function hideTooltip()
{
    document.getElementById( "resizableTooltip" ).style.visibility = "hidden";
}

function GetMoreInspired()
{
    window.open("GetMoreInspired.aspx", "InspiredWindow", "width=841,height=600,location,resizable,scrollbars");
}

function fadeInfoBanner()
{
    if ( g_infoBannerAlpha == 1.0 )
    {
        // first time through, set up to fade
        clearInterval( g_infoBannerIntervalTimer );
        g_infoBannerIntervalTimer = setInterval( fadeInfoBanner, 50 );
    }
    
    g_infoBannerAlpha -= g_infoBannerAlphaChangePerFrame;
    if ( g_infoBannerAlpha < g_infoBannerAlphaChangePerFrame ) g_infoBannerAlpha = 0.0; // catch rounding errors
    g_infoBanner.style["filter"] = "alpha(opacity=" + Math.round( g_infoBannerAlpha * 100 ) + ")";
    g_infoBanner.style["opacity"] = g_infoBannerAlpha;
    g_infoBanner.style["-moz-opacity"] = g_infoBannerAlpha;
    
    if ( g_infoBannerAlpha = 0.0 ) clearInterval ( g_infoBannerIntervalTimer );
}


