/**
 *	DSBN Top Bar
 *
 * @version: 1.2
 * @author: Matt Froese (matt.froese@dsbn.edu.on.ca)
 **/

var dsbnTopbar = "";
var dsbnTopbarStyle = "";

function DSBNTopBar()
{
	DSBNTopBar_createContent();
}

function DSBNTopBar_createContent()
{
	dsbnTopbarRSS = document.createElement( "link" );
	dsbnTopbarRSS.rel = "alternate";
	dsbnTopbarRSS.type = "application/rss+xml";
	dsbnTopbarRSS.title = "DSBN News Feed";
	dsbnTopbarRSS.href = "http://www.dsbn.edu.on.ca/news_xml.aspx?show=true";

	dsbnTopbarStyle = document.createElement( "link" );
	dsbnTopbarStyle.rel = "stylesheet";
	
	if( typeof document.body.style.maxHeight == "undefined" ) // testing for ie6
	{
		dsbnTopbarStyle.href = "http://www.dsbn.edu.on.ca/schools/shared/topbar/ie6.css";
	}
	else
	{
		dsbnTopbarStyle.href = "http://www.dsbn.edu.on.ca/schools/shared/topbar/style.css";
	}
	
	dsbnTopbarStyle.type = "text/css";
	
	dsbnTopbar = document.createElement( "div" );
	dsbnTopbar.id = "dsbn-topbar";
	
	var dsbnTopbarTransition = document.createElement( "div" );
	dsbnTopbarTransition.id = "dsbn-topbar-transition";
	
	var dsbnTopbarLogo = document.createElement( "div" );
	dsbnTopbarLogo.id = "dsbn-topbar-logo";
	
	var dsbnTopbarTransitionShadow = document.createElement( "div" );
	dsbnTopbarTransitionShadow.id = "dsbn-topbar-transition-shadow";
	
	var dsbnTopbarLogoImg = document.createElement( "img" );
	dsbnTopbarLogoImg.src = "http://www.dsbn.edu.on.ca/schools/shared/topbar/images/logo.jpg";
	dsbnTopbarLogoImg.alt = "DSBN Logo";
	
	var dsbnTopbarLogoLink = document.createElement( "a" );
	dsbnTopbarLogoLink.href = "http://www.dsbn.edu.on.ca/";
	dsbnTopbarLogoLink.target = "_blank";
	
	var dsbnTopbarNews = document.createElement( "div" );
	dsbnTopbarNews.id = "dsbn-topbar-news";	
	
	var dsbnTopbarNewsShadow = document.createElement( "div" );
	dsbnTopbarNewsShadow.id = "dsbn-topbar-news-shadow";
	
	var dsbnTopbarNewsContent = document.createElement( "div" );
	dsbnTopbarNewsContent.id = "dsbn-topbar-news-content";
	
	var spanNewsTitle = document.createElement( "span" );
	spanNewsTitle.className = "news-title";
	spanNewsTitle.innerHTML = "DSBN News:";
	
	var spanNewsDesc = document.createElement( "span" );
	spanNewsDesc.id = "news-description";
	spanNewsDesc.className = "news-description";
	spanNewsDesc.innerHTML = "Loading...";
	
	var aReadMore = document.createElement( "a" );
	aReadMore.id = "news-more";
	aReadMore.className = "news-more";
	aReadMore.target = "_blank";
	aReadMore.href = "javascript: topbarnews.more()";
	aReadMore.innerHTML = "More";

	var aPrevious = document.createElement( "a" );
	aPrevious.className = "news-previous";
	aPrevious.href = "javascript: topbarnews.previous()";
	aPrevious.innerHTML = "Previous";
	
	var aNext = document.createElement( "a" );
	aNext.className = "news-next";
	aNext.href = "javascript: topbarnews.next()";
	aNext.innerHTML = "Next";

	dsbnTopbarNewsContent.appendChild( spanNewsTitle );
	dsbnTopbarNewsContent.appendChild( spanNewsDesc );
	dsbnTopbarNewsContent.appendChild( aReadMore );
	dsbnTopbarNewsContent.appendChild( aPrevious );
	dsbnTopbarNewsContent.appendChild( aNext );
	dsbnTopbarNews.appendChild( dsbnTopbarNewsContent );
	
	dsbnTopbarLogoLink.appendChild( dsbnTopbarLogoImg );
	dsbnTopbarLogo.appendChild( dsbnTopbarLogoLink );
	dsbnTopbarTransition.appendChild( dsbnTopbarLogo );
	dsbnTopbarTransition.appendChild( dsbnTopbarTransitionShadow );
	
	dsbnTopbar.appendChild( dsbnTopbarNews );
	dsbnTopbar.appendChild( dsbnTopbarNewsShadow );
	dsbnTopbar.appendChild( dsbnTopbarTransition );
	
	document.getElementsByTagName("head")[0].appendChild( dsbnTopbarStyle );
	document.getElementsByTagName("head")[0].appendChild( dsbnTopbarRSS );
	
	document.write( "<div id=\"dsbn-topbar\">"  + dsbnTopbar.innerHTML + "</div>" );
}

// version 0.2
function createAjaxObj() 
{
	var req = false;
	if( window.XMLHttpRequest ) 
	{
		req = new XMLHttpRequest();

		if( req.overrideMimeType ) 
		{
			req.overrideMimeType( "text/xml" );
		}
	}
	else if( window.ActiveXObject ) 
	{
		var xmlHttp = false;
		try 
		{
		  req = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e2) 
			{
				req = false;
			}
		}
	}
	return req;
}

