var Elaborator = Class.create({
	populate: function (input) {
		e = input.getElementsByTagName("item");
		width = (this.width - 5) / e.length;
		this.items = new Array();
		real = this;
		links = input.getElementsByTagName("link");
		images = input.getElementsByTagName("image");
		widths = input.getElementsByTagName("width");
		for (i = 0; i < e.length; i++) {
			d = document.createElement("div");
			w = {
				index: i,
				node: $(d),
				link: links[i].firstChild.nodeValue,
				image: images[i].firstChild.nodeValue,
				width: parseInt(widths[i].firstChild.nodeValue)
			};
			itemd = w;
			zIndex = i * 10;
			margin = i * width;
			
			if (i==0) {
				itemd.node.setStyle({
					'background': 'transparent url("' + itemd.image + '") no-repeat center left',
					'width': width + 'px',
					'height': this.height + 'px',
					'float': 'left'
				});
			
			} else {
				itemd.node.setStyle({
					'background': 'transparent url("' + itemd.image + '") no-repeat center left',
					'width': width + 'px',
					'height': this.height + 'px',
					'float': 'left',
					'borderLeft': '1px solid #555'
				});
			
			}
			if (i + 1 < e.length) {
				dd = $(document.createElement("div"));
				dd.setStyle({
					'float': 'right',
					'width': '8px',
					'height': '300px',
					'background': 'transparent url("http://members.phpa.com/en/asset/557/shadow.png") repeat-y top right'
				});
				itemd.node.appendChild(dd);
			}
			Event.observe(itemd.node, 'click', function (event) {
			
				var targetLink = real.findItem(Event.element(event)).link;

				if (targetLink.substr(0, 7) == 'http://' || targetLink.substr(0, 7) == 'https://') {
					
					
					window.open(targetLink, '_blank');
									
				} else {
					location.href = real.findItem(Event.element(event)).link;			
				}
				
				return false;

				
			});
			Event.observe(itemd.node, 'mouseover', function (event) {
				ee = $(Event.element(event));
				ddd = real.findItem(ee);
				if (ddd != null) {
					if (ee.getWidth() == 597 /*ddd.width + 1*/ ) {
						return
					}
				} else {
					if (ee.parentNode.getWidth() == 597 /*ddd.width + 1*/ ) return
				}
				effects = new Array();
				for (j = 0; j < real.items.length; j++) {
					if (j == real.items.length - 1) {
						spaceOffset = 0;
					} else {
						spaceOffset = 0;
					}
				
					effects[j] = new Effect.Morph(real.items[j].node, {
						style: 'width: ' + (((real.width - 570) / real.items.length) - spaceOffset) + 'px',
						sync: true
					});
				}
				ddd = real.findItem(ee);
				
				
				if (ddd != null) {
					if (ddd == real.items[real.items.length-1]) {
						morphWidth = "596";
					} else {
						morphWidth = "596";
					}
					
					effects[j] = new Effect.Morph(ee, {
						style: 'width: ' + morphWidth + 'px; background: transparent url("' + ddd.image + '") no-repeat center left',
						sync: true
					});
				} else {
					ddd = real.findItem(ee.parentNode);
					
					
					if (ddd == real.items[real.items.length-1]) {
						morphWidth = "596";
					} else {
						morphWidth = "596";
					}
					effects[j] = new Effect.Morph(ee.parentNode, {
						style: 'width: ' + morphWidth + 'px; background: transparent url("' + ddd.image + '") no-repeat center left',
						sync: true
					});
				}
				new Effect.Parallel(effects, {
					queue: {
						position: 'end',
						scope: 'elaborator',
						limit: 2
					},
					duration: 0.1
				});
				real.mouseovers++
			});
			Event.observe(itemd.node, 'mouseout', function (event) {
				real.mouseovers--
			});
			this.target.appendChild(itemd.node);
			this.items[i] = itemd
		}
		Event.observe(this.target, 'mouseout', function (event) {
			setTimeout(real.flush, 1000)
		});

		function eventHandlerFunction(e) {
			real.mouseX = Event.pointerX(e), real.mouseY = Event.pointerY(e)
		}
		Event.observe($$('body')[0], "mousemove", eventHandlerFunction, false)
	},
	flush: function () {
		topCoords = real.target.cumulativeOffset();
		box = real.target.getDimensions();
		bottomCoords = {
			left: topCoords.left + box.width,
			top: topCoords.top + box.height
		};
		if (!(real.mouseX <= bottomCoords.left && real.mouseX >= topCoords.left && real.mouseY <= bottomCoords.top && real.mouseY <= bottomCoords.top)) {
			j = 0;
			$A(real.items).each(function (e) {
				
				effects[j] = new Effect.Morph(e.node, {
					style: 'width: ' + ((real.width - 5) / real.items.length) + 'px',
					sync: true
				});
				j++;
			});

			new Effect.Parallel(effects, {
				queue: {
					position: 'end',
					scope: 'elaborator',
					limit: 1
				},
				duration: 0.15
			})
		}
	},
	initialize: function (element, width, height, url) {
		element = $(element);
		this.target = element;
		this.width = width;
		this.height = height;
		this.lastNode = null;
		this.mouseovers = 0;
		real = this;
		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function (res, param) {
				real.populate(res.responseXML)
			}
		})
	},
	findItem: function (node) {
		for (i = 0; i < this.items.length; i++) {
			if (this.items[i].node == node) {
				return this.items[i]
			}
		}
		return null
	}
});
