// JavaScript Document
// contains function references to popup_image_script.js

var scroll_off = 0;
var scroll_status = false;
var scroll_fast_status = false;
var scroll_paused = false;

var scrollbox_list;
var scrollbox_count;

var scrollitem_list;
var scrollitem_count;
var scrollitem_index_next;
var scrollitem_index_prev;

var scroll_width;
var scroll_spacing;
var scroll_offset;						// pixel offset value used during a normal scroll
var scroll_iteration;					// no of iterations with a normal scroll
var scroll_offset_fast;					// pixel offset value used during a fast scroll
var scroll_iteration_fast;				// no of iterations within a fast scroll

var scroll_main_timer;
var scroll_shift_timer = 20;
var scroll_fast_count = 3;
var scroll_fast_timer = 60;

var scroll_popup_show_timer = 250;
var scroll_popup_hide_timer = 150;
var scroll_popup_onunload_timer = 5000;

var scroll_viewing_elem_id = '';
var scroll_popup_count = 0;

var scroll_dir_nesting = '';


function ScrollInit(list, list_count, box_count, width, spacing, offset, iteration, offset_fast, iteration_fast, timer, dir_nesting) {
	scrollitem_list = list;
	scrollitem_count = list_count;
	
	scroll_width = width;
	scroll_spacing = spacing;
	scroll_offset = offset;
	scroll_iteration = iteration;
	scroll_offset_fast = offset_fast;
	scroll_iteration_fast = iteration_fast;
	
	scroll_main_timer = timer;
	
	scrollitem_index_next = 0;
	scrollitem_index_prev = box_count - 1;
	
	scroll_dir_nesting = dir_nesting;
	
	scrollbox_count = box_count;
	scrollbox_list = new Array(scrollbox_count);
	for(var i = 0; i < scrollbox_count; i++) {
		scrollbox_list[i] = new ScrollBox(i, _get_x_position(i));
	}
	
	setInterval("_do_scroll()", timer);
}

function ScrollBox(index, x) {		// set scrollbox pointers to dhtml elements
	this.x = x;
	this.div = document.getElementById('itemscroll' + index);
	this.image = document.getElementById('itemscroll_image' + index);
	this.salespot = document.getElementById('itemscroll_salespot' + index);
	this.title = document.getElementById('itemscroll_title' + index);
	this.price = document.getElementById('itemscroll_price' + index);
}

function ScrollStop() {
	scroll_off++;
}

function ScrollStart() {
	setTimeout("scroll_off--", 300);
}

function ScrollPrev() {
	_do_fast_scroll(-scroll_offset_fast);
}

function ScrollNext() {
	_do_fast_scroll(scroll_offset_fast);
}

function _get_next_scrollitem() {// this needs to change
	scrollitem_index_next--;
	scrollitem_index_prev--;
	
	if (scrollitem_index_next < 0) scrollitem_index_next = scrollitem_count - 1;
	if (scrollitem_index_prev < 0) scrollitem_index_prev = scrollitem_count - 1;
	
	return scrollitem_list[scrollitem_index_next];
}

function _get_prev_scrollitem() {// this needs to change
	scrollitem_index_next++;
	scrollitem_index_prev++;
	
	if (scrollitem_index_next >= scrollitem_count) scrollitem_index_next = 0;
	if (scrollitem_index_prev >= scrollitem_count) scrollitem_index_prev = 0;
	
	return scrollitem_list[scrollitem_index_prev];
}

function _change_scrollbox_data(scrollbox, scrollitem) {// this needs to change
	scrollbox.image.firstChild.setAttribute('src', scrollitem.g);
	scrollbox.title.innerHTML = scrollitem.t;
	scrollbox.price.innerHTML = scrollitem.p;
	scrollbox.div.setAttribute('index', scrollitem.x)
	
	if (scrollitem.p.search("%") >= 0)
		scrollbox.salespot.setAttribute('src', scroll_dir_nesting + "images/misc/sale_spot18.gif");
	else scrollbox.salespot.setAttribute('src', scroll_dir_nesting + "images/misc/spacer.gif");
	
	//var url = scroll_dir_nesting + 'pages/product.php?i=' + scrollitem.i;
	var url = _parse_scroll_link(scroll_dir_nesting, scrollitem);
	scrollbox.image.setAttribute('href', url);
	scrollbox.title.setAttribute('href', url);
}

