var highlights_interval = 7000; // 7 seconds

var current_highlight = -1;
var highlights_timer = 0;
var highlights_timer_bar = 0;
var highlight_time = 0;

function highlights_next()
{
	next_highlight = current_highlight + 1;
	if (next_highlight >= highlights.length)
	{
		next_highlight = 0;
	}
	show_highlight(next_highlight);
	return false;
}

function highlights_prev()
{
	prev_highlight = current_highlight - 1;
	if (prev_highlight < 0)
	{
		prev_highlight = highlights.length - 1;
	}
	show_highlight(prev_highlight);
	return false;
}

function get_image_path(image)
{
	if (image.substr(0, 7) != 'http://')
	{
		image = '/images/highlight/images/' + image;
	}
	return image;
}

function show_highlight(i)
{
	highlight_time = 0;
	if (current_highlight >= 0)
	{
		$('.highlights-box img.bottom-img').attr('src', get_image_path(highlights[current_highlight].image));
	}
	$('.highlights-box img.top-img').hide();
	if (highlights_timer !== 0)
	{
		clearTimeout(highlights_timer);
		highlights_timer = setTimeout('highlights_next()', highlights_interval);
	}
	$('.highlight-indicator a').removeClass('active');
	$('.highlights-menu li:nth-child(' + (i + 3) + ') a').addClass('active');
	$('.highlights-box h4').html(highlights[i].heading);
	$('.highlights-box img.top-img').attr('src', get_image_path(highlights[i].image)).fadeIn(500);
	if (highlights[i].image_link != null)
	{
		$('.highlights-box img.top-img').css({ cursor: 'pointer' }).unbind('click').click(function () { 

			is_video = /\.(f4v|mp4|mov)$/
			
			if (is_video.test(highlights[i].image_link)) {
				
				// set defaults...
				image_player = 'flv';
				image_width = 320;
				image_height = 240;
				video_file = highlights[i].image_link;
				use_cc_files = true;
				
				// iPad...
			   if(navigator.userAgent.match(/iPad/i)) { 
					image_player = 'qt';
					use_cc_files = false;
			   } 
				
				// iPhone...
			   if(navigator.userAgent.match(/iPhone/i)) { 
					image_player = 'qt';
					use_cc_files = false;
					
					// don't use Shadowbox...
					location.href = highlights[i].image_link;
					return false;
			   } 
					
				// Android...
			   if(navigator.userAgent.match(/android/i)) { 
					image_player = 'flv'; // force use of Flash player
					
					// don't use Shadowbox...
					location.href = highlights[i].image_link;
					return false;
			   } 
				
				// "NetSafe Clips - Cell Phone & Photos" is a different size...
				if(highlights[i].image_link == 'http://eq.uen.org/emedia/items/5eb04aff-d716-5621-8f75-46716875b3de/1/1-CellPhones_Photos_iPhone.mp4') {
					image_width = 640;
					image_height = 360;
				}
				
				// Closed Captioned videos need extra variables...
				if(use_cc_files) {

					switch (video_file)
					{
						// "What is the Internet?"
						case 'http://eq.uen.org/emedia/items/0b77d543-e0ba-4d79-e710-fff71e5c2a80/1/uen1-final_high_QTCC.mp4':
							video_file = 'http://eq.uen.org/emedia/items/0b77d543-e0ba-4d79-e710-fff71e5c2a80/1/uen1-final_high_QTCC.mp4&captions.file=caption_files/uen1-final_high_Out.xml&plugins=captions-1&captions.state=false&autostart=true';
							break;
						
						// "What is Personal Information?"
						case 'http://eq.uen.org/emedia/items/c90d746d-cf5c-a7db-d95e-338bc493a9cb/1/uen2-final_high_QTCC.mp4':
							video_file = 'http://eq.uen.org/emedia/items/c90d746d-cf5c-a7db-d95e-338bc493a9cb/1/uen2-final_high_QTCC.mp4&captions.file=caption_files/uen2-final_high_Out.xml&plugins=captions-1&captions.state=false&autostart=true';
							
							break;
							
						// "Tell an Adult"
						case 'http://eq.uen.org/emedia/items/48d6aa51-8bd5-85d4-998e-d03c161fef2a/1/uen3-final_high_QTCC.mp4':
							video_file = 'http://eq.uen.org/emedia/items/48d6aa51-8bd5-85d4-998e-d03c161fef2a/1/uen3-final_high_QTCC.mp4&captions.file=caption_files/uen3-final_high_Out.xml&plugins=captions-1&captions.state=false&autostart=true';
							break;
							
						// "Be Kind Online"
						case 'http://eq.uen.org/emedia/items/672796eb-1ba4-5f35-bc69-9fab854bd74e/1/uen4-final_high_QTCC.mp4':
							video_file = 'http://eq.uen.org/emedia/items/672796eb-1ba4-5f35-bc69-9fab854bd74e/1/uen4-final_high_QTCC.mp4&captions.file=caption_files/uen4-final_high_Out.xml&plugins=captions-1&captions.state=false&autostart=true';
							break;
							
						// "Cyber Bullies are No Fun"
						case 'http://eq.uen.org/emedia/items/f9def275-e58b-e27b-36e4-c464f980b51e/1/uen5-final-02_high_QTCC.mp4':
							video_file = 'http://eq.uen.org/emedia/items/f9def275-e58b-e27b-36e4-c464f980b51e/1/uen5-final-02_high_QTCC.mp4&captions.file=caption_files/uen5-final-02_Out.xml&plugins=captions-1&captions.state=false&autostart=true';
							break;
							
						// "What is Personal Information?"
						case 'http://eq.uen.org/emedia/items/8b92e276-5fd9-4b3c-b1b5-b02dba77ecdc/1/UEN6-fc-03_high_QTCC.mp4':
							video_file = 'http://eq.uen.org/emedia/items/8b92e276-5fd9-4b3c-b1b5-b02dba77ecdc/1/UEN6-fc-03_high_QTCC.mp4&captions.file=caption_files/UEN6-fc-03_high_Out.xml&plugins=captions-1&captions.state=false&autostart=true';
							break;
							
						// "Online 'Friends'"
						case 'http://eq.uen.org/emedia/items/b01fdd48-d39a-4f87-b07d-87200ef66de3/1/UEN7-fc-03_high.mp4':
							video_file = 'http://eq.uen.org/emedia/items/b01fdd48-d39a-4f87-b07d-87200ef66de3/1/UEN7-fc-03_high.mp4&captions.file=caption_files/UEN7-fc-03_high_Out.xml&plugins=captions-1&captions.state=false&autostart=true';
							break;
							
						// "Talk to an Adult You Trust"
						case 'http://eq.uen.org/emedia/items/972413c7-764c-4434-ac2e-2164ff327f08/1/UEN8-fc-02_high.mp4':
							video_file = 'http://eq.uen.org/emedia/items/972413c7-764c-4434-ac2e-2164ff327f08/1/UEN8-fc-02_high.mp4&captions.file=caption_files/UEN8-fc-02_high_Out.xml&plugins=captions-1&captions.state=false&autostart=true';
							break;
							
						// "Posting Pictures Online"
						case 'http://eq.uen.org/emedia/items/9b25c8e7-deea-4b7c-b3ef-5f9b90d447e8/1/UEN9-fc-03_high.mp4':
							video_file = 'http://eq.uen.org/emedia/items/9b25c8e7-deea-4b7c-b3ef-5f9b90d447e8/1/UEN9-fc-03_high.mp4&captions.file=caption_files/UEN9-fc-03_Out.xml&plugins=captions-1&captions.state=false&autostart=true';
							break;
							
						// "Information about NetSafe Utah"
						case 'http://eq.uen.org/emedia/items/ceb0f137-3041-4b52-843b-3d9e5911dfec/1/UEN15-fc-02_qt-high_QTCC.mp4':
							video_file = 'http://eq.uen.org/emedia/items/ceb0f137-3041-4b52-843b-3d9e5911dfec/1/UEN15-fc-02_qt-high_QTCC.mp4&captions.file=caption_files/UEN15-fc-02_qt-high_Out.xml&plugins=captions-1&captions.state=false&autostart=true';
							break;
						
					}
				
				}
				
				Shadowbox.open({ 
					player: image_player, 
					content: video_file, 
					width: image_width, 
					height: image_height
				});
				
				
			}
			
			else {
				document.location = highlights[i].image_link
			}
			
			
		});
	}
	else
	{
		$('.highlights-box img.top-img').css({ cursor: '' }).unbind('click');
	}
	$('.highlights-box .highlight-text').html(highlights[i].text);
	current_highlight = i;
	return false;
}

