var thumb_grid = [];
var surrounding_items = [];
var sti;
var expanded = false;

var dur = 750;


var current_copy;


$(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();
	
	// replace the email string
	$('a.email').nospam({ filterLevel: 'low' });
	
	// ========================================
	// HANDLE RED ROLLOVER IMAGE REPLACEMENT
	// ========================================
	$('.related_link').mouseover(function()
	{
		$(this).find('.video_marker').css({'backgroundPosition':'0 -23px'});
		$(this).find('.related_thumb_thb').css({'display':'none'});  // norm image
		$(this).find('.related_thumb_thb_red').css({'display':'block'});  // red image
		$(this).find('.related_roll_label').css({'display':'block'});  // text inside of image
	});
	
	$('.related_link').mouseout(function()
	{
		$(this).find('.video_marker').css({'backgroundPosition':''});
		$(this).find('.related_thumb_thb').css({'display':'block'});  // norm image
		$(this).find('.related_thumb_thb_red').css({'display':'none'});  // red image
		$(this).find('.related_roll_label').css({'display':'none'});  // text inside of image
	});
	
	$('.arsenal_item').mouseover(function()
	{
		$(this).find('.product_mini_img').css({'display':'none'});  // norm image
		$(this).find('.product_mini_img_red').css({'display':'block'});  // red image
	});
	
	$('.arsenal_item').mouseout(function()
	{
		$(this).find('.product_mini_img').css({'display':'block'});  // norm image
		$(this).find('.product_mini_img_red').css({'display':'none'});  // red image
	});
	
	setup_bottom_rollovers();
});

function setup_bottom_rollovers(){
	var ath_mini_photos = $('.ath_mini_photos');
	var mob_mini_photos = $('.mob_mini_photos');
	
	ath_mini_photos.unbind("mouseover");
	ath_mini_photos.unbind("mouseout");
	ath_mini_photos.unbind("click");
	
	mob_mini_photos.unbind("mouseover");
	mob_mini_photos.unbind("mouseout");
	mob_mini_photos.unbind("click");

	ath_mini_photos.mouseover(function()
	{
		$(this).find('.video_marker').css({'backgroundPosition':'0 -23px'});
		$(this).find('.phot_vid_img').css({'display':'none'});  // norm image
		$(this).find('.phot_vid_img_red').css({'display':'block'});  // red image
		$(this).find('.ath_roll_label').css({'display':'block'});  // text inside of image
	});
	
	ath_mini_photos.mouseout(function()
	{
		$(this).find('.video_marker').css({'backgroundPosition':''});
		$(this).find('.phot_vid_img').css({'display':'block'});  // norm image
		$(this).find('.phot_vid_img_red').css({'display':'none'});  // red image
		$(this).find('.ath_roll_label').css({'display':'none'});  // text inside of image
	});

	ath_mini_photos.click(function()
	{
		findMe($(this).attr('id'));
		var link = $(this).find('a').get(0);
		link.onclick ? link.onclick() : window.location = link.href;
	});
	
	mob_mini_photos.mouseover(function()
	{
		$(this).find('.phot_vid_img').css({'display':'none'});  // norm image
		$(this).find('.phot_vid_img_red').css({'display':'block'});  // red image
		$(this).find('.ath_roll_label').css({'display':'block'});  // text inside of image
	});
	
	mob_mini_photos.mouseout(function()
	{
		$(this).find('.phot_vid_img').css({'display':'block'});  // norm image
		$(this).find('.phot_vid_img_red').css({'display':'none'});  // red image
		$(this).find('.ath_roll_label').css({'display':'none'});  // text inside of image
	});
}

// ========================================
// layout the thumb grid rows and columns
// ========================================
function setupGrid()
{
	// thumb_grid
	var thbs = $('#ath_photos_vids_grid').children();
	var row = [];
	
	for(var i = 0; i < thbs.length; i++)
	{
		row.push(thbs[i].id);
		if(row.length == 5)
		{
			thumb_grid.push(row);
			var row = [];
		}
		else if(i == thbs.length - 1)
		{
			thumb_grid.push(row);
		}
	}
	
	// $('#ath_photos_vids_grid').css({'height:':(109 * thumb_grid.length) + 'px'});
}


// ========================================
//  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;
	
	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 != 4)
				{
					next_col = j + 1;
				}
				
				curr_row = i;
				curr_col = j;

				// top
				if(prev_row != null)
				{
					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(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);
	}
}




// ========================================
// 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, name, mode)
{
	thisMovie("flash_container_pdp").loadPhoto(value, name, mode);
}

function loadVideo(value, name, mode)
{
	thisMovie("flash_container_pdp").loadVideo(value, name, mode);
}


