LSD.events.add(window, 'load', function() {
	var center = LSD.dom.get('class', 'center', 'div')[0];
	var products = LSD.dom.get('tag', 'a', center || false);
	if (products) {
		center.divs = new Array();
		center.arrows = new Array();
		
		
		var j = -1;
		var k = 0;
		var height = 0;
		for (var i = 0 ; i < products.length ; i++) {
			var height = Math.max(height, LSD.element.height(products[i]));
			if ((k%6) == 0) {
				j++;
				center.divs[j] = LSD.dom.create('div');
				center.divs[j].style.cssText = 'position: absolute; top: 0; left: 0; width: 413px; background: #FFF;';
			}
			products[i].style.cssText = 'display: inline; height: '+height+'px;';
			center.divs[j].appendChild(products[i]);
			i--;
			k++;
		}

		var div = LSD.dom.create('div');
		div.style.cssText = 'width: 100%; height: '+(height*3+30)+'px; overflow: hidden;';
		for (var i = 0 ; i < center.divs.length ; i++) {
			div.appendChild(center.divs[i]);
			center.divs[i].style.left = '414px';
		}
		center.appendChild(div);
		center.divs[0].style.left = 0;
		LSD.animate.fade_in(center.divs[0]);
		center.current = 0;

		// Arrow function
		center.horizontal = function(center) {
			if (center.current < (center.divs.length-1)) {
				center.arrows['right'].src = center.arrows['right'].src.replace('off', 'on');
				center.arrows['right'].style.cursor = 'pointer'; 
			} else {
				center.arrows['right'].src = center.arrows['right'].src.replace('on', 'off');
				center.arrows['right'].style.cursor = 'default';
			}
			if (center.current > 0) {
				center.arrows['left'].src = center.arrows['left'].src.replace('off', 'on');
				center.arrows['left'].style.cursor = 'pointer'; 
			} else {
				center.arrows['left'].src = center.arrows['left'].src.replace('on', 'off');
				center.arrows['left'].style.cursor = 'default';
			}
		}

		// Create Arrows
		var scrolling = false;
		var arrows = Array('right', 'left');
		for (var i = 0 ; i < arrows.length ; i++) {
			var arrow = LSD.dom.create('img');
			arrow.style.cssText = 'width: 23px; height: 18px; float: right;';
			arrow.alt = arrows[i] == 'right'?'scroll right':'scroll left';
			if (arrows[i] == 'right') {
				if (center.divs.length <= 1) {
					arrow.src = 'images/scroll_'+arrows[i]+'_off.gif';
				} else {
					arrow.src = 'images/scroll_'+arrows[i]+'_on.gif';
					arrow.style.cursor = 'pointer';
				}
				center.arrows['right'] = arrow;
				arrow.style.margin = '0 10px 0 5px';
				(function() {
					var div = center;
					LSD.events.add(arrow, 'mousedown', function() {
						if ((div.current < (div.divs.length-1)) && (!scrolling)) {
							for (var i = 0 ; i <= 150 ; i += 5) {
								(function(){
									var pos = i;
									scrolling = setTimeout(function(){
										center.divs[center.current-1].style.left = (0-(pos/150)*413)+'px';
									}, (pos+1)*10);
								})();
							}
					
							for (var i = 0 ; i <= 150 ; i += 5) {
								(function(){
									var pos = i;
									scrolling = setTimeout(function(){
										center.divs[center.current].style.left = (413-(pos/150)*413)+'px';
									}, (pos+1)*10);
								})();
							}
							setTimeout(function() { scrolling = false; }, 1600);
							center.current++;
						}
						center.horizontal(center);
					});
				})();
			} else {
				arrow.src = 'images/scroll_'+arrows[i]+'_off.gif';
				center.arrows['left'] = arrow;
				(function() {
					LSD.events.add(arrow, 'mousedown', function() {
						if ((center.current > 0) && (!scrolling)) {
							for (var i = 0 ; i <= 150 ; i += 5) {
								(function(){
									var pos = i;
									scrolling = setTimeout(function(){
										center.divs[center.current].style.left = ((pos/150)*413-413)+'px';
									}, (pos+1)*10);
								})();
							}
					
							for (var i = 0 ; i <= 150 ; i += 5) {
								(function(){
									var pos = i;
									scrolling = setTimeout(function(){
										center.divs[center.current+1].style.left = (0+(pos/150)*413)+'px';
									}, (pos+1)*10);
								})();
							}
							setTimeout(function() { scrolling = false; }, 1600);
							center.current--;
						}
						center.horizontal(center);
					});
				})();
			}
			LSD.dom.insert_last(arrow, center);
		}
	}

	// Start Right Column
	
	var right = LSD.dom.get('class', 'right', 'div')[0];
	var ul = LSD.dom.get('tag', 'ul', right || false)[0];
	if (ul) {
		timer = false;
		right.arrows = new Array();
		var div = LSD.dom.create('div');
		div.style.cssText = 'width: 100%; height: '+(height*3+30)+'px; overflow: hidden;';
		right.position_bottom = LSD.element.height(ul) - (height*3+30);
		ul.style.top = 0;
		div.appendChild(ul);
		right.appendChild(div);
		LSD.animate.show(ul);
		right.current_position = 0

		// Arrow Functions
		right.vertical = function(elem) {
			if (elem.current_position < 0) {
				elem.arrows['up'].src = elem.arrows['up'].src.replace('off', 'on');
				elem.arrows['up'].style.cursor = 'pointer'; 
			} else {
				elem.arrows['up'].src = elem.arrows['up'].src.replace('on', 'off');
				elem.arrows['up'].style.cursor = 'default';
			}
			if (elem.current_position > -elem.position_bottom) {
				elem.arrows['down'].src = elem.arrows['down'].src.replace('off', 'on');
				elem.arrows['down'].style.cursor = 'pointer'; 
			} else {
				elem.arrows['down'].src = elem.arrows['down'].src.replace('on', 'off');
				elem.arrows['down'].style.cursor = 'default';
			}
		}
		
		// Create Arrows
		var arrows = Array('up', 'down');
		for (var i = 0 ; i < arrows.length ; i++) {
			var arrow = LSD.dom.create('img');
			arrow.style.cssText = 'width: 18px; height: 23px; float: right; clear: both; margin: 0 10px 0 0;';
			arrow.alt = arrows[i] == 'up'?'scroll up':'scroll down';
			arrow.src = 'images/scroll_'+arrows[i]+'_off.gif';
			if (arrows[i] == 'up') {
				(function() {
					right.arrows['up'] = arrow;
					arrow.src = 'images/scroll_'+arrows[i]+'_off.gif';
					arrow.style.marginBottom = '5px';
					LSD.events.add(arrow, 'mousedown', function() {
						if (right.current_position < 0) {
								for (var i = 0 ; i <= 50 ; i += 5) {
									(function(){
										var pos = i;
										setTimeout(function(){
											if (right.current_position < 0) {
												right.current_position = right.current_position + 5;
												ul.style.top = right.current_position+'px';
											}
										}, (pos+1)*10);
									})();
								}
							timer = setInterval(function() {
								for (var i = 0 ; i <= 50 ; i += 5) {
									(function(){
										var pos = i;
										setTimeout(function(){
											if (right.current_position < 0) {
												right.current_position = right.current_position + 5;
												ul.style.top = right.current_position+'px';
											} else {
												clearInterval(timer);
											}
										}, (pos+1)*10);
									})();
								}
							}, 500);
						}
						setTimeout(function() { right.vertical(right); }, 550);
					});
					LSD.events.add(arrow, 'mouseup', function() { clearInterval(timer); right.vertical(right); });
				})();
			} else {
				(function() {
					right.arrows['down'] = arrow;
					if (right.position_bottom < 0) {
						arrow.src = 'images/scroll_'+arrows[i]+'_off.gif';
					} else {
						arrow.src = 'images/scroll_'+arrows[i]+'_on.gif';
						arrow.style.cursor = 'pointer';
					}
					LSD.events.add(arrow, 'mousedown', function() {
						if (right.current_position > -right.position_bottom) {
								for (var i = 0 ; i <= 50 ; i += 5) {
									(function(){
										var pos = i;
										setTimeout(function(){
											if (right.current_position > -right.position_bottom) {
												right.current_position = right.current_position - 5;
												ul.style.top = right.current_position+'px';
											}
										}, (pos+1)*10);
									})();
								}
							timer = setInterval(function() {
								for (var i = 0 ; i <= 50 ; i += 5) {
									(function(){
										var pos = i;
										setTimeout(function(){
											if (right.current_position > -right.position_bottom) {
												right.current_position = right.current_position - 5;
												ul.style.top = right.current_position+'px';
											} else {
												clearInterval(timer);
											}
										}, (pos+1)*10);
									})();
								}
							}, 500);
						}
						setTimeout(function () { right.vertical(right); }, 550);
					});
					LSD.events.add(arrow, 'mouseup', function() { clearInterval(timer); right.vertical(right); });
				})();
			}
			LSD.dom.insert_last(arrow, right);
		}
	}
});
