var expanded = false;

$(document).ready(function()
{
	var dur = 750;
	var pm_height = $('#product_desc')[0].scrollHeight;

	//  tell flashInterface that we are ready
	flashInit();
	
	//  find how many mini products we have
	var prod_mini_len = $('.product_mini').length;
	var prod_mini_height = Math.ceil(prod_mini_len / 16) * 37;
	$('#product_grid').css({'height':prod_mini_height + 'px'});

	
	if(parseInt(pm_height) >= 75)
	{
		$('#product_desc').css({height:'75px'});
	}
	else
	{
		$('#product_more').css({display:'none'});
	}
	
	$('#product_more').toggle(function(){
		$('#product_desc').animate({height:pm_height}, dur, 'easeInOutExpo', showless);
	},function(){
		$('#product_desc').animate({height:'75px'}, dur, 'easeInOutExpo', showmore);
	});
	
	$('#product_more').mouseover(function()
	{
		if(expanded)
		{
			$(this).css({'background':'#fff url(/assets/img/less_more.gif) no-repeat 0px -15px'});
		}
		else
		{
			$(this).css({'background':'#fff url(/assets/img/less_more.gif) no-repeat 0px -45px'});
		}
	});
	
	$('#product_more').mouseout(function()
	{
		if(expanded)
		{
			$(this).css({'background':'#fff url(/assets/img/less_more.gif) no-repeat 0px 0px'});
		}
		else
		{
			$(this).css({'background':'#fff url(/assets/img/less_more.gif) no-repeat 0px -30px'});
		}
	});
	
	
	
	// ========================================
	// HANDLE RED ROLLOVER IMAGE REPLACEMENT
	// ========================================
	$('.related_link').mouseover(function()
	{
		$(this).find('.video_marker').css({'display':'none'});
		$(this).find('.video_marker_hover').css({'display':'block'});
		$(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({'display':'block'});
		$(this).find('.video_marker_hover').css({'display':'none'});
		$(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
	});
	
	$('.related_link').click(function()
	{
		var link = $(this).find('a').get(0);
		link.onclick ? link.onclick() : window.location = link.href;
	});
	
	// ========================================
	// HANDLE SMALL GRID ROLLOVER
	// ========================================
	$('.product_mini').mouseover(function()
	{
//		$(this).find('.product_mini_img').css({'opacity':'0'});  // norm image
		$(this).find('.product_mini_img_lg').css({'display':'block'});  // red image
	});
	
	$('.product_mini').mouseout(function()
	{
//		$(this).find('.product_mini_img').css({'opacity':'1'});  // norm image
		$(this).find('.product_mini_img_lg').css({'display':'none'});  // red image
	});
});


function showless()
{
	$('#product_more').css({'background':'#fff url(/assets/img/less_more.gif) no-repeat 0px 0px'});
	expanded = true;
}
function showmore()
{
	$('#product_more').css({'background':'#fff url(/assets/img/less_more.gif) no-repeat 0px -30px'});
	expanded = false;
}


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

	$('#product_mini_name').stop();
	$('#product_mini_name > p').text(a_name);

	
	if(_left < 450)
	{
		_left += 54;
		$('#product_mini_name > p').css({'textAlign':'left'});
	}
	else
	{
		var column = parseInt(a_id.substr(6));
		if(column % 16 == 0)
		{
			$(a_id).find('.product_mini_img_lg').css({'left':'-51px'});
			_left -= 508;
		}
		else
		{
			_left -= 457;
		}
		
		$('#product_mini_name > p').css({'textAlign':'right'});
	}

	$('#product_mini_name > p').css({'left':_left + 'px'});


	//  Firefox on mac fix
	var browser = jQuery.browser;
	if(browser.mozilla)
	{
		$('#product_mini_name > p').css({'top':3 + 'px'});
	}
	
	$('#product_mini_name').animate({backgroundColor:'#DC0009'}, 500);
}

function removePhotoName()
{
	$('#product_mini_name').stop();
	$('#product_mini_name > p').text('');
	$('#product_mini_name').animate({backgroundColor:'#b7b7b7'}, 200);
}




// ========================================
// 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);
}

function loadVideo(value, id)
{
	thisMovie("flash_container").loadVideo(value, id);
}

function loadColorWay(id, label)
{
	$('#product_color_shot').attr('src', id);
	$('.product_color_shot_label').css({'display':'block'});
	$('.product_color_shot_label').text(label);
}

function removeColorLabel()
{
	$('.product_color_shot_label').css({'display':'none'});
}
