// JavaScript Document

$(document).ready(function() {
		
	// Function to black out the 'We Recommend' video bar when a 'We Recommend' category is selected.
	/*if ($('#recommend-box').length) {
		$('#recommend-mask').animate({opacity: 0.5}, 0).css('display', 'block');
		$('#recommend-header').hover(function() {
			$('#recommend-mask').stop().animate({opacity: 0}, 150, function() {
				$(this).css('display', 'none');											   
			});
		}, function() {
			$('#recommend-mask').css('display', 'block').stop().animate({opacity: 0.5}, 350);
		});
	}*/
	
	
	$("a[rel=hoverfade], a[rel^=hoverfadegroup], a img[rel=hoverfade], a img[rel^=hoverfadegroup]").hover(function() {
		var node = $(this).get(0).nodeName.toLowerCase();
		var group = $(this).attr('rel');
		if (group.indexOf("group") != -1) {
			if (node == 'img') {
				node = $(this).parent().get(0).nodeName.toLowerCase()+' '+node;
			}
			node = $(node+'[rel='+group+']');
		} else {
			node = $(this);
		}
		node.stop().animate({opacity: 0.5}, 500);							
	}, function() {
		var node = $(this).get(0).nodeName.toLowerCase();
		var group = $(this).attr('rel');
		if (group.indexOf("group") != -1) {
			if (node == 'img') {
				node = $(this).parent().get(0).nodeName.toLowerCase()+' '+node;
			}
			node = $(node+'[rel='+group+']');
		} else {
			node = $(this);
		}
		node.stop().animate({opacity: 1}, 500);
	});

	var running = false;
	var num = 0;
	var comp = 1;
	$('#tab_selectors .select_back[rel=on]').children('.tab_off').css('visibility', 'visible').animate({opacity: 0}, 0);
	$('.tab_text').not('#tab_text_1').animate({opacity: 0.55}, 0);
	
	$('#tab_selectors a, .tab_text').click(function(e) {
													
		e.preventDefault();
							
		var count = $(this).attr('id').split('_');
		count = count[2];
				 
		if (running === false && $('#select_back_'+count).attr('rel') != 'on') {
			
			num = 0;
			running = true;
			fadespeed = 200;
			
			var thisid = $('.select_back[rel=on]').attr('id');
			var thisfadeid = $('.select_back[rel=on]').children('.tab_off').attr('id');
			var thismoveid = $('.select_back[rel=on]').children('.tab_selector_group').attr('id');
			
			// Move old selected tab down
			$('#'+thismoveid).animate({backgroundPosition: '0px 0px;'}, fadespeed);
			
			// Fade old selected tab out (fade in DESELECT state)
			$('#'+thisfadeid).animate({opacity: 1}, fadespeed);
			
			// Fade new selected tab in (fade out DESELECT state)
			$('#tab_off_'+count).animate({opacity: 0}, fadespeed);
			
			// Move the new tab
			$('#tab_selector_'+count).animate({backgroundPosition: '0px -7px'}, fadespeed, function() {
				// Change the selected tab (using REL attribute)
				$('#tab_selectors .select_back').attr('rel', 'off');
				$('#select_back_'+count).attr('rel', 'on');
				running = false;																 
			});
			
			
			$('.prod_tabs').each(function() {
				var oldtop = $(window).scrollTop();
				if ($(this).css('opacity') > 0.9) {
					$(this).animate({opacity: 0}, fadespeed/2, function() {
						$(this).hide();
						if ($('#prod_tab_'+count).css('opacity') < 0.1) {
							$('#prod_tab_'+count).show().animate({opacity: 1}, fadespeed/3);
							$(window).scrollTop(oldtop);
						}
					});
				}
			});
			
			// Move the Red background to the correct location
			var newpos = 150-(count*50);
			$('#tab_selectors').animate({backgroundPosition: newpos+'% 100%'}, 100);
			
			// Change the text colour
			$('#tab_text_'+count).animate({opacity: 1}, 100);
			$('.tab_text').not('#tab_text_'+count).animate({opacity: 0.55}, 100);
		}
		return false;
	});
	
	return false;
	
});
