var timerID = null;
var timerRunning = false;
var pasttime = false;
var lastVisitMax = 0;
var last24HrsMax = 0;

function stopclock(){
    if(timerRunning)
        clearTimeout(timerID);
    timerRunning = false;
}

function startclock(){
    stopclock();
    showtime();
}

function showtime(){
    if (pasttime == false) {
        var date = new Date();
        var nextupdatetimestamp = document.getElementById("nextupdatetimestamp").innerHTML;
        var diff = nextupdatetimestamp - date.getTime(); 
        document.getElementById("myclock").innerHTML = formatDate(date, true);
        if (diff < 0) {
            document.getElementById("mynextupdate").innerHTML = '<a href="/th/topichotlist.shtml" onmouseover="window.status=this.title;return true;" onmouseout="window.status=\'\';return true;" title="Click to refresh the PNW Topic Hotlist"><b>' + document.getElementById("mynextupdate").innerHTML + "</b></a>";
            pasttime = true;
        } 
    } else {
        document.getElementById("myclock").innerHTML = formatDate(new Date(), true);
    }
    timerID = setTimeout("showtime()",900);
    timerRunning = true;
}

function preload() {
	this.length = preload.arguments.length;
	for (var i = 0; i < this.length; i++) {
		this[i+1] = new Image();
		this[i+1].src = preload.arguments[i];
	}
}

if (document.images) {
	var imgs = new preload(
		"/th/images/plus-dark-lg.gif",
		"/th/images/minus-dark-lg.gif",
		"/th/images/hotlistbutton.gif");
}

function showNewVisitIndicators () {
    // Loop thru elements to hide
    for (var idx = 0; idx <= last24HrsMax; idx++) {
        elem = document.getElementById("new24Hrs" + idx);
        elem.className = "newIndicatorHidden"; 
        divelem = document.getElementById("border24Hrs" + idx);
        if (divelem != null) {
            divelem.className = "borderIndicatorHidden"; 
        }
    }
    // Loop thru elements to show
    for (var idx = 0; idx <= lastVisitMax; idx++) {
        elem = document.getElementById("newVisit" + idx);
        elem.className = "newIndicatorShown"; 
        divelem = document.getElementById("borderVisit" + idx);
        if (divelem != null) {
            divelem.className = "borderIndicatorShown"; 
        }
    }
}

function showNew24HrsIndicators () {
    // Loop thru elements to hide
    for (var idx = 0; idx <= lastVisitMax; idx++) {
        elem = document.getElementById("newVisit" + idx);
        elem.className = "newIndicatorHidden"; 
        divelem = document.getElementById("borderVisit" + idx);
        if (divelem != null) {
            divelem.className = "borderIndicatorHidden"; 
        }
    }
    // Loop thru elements to show
    for (var idx = 0; idx <= last24HrsMax; idx++) {
        elem = document.getElementById("new24Hrs" + idx);
        elem.className = "newIndicatorShown"; 
        divelem = document.getElementById("border24Hrs" + idx);
        if (divelem != null) {
            divelem.className = "borderIndicatorShown"; 
        }
    }
}

function highlightText (entry) {
    document.getElementById("div" + entry).style.borderColor = "#CC8800";
    document.getElementById("div" + entry).style.borderStyle = "solid";
    document.getElementById("bt" + entry).className = "pnwhotlistblogtitle";
    document.getElementById("bet" + entry).className = "pnwhotlistentrytitle";
    document.getElementById("be" + entry).className = "pnwhotlistentrytext";
}

function unhighlightText (entry, classnamenumber, revertColor) {
    document.getElementById("div" + entry).style.borderColor = revertColor;
    document.getElementById("div" + entry).style.borderStyle = "dotted";
    document.getElementById("bt" + entry).className = "pnwhotlistblogtitle" + classnamenumber;
    document.getElementById("bet" + entry).className = "pnwhotlistentrytitle" + classnamenumber;
    document.getElementById("be" + entry).className = "pnwhotlistentrytext" + classnamenumber;
}

