var stopRotate = false;

function bannerRotator() {

	var arBanners = new Array();
	var currentBanner = 0;
	var lib = 'none';
	var intDelay = 1000;
	
	this.library = function(s) {
		lib = s;	
	};
	
	this.setDelay = function(i) {
		intDelay = i;
	};
					  
	this.public = {
	
		init : function(el) {
		
			// clear any HTML within the wrapper
			el.innerHTML = '';
			
			// add the images ul
			var oImages = document.createElement('ul');
			oImages.setAttribute('id', 'bannerImages');
			el.appendChild(oImages);
			
			// add the images
			var oItem;
			for(i = 0; i < arBanners.length; i++) {
				oItem = document.createElement('li');
				oItem.setAttribute('id','bannerImage0' + i);
				oImages.appendChild(oItem);
				document.getElementById('bannerImage0' + i).innerHTML = 
						'<a href="' + arBanners[i][0] + '"><img src="' + arBanners[i][1] + '" alt="' + arBanners[i][2] + '" /></a>';
				document.getElementById('bannerImage0' + i).style.display = 'none';
			};
			
			// add the navigation ul
			var oNav = document.createElement('ul');
			oNav.setAttribute('id', 'bannerNav');
			el.appendChild(oNav);
			
			// add the navigation links
			for(i = 0; i < arBanners.length; i++) {
				oItem = document.createElement('li');
				oItem.setAttribute('id','bannerNav0' + i);
				oNav.appendChild(oItem);
				document.getElementById('bannerNav0' + i).innerHTML = 
						'<a href="#" id="bannerLink0' + i + '" rel="' + i + '">' + (i + 1) + '</a>';
						
				// set the show and hide events for each of the links
				document.getElementById('bannerLink0' + i).onclick = function() {
					// hide all the banners and clear li classes
					for(n = 0; n < arBanners.length; n++) {
						if(n != currentBanner) {
							document.getElementById('bannerImage0' + n).style.display = 'none';
							document.getElementById('bannerNav0' + n).className = '';
						};
					};
					if(lib == 'jquery' && $) {
						// hide the current element
						$('#bannerImage0' + currentBanner).fadeOut(intDelay);						
						// show the new element
						//if(currentBanner == this.getAttribute('rel')) {
						//	$('#bannerImage0' + this.getAttribute('rel')).show()
						//} else {
							$('#bannerImage0' + this.getAttribute('rel')).fadeIn(intDelay);	
						//};
					} else {
						// hide the current element
						document.getElementById('bannerImage0' + currentBanner).style.display = 'none';
							
						// show the new element
						document.getElementById('bannerImage0' + this.getAttribute('rel')).style.display = 'block';						
					};
					
					// remove the CSS class of the current Nav
					document.getElementById('bannerNav0' + currentBanner).className = '';
					
					// set the CSS class of the new Nav
					document.getElementById('bannerNav0' + this.getAttribute('rel')).className = 'current';
					currentBanner = this.getAttribute('rel');
					
					// stop or start the animation
					stopRotate = true;//(stopRotate ? false : true);
					
					
					return false;
					
				};
			};
			
			// show the first image;
			document.getElementById('bannerImage00').style.display = 'block';
			
			// set the selected class on the relevant nav link
			document.getElementById('bannerNav00').className = 'current';
			
			// set the currentImage = 0
			currentBanner = 0;
		
		},
	
		addBanner : function(strTitle, strImg, strUrl) {
			arBanners.push([strTitle,strImg,strUrl]);
			return true;
		},
		
		numBanners : function() {
			return arBanners.length;
		},
		
		rotate : function(init) {
			if(init != 0 && !stopRotate) {
				var nextBanner = (currentBanner < arBanners.length - 1  ? currentBanner + 1 : 0) ;
				
				if(lib == 'jquery' && $) {
					// fade out the current banner and change the css class
					$('#bannerImage0' + currentBanner).fadeOut(intDelay);
					// fade in next banner
					$('#bannerImage0' + nextBanner).fadeIn(intDelay);	
				} else {			
					// hide the current banner and change the css class
					document.getElementById('bannerImage0' + currentBanner).style.display = 'none';
					// show next banner
					document.getElementById('bannerImage0' + nextBanner).style.display = 'block';
				};
				
				// set the css classes of the links
				document.getElementById('bannerNav0' + currentBanner).className = '';
				document.getElementById('bannerNav0' + nextBanner).className = 'current';
				
				currentBanner = nextBanner;
			};
		}
		
	};
					  
	var local = {
	
		currentBanner : 0,
		
		doTrans : function(el, t) {
			el = (el == 'object' ? el.element : el);
		
			// set max and min values for t and make sure it's numeric
			t = (local.isNum(t) ? t : 10);
			t = (t > 10 ? 10 : t);
			t = (t < 0 ? 0 : t);
		
			if(el.style.filters) {
				t = (t == 0 ? t : t * 10);
				el.style.filter = 'alpha(opacity=' + t + ')';
			} else {
				t = (t == 0 ? t : t * 0.1);
				el.style.opacity = t;
			};
		
		},
		
		nextItem : function() {
			alert('blah');
		},
		
		isNum : function(n) {

			var ValidChars = "0123456789.";
			var IsNumber = true;
			var Char;
			
			for (i = 0; i < n.length && IsNumber == true; i++) { 
				Char = n.charAt(i); 
				if (ValidChars.indexOf(Char) == -1) {
					IsNumber = false;
				};
			};
			
			return IsNumber;
		}


	};




};