function update_time_bar()
{
	$('.highlights-timer div').css({ width: ((highlight_time/highlights_interval)*100) + '%' });
	highlight_time = highlight_time + 50;
}

function highlights_play()
{
	highlights_next();
	//$('.highlights-timer').show();
	$('.highlight-play a').css({ backgroundImage: 'url(/images/highlight/btn-pause.gif)' })
	highlights_timer = setTimeout('highlights_next()', highlights_interval);
	//update_time_bar();
	//highlights_timer_bar = setInterval('update_time_bar()', 50);
	return false;
}

function highlights_pause()
{
	//$('.highlights-timer').hide();
	highlight_time = 0;
	//clearInterval(highlights_timer_bar);
	$('.highlight-play a').css({ backgroundImage: 'url(/images/highlight/btn-play.gif)' })
	clearTimeout(highlights_timer);
	highlights_timer = 0;
	return false;
}

$(document).ready(function()
{	
	// get first box id
	code = $('.highlights-box:first').attr('id');
	
	// get highlights
	$.getJSON('/tools/highlights_data.php', 'code=' + code, function (data)
	{
		highlights = data;
		
		// create slide indicators and preload images
		insert_after = $('.highlight-prev');
		for (i=0; i<highlights.length; i++)
		{
			insert_after.after('<li class="highlight-indicator"><a href=""><span>' + (i + 1) + '</span></a></li>');
			insert_after = $('.highlight-indicator:last');
			$('<img>').attr('src', get_image_path(highlights[i].image));
		}
		
		// set event handlers for menu
		$('.highlight-prev a').click(highlights_prev);
		$('.highlight-next a').click(highlights_next);
		$('.highlight-play a').toggle(highlights_pause, highlights_play);
		$('.highlight-indicator a').click(function() { show_highlight(($(this).find('span').html() - 1)); return false; });
		
		// hide loader
		$('.highlights-loading').fadeOut(100);
		$('.highlights-menu').fadeIn(100);
		highlights_play();
	});	
});
