var SLO_2 = [];
var SLO_3 = [];
var SLO_4 = [];
var SLO_5 = [];

var MED_2 = [];
var MED_3 = [];
var MED_4 = [];
var MED_5 = [];

var FST_2 = [];
var FST_3 = [];
var FST_4 = [];
var FST_5 = [];

var slow_dur = 4500;
var medi_dur = 3000;
var fast_dur = 1750;

var thumb_grid = [];
var surrounding_items = [];

var sti;

$(document).ready(function()
{
	//  setup a grid 2d array
	setupGrid();
	
	//  hide the gear nav
	$('#gear_sub_nav_ul').hide();

	//  tell flashInterface that we are ready
	flashInit();

	var i = 0;
	// ========================================
	// SLOW SPEED THUMBS
	// ========================================
	SLO_2 = $('.SLO__2');
	for(i = 0; i < SLO_2.length; i++)
	{
		setupCycle(SLO_2[i], 2, slow_dur);
	}

	SLO_3 = $('.SLO__3');
	for(i = 0; i < SLO_3.length; i++)
	{
		setupCycle(SLO_3[i], 3, slow_dur);
	}
	
	SLO_4 = $('.SLO__4');
	for(i = 0; i < SLO_4.length; i++)
	{
		setupCycle(SLO_4[i], 4, slow_dur);
	}
	
	SLO_5 = $('.SLO__5');
	for(i = 0; i < SLO_5.length; i++)
	{
		setupCycle(SLO_5[i], 5, slow_dur);
	}

	// ========================================
	// MEDIUM SPEED THUMBS
	// ========================================
	MED_2 = $('.MED__2');
	for(i = 0; i < MED_2.length; i++)
	{
		setupCycle(MED_2[i], 2, medi_dur);
	}

	MED_3 = $('.MED__3');
	for(i = 0; i < MED_3.length; i++)
	{
		setupCycle(MED_3[i], 3, medi_dur);
	}
	
	MED_4 = $('.MED__4');
	for(i = 0; i < MED_4.length; i++)
	{
		setupCycle(MED_4[i], 4, medi_dur);
	}
	
	MED_5 = $('.MED__5');
	for(i = 0; i < MED_5.length; i++)
	{
		setupCycle(MED_5[i], 5, medi_dur);
	}

	// ========================================
	// FAST SPEED THUMBS
	// ========================================
	FST_2 = $('.FST__2');
	for(i = 0; i < FST_2.length; i++)
	{
		setupCycle(FST_2[i], 2, fast_dur);
	}

	FST_3 = $('.FST__3');
	for(i = 0; i < FST_3.length; i++)
	{
		setupCycle(FST_3[i], 3, fast_dur);
	}
	
	FST_4 = $('.FST__4');
	for(i = 0; i < FST_4.length; i++)
	{
		setupCycle(FST_4[i], 4, fast_dur);
	}
	
	FST_5 = $('.FST__5');
	for(i = 0; i < FST_5.length; i++)
	{
		setupCycle(FST_5[i], 5, fast_dur);
	}

	// ========================================
	// HANDLE RED ROLLOVER IMAGE REPLACEMENT
	// ========================================
	$('.home_thumb').mouseover(function()
	{
		$(this).find('.video_marker').css({'backgroundPosition':'0 -23px'});
		$(this).find('.home_thumb_thb').css({'display':'none'});  // norm image
		$(this).find('.home_thumb_thb_red').css({'display':'block'});	//, 'clip': 'rect(0px, 117px, 72px, 0px)'});  // red image
		$(this).find('.hthb_roll_label').css({'display':'block'});  // text inside of image
	});
	
	$('.home_thumb').mouseout(function()
	{
		$(this).find('.video_marker').css({'backgroundPosition':''});
		$(this).find('.home_thumb_thb').css({'display':'block'});  // norm image
		$(this).find('.home_thumb_thb_red').css({'display':'none'});  // red image
		$(this).find('.hthb_roll_label').css({'display':'none'});  // text inside of image
	});
	
	$('.home_thumb').click(function()
	{
		findMe($(this).attr('id'));
		var link = $(this).find('a').get(0);
		(link.onclick) ? link.onclick() : window.location = link.href;
	});
	
	$('.home_thumb_full').mouseover(function()
	{
		$(this).find('.video_marker').css({'backgroundPosition':'0 -23px'});
		$(this).find('.home_thumb_thb').css({'display':'none'});  // norm image
		$(this).find('.home_thumb_thb_red').css({'display':'block'});	//, 'clip': 'rect(0px, 117px, 72px, 0px)'});  // red image
		$(this).find('.hthb_roll_label').css({'display':'block'});  // text inside of image
	});
	
	$('.home_thumb_full').mouseout(function()
	{
		$(this).find('.video_marker').css({'backgroundPosition':''});
		$(this).find('.home_thumb_thb').css({'display':'block'});  // norm image
		$(this).find('.home_thumb_thb_red').css({'display':'none'});  // red image
		$(this).find('.hthb_roll_label').css({'display':'none'});  // text inside of image
	});
	
	$('.home_thumb_full').click(function()
	{
		findMe($(this).attr('id'));
		var link = $(this).find('a').get(0);
		(link.onclick) ? link.onclick() : window.location = link.href;
	});
	
	//  init popup countdown
	setTimeout('delayedPopup()', 1500);
});

