var scion_ch = new Array();
var img_mass = new Array();
var mass_w = 0;
var SwitchDuration = 0.50;
var MenuScope = 'Menuscope';
var block = false;
var count = 0;
var offset = 0;
var scion_c = 0;
var move_distance = 0;
var center_place = 0;

function imageSlide() {
	
	
	this.scrollRight = function() {
		if (block) return;
		block = true;
		if (offset == scion_ch.length){
			elem.style.left = center_place + 'px';
			offset = 0;
		}

		el_this = parseFloat(scion_ch[scion_c].offsetWidth / 2);		
		if (scion_ch[scion_c+1]) {
			el_next = parseFloat(scion_ch[scion_c+1].offsetWidth / 2);
			scion_c++;
		} else {
			scion_c = 0;
			el_next = parseFloat(scion_ch[scion_c].offsetWidth / 2);
		}
		move_distance = el_this + 30 + el_next;
		
		
		new Effect.Move(elem, { x: -move_distance, y: 0, duration: SwitchDuration, queue: {position: 'end', scope: MenuScope, limit: 1}});
		offset++;
		setTimeout(function() {block=false;}, 500*SwitchDuration);
	}
	
	this.scrollLeft = function() {
		if(block) return;
		block = true;
		if (offset == -scion_ch.length){
			elem.style.left = center_place + 'px';
			offset = 0;
		}
		
		el_this = parseFloat(scion_ch[scion_c].offsetWidth / 2);		
		if (scion_c != 0) {
			el_next = parseFloat(scion_ch[scion_c-1].offsetWidth / 2);
			scion_c--;
		} else {
			scion_c = scion_ch.length - 1;
			el_next = parseFloat(scion_ch[scion_c].offsetWidth / 2);
		}
		move_distance = el_this + 30 + el_next;

		
		new Effect.Move(elem, { x: move_distance, y: 0, duration: SwitchDuration, queue: {position: 'end', scope: MenuScope, limit: 1}});
		offset--;
		setTimeout(function() {block = false;}, 1000*SwitchDuration);
	}
	
}

imageSlide.prototype.init = function() {

	elem = $('slideContainer');
	if (!elem) {return false;}

	this.scion = elem.firstDescendant();
	scion = this.scion;
	scion_ch = scion.firstDescendant().childElements();

	for (var i = 0; i <= (scion_ch.length-1); i++) {
		img_mass[i] = parseFloat(scion_ch[i].firstDescendant().firstDescendant().firstDescendant().style.width) + 30;
		mass_w += img_mass[i];
	}
	scion.firstDescendant().style.width = mass_w + 'px';
	scion.style.width = mass_w + 'px';


	var leftProxy_1=document.createElement('div');
	leftProxy_1.width = mass_w;
	leftProxy_1.style.position = 'absolute';

		leftProxy_1.style.top = 0 + 'px';
		leftProxy_1.style.left = -mass_w + 'px';
	leftProxy_1.id='leftSlideProxy_1';
	leftProxy_1.className='slider slideContainer';
	leftProxy_1.innerHTML = scion.innerHTML;

	var leftProxy_2=document.createElement('div');
	leftProxy_2.width = mass_w;
	leftProxy_2.style.position = 'absolute';
		leftProxy_2.style.width = mass_w + 'px';
		leftProxy_2.style.top = 0 + 'px';
		leftProxy_2.style.left = -mass_w*2 + 'px';
	leftProxy_2.id='leftSlideProxy_2';
	leftProxy_2.className='slider slideContainer';
	leftProxy_2.innerHTML = scion.innerHTML;

	var rightProxy_1=document.createElement('div');
	rightProxy_1.width = mass_w;
	rightProxy_1.style.position = 'absolute';
		rightProxy_1.style.width = mass_w + 'px';
		rightProxy_1.style.top = 0 + 'px';
		rightProxy_1.style.left = mass_w + 'px';
	rightProxy_1.id='rightSlideProxy';
	rightProxy_1.className='slider slideContainer';
	rightProxy_1.innerHTML = scion.innerHTML;



	elem.insertBefore(leftProxy_1, elem.firstDescendant());
	elem.insertBefore(leftProxy_2, leftProxy_1);
	elem.appendChild(rightProxy_1);

	center_place = ($('slide_block').getWidth() / 2) - (parseInt($(scion_ch[0]).firstDescendant().firstDescendant().firstDescendant().style.width) / 2);
	elem.style.left = center_place + 'px';
}

function frame_on(obj) {
	var point = obj;
	point.className = "slide_sheet slide_on";
}
function frame_out(obj) {
	var point = obj;
	point.className = "slide_sheet";
}

imageSlide_new = new imageSlide();

document.observe('dom:loaded', function(){
	imageSlide_new.init();
});

