/*
	DOMnews 1.0
	homepage: http://www.onlinetools.org/tools/domnews/
	released 11.07.05
*/

/* Variables, go nuts changing those! */
	// initial position
	var dn_startpos=120;
	// end position
	var dn_endpos=-320;
	// Speed of scroller higher number = slower scroller
	var dn_speed=38;
	// ID of the news box
	var dn_newsID='news';
	// class to add when JS is available
	var dn_classAdd='hasJS';
	// Message to stop scroller
	var dn_stopMessage='Stop scroller';
	// ID of the generated paragraph
	var dn_paraID='DOMnewsstopper';
	// dn_interval
	var dn_interval;
	// Preload flag for other links
	var other_lnks="";
  var rotate_banner = false;
	/* Initialise scroller when window loads */
	window.onload=function()
	{
		// check for DOM
    MM_preloadImages('/images/register-h.gif','/images/login-h.gif');
    // MM_preloadImages('/images/banner1.jpg','/images/banner2.jpg','/images/banner3.jpg','/images/banner4.jpg');
    //     rotate_banner = false;
    //     if(rotate_banner)
    //     {
    //          RotateImages(0);
    //     }
    if(other_lnks!=""){
      other_lnks = other_lnks.split(',');
      var i = 0;
      for(i = 0;i<other_lnks.length;i++){
        MM_preloadImages(other_lnks[i]);
      }
    }
		if(!document.getElementById || !document.createTextNode){return;}
		initDOMnews();
		document.getElementById('newsloading').style.display="none";
		document.getElementById('news').style.display="block";
		
		// Code to rotate images
		theRotator();
		
	}
	/* stop scroller when window is closed */
	window.onunload=function()
	{
		clearInterval(dn_interval);
	}
// image rotator
  function RotateImages(Start)
  {

    var ImageArr1 = new Array('/images/banner1.jpg','/images/banner2.jpg','/images/banner3.jpg','/images/banner4.jpg');
    var ImageHolder1 = document.getElementById('banner_main');
  	var a = ImageArr1;
  	var b = ImageHolder1;
  	if(Start>=a.length)
  		Start=0;
    //new Effect.Opacity('banner_main', { from: 1.0, to: 0.3 });
    //b.fade();
  	b.src = a[Start];
  	//new Effect.Opacity('banner_main', { from: 0.3, to: 1.0 });
    //b.appear();
  	window.setTimeout("RotateImages("+(Start+1)+")",4000);

  }

