/*
** This Module implements the ticker functionality based on concept by 
** Cameron Gregory (http://www.bloke.com/javascript/TickerTape) I changed it to fit on top 
** of a standard Framechannel slide show.
** The ticker will consist of a black bar on top of the current slide
** and inside the black bar will be a left scrolling circlular text message
** The location of where the ticker is 7/8 from to of the page, and it will be
** 1/8 wide. For the time the font is fixed but this will be expanded.
**
** History:
**        lec - Created 09/14/2009
*/

TICKERSCROLLSPEED = 200;        // 100 mills for now
TICKERBACKGROUND  = "#000000";  // Black of now but change to configurable later
TICKERFOREGROUND  = "#FFFF00";  // Yellow for the time being
TICKEROPACITY     = 100;        // full on

function tickerConfig( profile ) 
{
    var tickerHolder = new Object; // Hold the tiker goodies
    
    /*
    ** Pull width and height of the viewing area from our namespace.
    */
    var h = profile.height;
    var w = profile.width;
    
    tickerHolder.bedH  = h/8;       // start with a 1/8 heigth for the ticker bed
    tickerHolder.bedHPos = tickerHolder.bedH*7; // OK it will go 7/8s of the way down on the picture
    tickerHolder.bedVpos = w/16;    // Will start and end 1/16 of the way away from edges
    tickerHolder.bedW  = tickerHolder.bedVpos * 14;  // 1/16 on either side for a bed

    /*
    ** Ok we have the bed defined, now define the font size and location in
    ** the ticker bed
    */

    tickerHolder.fntH  = tickerHolder.bedH/16;  // location of font in bed from top
    /* We may need to add buffer zone for test to bed horizontally */

    tickerHolder.fontSize = parseInt( tickerHolder.fntH * 14);
    

    /*
    ** We don't know the string size that the ticker will hold because the font
    ** is not square, so we do a bit of hocus phocus to get the determine width
    */

    var b = document.getElementsByTagName("BODY")[0];
    var div = document.createElement("DIV");
    var spn = document.createElement("span");
    
    b.appendChild(spn);
    
    div.style.fontFamily = "sans";
    spn.style.fontFamily = "sans";
    
    spn.style.fontSize = "" + tickerHolder.fontSize + "px";
    spn.innerHTML      = "mwmwmwmwmw"; // ten 
    
    b.appendChild( div );
    var sw = spn.offsetWidth;
    var fw = sw/10;
    
    var len = parseInt(tickerHolder.bedW/fw); // Note may have to be adjusted if we want to add buffer from sides of bed
    b.removeChild( div );
    b.removeChild( spn );
    
    len+=40; // fudge
    
    var blanks="";
    for( var i=0; i<len; i++) 
    {
        blanks += " ";
    }

    var lft =  parseInt(tickerHolder.bedVpos) + 12;

    /*
    ** Now we are done with the configuration, just go save the important bits in a neat html
    ** snippet and store it away
    */
    
    
    tickerHolder.htmlTxt = '<form name=ticker><input name=scroll size=' + len;
    tickerHolder.htmlTxt += ' value="" style="position:absolute; top:' + parseInt(tickerHolder.bedHPos);
    tickerHolder.htmlTxt += 'px; left:' + lft +'px; ';
    tickerHolder.htmlTxt += 'background-color: '+TICKERBACKGROUND;
    tickerHolder.htmlTxt += '; color: '+TICKERFOREGROUND+'; z-index:10;';
    if( profile.browser == "MSIE") 
    {
        tickerHolder.htmlTxt += 'filter:alpha(opacity='+ TICKEROPACITY+ '); ';
        "alpha(opacity="
    } else {
        tickerHolder.htmlTxt += 'opacity:'+ TICKEROPACITY/100 + ';';
    }
    
    //    tickerHolder.htmlTxt += 'font name="fixed, courier"';
    tickerHolder.htmlTxt += 'font-size:' + parseInt(tickerHolder.bedVpos/2) + 'px;" ></form>';
    tickerHolder.tickBlank = blanks;
    tickerHolder.tickBlen = len;
    tickerHolder.cursor = -len;
    profile.tickerData = tickerHolder;
    
    
}

function tickerMove( profile ) 
{
    var msg = profile.tickerData.msg;
    
    var marker;
    var value;
    var cursor = profile.tickerData.cursor;
        
    var tmp=Math.min( cursor + profile.tickerData.tickBlen, msg.length );
    if( cursor < 0 )
    {
        marker = 0;
        value = profile.tickerData.tickBlank.substring(0,-cursor)+msg.substring(marker, tmp);
    } else {
        marker = cursor;
        value = msg.substring(marker, tmp);
    }
    if( ++cursor == msg.length)
    {
        cursor = -profile.tickerData.tickBlen;
    }
    profile.tickerData.cursor = cursor;
    profile.tickerData.timerId = window.setTimeout("tickerMove(profile)", TICKERSCROLLSPEED);
    profile.tickerData.tickerForm.scroll.value=value;
}

function tickerParse(tickerXmlDoc, profile)
{
    var payload="";
    var tickFeedTTL = tickerXmlDoc.getElementsByTagName("ttl")[0].textContent;
    
    var items = tickerXmlDoc.getElementsByTagName("item");
    for( var i=0;i< items.length;i++ ) 
    {
        var title = items[i].getElementsByTagName("title");
        payload += title[0].textContent;
    }
    
    profile.tickerData.msg = payload;
    tickerMove(profile);
    
    doTickTTL(profile, tickFeedTTL)
        

}

function doTickTTL( profile, mins )
{
    if( mins != null )
    {
        var secs = mins * 60000;
        
        profile.tickerData.timerId = setTimeout("kickoffTicker(profile)",secs);
    }
}


function collectTickerFeed(profile)
{
    profile.load_xml(profile.tickerFeed, tickerParse, profile );
}


function kickoffTicker( profile )
{
    /*
    ** Ok go and initialize  the ticker
    */
    
    tickerConfig( profile );
    /*
    ** Ok has the ticker ran before
    */
    if( profile.tickerData.timerId != null ) 
    {
        clearTimeout(profile.tickerData.timerId);
    }
            
    set_root_value( "tick",profile.tickerData.htmlTxt);

    profile.tickerData.tickerForm = document.ticker;
    profile.tickerData.msg = "";
    collectTickerFeed(profile);
            
}

        
    
                      

    
    
    
    
    
    
    
 


