
	
	var flipObj = new flip();
	
	function flip () {
		this.flipInterval = 2000;
		this.xmlURL = 'js/flip.xml';
		this.textPositions = {
			topLeft:true,topCenter:true,topRight:true,
			bottomLeft:true,bottomCenter:true,bottomRight:true
		}
		
		this.timer = null;
		this.flipStage = 1;
		this.httpObj = null;
		this.xml = null;
		this.stageContent = null;
		this.imgURL = '';
		
		this.getXML = function () {
			if (window.XMLHttpRequest) {  
				try {  
					this.httpObj = new XMLHttpRequest();  
				}
				catch(e) { }
			}
			else if (window.ActiveXObject) {
				try {
					this.httpObj = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e) { }
			}
			this.httpObj.open('GET',this.xmlURL,true);
			this.httpObj.onreadystatechange = function () {
				flipObj.parseXML();
			}
			this.httpObj.send(null);
		}
		
		this.parseXML = function () {
			var flipEle,flipNodes,noXML;
			
			noXML = (!this.xml);
			if (!noXML || (this.httpObj && this.httpObj.readyState==4 && (this.httpObj.status==200 || this.httpObj.status==0))) {
				if (noXML) {
					if (!this.httpObj.responseXML) return this.errorOut('XML Error: The XML sheet is invalid.');
					this.xml = this.httpObj.responseXML;
				}
				
				flipEle = this.xml.getElementsByTagName('layers');
				if (!flipEle.length) return errorOut('No layers');
				if (flipEle[0].getAttribute('flipInterval')) this.flipInterval = parseInt(flipEle[0].getAttribute('flipInterval'))*1000;
				
				flipNodes = flipEle[0].getElementsByTagName('layer');
				if (!flipNodes.length) return errorOut('No layer');
				this.stageContent = [false];
				for (var i=0; i<flipNodes.length; i++) {
					this.stageContent[i+1] = {
						img:new Image(),text:'',textPosition:'left'
					};
					if (flipNodes[i].getAttribute('image')) this.stageContent[i+1].img.src = this.imgURL+flipNodes[i].getAttribute('image');
					if (flipNodes[i].getAttribute('imageText')) this.stageContent[i+1].text = flipNodes[i].getAttribute('imageText');
					if (flipNodes[i].getAttribute('imageTextColor')) this.stageContent[i+1].textColor = flipNodes[i].getAttribute('imageTextColor');
					if (flipNodes[i].getAttribute('imageTextSize')) this.stageContent[i+1].textSize = flipNodes[i].getAttribute('imageTextSize');
					if (flipNodes[i].getAttribute('imageTextPosition') && typeof(this.textPositions[flipNodes[i].getAttribute('imageTextPosition')])!='undefined') this.stageContent[i+1].textPosition = flipNodes[i].getAttribute('imageTextPosition');
					if (flipNodes[i].getAttribute('link')) this.stageContent[i+1].link = flipNodes[i].getAttribute('link');
				}
				if (noXML) this.flipTimer();
				return true;
			}
		}
		
		this.flipNext = function () {
			var i = 0;
			if (!this.xml) this.getXML();
			if (!this.stageContent) return false;
			if (this.flipStage==(this.stageContent.length-1)) this.flipStage = 0;
			i = ++this.flipStage;
			this.flipGo(i);
			return true;
		}
		
		this.flipGo = function (stage) {
			var oldStage,contentImg,contentTextContainer,oldFlipIcon,newFlipIcon,flipArrow,flipIconStyle,flipIconHeight,newContentImg,d;
			
			if (stage>this.stageContent.length) return false;
			oldStage = (stage===1) ? this.stageContent.length-1 : (stage-1);
			
			contentImg = document.getElementById('flipContentImage');
			if (!contentImg) return false;
			contentTextContainer = document.getElementById('flipText');
			if (!contentTextContainer) return false;
			
			oldFlipIcon = document.getElementById('flipIcon'+oldStage);
			this.flipStage = stage;
			if (!oldFlipIcon && document.getElementById('flipBox'+oldStage)) {
				oldFlipIcon = document.getElementById('flipBox'+oldStage);
				newFlipIcon = document.getElementById('flipBox'+this.flipStage);
			}
			else newFlipIcon = document.getElementById('flipIcon'+this.flipStage);
			
			flipArrow = document.getElementById('flipArrow');
			
			if (typeof(jQuery)=='undefined') {
				if (typeof(this.stageContent[this.flipStage])=='undefined') return this.errorOut('Undefined '+this.flipStage);
				contentImg.src = this.stageContent[this.flipStage].img.src;
				if (typeof(this.stageContent[this.flipStage].link)!='undefined') contentImg.parentNode.href = this.stageContent[this.flipStage].link;
				else contentImg.parentNode.href = '';
			}
			else {
				newContentImg = document.getElementById('newFlipContentImage');
				newContentImg.src = this.stageContent[this.flipStage].img.src;
				if (typeof(this.stageContent[this.flipStage].link)!='undefined') newContentImg.parentNode.href = this.stageContent[this.flipStage].link;
				else newContentImg.parentNode.href = '#';
				jQuery(contentImg).fadeOut('fast');
				jQuery(newContentImg).fadeIn('fast',function () {
					var contentImg = document.getElementById('flipContentImage');
					var newContentImg = document.getElementById('newFlipContentImage');
					contentImg.src = newContentImg.src;
					contentImg.parentNode.href = newContentImg.parentNode.href;
					jQuery(newContentImg).hide();
					jQuery(contentImg).show();
				});
			}
			
			if (typeof(this.stageContent[this.flipStage]['text'])!='undefined' && this.stageContent[this.flipStage]['text']) {
				contentTextContainer.style.visibility = 'visible';
				if (typeof(this.stageContent[this.flipStage].textColor)!='undefined' && this.stageContent[this.flipStage].textColor) contentTextContainer.style.color = this.stageContent[this.flipStage].textColor;
				else contentTextContainer.style.color = '';
				if (typeof(this.stageContent[this.flipStage]['textSize'])!='undefined' && this.stageContent[this.flipStage]['textSize']) contentTextContainer.style.fontSize = this.stageContent[this.flipStage]['textSize'];
				else contentTextContainer.style.fontSize = '1em';
				if (typeof(this.stageContent[this.flipStage]['textPosition'])!='undefined' && this.stageContent[this.flipStage]['textPosition']) contentTextContainer.className = this.stageContent[this.flipStage]['textPosition'];
				else contentTextContainer.className = '';
				contentTextContainer.innerHTML = this.stageContent[this.flipStage]['text'];
			}
			else {
				contentTextContainer.style.visibility = 'hidden';
			}
			for (var i=1; i<=this.stageContent.length; i++) {
				d = document.getElementById('flipIcon'+i);
				if (!d) d = document.getElementById('flipBox'+i);
				if (d) d.className = d.className.replace(/on/,'off');
			}
			newFlipIcon.className = newFlipIcon.className.replace(/off/,'on');
			
			if (flipArrow) {
				if (document.defaultView && document.defaultView.getComputedStyle) {
					flipIconStyle = document.defaultView.getComputedStyle(oldFlipIcon,'');
					flipIconHeight = parseInt(flipIconStyle.getPropertyValue('height'));
					flipIconHeight += parseInt(flipIconStyle.getPropertyValue('padding-top'));
					flipIconHeight += parseInt(flipIconStyle.getPropertyValue('padding-bottom'));
				}
				else flipIconHeight = parseInt(oldFlipIcon.currentStyle.height) + parseInt(oldFlipIcon.currentStyle.paddingTop) + parseInt(oldFlipIcon.currentStyle.paddingBottom);
				flipArrow.style.top = (flipIconHeight*(this.flipStage-1))+'px';
			}
			
			else if (document.getElementById('flipStageButton1')) {
				document.getElementById('flipStageButton'+oldStage).style.display = 'none';
				document.getElementById('flipStageButton'+this.flipStage).style.display = 'block';
			}
			
			return true;
		}
		
		this.flipClick = function (stage) {
			if (this.timer) window.clearTimeout(this.timer);
			this.flipGo(stage);
			return true;
		}
		
		this.flipTimer = function () {
			if (!this.xml) {
				this.getXML();
				return true;
			}
			if (this.timer) {
				this.flipStop();
				this.flipNext();
			}
			this.timer = window.setTimeout('flipObj.flipTimer()',this.flipInterval);
			return true;
		}
		
		this.flipStop = function () {
			if (this.timer) {
				window.clearTimeout(this.timer);
				this.timer = null;
			}
		}
		
		
		this.errorOut = function (str) {
			alert(str);
			return false;
		}
	}