function expandCollapseTopic (topicid, expandorcollapse) { 
    whichTopic = document.getElementById(topicid); 
    whichTopicAlt = document.getElementById(topicid + "alt"); 
    whichTopicAlt2 = document.getElementById(topicid + "alt2");
    if (whichTopic != null) { 
        toggleid = topicid + "toggle"; 
        toggleNode = document.getElementById(toggleid);
        if (expandorcollapse == "collapse") { 
            whichTopic.className = "topicEntriesHidden"; 
            whichTopicAlt.className = "topicEntriesShown"; 
            whichTopicAlt2.className = "topicEntriesHidden"; 
        } else if (expandorcollapse == "expand") { 
            whichTopic.className = "topicEntriesShown"; 
            whichTopicAlt.className = "topicEntriesHidden"; 
            whichTopicAlt2.className = "topicEntriesShown"; 
        } else if (whichTopic.className == "topicEntriesShown") { 
            whichTopic.className = "topicEntriesHidden"; 
            whichTopicAlt.className = "topicEntriesShown"; 
            whichTopicAlt2.className = "topicEntriesHidden"; 
        } else { 
            whichTopic.className = "topicEntriesShown"; 
            whichTopicAlt.className = "topicEntriesHidden"; 
            whichTopicAlt2.className = "topicEntriesShown"; 
        }
        togglePlusMinusIndicator(toggleNode,expandorcollapse);
    }
}

function togglePlusMinusIndicator(toggleNode, expandorcollapse) {
	if (toggleNode.src) {
		if (expandorcollapse == "collapse")
			toggleNode.src = "/th/images/plus-dark-lg.gif";
		else if (expandorcollapse == "expand")
			toggleNode.src = "/th/images/minus-dark-lg.gif";
		else if (toggleNode.src.indexOf("minus-dark-lg.gif") != -1)
			toggleNode.src = "/th/images/plus-dark-lg.gif";
		else if (toggleNode.src.indexOf("plus-dark-lg.gif") != -1)
			toggleNode.src = "/th/images/minus-dark-lg.gif";
	} else {
		if (expandorcollapse == "collapse")
			toggleNode.innerHTML = "[+] ";
		else if (expandorcollapse == "expand")
			toggleNode.innerHTML = "[-] ";
		else if (toggleNode.innerHTML == "[-] ")
			toggleNode.innerHTML = "[+] ";
		else
			toggleNode.innerHTML = "[-] ";
	}
}

//
//  Start of Bill Dortch's Cookie Functions
//
//  Cookie Functions - Second Helping  (21-Jan-96)
//  Written by:  Bill Dortch, hIdaho Design (bdortch at netw dot com)
//  The following functions are released to the public domain.
//

// 'Internal' function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (';', offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to return the value of the cookie specified by 'name'.
//
function GetCookie (name) {
  var arg = name + '=';
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(' ', i) + 1;
    if (i == 0) break;
  }  
  return null;
}

//
//  Function to create or update a cookie.
//
function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + ' = ' + escape (value) +
    ((expires == null) ? '' : ('; expires=' + expires.toGMTString())) +
    ((path == null) ? '' : ('; path=' + path)) +
    ((domain == null) ? '' : ('; domain=' + domain)) +
    ((secure == true) ? '; secure' : '');
}
//
//  End of Bill Dortch's Cookie Functions
//

//
//  Record Visit timestamp
//
function RecordVisit(cookiename) {
    var expdate = new Date ();
    var now = new Date ();
    var hotlistUpdated = document.getElementById("lastupdatetimestamp").innerHTML;
    expdate.setTime (expdate.getTime() + (3*365*24 * 60 * 60 * 1000)); // 3 yrs from now
    SetCookie (cookiename, now.getTime(), expdate, '/'); 
    SetCookie (cookiename + "Update", hotlistUpdated, expdate, '/'); 
}

