// functions for showing product categories
function hide_all_product_categories() {
	for (i = 0; i < categories; i++) {
		var target_id = 'homepage_product_scrollbar_em_' + i;
		var item_to_hide = document.getElementById(target_id);
		item_to_hide.style.display = 'none';
		var target_tab = 'product_scrollbar_tab_em_' + i;
		var tab_to_change = document.getElementById(target_tab);
		tab_to_change.className = 'product_scrollbar_tab';
	}
}
function show_product_category(selected_category) {
	hide_all_product_categories();
	var target_id = 'homepage_product_scrollbar_em_' + selected_category; 
	var item_to_show = document.getElementById(target_id);
	item_to_show.style.display = 'block';
	var target_tab = 'product_scrollbar_tab_em_' + selected_category;
	var tab_to_change = document.getElementById(target_tab);
	tab_to_change.className = 'product_scrollbar_tab_selected';
	
	
	// Call AJAX
	if (selected_category != current_category) { 
		current_category = selected_category;
		$("#titlebar_Main").html('<div align="center" style="margin-left:-100px; margin-top:200px;"><img src="'+$("#progress_img").attr("src")+'"/><br/><br/>Loading...</div>');
		$("#slide_features_Main").html('<div align="center" style="margin-left:300px; margin-top:200px;"><img src="'+$("#progress_img").attr("src")+'"/><br/><br/>Loading...</div>');
		
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "fetchContent=1&cat="+selected_category,
			error: function(request, status, error) {
				alert("Failure ("+request.status+" "+request.statusText+"): " + request.responseText);
			},
			success: function(data) {
				$("#content_slide_Main").html(data);
			}
		});
	}
}

function show_product(selected_category, selected_tool) {
	// Call AJAX
	if (selected_tool != current_tool) {
		// reset class styles for current product selection items
		document.getElementById('select_' + current_tool + '_em_' + current_category).className = 'product_scroll';
		// set class styles for selected product items
		document.getElementById('select_' + selected_tool + '_em_' + current_category).className = 'product_scroll_selected';
		current_tool = selected_tool;
		$("#titlebar_Main").html('<div align="center" style="margin-left:-100px; margin-top:200px;"><img src="'+$("#progress_img").attr("src")+'"/><br/><br/>Loading...</div>');
		$("#slide_features_Main").html('<div align="center" style="margin-left:300px; margin-top:200px;"><img src="'+$("#progress_img").attr("src")+'"/><br/><br/>Loading...</div>');
		
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "fetchContent=1&cat="+selected_category+'&tool='+selected_tool,
			error: function(request, status, error) {
				alert("Failure ("+request.status+" "+request.statusText+"): " + request.responseText);
			},
			success: function(data) {
				$("#content_slide_Main").html(data);
			}
		});
	}
}

// functions for showing product content
function select_product(category, tool, stl) {
	$("#slide_features_Main").html('<div align="center" style="margin-left:300px; margin-top:200px;"><img src="'+$("#progress_img").attr("src")+'"/><br/><br/>Loading...</div>');
		
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "fetchSWF=1&cat="+category+'&tool='+tool+'&stl='+stl,
		error: function(request, status, error) {
			alert("Failure ("+request.status+" "+request.statusText+"): " + request.responseText);
		},
		success: function(data) {
			$("#slide_features_Main").html(data);
		}
	});
}
function animate_content_slide(product, old_product) {
	var current_position = Math.ceil(animation_position_start + (animation_position_end - animation_position_start) * Math.pow((animation_time_index/animation_duration), .25));
	document.getElementById('content_slide_' + product).style.top = current_position + 'px';
	if (current_position > 0) { //continue calling this function
		animation_time_index = animation_time_index + animation_interval;
		setTimeout("animate_content_slide('" + product + "', '" + old_product + "')", animation_interval);
	} else {
		content_slide_zindex = content_slide_zindex + 1;
		document.getElementById('content_slide_' + old_product).style.top = '530px';
		// reset animation time index
		animation_time_index = 0;
	}
}

