AC.FB = {
	_rotate_time: [],
	_fade: true,
	_rotate_interval: 4000
};

AC.FB.selectFeatureTwoTab = function (tab_id, image_id, fade, parent_id) {
	var tab = $('#' + tab_id),
		img = $('#' + image_id),
		tab_array = $('#' + parent_id + ' .tab'),
		image_array = $('#' + parent_id + ' .tab_image');;

	if (false == tab.length || false == img.length) {
		return false;
	}

	if (tab.hasClass('tab_over')) {
		return false;
	}

	AC.FB._fade_lock = true;

	tab_array.each(function(i, elm){
		if ($(this).hasClass('tab_over')){
			$(this).removeClass('tab_over');

			if(fade) {
				$(image_array[i]).stop(true, true)
					.fadeOut(1500);
			}
			else {
				$(image_array[i]).hide();
			}

			return false;
		}
	});

	tab.addClass('tab_over');

	if(fade) {
		img.stop(true, true)
			.fadeIn(1500);
	}
	else {
		img.show();
	}
};

AC.FB.rotateFeatureTwoTab = function (parent_id) {
	if (AC.FB._rotate_time[parent_id] != ''){
		clearTimeout(AC.FB._rotate_time[parent_id]);
	}

	var tab_array = $('#' + parent_id + ' .tab'),
		image_array = $('#' + parent_id + ' .tab_image');

	tab_array.each(function(index, elm){
		if ($(elm).hasClass('tab_over')) {
			if ((index+1) < tab_array.length) {
				AC.FB.selectFeatureTwoTab(tab_array[index+1].id, image_array[index +1].id, AC.FB._fade, parent_id);
			} else {
				AC.FB.selectFeatureTwoTab(tab_array[0].id, image_array[0].id, AC.FB._fade, parent_id);
			}

			AC.FB._rotate_time[parent_id] = setTimeout("AC.FB.rotateFeatureTwoTab('" + parent_id + "')", AC.FB._rotate_interval);
			return false;
		}
	});
};

AC.FB.mouseOutFeatureTwoTab = function (evt, tab_id, parent_id) {
	clearTimeout(AC.FB._rotate_time[parent_id]);

	if ($(evt.relatedTarget).is(tab_id)) {
		$(evt).stopPropagation();
		return;
	}

	AC.FB._rotate_time[parent_id] = setTimeout("AC.FB.rotateFeatureTwoTab('" + parent_id + "')", AC.FB._rotate_interval);
};

AC.FB.mouseOverFeatureTwoTab = function (tab_id, image_id, parent_id) {
	clearTimeout(AC.FB._rotate_time[parent_id]);
	AC.FB.selectFeatureTwoTab(tab_id, image_id, false, parent_id);
};

$(function(){
	var tab_array = $('.feature_two > .tab'),
		parent_id = $('.feature_two').attr('id'),
		image_array = $('.feature_two > .tab_image'),
		el = null;

	tab_array.each(function(index, elm){
		el = $(elm);

		el.mouseover(function(evt){
			AC.FB.mouseOverFeatureTwoTab($(this).attr('id'), $(image_array[index]).attr('id'), parent_id);
		}).mouseout(function(evt){
			AC.FB.mouseOutFeatureTwoTab(evt, el.attr('id'), parent_id);
		});
	});

	AC.FB._rotate_time[parent_id] = setTimeout("AC.FB.rotateFeatureTwoTab('" + parent_id + "')", AC.FB._rotate_interval);
});