/*
	This is the functional bit, do not press any buttons or flick any switches
	without knowing what you are doing!
*/

	var dn_scrollpos=dn_startpos;
	/* Initialise scroller */
	function initDOMnews()
	{
		var n=document.getElementById(dn_newsID);
		if(!n){return;}
		n.className=dn_classAdd;
		dn_interval=setInterval('scrollDOMnews()',dn_speed);
		var newa=document.createElement('a');
		var newp=document.createElement('p');
		newp.setAttribute('id',dn_paraID);
		newa.href='#';
		newa.appendChild(document.createTextNode(dn_stopMessage));
		newa.onclick=stopDOMnews;
		newp.setAttribute('class',"page-links");
		newp.setAttribute('style',"padding-top:10px;")
		newp.appendChild(newa);
		//n.parentNode.insertBefore(newp,n.nextSibling);
		n.onmouseover=function()
		{
			clearInterval(dn_interval);
		}
		n.onmouseout=function()
		{
			dn_interval=setInterval('scrollDOMnews()',dn_speed);
		}
	}

	function stopDOMnews()
	{
		clearInterval(dn_interval);
		var n=document.getElementById('news');
		n.className='';
		n.parentNode.removeChild(n.nextSibling);
		return false;
	}
	function scrollDOMnews()
	{
		var n=document.getElementById(dn_newsID).getElementsByTagName('ul')[0];
		n.style.top=dn_scrollpos+'px';
		if(dn_scrollpos==dn_endpos){dn_scrollpos=dn_startpos;}
		dn_scrollpos--;
	}
	
	
	// =============================================   Another Scroller ===============================================
	/*
   * This is a simple JavaScript vertical scroller that is crossbrowser and also validates
   * as XHTML Strict.
   *
   * Usage:
   * (1) Define a <div> tag with a specified ID, containing the scrolling text. One <div> for each line.
   * (2) Define an INLINE CSS width and height (important, MUST be inline)
   * (3) Execute the divScroller function, passing id, mode (h or v),  speed (higher number means
   *     slower) and delay (in ms).
   *
   */

  /****************************************************************************************************
   * Cross browser getElementByID.
   * From: www.quirksmode.org
   *
   * @param	id
   */
  function getObj(name)
  {
  	if (document.getElementById)
  	{
  		this.obj = document.getElementById(name);
  		this.style = document.getElementById(name).style;
  	}
  	else if (document.all)
  	{
  		this.obj = document.all[name];
  		this.style = document.all[name].style;
  	}
  	else if (document.layers)
  	{
  		this.obj = document.layers[name];
  		this.style = document.layers[name];
  	}
  }

  /****************************************************************************************************
   * Enables the scrolling for the specified div (matching id).
   *
   * @param	string id of the tag
   * @param	speed
   * @param	delay
   */
  function divScroller(id, direction, speed, delay)
  {
  	if (document.getElementById)
  	{
  		// DOM3 = IE5+, NS6+, FF0.7+
  		// *** Scroller
  		var scroller = new getObj(id);

  		// *** Debug force vars
  		//scroller.style.height = "30px";
  		//scroller.style.backgroundColor = "#FFEEEE";

  		// *** Computed Styles
  		//alert(scroller.obj.currentStyle.height);
  		/*if (!scroller.style.height)
  			if (scroller.obj.currentStyle)
  				scroller.style.height = scroller.obj.currentStyle.height;
  			else
  				scroller.style.height = document.defaultView.getComputedStyle(scroller.obj, null).getPropertyValue("height");*/

  		// *** Needed vars
  		scroller.style.position = "relative";
  		scroller.style.overflow = "hidden";

  		// *** Generate scrolling inner <div>
  		scroller.obj.innerHTML = "<div id=\"" + id + "_inner\">" + scroller.obj.innerHTML + "</div>";

  		// *** Inner
  		var inner = new getObj(id + "_inner");
  		inner.style.position = "absolute";
  		inner.style.left = parseInt(scroller.style.width) + "px";
  		inner.style.top = parseInt(scroller.style.height) + "px";

  		// *** Apply sub-styles
  		//divScrollItemsStyler(id, direction);

  		// *** Worker
  		if (direction == "h" || direction == "horizontal")
  		{
  			// ****** HORIZONTAL
  			// MouseOver: pauses the ticker
  			scroller.obj.onmouseover = function() { divScroll_onMouseOver(id); };
  			scroller.obj.onmouseout = function() { divScroll_onMouseOut(id); };

  			// Create a temp element to evaluate the size (awful, but no better way to do this)
  			fxpatch = navigator.userAgent.indexOf("Firefox") > -1 ? " left: -9000px;" : ""; // Firefox different CSS (on every other browser since IE5+ isn't needed)
  			spanContent = "<span id=\"" + id + "_widthEval\" style=\"visibility: hidden; position: absolute; top: -100px; left: -1px; z-index: -10; white-space: nowrap;" + fxpatch + "\"><nobr>" + inner.obj.innerHTML + "</nobr></span>";
  			if (document.createElement)
  			{
  				var span = document.createElement('span');
  				span.innerHTML = spanContent;
  				scroller.obj.appendChild(span);
  			}
  			else
  			{
  				document.write(spanContent);
  			}
  			var widthEval = new getObj(id + "_widthEval");

  			// Setup the scrolling inner drawer
  			inner.style.top = "0px";
  			inner.style.whiteSpace = "nowrap";
  			inner.style.width = widthEval.obj.offsetWidth + "px";
  			limit = parseInt(inner.style.width);

  			// Execute
  			setTimeout("divScrollHelperH(\"" + id + "\", " + limit + ", " + speed + ", " + delay + ")", parseInt(speed));
  		}
  		else if (direction == "v" || direction == "vertical")
  		{
  			// ****** VERTICAL
  			// Setup the scrolling inner drawer
  			inner.style.left = "0px";
  			inner.style.width = parseInt(scroller.style.width) + "px";
  			limit = (inner.obj.getElementsByTagName('div').length * parseInt(scroller.style.height)) / 4;

  			// Execute
  			setTimeout("divScrollHelperV(\"" + id + "\", " + limit + ", " + speed + ", " + delay + ")", parseInt(speed));
  		}
  	}
  }

  /****************************************************************************************************
   * Helper for the HORIZONTAL scrolling for the specified div (matching id).
   * This is the real "ticker" function, executed to move the div.
   *
   * @param	string id of the tag
   * @param	pre-calculated height limit (to speed up execution)
   * @param	speed
   * @param	delay
   */
  function divScrollHelperH(id, limit, speed, delay)
  {
  	if (document.getElementById)
  	{
  		// DOM3 = IE5+, NS6+, FF0.7+
  		var scroller = new getObj(id);
  		var inner = new getObj(id + "_inner");

  		// *** Tick duration
  		nextTick = speed;

  		// *** Avoiding some errors
  		if (!inner.style.left) inner.style.left = "0px";

  		// *** Moving the inner div. At the end, restart.
  		if (parseInt(inner.style.left) < -limit)
  		{
  			inner.style.left = parseInt(scroller.style.width) + "px";
  		}
  		else if (!scroller.obj.pause || scroller.obj.pause == false)
  		{
  			inner.style.left = (parseInt(inner.style.left) - 1) + "px";

  			// Deceiving Opera8 stupidity
  			//inner.style.width = parseInt(scroller.style.width) - (parseInt(inner.style.left) - 4) + "px";
  		}

  		// *** Bigger delay on item found
  		// Skips borders to make transition without delays on loop
  		/*if (!(parseInt(inner.style.left) == parseInt(scroller.style.width)) &&
  			!(parseInt(inner.style.left) == -limit) &&
  			(parseInt(inner.style.left) % parseInt(scroller.style.width)) == 0)
  		{
  			nextTick = delay;
  		}*/

  		// *** Tick!
  		setTimeout("divScrollHelperH(\"" + id + "\", " + limit + ", " + speed + ", " + delay + ")", parseInt(nextTick));
  	}
  }

  /****************************************************************************************************
   * Helper for the VERTICAL scrolling for the specified div (matching id).
   * This is the real "ticker" function, executed to move the div.
   *
   * @param	string id of the tag
   * @param	pre-calculated height limit (to speed up execution)
   * @param	speed
   * @param	delay
   */
  function divScrollHelperV(id, limit, speed, delay)
  {
  	// DOM3 = IE5+, NS6+, FF0.7+
  	var scroller = new getObj(id);
  	var inner = new getObj(id + "_inner");

  	// *** Tick duration
  	nextTick = speed;

  	// *** Avoiding some errors
  	if (!inner.style.top) inner.style.top = "0px";

  	// *** Moving the inner div. At the end, restart.
  	if (parseInt(inner.style.top) < -limit)
  	{
  		inner.style.top = parseInt(scroller.style.height) + "px";
  	}
  	else
  	{
  		inner.style.top = (parseInt(inner.style.top) - 1) + "px";
  	}

  	// *** Bigger delay on item found
  	// Skips borders to make transition without delays on loop
  	if (!(parseInt(inner.style.top) == parseInt(scroller.style.height)) &&
  		!(parseInt(inner.style.top) == -limit) &&
  		(parseInt(inner.style.top) % parseInt(scroller.style.height)) == 0)
  	{
  		nextTick = delay;
  	}

  	// *** Tick!
  	setTimeout("divScrollHelperV(\"" + id + "\", " + limit + ", " + speed + ", " + delay + ")", parseInt(nextTick));
  }

  /****************************************************************************************************
   * OnMouseOver helper for the HORIZONTAL scrolling for the specified div (matching id).
   *
   * @param	string id of the tag
   */
  function divScroll_onMouseOver(id)
  {
  	var scroller = new getObj(id);
  	scroller.obj.pause = true;
  }

  function divScroll_onMouseOut(id)
  {
  	var scroller = new getObj(id);
  	scroller.obj.pause = false;
  }

  /****************************************************************************************************
   * Apply essential working styles to each <div> inside the scroller.
   *
   * @param	string id of the inner div
   */
  function divScrollItemsStyler(id, direction)
  {
  	// DOM3 = IE5+, NS6+, FF0.7+
  	var scroller = new getObj(id);
  	var inner = new getObj(id + "_inner");

  	elements = inner.obj.getElementsByTagName('div');

  	for (var i = 0; i < elements.length; i++)
  	{
  		var item = elements.item(i);

  		item.style.width = scroller.style.width;
  	}
  }
  
// ================================== IMAGE ROTATOR JQUERY ==============================================

function theRotator() {
	//Set the opacity of all images to 0
	$('#rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('#rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',6000);
	
}

function rotate() {	
	//Get the first image
	var current = ($('#rotator ul li.show')?  $('#rotator ul li.show') : $('#rotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#rotator ul li:first') :current.next()) : $('#rotator ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};