// ========================================
// layout the thumb grid rows and columns
// ========================================
function setupGrid()
{
	// thumb_grid
	var thbs = $('#thumb_rows_container').children();
	var row = [];	

	for(var i = 0; i < thbs.length; i++)
	{
		if(thbs[i].className == 'home_thumb_full')
		{
			var row = [];	
			for(var j = 0; j < 7; j++)
			{
				row.push(thbs[i].id);
			}
			thumb_grid.push(row);
			var row = [];
		}
		else
		{
			row.push(thbs[i].id);
			if(row.length == 7)
			{
				thumb_grid.push(row);
				var row = [];
			}
			else if(i == thbs.length - 1)
			{
				thumb_grid.push(row);
			}
		}
	}
}

// ========================================
//  turn surrounding members white on click
// ========================================
function findMe(a_id)
{
	//  just to make sure on thrash clicking
	clearTimeout(sti);
	showAgain(false);
	
	var curr_row = null;
	var prev_row = null;
	var next_row = null;
	var curr_col = null;
	var prev_col = null;
	var next_col = null;
	
	var full_thb = false;
	var regex = /full/;
	if(a_id.match(regex))
	{
		full_thb = true;
	}
	
	surrounding_items = [];
	
	for(var i = 0; i < thumb_grid.length; i++)
	{
		for(var j = 0; j < thumb_grid[i].length; j++)
		{
			if (a_id == thumb_grid[i][j])
			{
				if(i - 1 >= 0)
				{
					prev_row = i - 1;
				}
				
				if(i + 1 < thumb_grid.length)
				{
					next_row = i + 1;
				}
				
				if(j - 1 >= 0)
				{
					prev_col = j - 1;
				}
				
				if(j + 1 < thumb_grid[i].length || j != 6)
				{
					next_col = j + 1;
				}
				
				curr_row = i;
				curr_col = j;

				// top
				if(prev_row != null)
				{
					if(full_thb)
					{
						if(thumb_grid[prev_row][0].match(regex))
						{
							surrounding_items.push(thumb_grid[prev_row][0]);
						}
						else
						{
							for(var k = 0; k < thumb_grid[i].length; k++)
							{
								surrounding_items.push(thumb_grid[prev_row][k]);
							}
						}
					}
					else
					{
						if(surrounding_items[surrounding_items.length-1] != thumb_grid[prev_row][prev_col])
						{
							surrounding_items.push(thumb_grid[prev_row][prev_col]);
						}
						if(surrounding_items[surrounding_items.length-1] != thumb_grid[prev_row][curr_col])
						{
							surrounding_items.push(thumb_grid[prev_row][curr_col]);
						}
						if(surrounding_items[surrounding_items.length-1] != thumb_grid[prev_row][next_col])
						{
							surrounding_items.push(thumb_grid[prev_row][next_col]);
						}
					}
				}
				
				// left
				if(prev_col != null && thumb_grid[i][prev_col] != a_id)
				{
					surrounding_items.push(thumb_grid[curr_row][prev_col]);
				}
				// right
				if(next_col != null && thumb_grid[i][next_col] != a_id)
				{
					surrounding_items.push(thumb_grid[curr_row][next_col]);
				}
				
				// bottom
				if(next_row != null)
				{
					if(full_thb)
					{
						if(thumb_grid[next_row][0].match(regex))
						{
							surrounding_items.push(thumb_grid[next_row][0]);
						}
						else
						{
							for(var k = 0; k < thumb_grid[i].length; k++)
							{
								surrounding_items.push(thumb_grid[next_row][k]);
							}
						}
					}
					else
					{
						if(surrounding_items[surrounding_items.length-1] != thumb_grid[next_row][prev_col])
						{
							surrounding_items.push(thumb_grid[next_row][prev_col]);
						}
						if(surrounding_items[surrounding_items.length-1] != thumb_grid[next_row][curr_col])
						{
							surrounding_items.push(thumb_grid[next_row][curr_col]);
						}
						if(surrounding_items[surrounding_items.length-1] != thumb_grid[next_row][next_col])
						{
							surrounding_items.push(thumb_grid[next_row][next_col]);
						}
					}
				}
				
				hideItems();
				return;
			}
		}
	}
}

