var _Cells = new Array();
var _lastCallerNo = 0;
var _currentSelection = 0;

var _cache = new Array();

$.preloadimages = function()
{	for(var i = 0; i<arguments.length; i++)
	{	img = new Image();
		img.src = arguments[i];
	}
}

/**
 *	$.LoadWIndow
 *	On mouseover this is called to create a random visual. That's it's
 *	original mode, this is being changed to create one random and cache it
 *	and use it on every subsequent mouseover for that particular item.
 *
 *	Flow Example
 *	Original:	[mouse over > loadWindow > Returns random product/image/information]
 *	New:		[mouse over > loadWindow > check to see if a selection has been made for button already > returns random (preselected) item]
 *
 */

$.LoadWindow = function(CallerNo)
{	
	_lastCallerNo = CallerNo;
	
	if (_cache[_lastCallerNo] != undefined) {
		var _selection = _cache[_lastCallerNo];
	} else {
		var _randSel = Math.floor(Math.random() * _Cells[_lastCallerNo].length);
		var _selection = _Cells[_lastCallerNo][_randSel];
			_cache[_lastCallerNo] = _selection;
	}
	
	var _customClass = (_selection['customclass']) ? ' class="'+_selection['customclass']+'"' : '';
	
	$('div#hmain').html('<a href="/'+_selection['productid']+'"><img'+_customClass+' src="images/home/spacer.gif" border="0" width="309" height="426" id="mainImg" /></a><div'+_customClass+' id="mainText">'+_selection['text']+'</div>');
	
	var img = '../images/home/bgs/'+_selection['backgroundimage'];
	
	$('div#hmain').css('background-image', 'url("' + img+'")');
	$('div#hmain').css('background-color', _selection['backgroundcolor']);
	
	for (var i =1; i <= 5; i++) {
		$('#hcontent').removeClass('hcontent'+i);
	}
	
	var _color = _selection['backgroundcolor'].split('#');
	
	switch (String(_color[1]).toLowerCase())
	{
		case '323e3f':
		$('#hcontent').addClass('hcontent2');
		break;
		
		case '000000':
		$('#hcontent').addClass('hcontent3');
		break;
		
		case 'f3f8fe':
		$('#hcontent').addClass('hcontent4');
		break;

		case 'ffffff':
		$('#hcontent').addClass('hcontent5');
		break;

		default:
		$('#hcontent').addClass('hcontent1');
		break;

	}
	
}

