// JavaScript Document

// Controls the speed at which the background scrolls.

$(document).ready(init);
var spot = 4000;
var t;
var imgs = new Array;
var imgn = 0;

function init() {
	makespotlight();
	adscript();
	imgfix();
}

function makespotlight() {
	// $('.callout').hover(function(){$(this).toggleClass('hover')});
	$('#spotlight-img img').fadeTo(0,0);
	x = 0;
	$('#spotlight-img img').each(function(){
		imgs[x] = this;
		x++;
	});
	imgs.sort(function(){ return Math.random()-0.5; });
	$(imgs[imgn]).fadeTo(300,1);
	$('#spotlight-name').html($(imgs[imgn]).attr('alt'));
	t = setTimeout('spotlight()',spot);
}

function spotlight() {
	$(imgs[imgn]).stop().fadeTo(300,0);
	imgn++;
	if (imgn == imgs.length) {
		imgn = 0;
	}
	$(imgs[imgn]).stop().fadeTo(300,1);
	$('#spotlight-name').html($(imgs[imgn]).attr('alt'));
	t = setTimeout('spotlight()',spot);
}

function adscript() {
	$('.ad a').each(function(){
		adimg = $(this).find('img');
		if (adimg.attr('alt') != '') {
			adname = adimg.attr('alt');
		} else if (adimg.attr('alt') != '') {
			adname = adimg.attr('title');
		} else {
			adname = adimg.attr('src');
		}
		_gaq.push(['_trackEvent', 'Ads', 'Impression', adname]);
		$(this).click(function(){
			_gaq.push(['_trackEvent', 'Ads', 'Click', adname]);
		});
	});
}

function imgfix() {
	$('img').each(function(){
		if ($(this).width() >= 610) {
			var img = $(this);
			$("<img/>")
				.attr("src", $(img).attr("src"))
				.load(function() {
					if (this.width > img.width()) {
						img.height(img.height()*img.width() / this.width);
					}
				});
		}
	});
}