function _do_fast_scroll(offset) {
	if (offset != 0 && scroll_fast_status == false) {
		if (offset > 0) {
			rotate = "_rotate_box_next();";
			direction = 1;
		} else {
			rotate = "_rotate_box_prev();";
			direction = -1;
		}
			
		scroll_off++;
		scroll_status = false;
		scroll_fast_status = true;
		
		for(t = 0; t < scroll_fast_count; t++) {
			setTimeout(rotate + "_do_scroll_shift(" + direction + "," + _get_scroll_shift_x_position(1, offset, scroll_iteration_fast) + ")", scroll_fast_timer * (t * scroll_iteration_fast + 1));
			for(i = 2; i <= scroll_iteration_fast; i++)
				setTimeout("_do_scroll_shift(" + direction + "," + _get_scroll_shift_x_position(i, offset, scroll_iteration_fast) + ")", scroll_fast_timer * (t * scroll_iteration_fast + i));
		}
		setTimeout("scroll_fast_status = false", scroll_fast_timer * (scroll_fast_count * scroll_iteration_fast + 1));
		setTimeout("scroll_off--", (scroll_fast_timer * scroll_fast_count * scroll_iteration_fast) + scroll_main_timer);
	}
}

function _do_scroll() {
	if (scroll_offset != 0 && scroll_off == 0) {
		scroll_status = true;
		
		if (scroll_offset > 0) _rotate_box_next();
		else _rotate_box_prev();
		
		for(i = 1; i <= scroll_iteration; i++)
			setTimeout("_do_scroll_conditioned(" + _get_scroll_shift_x_position(i, scroll_offset, scroll_iteration) + ")", scroll_shift_timer * i);
	}
}

function _do_scroll_conditioned(offset) {
	if (scroll_status == true) 
		_do_scroll_shift(1, offset);
}

function _do_scroll_shift(direction, offset) {
	if (direction > 0) {		// scroll next
		index = scrollbox_count - 1;
		iterator = -1;
	} else if (direction < 0) {		// scroll prev
		index = 0;
		iterator = 1;
	} else return;
	
	for(i = 0; i < scrollbox_count; i++, index += iterator) {
		scrollbox = scrollbox_list[index];
		scrollbox.div.style.left = (scrollbox.x - offset) + "px";
	}
}

function _rotate_box_next() {// this needs to change
	scrollbox = scrollbox_list[scrollbox_count - 1];
	_change_scrollbox_data(scrollbox, _get_next_scrollitem());
	scrollbox.x = _get_x_position(0);
	for(var i = scrollbox_count - 1; i > 0; i--) {
		var index = i - 1;
		scrollbox_list[i] = scrollbox_list[index];
		scrollbox_list[i].x = _get_x_position(i);
	}
	scrollbox_list[0] = scrollbox;
	//DebugAlertScrollBox();
}

function _rotate_box_prev() {// this needs to change
	scrollbox = scrollbox_list[0];
	_change_scrollbox_data(scrollbox, _get_prev_scrollitem());
	scrollbox.x = _get_x_position(scrollbox_count - 1);
	
	for(var i = 1; i < scrollbox_count; i++) {
		var index = i - 1;
		scrollbox_list[index] = scrollbox_list[i];
		scrollbox_list[index].x = _get_x_position(index);
	}
	scrollbox_list[scrollbox_count - 1] = scrollbox;
	//DebugAlertScrollBox();	
}

function _get_x_position(index) {// this needs to change
	return scroll_spacing + ((scroll_width + scroll_spacing) * (index - 1));
}

function _get_scroll_shift_x_position(i, offset, iteration) {
	return offset * (iteration - i);
}