$(document).ready(function(){
	
	if ($('ul#hMenu').length) {
	
		$.preloadimages('../images/home/home_hcbox_bg_577385.jpg');
		$.preloadimages('../images/home/home_hcbox_bg_323e3f.gif');
		$.preloadimages('../images/home/home_hcbox_bg_000000.jpg');
		
		//$.get('homepage.xml', function(xml) {
		$.get('homenew.xml', function(xml) {
			for (var i = 1; i <= $('category', xml).length; i++) {
				var __i = 0;
				$('category[id='+i+'] item', xml).each(
					function() {
						if (_Cells[i] == undefined) {
							_Cells[i] = new Array();
						}
						_Cells[i][__i] = new Array();
						_Cells[i][__i]['image'] = $('image', this).text();
						_Cells[i][__i]['backgroundimage'] = $('background', this).text();
						_Cells[i][__i]['backgroundcolor'] = $('backgroundcolor', this).text();
						_Cells[i][__i]['customclass'] = $('customclass', this).text();
						_Cells[i][__i]['text'] = $('description', this).text();
						_Cells[i][__i]['productid'] = $('productid', this).text();
						__i++;
					}
				);
			}
			
			$('ul#hMenu li').each(function() {
				if ($(this).attr('no') != undefined) {
					var _t = $(this).attr('no');
					var _randSel = Math.floor(Math.random() * _Cells[_t].length);
					var _selection = _Cells[_t][_randSel];
					_cache[_t] = _selection;
					$.preloadimages('/images/home/bgs/'+_selection['backgroundimage']);
				}
			});
			
		});
		
		$('div#mainNav').mouseover(function() {
			var _over = 'Over';

			$('ul#hMenu li[no="'+_currentSelection+'"]').each(function() {
				$(this).find('img.arrow').hide();
				$(this).removeClass('item'+$(this).attr('no')+'_On');
				$(this).removeClass(_over);
			});
			
			_currentSelection = 0;
			
			for (var i =1; i <= 7; i++) {
				$('#hcontent').removeClass('hcontent'+i);
			}
			
			$('#hcontent').addClass('hcontent1');
			$('div#hmain').css('background-color', '#577e85');
			$('div#hmain').html('<img src="images/home/h_box_cover.jpg" height="426" width="311" />');
		
		});

		$('div#tagline').mouseover(function() {
			var _over = 'Over';

			$('ul#hMenu li[no="'+_currentSelection+'"]').each(function() {
				$(this).find('img.arrow').hide();
				$(this).removeClass('item'+$(this).attr('no')+'_On');
				$(this).removeClass(_over);
			});
			
			_currentSelection = 0;
			
			for (var i =1; i <= 7; i++) {
				$('#hcontent').removeClass('hcontent'+i);
			}
			
			$('#hcontent').addClass('hcontent1');
			$('div#hmain').css('background-color', '#577e85');
			$('div#hmain').html('<img src="images/home/h_box_cover.jpg" height="426" width="311" />');
		
		});
		
		$('div#hright').mouseover(function() {
			var _over = 'Over';

			$('ul#hMenu li[no="'+_currentSelection+'"]').each(function() {
				$(this).find('img.arrow').hide();
				$(this).removeClass('item'+$(this).attr('no')+'_On');
				$(this).removeClass(_over);
			});
			
			_currentSelection = 0;
			
			for (var i =1; i <= 7; i++) {
				$('#hcontent').removeClass('hcontent'+i);
			}
			
			$('#hcontent').addClass('hcontent1');
			$('div#hmain').css('background-color', '#577e85');
			$('div#hmain').html('<img src="images/home/h_box_cover.jpg" height="426" width="311" />');
		
		});
		
		$('div#hmain').mouseenter(function() {
			if (_currentSelection != 0) {
				if (_lastCallerNo != _currentSelection) {
					var _overorg = 'item'+_currentSelection+'_On';
					var _over = 'Over';
					$('ul#hMenu li[no="'+_currentSelection+'"]').each(function() {
						$(this).addClass(_overorg);
						$(this).addClass(_over);
						$(this).find('img.arrow').show();
					});
				
					_lastCallerNo = _currentSelection;
					$.LoadWindow(_currentSelection);
				}
			}
		}).mouseleave(function(e) {
			
			var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
			if (reltg.id == 'mainImg') {
				return;
			}
			while (reltg && reltg != e.target) { 
				reltg = reltg.parentNode; 
			}
			
			if (reltg != undefined && reltg != null) {
				return;
			}
			
			var _overorg = 'item'+_currentSelection+'_On';
			var _over = 'Over';

			$('ul#hMenu li[no="'+_currentSelection+'"]').each(function() {
				$(this).find('img.arrow').hide();
				$(this).removeClass('item'+$(this).attr('no')+'_On');
				$(this).removeClass(_over);
			});
			
			for (var i =1; i <= 7; i++) {
				$('#hcontent').removeClass('hcontent'+i);
			}
			
			$('#hcontent').addClass('hcontent1');
			$('div#hmain').css('background-color', '#577e85');
			$('div#hmain').html('<img src="images/home/h_box_cover.jpg" height="426" width="311" />');
			
		});
		
		if ($('ul#hMenu').length) {
			$('ul#hMenu li').each(function(i) {
			
				
				if ($(this).attr('no') != undefined) {
					$(this).find('img.arrow').css('padding-top', (($(this).height()/2)-5));
					
					if ($(this).attr('no') == undefined) {
						return;
					}
					var _overorg = 'item'+$(this).attr('no')+'_On';
					var _over = 'Over';
					
					$(this).click(function() { 
						window.location = $(this).find('a').attr('href');
					});
					
					
					$(this).mouseenter(function(e) {
						if ($(this).attr('no') != undefined) {
							_currentSelection = $(this).attr('no');
						}
					
						$('ul#hMenu li').each(function() {
							$(this).find('img.arrow').hide();
							$(this).removeClass('item'+$(this).attr('no')+'_On');
							$(this).removeClass(_over);
						});
						$(this).addClass(_overorg);
						$(this).addClass(_over);
						$(this).find('img.arrow').show();
						$.LoadWindow($(this).attr('no'));
						
					}).mouseleave(function(e){
					
						var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
						while (reltg && reltg != e.target) { 
							reltg = reltg.parentNode; 
							if (reltg && reltg.id == 'hmain') {
								return;
							}
						}
						
						if (reltg != undefined && reltg != null) {
							return;
						}
					
						$(this).find('img.arrow').hide();
						$(this).removeClass('item'+$(this).attr('no')+'_On');
						$(this).removeClass(_over);
						
						for (var i =1; i <= 7; i++) {
							$('#hcontent').removeClass('hcontent'+i);
						}
						
						$('#hcontent').addClass('hcontent1');
						$('div#hmain').css('background-color', '#577e85');
						$('div#hmain').html('<img src="images/home/h_box_cover.jpg" height="426" width="311" />');
						
					});
					
				}
			});
			
			$('ul#hMenu').show();
		}
		
    }
});
