jQuery(document).ready(function() {
	cdc.util.addCorners('.c50-pilot','use divs');

	// Set Carousel Variables
	var i = 0;
	var currentframe = 0;
	var rotating = true;
	var firsttime = "";
	var frameHeight = new Array();
	var maxHeight = 0;
	// Check for direct linking to a specific carousel frame
	var directlink = false;
	var url= window.location.href;
	if (url.indexOf('carousel=1') > 0) { i = 0; currentframe = 0; directlink = true; }
	if (url.indexOf('carousel=2') > 0) { i = 1; currentframe = 1; directlink = true; }
	if (url.indexOf('carousel=3') > 0) { i = 2; currentframe = 2; directlink = true; }
	var tttext = ["","",""];  // Support for only three carousel frames
	var frames = 0;
	trimFrames();

	// Populate Navigation Control Strip and Automate Play Buttons
	for (var m = 0; m < frames; m++) {
		// Add Lightbox Image to CTA Button
		var lightboxButton = jQuery('.c50-pilot .frame:eq(' + m + ') .c50-text p.cta-link a.cta');
		if (lightboxButton.length > 0) {
			if (lightboxButton.attr('rel').length > 0) {
				var buttonText = lightboxButton.find('b');
				jQuery('<span class="lightbox"></span>').appendTo(buttonText);
				}
			}
		// Add Play Button to Image
		var lightboxImage = jQuery('.c50-pilot .frame:eq(' + m + ') .c50-image a');
		if (lightboxImage.length > 0) {
			if (lightboxImage.attr('rel').length > 0) {
				lightboxImage.addClass('lightbox');
				jQuery('<span rel="' + lightboxImage.attr('rel') + '"></span>').appendTo(lightboxImage);
				}
			}
		// Pull Thumbnail Text for Navigation Bar
		var ttsource = jQuery('.c50-pilot .frame:eq(' + m + ') .c50-thumbnail').text();
		tttext[m] = ttsource;
		// Update metrics linkText for navigation chicklets
		cdc.util.addMetricsRule('a#thumbnail' + m + '[href]', { linktext: ttsource });
		// Drop Thumbnail Source DIV from DOM
		jQuery('.c50-pilot .frame:eq(' + m + ') .c50-thumbnail').remove();
		frameHeight[m] = jQuery('.c50-pilot .frame:eq(' + m + ')').height();
		if (maxHeight < frameHeight[m]) {
			maxHeight = frameHeight[m];
			}
		}

	// Set all frames to the same height in case of blowout
	for (var q = 0; q < frames; q++) {
		jQuery('.c50-pilot .frame:eq(' + q + ')').css('height',maxHeight);
		}

	// START Code to Fix IE Video Bug
	var ob2 = null;
	function playerReady(obj) { ob2 = document.getElementById(obj.id); }
	function stopVideo(ob1) { if (jQuery.browser.msie) {ob1.sendEvent('STOP');}}
	// END Code to Fix IE Video Bug

	// Carousel Frame Function
	jQuery.fn.pager = function(clas, options) {
		var settings = {
			navId: 'nav',
			navClass: 'nav',
			navAttach: 'append',
			highlightClass: 'highlight',
			prevText: '&laquo;',
			nextText: '&raquo;',
			linkText: null,
			linkWrap: null,  // Not used?
			height: null  // Not used?
			}
		if (options) jQuery.extend(settings, options);
		var timer = "";
		var timercheck = "";
		var spinnums = 0;

		return this.each( function() {
			var me = jQuery(this);
			var size;
			var navid = '#' + settings.navId;
			fnums = frames;
			iTime = 5000;

			function init() {
				size = jQuery(clas, me).not(navid).length;
				if (size > 1) {
					makeNav();
					show();
					}

				// What is linkWrap?
				if (settings.linkWrap != null) {
					linkWrap();
					}
				}

			function makeNav() {
				var str = "";
				var j = 0;
				for (var n = 0; n < size; n++) {
					j = n + 1;
					str += '<a href="javascript:void(0);" rel="' + n + '" id="thumbnail' + n + '" title="' + tttext[n] + '">';
					str += (settings.linkText == null) ? j : settings.linkText[n];
					str += '</a>';
					}
				jQuery('#thumbnails').append(str);
				}

			function show() {
				if (rotating) jQuery(me).find(clas).not(navid).hide();
				var showme = jQuery(me).find(clas).not(navid).get(i);
				if (rotating) jQuery(showme).fadeIn(800);
				highlight();

				/* ======== CODE TO FIX IE VIDEO BUG ======== */
				obj = "videoplayer" + currentframe;
				obj = document.getElementById(obj);
				if (obj != null) {
					if (firsttime == "") { firsttime = "true"; }
					else { stopVideo(obj); }
					}
				if (i == 0) {currentframe = (frames - 1);}
				else {currentframe = (i - 1);}
				/* ======== END CODE TO FIX IE VIDEO BUG ======== */

				var static = jQuery('.c50-pilot').hasClass('static');
				if ((static == false) && (timercheck == "")) {
					timer = setTimeout(function() { show() }, iTime);
					jQuery(".c50-pilot").click(function() { clearTimeout(timer); });
					if (directlink) { clearTimeout(timer); }
					jQuery("#videoplayer0").mousedown(function() { clearTimeout(timer); });
					jQuery("#videoplayer1").mousedown(function() { clearTimeout(timer); });
					jQuery("#videoplayer2").mousedown(function() { clearTimeout(timer); });
					spinstops = fnums*2;
					if (spinnums >= spinstops){ clearTimeout(timer); }
					spinnums++;
					}
				else {
					clearTimeout(timer);
					}
				if ( i >= fnums - 1 ){i = 0;}
				else {i++;}
				}

			function highlight() {
				jQuery(me).find(navid).find('a').removeClass(settings.highlightClass);
				var shownav = jQuery(me).find(navid).find('a').get(i);
				jQuery(shownav).addClass(settings.highlightClass);
				}

			function linkWrap() {
				jQuery(me).find(navid).find("a").wrap(settings.linkWrap);
				}

			init();
			jQuery(this).find(navid).find("a").click(function() {
				var z = jQuery(this).attr('rel');
				i = z;
				timercheck = "cancel";
				clearTimeout(timer);
				show();
				return false;
				});
			});
		}

	// Trim off any frames over the maximum of 3
	function trimFrames() {
		frames = jQuery('.c50-pilot .frame').length;
		if (frames > 3) {
			for (x = frames; x > 3; x--) {
				jQuery('.c50-pilot .frame:nth-child(' + x + ')').remove();
				frames--;
				}
			}
		}

	// Create thumnail navigation
	var thumbnails = new Array();
	for (t = 0; t <= frames - 1; t++) {
		thumbnails[t] = '';
		}
	jQuery('.c50-pilot').pager('.frame', {
		navId: 'thumbnails',
		prevText: '',
		nextText: '',
		navAttach: 'append',
		linkText: thumbnails,
		linkWrap: ''
		})

	if (tttext[0] != "") {
		jQuery('.c50-pilot #thumbnails a').cluetip({
			splitTitle: '|',
			showTitle: false ,
			cursor: 'pointer',
			width: '120px',
			cluetipClass: 'dcartip',
			dropShadow: false,
			hoverIntent: false,
			positionBy: 'fixed',
			topOffset: '-45px',
			leftOffset: '0'
			});
		}
	}); /* END jQuery(document).ready */