function whatsShowingOnLoad() {
    document.getElementById("loadingindicator").className = "poweredbyLoadingHidden";
    document.getElementById("leftmenu").className = "menuShowing";
    document.getElementById("midmenu").className = "menuShowing";
    document.getElementById("rightmenu").className = "menuShowing";
    if (GetCookie('PNWTopicHotlistUpdate') == null) {
        showNew24HrsIndicators();
    }
}

// Note: PNWTopicHotlist cookie won't be set until after all Checks are called
// and the page has fully loaded.  So PNWTopicHotlist cookie always represents the
// time the page was last loaded.
function Check(entrytime, headerid, entrynum) {
    var now = new Date();
    var wordsindicator = "";
    var iconindicator = "&raquo;";
    var nowMinus24Hours = now.getTime() - (24 * 60 * 60 * 1000);
    // If cookie doesn't exist then we default to "Last 24 Hours" display
    if (GetCookie('PNWTopicHotlistUpdate') == null) {
        if (entrytime > nowMinus24Hours) {
            last24HrsMax++;
            document.write('<span id="new24Hrs' + last24HrsMax + '" class="newIndicatorHidden"><font color="red" size="1">' + wordsindicator + '</font></span>');
            document.getElementById("24hrdiv" + entrynum).className = "borderIndicatorHidden";
            document.getElementById("24hrdiv" + entrynum).id = "border24Hrs" + last24HrsMax; // change id so we can find it later
            // Set topic level "new" indicator
            if (headerid != "") {
                var header = document.getElementById(headerid);
                if (header.innerHTML.indexOf("new24Hrs") == -1) {
                    last24HrsMax++;
                    header.innerHTML = '<span id="new24Hrs' + last24HrsMax + '" class="newIndicatorHidden" title="There are new entries in last 24 hours for this topic." onmouseover="window.status=this.title;this.style.cursor=\'help\';return true;" onmouseout="window.status=\'\';this.style.cursor=\'default\';return true;"><font color="red"><b>' + iconindicator + '</b></font></span>' + header.innerHTML;
                }
            }
        }
    // If cookie does exist then we use "Last Visit" display
    } else {
        var newHeaderContent = "";
        if (entrytime > GetCookie('PNWTopicHotlistUpdate')) {
            lastVisitMax++;
            document.write('<span id="newVisit' + lastVisitMax + '" class="newIndicatorShown"><font color="red" size="1">' + wordsindicator + '</font></span>');
            document.getElementById("visitdiv" + entrynum).className = "borderIndicatorShown";
            document.getElementById("visitdiv" + entrynum).id = "borderVisit" + lastVisitMax; // change id so we can find it later
            // Set topic level "new" indicator
            if (headerid != "") {
                var header = document.getElementById(headerid);
                if (header.innerHTML.indexOf("newVisit") == -1) {
                    lastVisitMax++;
                    newHeaderContent = newHeaderContent + '<span id="newVisit' + lastVisitMax + '" class="newIndicatorShown" title="There are new entries since your last visit for this topic." onmouseover="window.status=this.title;this.style.cursor=\'help\';return true;" onmouseout="window.status=\'\';this.style.cursor=\'default\';return true;"><font color="red"><b>' + iconindicator + '</b></font></span>';
                }
            }
        }
        if (entrytime > nowMinus24Hours) {
            last24HrsMax++;
            document.write('<span id="new24Hrs' + last24HrsMax + '" class="newIndicatorHidden"><font color="red" size="1">' + wordsindicator + '</font></span>');
            document.getElementById("24hrdiv" + entrynum).className = "borderIndicatorHidden";
            document.getElementById("24hrdiv" + entrynum).id = "border24Hrs" + last24HrsMax; // change id so we can find it later 
            // Set topic level "new" indicator
            if (headerid != "") {
                var header = document.getElementById(headerid);
                if (header.innerHTML.indexOf("new24Hrs") == -1) {
                    last24HrsMax++;
                    newHeaderContent = newHeaderContent + '<span id="new24Hrs' + last24HrsMax + '" class="newIndicatorHidden" title="There are new entries in last 24 hours for this topic." onmouseover="window.status=this.title;this.style.cursor=\'help\';return true;" onmouseout="window.status=\'\';this.style.cursor=\'default\';return true;"><font color="red"><b>' + iconindicator + '</b></font></span>';
                }
            }
        }
        if (newHeaderContent != "") {
            header.innerHTML = newHeaderContent + header.innerHTML;
        }
    }
}

