/*
   ======================================================================
   Pausing RSS scroller JavaScript engine- © Dynamic Drive (http://www.dynamicdrive.com)
Docs: http://www.dynamicdrive.com/dynamicindex17/rsspausescroller/
Last modified: March 16th, 2006.
Last modified: August 21th, 2008 at MUSC
======================================================================
*/

// THIS IS SPECIAL -- SHOWS only one alert, rest accessed by view all.

/* play nice with prototype.js. we use JQuery, but if the page also has prototype, this will avoid problems */
jQuery.noConflict();

//URL to "scrollerbridge.php" on your server:
var jsonurl="http://www.carc.musc.edu/aprfeed/info_alert.js";
var timeout = 300000;

/*    var alertwindow = jQuery.cookie('alertwindow');
      if (alertwindow == 'visible') {
      jQuery("#alertwindow").css("display", "block");
      }
      if (alertwindow == 'hidden') {
      jQuery("#alertwindow").css("display", "none");
      }*/

//Advanced users: Edit below function to format the RSS feed output as desired
//formatrssmessage(divid, message_index_within_array, linktarget, logicswitch)
//

    function truncate(s,l) {
        if (s.length>=l)
            return s.substring(0,l)+'...';
        else
            return s;
    };

function formatrssmessage(divid, msgnumber, linktarget, logicswitch){
    var rsscontent=rsscontentdata[msgnumber];
    if(rsscontent==null) {
        return "--";
    }
    var linktitle='<div class="rsstitle severity'+unescape(rsscontent.severity)+'"><a href="'+unescape(rsscontent.link)+'" target="'+linktarget+'">'+unescape(truncate(rsscontent.title, 52))+'</a></div>';
    var description='<div class="rssdescription"><a href="'+unescape(rsscontent.description)+'" target="'+linktarget+'">'+unescape(rsscontent.description)+'</a></div>';
    var feeddate='<div class="rssdate">'+unescape(rsscontent.age)+'</div>';
    if (logicswitch.indexOf("description")!=-1 && logicswitch.indexOf("date")!=-1) //Logic switch- Show description and date
        return linktitle+feeddate+description;
    else if (logicswitch.indexOf("description")!=-1) //Logic switch- Show just description
        return linktitle+description;
    else if (logicswitch.indexOf("date")!=-1) //Logic switch- Show just date
        return linktitle+feeddate;
    else
        return linktitle; //Default- Just return hyperlinked RSS title
};

//////NO NEED TO EDIT BEHIND HERE///////////////////////////

var rsscontentdata=new Array(); //global array to hold RSS feeds contents
function json_callback(obj) {
    rssmenudata=new Array();
    if (obj.length >= 1) {
        rsscontentdata=obj;
        message = "";
        for (var i=0; i<obj.length; i++){ 
            rssmenudata[i]='<a class="severity'+obj[i].severity+'" href="'+obj[i].link+'">'+truncate(obj[i].title, 52)+'</a>';
            if (obj[i].severity == "1") {
                message = message+'<span class="title">'+obj[i].title+'</span><p>'+obj[i].description+'</p>';
            }
        }
        //document.getElementById('view_all').style.display=null;
        if (message != "") {
            jQuery("#alertwindow").html(message);
            jQuery("#alertwindow").slideDown("slow");
            //	jQuery.cookie('alertwindow', 'visible')
        }
        else if (message == "") {
            jQuery("#alertwindow").slideUp("slow");
            //	jQuery.cookie('alertwindow', 'hidden')
        }
    }
    else {
        //document.getElementById('view_all').style.display='none';
        rsscontentdata=new Array();
        //rsscontentdata=[{'severity':3,'title':'','description':'None','age':'','link':'http://my.musc.edu'}];
        rsscontentdata=[{'severity':3,'title':'No Alerts or Notices at this time','description':'None','age':'Now','link':'http://mymusc.edu'}];
        rssmenudata[0]='<span class="title">No Alerts or Notices at this time.</span>';
    }
};

//rsspausescroller(RSS_id, divId, divClass, delay, linktarget, optionalswitch)