// functions for scrolling product scrollbar
function scroll_left(os_to_scroll) {	
	var scrolling_element_offset;
	var offset_variable_name = 'offset_em_' + os_to_scroll;
	eval('scrolling_element_offset = ' + offset_variable_name + ';');
	var scrolling_element_min_offset;
	var min_offset_variable_name = 'min_offset_em_' + os_to_scroll;
	eval('scrolling_element_min_offset = ' + min_offset_variable_name + ';');
	if (scrolling_element_offset < 0) { // we can scroll this element
		var scrolling_element_id = 'scroll_content_area_em_' + os_to_scroll;
		// set animation settings
		animation_position_start = scrolling_element_offset;
		animation_position_end = scrolling_element_offset + 750;
		animation_interval = 25;
		animation_time_index = 0;
		animation_duration = 350;
		// call to animation
		animate_scrollbar(scrolling_element_id);
		// set offset to new offset
		eval(offset_variable_name + ' = animation_position_end;');
		// check to see if button states need to change
		if (animation_position_end == 0) { // we need to to disable the left scroll button
			var left_scroll_button_id = 'left_scroll_button_em_' + os_to_scroll;
			var left_scroll_button = document.getElementById(left_scroll_button_id);
			left_scroll_button.href = '#';
			left_scroll_button.className = 'left_scroll_button_disabled';
		}
		if (animation_position_end > scrolling_element_min_offset) { // we need to enable the right scroll button
			var right_scroll_button_id = 'right_scroll_button_em_' + os_to_scroll;
			var right_scroll_button = document.getElementById(right_scroll_button_id);
			right_scroll_button.className = 'right_scroll_button';
		}					
	}
}
function scroll_right(os_to_scroll) {
	var scrolling_element_offset;
	var offset_variable_name = 'offset_em_' + os_to_scroll;
	eval('scrolling_element_offset = ' + offset_variable_name + ';');
	var scrolling_element_min_offset;
	var min_offset_variable_name = 'min_offset_em_' + os_to_scroll;
	eval('scrolling_element_min_offset = ' + min_offset_variable_name + ';');
	if (scrolling_element_offset > scrolling_element_min_offset) { // we can scroll this element
		var scrolling_element_id = 'scroll_content_area_em_' + os_to_scroll;
		// set animation settings
		animation_position_start = scrolling_element_offset;
		animation_position_end = scrolling_element_offset - 750;
		animation_interval = 25;
		animation_time_index = 0;
		animation_duration = 350;
		// call to animation
		animate_scrollbar(scrolling_element_id);
		// set offset to new offset
		eval(offset_variable_name + ' = animation_position_end;');
		// check to see if button states need to change
		if (animation_position_end < 0) { // we need to to enable the left scroll button
			var left_scroll_button_id = 'left_scroll_button_em_' + os_to_scroll;
			var left_scroll_button = document.getElementById(left_scroll_button_id);
			left_scroll_button.href = '#';
			left_scroll_button.className = 'left_scroll_button';
		}
		if (animation_position_end == scrolling_element_min_offset) { // we need to disable the right scroll button
			var right_scroll_button_id = 'right_scroll_button_em_' + os_to_scroll;
			var right_scroll_button = document.getElementById(right_scroll_button_id);
			right_scroll_button.className = 'right_scroll_button_disabled';
		}		
	}
}
function animate_scrollbar(scrolling_element_id) {
	var current_position = Math.ceil(animation_position_start + (animation_position_end - animation_position_start) * Math.pow((animation_time_index/animation_duration), .25));
	document.getElementById(scrolling_element_id).style.left = current_position + 'px';
	if (current_position != animation_position_end) { //continue calling this function
		animation_time_index = animation_time_index + animation_interval;
		setTimeout("animate_scrollbar('" + scrolling_element_id + "')", animation_interval);
	} else {
		// reset animation time index
		animation_time_index = 0;
	}
}