var arrHeroCustomTabs=[], arrHeroIndustries=[], arrHeroProductSearch=[];
var industryRotatorID=0;

if (homeheroRotatingImageDisplayedFor == undefined)
{
	var homeheroRotatingImageDisplayedFor = 10000;
}
else
{
	homeheroRotatingImageDisplayedFor=parseInt(homeheroRotatingImageDisplayedFor, 10)*1000;
}

if (homeheroFadeSpeed == undefined)
{
	var homeheroFadeSpeed = 'fast';
}

$(function()
{
	var hero=$('#home-hero');
	
	if (hero.length)
	{
		$('#home-hero-tab_1', hero).bind('click', function() {
			startIndustryRotation();
			$('#home-arrow-tab', hero).removeClass().addClass('tab_1');
			$('#home-arrow-tab li',hero).removeClass('active');
			$(this).addClass('active');
			
			// for reshowing the product search button, lest its been hidden by visiting another tab
			$('.hero-action span', hero).css('display','block');
			$('a.hero-action', hero).css('display','block');
			$('.description').css('width','270px');
			
			return false;
		});
		
		$('#home-hero-tab_2', hero).bind('click', function() {
			showProductSearch();
			return false;
		});
		
		$('.hero-action', hero).bind('click', function() {
			showProductSearch();
			return false;
		});
		
		$('.home-hero-customtab').bind('click', function() {
			stopIndustryRotation();
			hideProductSearch();
			
			var intIndex=getID($(this).attr('id'));
			
			if (0==$('#homehero-custom-image_'+intIndex, hero).length) {
				$('<img src="'+arrHeroCustomTabs[intIndex][0]+'" alt="'+arrHeroCustomTabs[intIndex][1]+'" '+
				'id="homehero-custom-image_'+intIndex+'" style="display:none"/>')
				.load(function() {
					$(this).insertAfter($('.home-hero-item img:last', hero));
					showHero(intIndex, 'custom', arrHeroCustomTabs);
				});
			}
			else
			{
				showHero(intIndex, 'custom', arrHeroCustomTabs);
			}
			
			$('#home-arrow-tab', hero).removeClass().addClass('tab_'+intIndex);
			$('#home-arrow-tab li',hero).removeClass('active');
			$(this).addClass('active');		
			
			// this is for removing the 'product search' button
			$('.hero-action span', hero).css('display','none');
			$('a.hero-action', hero).css('display','none');
			$('.description').css('width','430px');
			
			return false;
		});
		
		$('.home-hero-productsearch-cat', hero).live('click', function() {
			showProductSearchCat(getID($(this).attr('id')));
		});
		
		function rotateIndustries()
		{
			if (arrHeroIndustries.length <= 1)
			{
				return;
			}
		
			intIndex=getID($('img:visible', hero).fadeOut(homeheroFadeSpeed).attr('id'));
			intIndex++;
			if (intIndex == arrHeroIndustries.length)
			{
				intIndex=0;
			}
			
			if (0==$('#homehero-industry-image_'+intIndex, hero).length) {
				$('<img src="'+arrHeroIndustries[intIndex][0]+'" alt="'+arrHeroIndustries[intIndex][1]+'" '+
				'id="homehero-industry-image_'+intIndex+'" style="display:none"/>')
				.load(function() {
					$(this).insertAfter($('.home-hero-item img:last', hero));
					showHero(intIndex, 'industry', arrHeroIndustries);
				});
			}
			else
			{
				showHero(intIndex, 'industry', arrHeroIndustries);
			}
			
			// need to update indutries link
			$('a#industrydesclink').attr('href', arrHeroIndustries[intIndex][4] + '\&ProductCategory=' + arrHeroIndustries[intIndex][2]);
			
		}
		
		function startIndustryRotation()
		{
			stopIndustryRotation();
			hideProductSearch();
			
			if ('homehero-industry-image_0'!=$('img:visible', hero).attr('id'))
			{
				showHero(0, 'industry', arrHeroIndustries);
			}
			
			industryRotatorID=setInterval(rotateIndustries, homeheroRotatingImageDisplayedFor);
		}
		
		function stopIndustryRotation()
		{
			clearInterval(industryRotatorID);
		}
		
		function showHero(intIndex, strType, arrItem)
		{
			$('img:visible', hero).fadeOut(homeheroFadeSpeed);
			$('#homehero-'+strType+'-image_'+intIndex, hero).fadeIn(homeheroFadeSpeed);
			$('.description h3', hero).text(arrItem[intIndex][2]);
			$('.descriptiontext', hero).replaceWith('<div class="descriptiontext">'+arrItem[intIndex][3]+'</div>');

		}
		
		function buildProductSearch()
		{
			hero.append('<div id="home-hero-productsearch" style="display:none"></div>');
			
			var productsearch=$('#home-hero-productsearch', hero);
			
			
			productsearch.append('<h2 class="choose">'+productsearchChooseText+'</h2>')
				.append('<div class="buttons"><div>');
			
			for (i=0; i<arrHeroProductSearch.length; i++)
			{
				$('.buttons', productsearch).append('<div class="home-hero-productsearch-cat" id="home-hero-productsearch-cat_'+i
					+'">'+arrHeroProductSearch[i][1]+'</div>');
				
//				if (window.console && window.console.firebug) {
//					console.log('1.'+arrHeroProductSearch[i][0]);//field
//					console.log('2.'+arrHeroProductSearch[i][1]);//label
//					//console.log('3.'+arrHeroProductSearch[i][2][j]);//field
//					//console.log('4.'+arrHeroProductSearch[i][3][j]);//label
//				}				
				
				productsearch.append('<ol style="display:none" id="home-hero-productsearch-cats-cat_'+i+'"></ol>');
				
				for (j=0; j<arrHeroProductSearch[i][2].length; j++)
				{
					$('#home-hero-productsearch-cats-cat_'+i).append('<li><a href="'+
					productPageURL+'?TagName='+arrHeroProductSearch[i][0]+
					'&ProductCategory='+arrHeroProductSearch[i][2][j]+
					'">'+arrHeroProductSearch[i][3][j]+'</a></li>');

				}
			}
			
			showProductSearchCat(0);
		}
		
		function showProductSearch()
		{
			stopIndustryRotation();
			$('.home-hero-item').fadeOut(homeheroFadeSpeed, function() {
				$('#home-hero-productsearch', hero).fadeIn(homeheroFadeSpeed);
				$('#home-arrow-tab', hero).removeClass().addClass('tab_2');
				$('#home-arrow-tab li',hero).removeClass('active');
				$('#home-hero-tab_2', hero).addClass('active');
			});
		}
		
		function hideProductSearch()
		{
			$('#home-hero-productsearch').fadeOut(homeheroFadeSpeed, function() {
				$('.home-hero-item', hero).fadeIn(homeheroFadeSpeed);
			});
		}
		
		function showProductSearchCat(intIndex)
		{
			$('#home-hero-productsearch ol', hero).hide();
			$('#home-hero-productsearch-cats-cat_'+intIndex, hero).show();

			$('.home-hero-productsearch-cat', hero).removeClass('active');			
			$('#home-hero-productsearch-cat_'+intIndex, hero).addClass('active');
		}
		
		startIndustryRotation();
		buildProductSearch();
	}
});