// Note: PNWTopicHotlist cookie won't be set until after all Checks are called
// and the page has fully loaded.  So PNWTopicHotlist cookie always represents the
// time the page was last loaded.
function CheckMoreNew(entrytime) {
    var now = new Date();
    var wordsindicator = " + new!";
    var nowMinus24Hours = now.getTime() - (24 * 60 * 60 * 1000);
    if (GetCookie('PNWTopicHotlistUpdate') == null) {
        if (entrytime > nowMinus24Hours) {
            last24HrsMax++;
            document.write('<span id="new24Hrs' + last24HrsMax + '" class="newIndicatorHidden"><font color="red"><nobr>' + wordsindicator + '</nobr></font></span>');
        }
    } else {
        var newHeaderContent = "";
        if (entrytime > GetCookie('PNWTopicHotlistUpdate')) {
            lastVisitMax++;
            document.write('<span id="newVisit' + lastVisitMax + '" class="newIndicatorShown"><font color="red"><nobr>' + wordsindicator + '</nobr></font></span>');
        }
        if (entrytime > nowMinus24Hours) {
            last24HrsMax++;
            document.write('<span id="new24Hrs' + last24HrsMax + '" class="newIndicatorHidden"><font color="red"><nobr>' + wordsindicator + '</nobr></font></span>');
        }
    }
}

function getLastVisit() {
    var timestamp = GetCookie('PNWTopicHotlist');
    if (timestamp == null) {
        document.write('N/A');
    } else {
        var lastVisitDate = new Date();
        lastVisitDate.setTime(timestamp);
        document.write(formatDate(lastVisitDate, false));
    }
}

function getNow() {
    document.write(formatDate(new Date(), false));
}

//  Format Date
function formatDate(date, withSecs) {
    var pstGMTOffset = 480;
    var zoneoffset = pstGMTOffset + (getGMTOffset(date) * 60);
    date.setTime(date.getTime() - (zoneoffset * 1000 * 60));
    var monArr = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');    
    var M = date.getMonth();
    var d = date.getDate();
    var H = date.getHours();
    var m = date.getMinutes();
    if (H < 10)
        H = "0" + H;
    if (m < 10)
        m = "0" + m;

    var formattedDate;
    if (withSecs == true) {
        var s = date.getSeconds();
        if (s < 10)
            s = "0" + s;
        // Format: Apr 21, 13:01:14 PT
        formattedDate = monArr[M] + " " + d + ", " + H + ":" + m + ":" + s + " PT";
    } else {
        // Format: Apr 21, 13:01 PT
        formattedDate = monArr[M] + " " + d + ", " + H + ":" + m + " PT";
    }
    
    return formattedDate;
}

function getGMTOffset(date) {
    var date1 = new Date(date.getFullYear(), 0, 1, 0, 0, 0, 0);
    var date2 = new Date(date.getFullYear(), 6, 1, 0, 0, 0, 0);
    var temp = date1.toGMTString();
    var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
    var temp = date2.toGMTString();
    var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
    var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
    var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
    if (hoursDiffDaylightTime == hoursDiffStdTime) {
        //alert("Time zone is GMT " + hoursDiffStdTime + ".\nDaylight Saving Time is NOT observed here.");
        return hoursDiffStdTime - 1; // DST not observed 
    } else {
        //alert("Time zone is GMT " + hoursDiffStdTime + ".\nDaylight Saving Time is observed here.");
        return hoursDiffStdTime; // DST is observed 
    }
}
