/**
 * Slider - Animated SDI by Flaire V.O.F.
 *
 * LICENSE: This codebase is commercial software.
 * You may not copy, redistribute and/or modify it.
 *
 * @category   Interface
 * @author     Steven Voorn <svoorn@flaire.nl>
 * @copyright  Copyright (C) 2006 Flaire V.O.F.
 * @license    Commercial
 * @link       http://www.flaire.nl
 */

var slider_c = function()
{
	this.collector_id_s = '';
	this.slide_id_a = [];
	this.previous_selector_id_s = '';
	this.next_selector_id_s = '';
	this.easing_i = 10;
	this.current_slide_index_i = 0;
	this.slide_interval_i = 0;

	/**/

	this.getXByElement = function(element_o)
	{
		return element_o.offsetLeft;
	};

	this.getYByElement = function(element_o)
	{
		return element_o.offsetTop;
	};

	this.getCleanNumber = function(content_s)
	{
		return new Number(content_s.replace(/[^0-9\.\+\-]/g, ''));
	};

	this.setElementVisibility = function(id_s, state_s)
	{
		var element_o = document.getElementById(id_s);
			element_o.style.visibility = state_s;
	};

	this.setSelectorVisibility = function()
	{
		if (this.previous_selector_id_s)
		{
			this.setElementVisibility(this.previous_selector_id_s, (this.current_slide_index_i <= 0) ? 'hidden' : 'visible');
			this.setElementVisibility(this.next_selector_id_s, (this.current_slide_index_i >= (this.slide_id_a.length - 1)) ? 'hidden' : 'visible');
		}
	};

	this.setSlideToPosition = function(element_o, from_x_i, to_x_i, from_y_i, to_y_i)
	{
		var this_o = this;

		var offset_i = 2;

		var left_b = (to_x_i < from_x_i);
		var current_x_i = from_x_i;
		var change_x_b = (from_x_i != to_x_i);

		var top_b = (to_y_i < from_y_i);
		var current_y_i = from_y_i;
		var change_y_b = (from_y_i != to_y_i);

		var arrived_x_b = true;
		var arrived_y_b = true;

		var difference_x_i = 0;
		var difference_y_i = 0;

		var step_x_i = 0;
		var step_y_i = 0;

		var setUpdate = function()
		{
			if (change_x_b)
			{
				difference_x_i = (to_x_i + (left_b ? (offset_i * -1) : offset_i)) - current_x_i;
				step_x_i = difference_x_i / this_o.easing_i;

				current_x_i = current_x_i + step_x_i;

				element_o.style.left = current_x_i + 'px';

				arrived_x_b = left_b ? (current_x_i < to_x_i) : (current_x_i > to_x_i);
			}

			if (change_y_b)
			{
				difference_y_i = (to_y_i + (top_b ? (offset_i * -1) : offset_i)) - current_y_i;
				step_y_i = difference_y_i / this_o.easing_i;

				current_y_i = current_y_i + step_y_i;

				element_o.style.top = current_y_i + 'px';

				arrived_y_b = top_b ? (current_y_i < to_y_i) : (current_y_i > to_y_i);
			}

			if (arrived_x_b && arrived_y_b)
			{
				element_o.style.left = to_x_i + 'px';
				element_o.style.top = to_y_i + 'px';

				clearInterval(this_o.slide_interval_i);
			}
		};

		clearInterval(this.slide_interval_i);
		this.slide_interval_i = setInterval(setUpdate, 10);
	};

	/**/

	this.setCollectorById = function(id_s)
	{
		this.collector_id_s = id_s;
	};

	this.setSlideById = function(id_s)
	{
		this.slide_id_a[this.slide_id_a.length] = id_s;
	};

	this.setPreviousSelectorById = function(id_s)
	{
		this.previous_selector_id_s = id_s;
	};

	this.setNextSelectorById = function(id_s)
	{
		this.next_selector_id_s = id_s;
	};

	this.setEasing = function(value_i)
	{
		this.easing_i = value_i;
	};

	/**/

	this.setSlider = function()
	{
		this.setSelectorVisibility();
	};

	/**/

	this.setSlide = function(index_i)
	{
		var valid_index_b = ((index_i >= 0) && (index_i <= (this.slide_id_a.length - 1)));

		this.current_slide_index_i = valid_index_b ? index_i : 0;
		this.setSelectorVisibility();

		var start_element_o = document.getElementById(this.collector_id_s);
		var start_element_x_i = this.getXByElement(start_element_o);
		var start_element_y_i = this.getYByElement(start_element_o);

		var target_element_o = document.getElementById(this.slide_id_a[this.current_slide_index_i]);
		var target_element_x_i = this.getXByElement(target_element_o);
		var target_element_y_i = this.getYByElement(target_element_o);

			target_element_x_i = start_element_x_i + target_element_x_i;
			target_element_y_i = start_element_y_i + target_element_y_i;

		var left_b = (start_element_x_i < target_element_x_i);
		var top_b = (start_element_y_i < target_element_y_i);

		var to_x_i = left_b ? (start_element_x_i - target_element_x_i) : (start_element_x_i + (target_element_x_i * -1));
		var to_y_i = top_b ? (start_element_y_i - target_element_y_i) : (start_element_y_i + (target_element_y_i * -1));

		this.setSlideToPosition(start_element_o, start_element_x_i, to_x_i, start_element_y_i, to_y_i);
	};

	this.setPrevious = function()
	{
		var valid_index_b = (this.current_slide_index_i > 0);
		var index_i = valid_index_b ? (this.current_slide_index_i - 1) : 0;

		this.setSlide(index_i);
	};

	this.setNext = function()
	{
		var valid_index_b = (this.current_slide_index_i < (this.slide_id_a.length - 1));
		var index_i = valid_index_b ? (this.current_slide_index_i + 1) : (this.slide_id_a.length - 1);

		this.setSlide(index_i);
	};

	this.getSlideCount = function()
	{
		return this.slide_id_a.length;
	};
};