//rotateSpeed variable is defined on the page.
var autorotate = true;

jQuery(function ($) {

	var carousel    = $('#home-carousel'),
		home_sets   = carousel.find('.set'),
		footer      = $('#footer'),
		changing    = false;

	$('.headline-banner').browserOffsetFix('marginLeft', 1);

	// Make entire TDIH module hover and clickable.
    $('div.this-day').hover(function() {
    	$(this).addClass('this-day-hover');
    }, function() {
    	$(this).removeClass('this-day-hover');
    }).click(function() {
        window.location = $(this).find('a:first').attr('href');
        return false;
    });

	if (home_sets.length > 0) {

		// Reveal items hidden by default.
		home_sets.show();

		carousel.css('overflow', 'hidden')
		        .before('<div class="button button-prev"><a href="#">Prev</a></div>')
		        .after('<div class="button button-next"><a href="#">Next</a></div>');

		carousel.cycle({
			fx      : 'scrollHorz',
			timeout : rotateSpeed,
			speed   : 1500,
            pause   : 1,
			before  : function (currSlideElement, nextSlideElement, options, forwardFlag) {
				carousel.css('overflow', 'hidden');
				changing = true;
			},
			after   : function (currSlideElement, nextSlideElement, options, forwardFlag) {

				var next_headline_graphic = $(".headline-graphic img",nextSlideElement);
                var sponsorHide = $(".sponsorHide",nextSlideElement);

                if (sponsorHide.length > 0)
                    $('#sponsor-wrapper').hide();
                else
                    $('#sponsor-wrapper').show();
				carousel.css('overflow','visible');

				if (next_headline_graphic.length > 0) {
					if ($.browser.msie) {
						if ($.browser.version < 7) {
							next_headline_graphic.ifixpng();
						}
						next_headline_graphic.show();
					} else {
						next_headline_graphic.fadeIn(250);
					}
				}
				changing = false;
			}
		});

        

		$('#home-carousel-wrap .button-prev a').click(function (e) {
			e.preventDefault();

			carousel.find('.headline-graphic img').hide();

			if (!changing) {
				carousel.cycle.prev(carousel.data('cycle.opts'));
				//bg_carousel.cycle.prev(bg_carousel.data('cycle.opts'));

				// Delay omniture by 3/4 a second to improve animation.
				setTimeout(function(){
					HISTORY.trackClick(
						'HIS:HomePage:Placement:Hero:LeftSlideButton',
						'HIS:HomePage:Content:Hero:LeftSlideButton'
					);
				}, 750);
			}

            carousel.cycle('pause');
            //bg_carousel.cycle('pause');
            autorotate = false;
		});

		$('#home-carousel-wrap .button-next a').click(function (e) {
			e.preventDefault();

			carousel.find('.headline-graphic img').hide();

			if (!changing) {
				carousel.cycle.next(carousel.data('cycle.opts'));

				// Delay omniture by 3/4 a second to improve animation.
				setTimeout(function(){
					HISTORY.trackClick(
						'HIS:HomePage:Placement:Hero:RightSlideButton',
						'HIS:HomePage:Content:Hero:RightSlideButton'
					);
				}, 750);

			}
            carousel.cycle('pause');
            autorotate = false;
		});
	}

	$('#features > .tdih-picks-wrap, #features > .extras').matchColumns();
	$('div.time-zone ul').ulSelectList();

	/*
		===============================================
		= Omniture Implementation for Global Elements =
		===============================================
	*/
	$('#header li a').live('click', function () {
		var a, b, obj, par;
		obj = $(this);
		par = obj.parent();
		if (par.hasClass('today')) {
			a = 'HIS:HomePage:Placement:Header:Primary';
			b = 'HIS:HomePage:Content:Header:Today';
		} else if (par.hasClass('topic')) {
			a = 'HIS:HomePage:Placement:Header:Primary';
			b = 'HIS:HomePage:Content:Header:HistoryByTopic';
		} else if (par.hasClass('shows')) {
			a = 'HIS:HomePage:Placement:Header:Primary';
			b = 'HIS:HomePage:Content:Header:TVShows';
        } else if (par.hasClass('tdih')) {
			a = 'HIS:HomePage:Placement:Header:Primary';
			b = 'HIS:HomePage:Content:Header:ThisDayInHistory ';
		} else if (par.hasClass('video')) {
			a = 'HIS:HomePage:Placement:Header:Secondary';
			b = 'HIS:HomePage:Content:Header:Videos';
		} else if (par.hasClass('games')) {
			a = 'HIS:HomePage:Placement:Header:Secondary';
			b = 'HIS:HomePage:Content:Header:Games';
		} else if (par.hasClass('shop')) {
			a = 'HIS:HomePage:Placement:Header:Secondary';
			b = 'HIS:HomePage:Content:Header:Shop';
		}
		HISTORY.trackClick(a, b);
	});

	$('#search-form').submit(function () {
		HISTORY.trackClick(
			'HIS:HomePage:Placement:Header:Search',
			'HIS:HomePage:Content:Header:Search'
		);
	});

	$('ul.kickers a', footer).live('click', function () {
		var index, strong, span;

		index  = $('ul.kickers li', footer).index($(this).parent()) + 1;
		strong = $('strong', this).text();
		span   = $('span.text > span', this).text();

		HISTORY.trackClick(
			'HIS:HomePage:Placement:MoreHistory:Postition' + index,
			'HIS:HomePage:Content:Footer:' + strong + ':' + span
		);
	});

	$('div.linkset a', footer).live('click', function () {
		var group, text;

		group = $(this).parents('div.linkset').attr('id').replace('linkset', '');
		text  = $(this).text();

		HISTORY.trackClick(
			'HIS:HomePage:Placement:Footer:' + group,
			'HIS:HomePage:Content:Footer:' + text
		);

	});

});
