
// Fix IE's lack of indexOf
if (!Array.prototype.indexOf)
{
	Array.prototype.indexOf = function(elt /*, from*/)
	{
		var len = this.length;
		
		var from = Number(arguments[1]) || 0;
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0)
			from += len;
		
		for (; from < len; from++)
		{
			if (from in this && this[from] === elt)
				return from;
		}
		
		return -1;
	};
}

var cleared = new Array();

function clearBox(id) {
	if (!(cleared.indexOf(id) != -1)) {
		cleared.push(id);
		$(id).val('');
	}
}

var rotatorEndStretchyness = 16;
var rotatorSpeed = 300;
var currentRotatorPosition = Array();

var resizeCount = 11;
function resizeLocationSearchContent(e) {
	var maxHeight = 0;
	$('#zoneContent .location-search .location-search-content-inner').each(function() {
		var height = $(this).css('height');
		height = parseInt(height.substr(0, height.length-2));
		if (height > maxHeight) {
			maxHeight = height;
		}
	});
	$('#zoneContent .location-search .location-search-content').css('height', maxHeight + 'px');
	resizeCount = resizeCount - 1;
	if (resizeCount > 0) {
		setTimeout("resizeLocationSearchContent()", 200);
	}
}

$(document).ready(function() {
	// Setup the search box.
	$('#plc_lt_zoneTop_cmssearchbox_txtWord').val("Site search");
	$('#plc_lt_zoneTop_cmssearchbox_txtWord').bind('focus', 
		function() { clearBox("#plc_lt_zoneTop_cmssearchbox_txtWord"); });
	
	// $('img[src=""]').remove(); // This should work, but IE doesn't like it.
	if ($('#header-image img').attr('src') == "") {
		$('#header-image img').remove();
	}
	
	//$('#zoneContent > .content-wrapper > .content-inner a[href^=http://], #zoneContent > .content-wrapper > .content-inner a[href^=https://]').addClass('external');
	
	// Hide empty intro areas.
	$('p.intro').each(function() {if ($(this).text() == "") {$(this).css('display', 'none');}});
	
	// Setup the accordion panels on the FAQ pages
	$('.accordion .head').click(function() {
		$(this).removeClass('expanded');
		$(this).parent().children('.open').not(this).not('.expanded').removeClass('open').next().toggle('slow');
		$(this).toggleClass('open').next().toggle('slow');
		return false;
	}).prepend('<div class="arrow"></div>').not('.expanded').next().hide();
	
	// Make sure the two location search columns are the same height
	//*
	resizeLocationSearchContent();
	setTimeout("resizeLocationSearchContent()", 200);
	//*/
	
	// Setup the video rotators
	$('.rotator').each(function() {
		var rot = $(this);
		
		if (rot.children('.rotator-inner').children('.item').length < 2) {
			rot.css('display', 'none');
			return;
		}
		
		rot.children('.rotator-inner').each(function() {
			var currentItem = 0;
			
			// Make sure there is an active element in the rotator
			if ($(this).children('.item.active').length < 1) {
				$(this).children('.item:first').addClass('active');
			}
			
			$(this).children('.item').each(function(i) {
				this.style.position = "absolute";
				this.style.top = "0px";
				if ($(this).hasClass('active')) {
					currentItem = i;
				}
			});
			//if (currentItem == $(this).children('.item').length - 1) { currentItem--; }
			$(this).children('.item').each(function(i) {
				var width = $(this).css('width');
				width = parseInt(width.substr(0, width.length-2));
				this.style.left = ((i - currentItem) * width) + "px";
			});
			currentRotatorPosition[this] = currentItem;
		});
		rot.prepend('<a href="#" class="navigation previous"></a>').append('<a href="#" class="navigation next"></a>');
	});
	
	$('.rotator .previous').live('click', function() {
		var base = $(this).parent().children('.rotator-inner');
		var domElement = base.get(0);
		var width = base.children('.item:eq(0)').css('width');
		var left = base.children('.item:last').css('left');
		left = parseInt(left.substr(0, left.length-2));
		if (currentRotatorPosition[domElement] != 0) {
			currentRotatorPosition[domElement] = currentRotatorPosition[domElement] - 1;
			base.animate({backgroundPosition: '+='+width+' 0'}, rotatorSpeed).children('.item').animate({left: '+='+width}, rotatorSpeed);
		} else {
			base.animate({backgroundPosition: '+=' + rotatorEndStretchyness + 'px 0'}, rotatorSpeed/3).animate({backgroundPosition: '-=' + rotatorEndStretchyness + 'px 0'}, rotatorSpeed*2/3)
				.children('.item').animate({left: '+=' + rotatorEndStretchyness + 'px'}, rotatorSpeed/3).animate({left: '-=' + rotatorEndStretchyness + 'px'}, rotatorSpeed*2/3);
		}
		return false;
	});
	
	$('.rotator .next').live('click', function() {
		var base = $(this).parent().children('.rotator-inner');
		var domElement = base.get(0);
		var width = base.children('.item:eq(0)').css('width');
		var left = base.children('.item:first').css('left');
		left = parseInt(left.substr(0, left.length-2));
		if (currentRotatorPosition[domElement] != base.children('.item').length-1) {
			currentRotatorPosition[domElement] = currentRotatorPosition[domElement] + 1;
			base.animate({backgroundPosition: '-='+width+' 0'}, rotatorSpeed).children('.item').animate({left: '-='+width}, rotatorSpeed);
		} else {
			base.animate({backgroundPosition: '-=' + rotatorEndStretchyness + 'px 0'}, rotatorSpeed/3).animate({backgroundPosition: '+=' + rotatorEndStretchyness + 'px 0'}, rotatorSpeed*2/3)
				.children('.item').animate({left: '-=' + rotatorEndStretchyness + 'px'}, rotatorSpeed/3).animate({left: '+=' + rotatorEndStretchyness + 'px'}, rotatorSpeed*2/3);
		}
		return false;
	});
});