function DSBNNews()
{
	this.feedUrl = ( window.location.href.indexOf( "https://" ) == 0 ) ? "https://" : "http://";

	if( window.location.href.indexOf( "schools.dsbn.edu.on.ca" ) != -1 )
	{
		this.feedUrl += "www.dsbn.edu.on.ca/news_xml.aspx?show=true";
	}
	else
	{
		this.feedUrl += "www.dsbn.edu.on.ca/news_xml.aspx?show=true";
	}

	this.containerId = "news-description";
	this.ajaxObj = createAjaxObj();
	this.mouseoverBol = false;
	this.msg = 0;
	this.firstRun = true;
	this.timer = 6;
	this.errorMessage = "";
}

DSBNNews.prototype.init = function() 
{
	if( this.feedUrl == "" ) 
	{
		this.error( "Feed URL empty" );
		return false;
	}
	if( this.ajaxObj ) 
	{
		DSBNNewsInstance = this;		
		this.initLoader();
		this.ajaxObj.onreadystatechange = function() { DSBNNewsInstance.getFeedCallback() };
		try 
		{
			this.ajaxObj.open( "GET", this.feedUrl, true );
		} 
		catch( err ) 
		{ 
			this.error( "XMLHttpRequest.open() failed: " + err ); return;
		}
		this.ajaxObj.send( null );
	}
	else
	{
		this.error( "Init failed" ); return;
	}
}

DSBNNews.prototype.getFeedCallback = function() 
{ 
	if( this.ajaxObj.readyState == 4 ) 
	{
		if( this.ajaxObj.status == 200 ) 
		{
			var xml = this.ajaxObj.responseXML;		

			this.items = xml.getElementsByTagName("item");

			if( this.items.length == 0 ) 
			{
				this.error( "Could not find news (" + this.items.length + ")" );
				return;
			}
			else
			{
				var DSBNNewsInstance = this;
				for( var i = 0; i < this.items.length; i++ ) 
				{
					if( this.items[i].getElementsByTagName("link")[0].firstChild ) 
					{
						this.items[i].setAttribute( "clink", this.items[i].getElementsByTagName("link")[0].firstChild.nodeValue );
					}
					title = this.items[i].getElementsByTagName("title")[0].firstChild.nodeValue;
					title = title.replace(/(<([^>]+)>)/ig,"" ); 
					this.items[i].setAttribute( "ctitle", title );
					// this.items[i].setAttribute( "cdescription", this.items[i].getElementsByTagName("description")[0].firstChild.nodeValue );
				}
				document.getElementById( this.containerId ).onmouseover = function(){ DSBNNewsInstance.mouseoverBol = true }
				document.getElementById( this.containerId ).onmouseout = function(){ DSBNNewsInstance.mouseoverBol = false }
			}
			this.rotate();
		}
	}
}

DSBNNews.prototype.rotate = function() 
{	
	if( this.timer >= 6 ) 
	{
		this.timer = 0;
		if( this.mouseoverBol == false ) 
		{
			if( this.firstRun )
			{
				this.firstRun = false;
				this.msg = 0;
			}
			else
			{
				this.msg = ( this.msg < this.items.length - 1 ) ? this.msg + 1 : 0;
			}
			this.changeMessage( this.msg );
		}
	}
	this.timer++;
	var DSBNNewsInstance = this;
	setTimeout( function() { DSBNNewsInstance.rotate() } , 1000 );	
}

DSBNNews.prototype.next = function() 
{
	if( this.mouseoverBol == false ) 
	{
		this.msg = ( this.msg < this.items.length - 1 ) ? this.msg + 1 : 0;
		this.changeMessage( this.msg );
		this.timer = 0;
	}
}

DSBNNews.prototype.previous = function() 
{
	if( this.mouseoverBol == false ) 
	{
		this.msg = ( this.msg == 0 ) ? this.items.length - 1 : this.msg - 1;
		this.changeMessage( this.msg );
		this.timer = 0;
	}	
}

DSBNNews.prototype.changeMessage = function( msg ) 
{	
	var container = document.getElementById( this.containerId );
	container.innerHTML = "";
	DSBNNewsInstance = this;

	var title = this.items[ msg ].getAttribute("ctitle");
	title = title.length > 30 ? title.substring( 0, 30 ) + "..." : title;
	
	var link = document.createElement( "a" );
	link.href = this.items[ msg ].getAttribute("clink");
	link.target = "_blank";

	link.innerHTML = title;
	container.appendChild( link );

	document.getElementById( "news-more" ).href = this.items[ msg ].getAttribute("clink");
}

DSBNNews.prototype.initLoader = function()
{
	var container = document.getElementById( this.containerId );
	container.innerHTML = "<img id=\"dsbn-topbar-loading\" src=\"http://www.dsbn.edu.on.ca/schools/shared/topbar/images/loading.gif\" alt=\"Loading...\" />";
}

DSBNNews.prototype.error = function( errorMessage ) {
	var container = document.getElementById( this.containerId );
	container.innerHTML = "Error: " + errorMessage;
	container.title = "Error: " + errorMessage;
	this.errorMessage = errorMessage;
}

topbarnews = new DSBNNews();
window.onload = function()
{	
	topbarnews.init();
}
