/*jQuery.jQueryRandom = 0;  
jQuery.extend(jQuery.expr[":"],  
{  
    random: function(a, i, m, r) {  
    	//alert(r.length)
        if (i == 0) {  
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);  
        };  
        return i == jQuery.jQueryRandom;  
    }  
});*/ 
var $intro = {
	currentImage:null,
	currentImageIndex:0,
	currentTextIndex:-1,
	images:new Array,
	currentWidth:0,
	currentHeight:0,
	initialHeight:0,
	initialWidth:0,
	nav:null,
	text:null,
	options:{displayInterval:2,animationInterval:4,fadeDuration:0.5,flashRatio:2.35},
	timer:null,
	showing:false,
	flashUrl:"/homeintro.swf",
	//firstImgID:null,
	load : function(options) {
		this.options = jQuery.extend(this.options, options);
		
		this.container = jQuery('#intro-container');
		
		var _images = jQuery('#intro-container img');
		var _img = _images[Math.floor(Math.random()*_images.length)];
		document.observe('lightview:opened', function(event) {
			$intro.stop();
		});
		document.observe('lightview:hidden', function(event) {
			$intro.animate();
		});
		
		/*if (DetectFlashVer(6, 0, 65) && DetectFlashVer(9, 0, 12) && document.location.hash.toString() != "#showintro") {
			
			var w = this.container.width();
			this.container.html(AC_FL_GetContent(
			"src", this.flashUrl,
			"FlashVars", "t="+encodeURIComponent($_rotatingText)+"&v="+$_videoUrl+"&i="+_img.src,
			"width", w,
			"height", Math.round(w/this.options.flashRatio),
			"align", "middle",
			"id", "homeIntro",
			"quality", "high",
			"bgcolor", "#eaeaea",
			"name", "homeIntro",
			"allowScriptAccess","sameDomain",
			"type", "application/x-shockwave-flash",
			"pluginspage", "http://www.adobe.com/go/getflashplayer"));
			jQuery(window).resize(function() {$intro.resizeFlash()});
			return;
			
		}*/
		//width: 1256px; height: 534px
		jQuery('#intro-container img').each($intro.addImage);
		//this.firstImg = jQuery('#'+this.firstImgID);
		this.nav = jQuery('#intro-nav');
		this.navLinks = jQuery('#intro-nav div.intro-button');
		this.navLinks[0].className = "intro-button-on";
		this.text = $_rotatingText.split('|');
		this.overlay = jQuery('#intro-text-overlay');
		this.textbox = jQuery('#intro-text-container');
		//this.textbox.html(this.text[0]);
		jQuery(window).load(function() {$intro.resize(true)});
		//this.resize();
		//
		
	},
	resizeFlash : function() {
		var elm = (document["homeIntro"])?document["homeIntro"]:window["homeIntro"];
		var w = this.container.width();
		elm.width = w;
		elm.height = Math.round(w/this.options.flashRatio);
	},
	resize : function(first) {
		this.currentWidth = this.container.width();
		this.currentHeight = this.container.height();
		if (first) {
			this.initialHeight = this.currentHeight;
			this.initialWidth = this.currentWidth;
		} else {
			this.currentHeight = (this.currentWidth*this.initialHeight)/this.initialWidth;
		}
		this.container.css({height:this.currentHeight});
		//console.log('w='+this.currentWidth+' h='+this.currentHeight);
		var props = {width:this.currentWidth, height:this.currentHeight, "line-height":(this.currentHeight)+"px"};
		if (first) props.display = "block";
		this.overlay.css(props);
		this.textbox.css(props);
		this.nav.css("top",this.currentHeight-this.nav.height());
		
		if (first) {
			this.nav.css("display","block");
			jQuery(window).resize(function() {$intro.resize()});
			jQuery.each(this.images, function(i,img) {img.style.position = "absolute";});
			this.overlay.css("left", this.currentWidth);
			this.textbox.css("left", -this.currentWidth);
			this.navLinks.each(function(i,elm) {
				jQuery(this).click(
					function() {
						$intro.moveNext(i);
					}
				);
			})
			this.timer = setTimeout($intro.animate, this.options.animationInterval*1000);
		}
	},
	moveNext : function(i) {
		//alert(i);
		this.showing = false;
		this.currentTextIndex = i;
		this._animate(true);
		//this._updateIndex();
		//this._animate();
	},
	animate : function() {
		$intro._animate();
	},
	_resetLinks : function() {
		this.navLinks.each(function() {
			this.className = "intro-button";
		});
	},
	_animate : function(noChange) {
		this.stop();
		var time = this.options.animationInterval;
		if (!this.showing) {
			time = this.options.displayInterval;
			this._resetLinks();
			//if (this.currenTextIndex >= 0) this.navLinks[this.currenTextIndex].className = "intro-button";
			if (!noChange) {
				this.currentTextIndex++;
				if (this.currentTextIndex >= this.text.length) this.currentTextIndex = 0;
			}
			//console.log("text = "+this.text[this.currentTextIndex]+' '+this.currentTextIndex)
			
			
			this.navLinks[this.currentTextIndex].className = "intro-button-on";
			
			this.textbox.html(this.text[this.currentTextIndex]);
			this.overlay.css("left", this.currentWidth).animate({left:0},500, "easeInOut");
			this.textbox.css("left", -this.currentWidth).animate({left:0},500, "easeInOut");
		} else {
			this.overlay.animate({left:-this.currentWidth},500, "easeInOut");
			this.textbox.animate({left:this.currentWidth},500, "easeInOut");
			this.currentImageIndex++;
			if (this.currentImageIndex >= this.images.length) this.currentImageIndex = 0;
			setTimeout(function() {$intro._showCurrentImage()}, 500)
			//this.currentImage.css("display", "block");
			//this.currentImage.fadeIn(this.options.fadeDuration * 1000);
			//this.images[]
		}
		this.showing = !this.showing;
		//console.log("time = "+time)
		this.timer = setTimeout($intro.animate, time*1000);
	},
	_showCurrentImage: function() {
		this.currentImage.fadeOut(this.options.fadeDuration * 1000);
		//console.log('currImageIndex = '+)
		this.currentImage = jQuery(this.images[this.currentImageIndex]);
		this.currentImage.fadeIn(this.options.fadeDuration * 1000);
	},
	stop : function() {
		if (this.timer) clearTimeout(this.timer);
	},
	addImage : function() {
		var img = jQuery(this)
		if ($intro.currentImage == null) {
			$intro.currentImage = img;
			img.css({display:"block", opacity:1});
		} else {
			//img.css("opacity",0);
		}
		$intro.images.push(this);
	}
}