var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])
ie6 = (version >= 5.5 && version < 7);

function ieHack()
{
	for(var i=0; i<document.images.length; i++)
	{
		var img = document.images[i]
		var imgName = img.src.toUpperCase()

		if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		{
			
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			var imgStyle = "display:inline-block;" + img.style.cssText 
			if (img.align == "left") imgStyle = "float:left;" + imgStyle
			if (img.align == "right") imgStyle = "float:right;" + imgStyle
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
			
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
			
			img.outerHTML = strNewHTML;
			i--;
		}
	}
}

 
var Stage = new Class({

	initialize: function(options) {
		options = options || {};

		this.stage = options.stage;
		this.slides = options.slides;
		this.container = options.container;

		this.content = [];
		this.items = this.slides.getElements('li');
		this.tabs = {};
		this.max = this.items.length - 1;
		this.current = 0;

		this.timer = null;
		this.timeout = 10000;
		this.paused = false;
		if (ie6){
		  ieHack()
	  }
	},

	load: function() {

		this.items.each(function(item) {

			var header = item.getElement('h2');
			
			
			this.content[this.content.length] = {
				'header' : header.get('text'),
				'html': item.get('html') + ''
			};

		}, this);

		this.container.empty();

		// create new container
		this.slides = new Element('div', {
			'id': 'c0',
			'class': 'slides',
			'html': this.content[this.current].html
		});

		// add to stage
		this.container.grab(this.slides);

		// show tabs
		this.showtabs(this.current);

		// start timer
		this.start();

	},

	start: function() {
		// set timer
		this.timer = this.play.delay(this.timeout, this);
	},

	play: function() {
		// next key
		var key = (this.current == this.max) ? 0 : this.current + 1 ;

		// show slide
		this.show(key);

		// set timer
		this.timer = this.play.delay(this.timeout, this);

		// set stauts
		this.paused = false;
	},

	pause: function() {
		// clear timer
		this.timer = $clear(this.timer);

		// set status
		this.paused = true;
	},

	show: function(key) {

		// same key return
		if(this.current == key) {
			return;
		}

		// set new key
		this.current = key;
		
		// create new container
		var slide = new Element('div', {
			'id': 'c' + this.current,
			'class': 'slides',
			'html': this.content[this.current].html,
			'styles': {
				'left': 960 // start element off screen
			}
		});

		// add to stage
		this.container.grab(slide);

		// move slides
		var options = {
			'link': 'ignore',
			'transition': Fx.Transitions.Back.easeInOut
		};
		var fx1 = new Fx.Tween(slide, options);
		fx1.start('left', 0);

		var fx2 = new Fx.Tween(this.slides, options);
		fx2.start('left', -960);

		// replace holder
		fx2.addEvent('complete', function(){

			this.slides.destroy();
			this.slides = slide;

		}.bind(this));

		// show tabs
		this.showtabs();
		
	},

	showtabs: function() {

		if($chk($('tabs'))) {
			$('tabs').destroy();
		}
		
		// tabs
		this.tabs = new Element('ul', {
			'id': 'tabs'
		});

		this.items.each(function(item, index) {

			var li = new Element('li');

			// class name
			var classes = [];

			if(index == 0) {
				classes.push('first');
			}
			if(index == this.current) {
				classes.push('selected');
			}
			if(index == (this.items.length - 1)) {
				classes.push('last');
			}

			var a = new Element('a', {
				'href': '#',
				'text': this.content[index].header,
				'class': classes.join(' '),
				'events': {

					'click': function(e) {

						this.show(index);
						this.pause();
						e.stop();

					}.bind(this)/*,

					'mouseenter': function(e) {

						var c = e.target.className;
						// make sure its selected
						if(c.indexOf('selected') != -1) {
							if(!this.paused) this.pause();
						}

					}.bind(this),

					'mouseleave': function(e) {

						var c = e.target.className;
						// make sure its selected
						if(c.indexOf('selected') != -1) {
							if(this.paused) {
								this.start();
							}
						}
						
					}.bind(this)*/

				}
			});

			li.grab(a);
			this.tabs.grab(li);

		}, this);

		this.stage.grab(this.tabs);

	}

});

var Font = new Class({
	initialize: function(src, options) {
		options = options || {};

		this.swf = new Swiff(src, {
			width: (options.width) ? options.width : 100 ,
			height: (options.height) ? options.height : 20,
			vars: options
		});
	},
	replace: function(id) {
		var element = $(id);
		this.swf.replaces(element);
	}
});




function myAddEvent( obj, type, fn ) {  

   if (obj.addEventListener) {   
        obj.addEventListener( type, fn, false );   
    }   
    else if (obj.attachEvent) {   
        obj["e"+type+fn] = fn;   
        obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }   
        obj.attachEvent( "on"+type, obj[type+fn] );   
    }   
    else {   
        obj["on"+type] = obj["e"+type+fn];   
    }   
}   

/* SCROLLING TEXT ON HOME PAGE */

var delayb4scroll=2000 //Specify initial delay before marquee starts to scroll on page (2000=2 seconds)
var marqueespeed=1 //Specify marquee scroll speed (larger is faster 1-10)
var pauseit=1 //Pause marquee onMousever (0=no. 1=yes)?

var copyspeed=marqueespeed;
var pausespeed=(pauseit==0)? copyspeed: 0;
var actualheight='';

function scrollmarquee(){
if (parseInt(cross_marquee.style.top)>(actualheight*(-1)+8)) //if scroller hasn't reached the end of its height
cross_marquee.style.top=parseInt(cross_marquee.style.top)-copyspeed+"px" ;//move scroller upwards
else //else, reset to original position
cross_marquee.style.top=parseInt(marqueeheight)+8+"px";
}

function initializemarquee(){
cross_marquee=document.getElementById("vmarquee");
cross_marquee.style.top=0;
marqueeheight=document.getElementById("marqueecontainer").offsetHeight;
actualheight=cross_marquee.offsetHeight ;//height of marquee content (much of which is hidden from view)
if (window.opera || navigator.userAgent.indexOf("Netscape/7")!=-1){ //if Opera or Netscape 7x, add scrollbars to scroll and exit
cross_marquee.style.height=marqueeheight+"px";
cross_marquee.style.overflow="scroll";
return;
}
setTimeout('lefttime=setInterval("scrollmarquee()",50)', delayb4scroll);
}

if (window.addEventListener)
window.addEventListener("load", initializemarquee, false);
else if (window.attachEvent)
window.attachEvent("onload", initializemarquee);
else if (document.getElementById)
window.onload=initializemarquee;

  /* END SCROLLING TEXT ON HOME PAGE */