function hideItems()
{
	for(var i = 0; i < surrounding_items.length; i++)
	{
		$('#' + surrounding_items[i]).fadeTo(50 + (Math.random() * 950), 0);
	}
	
	sti = setTimeout('showAgain(true)', 1500);
}

function showAgain(a_bFromTimer)
{
	if(!a_bFromTimer)
	{
		for(var i = 0; i < surrounding_items.length; i++)
		{
			$('#' + surrounding_items[i]).fadeTo(10, 1);
		}
	}
	else
	{
		for(var i = 0; i < surrounding_items.length; i++)
		{
			$('#' + surrounding_items[i]).fadeTo(50 + (Math.random() * 950), 1);
		}
		
		$.scrollTo(0, 750, {easing:'easeInOutExpo'});
		// window.scrollTo(0,0);
	}
}


// ========================================
// CYCLE the multiframe thumbs
// ========================================
function setupCycle(a_element, num_frames, duration)
{
	var owner = a_element;
	var elm = $(a_element);
	a_element.num_frames = num_frames;
	a_element.duration = duration;
	a_element.count = 1;
	a_element.si = setInterval(function()
	{
		clipifyMe(a_element);
		owner.count++;
		if(owner.count > owner.num_frames)
		{
			owner.count = 1;
		}
	}, duration);

	//  css doesn't stick, re-apply clip style
	clipifyMe(a_element);
}


// ========================================
// SOMETIMES FFox on Mac needs a little help
// ========================================
function clipifyMe(a_element)
{
	if(a_element.si)
	{
		var owner = a_element;
		var elm = $(a_element);
		var right = owner.count * 117;
		var left = (owner.count * 117) - 117;
		elm.find('.home_thumb_thb').css({'clip':'rect(0px, '+right+'px, 72px, '+left+'px)', 'left':-left+'px'});
		elm.find('.home_thumb_thb_red').css({'clip':'rect(0px, '+right+'px, 72px, '+left+'px)', 'left':-left+'px'});
	}
}

function delayedPopup()
{
	//popup :-(
	// $('#base_sign_up').click();
}


// ========================================
// External Interface for Flash
// ========================================
var jsReady = false;
function isReady()
{
	return jsReady;
}

function flashInit()
{
	jsReady = true;
}

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

function loadPhoto(value, id)
{
	thisMovie("flash_container").loadPhoto(value, id);
	// window.scrollTo(0,0);
	//window.location = '#top';
}

function loadVideo(value, id)
{
	thisMovie("flash_container").loadVideo(value, id);
	// window.scrollTo(0,0);
	//window.location = '#top';
}

function sendToJavaScript(value)
{
	alert("ActionScript says: " + value);
}

$(document).ready(function()
{
	// ========================================
	// HANDLE SMALL GRID RECENT ROLLOVER
	// ========================================
	$('.recent_item').mouseover(function()
	{
		$(this).find('.recent_mini_img_lg').css({'display':'block'});  // lrg image
	});
	
	$('.recent_item').mouseout(function()
	{
		$(this).find('.recent_mini_img_lg').css({'display':'none'});  // lrg image
	});
});

// ========================================
// Pulled from family.js
// ========================================

var z_ind = 10;
function loadPhotoName(a_name, item)
{
	item.style.zIndex = ++z_ind;
	var _left = item.offsetLeft + item.offsetWidth;

	$('#wn_recent_activity_static').html("<p class='recent_rollover'>" + a_name + "</p>");

	
	if(_left < 415)
	{
		_left += 40;
		$('#wn_recent_activity_static > p').css({'textAlign':'left'});
	}
	else
	{
		var column = parseInt(item.id.substr(6));
		if(column % 10 == 0)
		{
			$(item).find('.recent_mini_img_lg').css({'left':'-38px'});
			$(item).find('.video_marker').addClass('video_hover_last');
			_left -= 942;
		}
		else
		{
			_left -= 905;
		}
		
		$('#wn_recent_activity_static > p').css({'textAlign':'right'});
	}

	$('#wn_recent_activity_static').css({'left':_left + 'px'});
	$(item).find('.video_marker').addClass('video_hover');
}

function removePhotoName(div)
{
	$('#wn_recent_activity_static').css({'left':0 + 'px'});
	$('#wn_recent_activity_static').html("<img id='wn_recent_activity_static_img' src='/assets/img/wn/wn_recent_activity.gif' width='120' height='26' alt='Warrior Nation Recent Activity' />");
	$(div).find('.video_marker').removeClass('video_hover');
	$(div).find('.video_marker').removeClass('video_hover_last');
}