function rsspausescroller(divId, divClass, delay, linktarget, logicswitch){
    this.tickerid=divId; //ID of ticker div to display information
    this.delay=delay; //Delay between msg change, in miliseconds.
    this.linktarget=(typeof linktarget!="undefined")? linktarget : "";
    this.logicswitch=(typeof logicswitch!="undefined")? logicswitch : "";
    this.mouseoverBol=0; //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
    this.msgpointer=0; //index of message array for hidden div
    this.js_is_loaded=0;
    this.number_of_tries=0;
    document.write('<div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+divId+'1"><span style="position: absolute">Initializing RSS scroller...</span></div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2"></div><div class="menu"><a href="#" onClick="return dropdownmenu(this, event, rssmenudata, \'400px\')" onMouseout="delayhidemenu()" id="view_all">View All</a></div></div>');
    if (document.getElementById){ //perform basic DOM browser support
        rsspausescroller.getRSScontentJS(jsonurl);
        this.do_onjsload();
    }
};

// -------------------------------------------------------------------
// do_onjsload()- Checks if external JS containing RSS feed is loaded yet
// -If not, continue to check until yes, or abort after certain tries.
// -------------------------------------------------------------------

rsspausescroller.prototype.do_onjsload=function(){
    var scrollerinstance=this;
    if ((typeof rsscontentdata=="undefined" || rsscontentdata.length==0) && this.number_of_tries<40){ //if JS array holding RSS content not yet loaded
        this.number_of_tries++;
        setTimeout(function(){scrollerinstance.do_onjsload()}, 200); //recheck
    }
    else if (typeof rsscontentdata!="undefined"){ //if JS array has loaded
        this.tickerdiv=document.getElementById(this.tickerid);
        this.visiblediv=document.getElementById(this.tickerid+"1");
        this.visibledivtop=parseInt(rsspausescroller.getCSSpadding(this.tickerdiv));
        //set width of inner DIV to outer DIV width minus padding (padding assumed to be top padding x 2)
        this.visiblediv.style.width=this.tickerdiv.offsetWidth-(this.visibledivtop*2)+"px";
        this.visiblediv.innerHTML=formatrssmessage(this.tickerid, 0, this.linktarget, this.logicswitch);
        this.do_ondivsinitialized();
    }
    else
        document.getElementById(this.tickerid).innerHTML=rsscontentdata+"I give up trying to fetch RSS feed.";
};

// -------------------------------------------------------------------
// do_ondivsinitialized()- Checks if two divs of scroller is each populated with RSS message yet
// -If not, continue to check until yes, or abort after certain tries.
// -------------------------------------------------------------------

rsspausescroller.prototype.do_ondivsinitialized=function(){
    var scrollerinstance=this;
    if (parseInt(this.visiblediv.offsetHeight)==0) // || parseInt(this.hiddendiv.offsetHeight)==0)
        setTimeout(function(){scrollerinstance.do_ondivsinitialized()}, 100);
    else
        this.initialize();
}

// -------------------------------------------------------------------
// initialize()- Initialize scroller method.
// -Get div objects, set initial positions, start up down animation
// -------------------------------------------------------------------

rsspausescroller.prototype.initialize=function(){
    var scrollerinstance=this;
    //set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
    this.visiblediv.style.width=this.tickerdiv.offsetWidth-(this.visibledivtop*2)+"px";
    this.tickerdiv.onmouseover=function(){scrollerinstance.mouseoverBol=1};
    this.tickerdiv.onmouseout=function(){scrollerinstance.mouseoverBol=0};
    if (window.attachEvent) //Clean up loose references in IE
        window.attachEvent("onunload", function(){scrollerinstance.tickerdiv.onmouseover=scrollerinstance.tickerdiv.onmouseout=null});
    setTimeout(function(){scrollerinstance.rotatemessage()}, this.delay);
};


// -------------------------------------------------------------------
// rotatemessage()- Populate the hidden div with the next message before it's visible
// -------------------------------------------------------------------

rsspausescroller.prototype.rotatemessage=function(){
    return; // no rotation
    var scrollerinstance=this;
    if (this.mouseoverBol==1) //if mouse is currently over scoller, do nothing (pause it)
        setTimeout(function(){scrollerinstance.rotatemessage()}, 100);
    else {
        var i=this.msgpointer;
        var ceiling=rsscontentdata.length;
        this.msgpointer=(i+1>ceiling-1)? 0 : i+1;
        this.visiblediv.innerHTML=formatrssmessage(this.tickerid, this.msgpointer, this.linktarget, this.logicswitch);
        setTimeout(function(){scrollerinstance.rotatemessage()}, this.delay);
    }
};


// -------------------------------------------------------------------
// getRSScontentJS()- Fetch RSS feed as external JavaScript
// -------------------------------------------------------------------

rsspausescroller.getRSScontentJS=function(scripturl){
    //var scriptref=document.createElement('script');
    tempurl = scripturl;
    //scriptref.setAttribute("type","text/javascript");
    //scriptref.setAttribute("src", scripturl);
    //document.getElementsByTagName("head").item(0).appendChild(scriptref);
    jQuery.getJSON(jsonurl+'?callback=?');
    //updateRSSdata(tempurl);
    setTimeout("rsspausescroller.getRSScontentJS(tempurl)",timeout);
};


    rsspausescroller.getCSSpadding=function(tickerobj){ //get CSS padding value, if any
        if (tickerobj.currentStyle)
            return tickerobj.currentStyle["paddingTop"];
        else if (window.getComputedStyle) //if DOM2
            return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top");
        else
            return 0;
    };

