/*----------------------------------------------------------------------
 *				Released under GNU General Public Licence version >=2.0	*
 *
 *				Author: Oleg Romanov
 *				oleg@bintime.com
/*--------------------------------------------------------------------*/

var slocked = 0;
var pass_automove = 0;
var autoscroll_delay = 3000;
var sfield;

window.addEvent('domready', function() {

		var field = $('flash_movies_wrap');
		sfield = field;

		if (field) {
			var tmp = field.getElementsByTagName('div');
			var mask_item = new RegExp(/^fl_pr_/);
			var items = new Array();
			var i = 0;
			var j = 0;
			var k = 0;
			var tmp3;
			var ptr = 0;
			var id1, id2, id3, id4, id5;
			var item1, item2, item3, item4, item5;
			
			for (i=0;i<tmp.length;i++) {
				if (mask_item.test(tmp[i].id)) {
					items[k] = tmp[i].id;
					k++;
				}
			}
			
			var itemscount = items.length;
		}

		function movel(e,withstop) {
			e.stop();

			if (withstop=="undefined") withstop = 0;

			if (itemscount>4) {

				if (withstop) pass_automove = 1;

				id1 = ptr;
				id2 = ptr+1;
				id3 = ptr+2;
				id4 = ptr+3;
				id5 = ptr+4;
				if (id2>=itemscount) { id2 = 0; id3 = 1; id4 = 2; id5 = 3; }
				if (id3>=itemscount) { id3 = 0; id4 = 1; id5 = 2; }
				if (id4>=itemscount) { id4 = 0; id5 = 1; }
				if (id5>=itemscount) { id5 = 0; }
				
				item1 = new Fx.Morph(items[id1]);
				item2 = new Fx.Morph(items[id2]);
				item3 = new Fx.Morph(items[id3]);
				item4 = new Fx.Morph(items[id4]);
				item5 = new Fx.Morph(items[id5]);															  

				item1.start({'left':-180});
				item2.start({'left':0});
				item3.start({'left':180});
				item4.start({'left':360});
				$(items[id5]).setStyle('left','720px');
				item5.start({'left':540});

				ptr++;
				if (ptr>=itemscount) { ptr = 0; }
			}
		}

		function mover(e,withstop) {
			e.stop();

			if (withstop=="undefined") withstop = 0;

			if (itemscount>3) {

				if (withstop) pass_automove = 1;

				id1 = ptr;
				id2 = ptr+1;
				id3 = ptr+2;
				id4 = ptr+3;
				id5 = ptr-1;
				if (id2>=itemscount) { id2 = 0; id3 = 1; id4 = 2; }
				if (id3>=itemscount) { id3 = 0; id4 = 1; }
				if (id4>=itemscount) { id4 = 0; }
				if (id5<0) { id5 = itemscount-1; }

				item1 = new Fx.Morph(items[id1]);
				item2 = new Fx.Morph(items[id2]);
				item3 = new Fx.Morph(items[id3]);
				item4 = new Fx.Morph(items[id4]);
				item5 = new Fx.Morph(items[id5]);															  

				item1.start({'left':180});
				item2.start({'left':360});
				item3.start({'left':540});
				item4.start({'left':720});
				$(items[id5]).setStyle('left','-180px');
				item5.start({'left':0});

				ptr--;
				if (ptr<0) { ptr = itemscount-1; }
			}

		}

	if (field) {
		$('flash_scroll_larr').addEvent('click',function(e) { movel(e,1) });
		$('flash_scroll_rarr').addEvent('click',function(e) { mover(e,1) });

		$('latest_autoscroll').addEvent('click',function(e) { movel(e) });

		$('flash_movies_tbl').addEvent('mouseover',function(e) { lockscroll(1) });
		$('flash_movies_tbl').addEvent('mouseout',function(e) { lockscroll(0) });
	}

});

function autoscroll() {
	if (slocked==0 && !pass_automove && sfield) {
		if (document.createEvent) {
			evt = document.createEvent('MouseEvents');
			evt.initMouseEvent('click', true, true, document.defaultView,1,0,0,0,0,false,false,false,false,0,null);
			$('latest_autoscroll').dispatchEvent(evt);
		} else {
			$('latest_autoscroll').click();
		}
	} else {
		pass_automove = 0;
	}
	setTimeout("autoscroll()",autoscroll_delay);
}

function lockscroll(state) {

	slocked = state;
	
}

setTimeout("autoscroll()",autoscroll_delay);
