$(document).ready(function(){
	// Page load styles
	$('#print-thumbs-container').addClass('hidden');
	$('#tv-tab-link').addClass('active');
	
	// First Video Rest State Click
	$('#first-video-rest-state').click(function(event) {
		$(this).hide();
		playFirstMovie();
		
		event.preventDefault();
	});
		
	// TV Tab Click State
	$('#tv-tab-link').click(function(event) {
		$('#print-tab-link').removeClass('active');
		$(this).addClass('active');
		$('#tabs-container').removeClass('print');
		$('#print-thumbs-container').addClass('hidden');
		$('#tv-thumbs-container').show();
		
		event.preventDefault();
	});
	
	// Print Tab Click State
	$('#print-tab-link').click(function(event) {
		$('#tv-tab-link').removeClass('active');
		$(this).addClass('active');
		$('#tabs-container').addClass('print');
		$('#tv-thumbs-container').hide();
		$('#print-thumbs-container').removeClass('hidden');
		
		event.preventDefault();
	});	
			
	// TV Thumbs Click State
	$('#tv-carousel a').click(function(event) {
		scroll(0, 0);
		$('#first-video-rest-state').hide();
		$('#tv-carousel a, #print-carousel a').removeClass('active');
		$(this).addClass('active');
		var movieLocation = buildMovieLocation($(this).parent().attr("class"));
		var textLine1 = $("span.text-line-1", $(this)).html();
		var textLine2 = $("span.text-line-2", $(this)).html();
		var textLine3 = $("span.text-line-3", $(this)).html();
		var textBeforePlay = $("span.text-before-play", $(this)).html();
		var textBeforeReplay = $("span.text-before-replay", $(this)).html();
		thisMovie("amp").loadFLV( movieLocation, textLine1, textLine2, textLine3, textBeforePlay, textBeforeReplay, "true", buildMovieStillLocation($(this).parent().attr("class")));
		var moreInfoURL = $(this).parent().find('span.moreInfoURL').html();
		$('#moreInfoLink').empty().html(moreInfoURL);	
		
		event.preventDefault();
	});

	// Print Thumbs Click State
	$('#print-carousel a').click(function(event) {	
		scroll(0, 0);
		$('#first-video-rest-state').hide();
		$('#tv-carousel a, #print-carousel a').removeClass('active');
		$(this).addClass('active');
		var printLocation = buildPrintLocation($(this).parent().attr("class"));
		var printLocationLarge = buildPrintLocationLarge($(this).parent().attr("class"));
		thisMovie("amp").loadImage( printLocation, printLocationLarge );
		var moreInfoURL = $(this).parent().find('span.moreInfoURL').html();
		$('#moreInfoLink').empty().html(moreInfoURL);
		
		event.preventDefault();
	});	
	
	// Mouse Pointer State
	$('#tabs-container div, #tv-carousel li, #print-carousel li, #first-video-rest-state').hover(function() {
		$(this).css('cursor', 'pointer');
	}, function () {
		 $(this).css('cursor', 'auto');
	});
	
	// Let Flash know that JS is ready
	pageInit();
	
	// If No Flash
	if ($("#noflash").hasClass("hide") == false) {
		$("#tabs-container, #tv-thumbs-container, #print-thumbs-container").hide();
	};
	
	// Hide Carousel Arrows
	var carouselItemsTV = $("#tv-carousel ul li").length;
	var carouselItemsPrint = $("#print-carousel ul li").length;
	if (carouselItemsTV < 5){
		$("#tv-thumbs-container .left-button, #tv-thumbs-container .right-button").hide();
	}
	if (carouselItemsPrint < 5){
		$("#print-thumbs-container .left-button, #print-thumbs-container .right-button").hide();
	}
	
});


// Movie Location based on Class name
function buildMovieLocation(movieClass) {
	return "/flv/personal/visa_brings_you/advertising/" + movieClass + ".flv";
};

function buildMovieStillLocation(movieClass) {
	return "/img/other/personal/visa_brings_you/advertising/" + movieClass + ".jpg";
};

// Print Location based on Class name
function buildPrintLocation(printClass) {
	return "/img/other/personal/visa_brings_you/advertising/" + printClass + ".jpg";
};

// Print Location based on Class name for Large Image
function buildPrintLocationLarge(printClass) {
	return "/img/other/personal/visa_brings_you/advertising/" + printClass + "-lg.jpg";
};

// Advertising Media Player (AMP)
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};

// ------- functions called by ActionScript -------
var jsReady = false;

// called to check if the page has initialized and JavaScript is available
function isReady() {
	return jsReady;
};

// called by the onload event of the <body> tag
function pageInit() {
	// Record that JavaScript is ready to go.
	jsReady = true;
};

// Close Full Screen
function flashCloseFullscreen(){
	this.focus();
};

// Play First Movie
function playFirstMovie(){
	$("#tv-carousel a:first").addClass("active");
	var movieLocation = buildMovieLocation($("#tv-carousel li:first").attr("class"));
	var textLine1 = $("span.text-line-1", $("#tv-carousel a:first")).html();
	var textLine2 = $("span.text-line-2", $("#tv-carousel a:first")).html();
	var textLine3 = $("span.text-line-3", $("#tv-carousel a:first")).html();
	var textBeforePlay = $("span.text-before-play", $("#tv-carousel a:first")).html();
	var textBeforeReplay = $("span.text-before-replay", $("#tv-carousel a:first")).html();
	thisMovie("amp").loadFLV( movieLocation, textLine1, textLine2, textLine3, textBeforePlay, textBeforeReplay, "true", buildMovieStillLocation($("#tv-carousel li:first").attr("class")));
};

// Play First Print
function flashReady(){
	/*
	$("#print-carousel a:first").addClass("active");
	var printLocation = buildPrintLocation($("#print-carousel li:first").attr("class"));
	var printLocationLarge = buildPrintLocationLarge($("#print-carousel li:first").attr("class"));
	thisMovie("amp").loadImage( printLocation, printLocationLarge );
	*/
	$('#first-video-rest-state').hide();
	playFirstMovie();
};

