/* Global functions for Facilities Midwest Expo
Pre lod images - changeImage, preloadImages
Rollovers - sfHover
Countdown - montharray, 
*/

/*
This file contains other functions and logic to perform
*/


// rollover image function
function changeImage(image_name,image_src) {
   	document.images[image_name].src = image_src;
}

// preload images
function preloadImages(){
	
	if (document.images){
	
		var nav_register_over = new Image(100,30);
		nav_register_over.src = "http://www.facilitiesnet.com/graphics/nav/register_on.gif";
		
		var nav_sessions_over = new Image(100,30);
		nav_sessions_over.src = "http://www.facilitiesnet.com/graphics/nav/sessions_on.gif";
		
		var nav_events_over = new Image(100,30);
		nav_events_over.src = "http://www.facilitiesnet.com/graphics/nav/events_on.gif";
		
		var nav_exhibithall_over = new Image(100,30);
		nav_exhibithall_over.src = "http://www.facilitiesnet.com/graphics/nav/exhibithall_on.gif";
		
		var nav_travel_over = new Image(100,30);
		nav_travel_over.src = "http://www.facilitiesnet.com/graphics/nav/travel_on.gif";
		
		var nav_exhibitors_over = new Image(100,30);
		nav_exhibitors_over.src = "http://www.facilitiesnet.com/graphics/nav/exhibitors_on.gif";
		
		var nav_speakers_over = new Image(121,30);
		nav_speakers_over.src = "http://www.facilitiesnet.com/graphics/nav/speakers_on.gif";
		
	} else {
		return;
	}
	
}

// exclude old browsers
if (!document.getElementById){
	top.location.replace('info/browser.html');
}

// event handler used for the navigation rollovers
sfHover = function(){
	var sfEls = document.getElementById("mainNav").getElementsByTagName("li");
	for (var i = 0; i < sfEls.length; i++){
		sfEls[i].onmouseover = function(){
			this.className += " sfhover";
		}
		sfEls[i].onmouseout = function(){
			this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/*
Countdown script by Mike Thompson 
*/ 
//http://www.members.tripod.com/webdesign123now/
//registered on script by Website Abstraction (http://wsabstract.com)

var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countdown(yr,m,d){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy
var futurestring=montharray[m-1]+" "+d+", "+yr
var difference=(Math.round((Date.parse(futurestring)-Date.parse(todaystring))/(24*60*60*1000))*1)
if (difference==0)
//change msg here
document.write("Today is Facility Decisions 2010 show!")
else if (difference>0)
//change msg here
document.write("<strong><br />" + difference + "</strong> days left")
}
//enter the count down date using the format year/month/day
//countdown(2007,9,18)

// Text Rotation
if (document.all || document.getElementById){ //if IE4 or NS6+
 document.write('<style type="text/css">\n');
 document.write('.dyncontent{display: none; width: 330px; height: 155px;}\n');
 document.write('</style>');
}

var curcontentindex=0;
var messages=new Array();

function getElementByClass(classname){
 var inc=0;
 var alltags=document.all? document.all : document.getElementsByTagName("*");
 for (i=0; i<alltags.length; i++){
   if (alltags[i].className==classname)
     messages[inc++]=alltags[i];
 }
}

function rotatecontent(){
 //get current message index (to show it):
 curcontentindex=(curcontentindex<messages.length-1)? curcontentindex+1 : 0;
 //get previous message index (to hide it):
 prevcontentindex=(curcontentindex==0)? messages.length-1 : curcontentindex-1;
 messages[prevcontentindex].style.display="none"; //hide previous message
 messages[curcontentindex].style.display="block"; //show current message
}

window.onload=function(){
 if (document.all || document.getElementById){
   getElementByClass("dyncontent");
   setInterval("rotatecontent()", 8000);
 }
}


/***********************************************
* Clear default value on focus
***********************************************/

function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}

/*
Twitter blogger script 
*/ 

function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<br /><a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://www.twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<br /><li><span>'+status+'</span> <a style="font-size:10px; color:#999999" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
  }
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}