function _do_scroll_popup_hide() {
	if (scroll_popup_count <= 0) {
		var popup = document.getElementById('scroll_popup');
		var main = document.getElementById('scroll_popup_main');
		
		// hide popup
		popup.style.visibility = 'hidden';
		main.parentNode.style.visibility = 'hidden';
		
		popup.style.left = '-5000px';
		main.parentNode.style.left = '-5000px';
		
		scroll_popup_count = 0;
	}
}

function _do_scroll_popup_show(elem_img_name, elem_div_name) {
	if (scroll_popup_count > 0 && scroll_viewing_elem_id == elem_img_name) {
		var elem_img = document.getElementById(elem_img_name);
		var pos = getAbsolutePosition(elem_img);
		var pos_parent = getAbsolutePosition(document.getElementById('itemscroll_parent'));
		
		var div_elem = document.getElementById(elem_div_name);
		var index = div_elem.attributes.getNamedItem('index').value;
		var scrollitem = scrollitem_list[parseInt(index)];
		
		var main = document.getElementById('scroll_popup_main');
		//main.firstChild.setAttribute('href', scroll_dir_nesting + 'pages/product.php?i=' + scrollitem.i);
		main.firstChild.setAttribute('href', _parse_scroll_link(scroll_dir_nesting, scrollitem));
		
		var popup_img_elem = main.firstChild.firstChild;
		popup_img_elem.setAttribute('src', scroll_dir_nesting + 'images/misc/spacer.gif');
		popup_img_elem.setAttribute('src', scrollitem.l);
		
		var popup = document.getElementById('scroll_popup');
		var x = pos.x + parseInt(elem_img.firstChild.width) / 2 - parseInt(popup_img_elem.width) / 2;
		var y = pos_parent.y + 30;
		
		popup.style.left = (x - 6) + "px";
		popup.style.top = (y - 6) + "px";
		
		main.parentNode.style.left = x + "px";
		main.parentNode.style.top = y + "px";
		
		// show popup
		popup.style.visibility = '';
		main.parentNode.style.visibility = '';
	}
}

function ScrollControlMouseUp() {
	if (scroll_paused) {
		var element = document.getElementById('scroll_control');
		if (element != null) {
			element.src = scroll_dir_nesting + 'images/buttons/button_pause.gif';
			scroll_off--; 
			scroll_paused = false;
		}
	} else { 
		var element = document.getElementById('scroll_control');
		if (element != null) {
			element.src = scroll_dir_nesting + 'images/buttons/button_play.gif'; 
			scroll_off++; 
			scroll_paused = true;
		}
	}
}

function ScrollPopupMouseOver() {
	scroll_popup_count++;
	ScrollStop();
}

function ScrollPopupMouseOut() {
	scroll_popup_count = 0;
	setTimeout("_do_scroll_popup_hide()", scroll_popup_hide_timer);
	ScrollStart();
}

function ScrollPopupParentMouseOver(elem_img_name, elem_index_name) {
	scroll_viewing_elem_id = elem_img_name;
	scroll_popup_count++;
	setTimeout("_do_scroll_popup_show('" + elem_img_name + "','" + elem_index_name + "')", scroll_popup_show_timer);
}

function ScrollPopupParentMouseOut() {
	scroll_viewing_elem_id = '';
	scroll_popup_count = 0;
	setTimeout("_do_scroll_popup_hide()", scroll_popup_hide_timer);
}

function ScrollPopupInstantHide() {
	scroll_popup_count = 0;
	_do_scroll_popup_hide();
}

function ScrollPopupOnUnload() {
	setTimeout("ScrollPopupInstantHide()", scroll_popup_onunload_timer);
}

function _parse_scroll_link(scroll_dir_nesting, scrollitem) {
	return scroll_dir_nesting + 'pages/product/' + scrollitem.t.toLowerCase().replace(' ', '_').replace('&amp;','and').replace(/&[a-zA-Z]+;/g, '').replace('&','and').replace('+','plus').replace(/[^a-z0-9_-]/g, '_') + '/' + scrollitem.i + '/';
}

function DebugAlertScrollBox() {
	var text = '';
	for(var i = 0; i < scrollbox_count; i++) {
		text += scrollbox_list[i].title + '\n';
	}
	alert(text);
}