/********* CLM INIT **********/
if(typeof CLM != 'object') {
	var CLM = {NAME : "CLM"};
};

/*****************************/
/**
* MenuAnimator class
*/
CLM.MenuAnimator = SZN.ClassMaker.makeClass({
	NAME : "MenuAnimator",
	VERSION : "1.0",
	CLASS : "class"
});

CLM.MenuAnimator.prototype.MenuAnimator = function(direction, menu, menuBg, menuItemImgSrc, optionsObj)
{
	this.direction = direction;
	this.menu = menu;
	this.menuBg = menuBg;
	this.menuItemImgSrc = menuItemImgSrc;
	
	this.menuItems = [];
	
	var elmMenuItems = this.menu.getElementsByTagName('li');
	for (var i = 0; i < elmMenuItems.length; i++) {
		this.menuItems.push(new CLM.MenuAnimator.MenuItem(this, i, elmMenuItems[i]));
	}
	
	this.animCurPos = 0;
	this.animDestinationPos = 0;
	this.adminSpeed = 0;
	
	this.moveArrowToItemIndex = 0;
	
	this.options = {
		'speed' : 1,                   /* speed in seconds */
		'delta' : 2                    /* delta move */
	}
	if (optionsObj) {
		for (var p in optionsObj) {
			this.options[p] = optionsObj[p];
		}
	}
}
CLM.MenuAnimator.prototype.arrowDestinationReached = function(menuItemIndex)
{
	this.menuBg.style.background = 'none';
	SZN.Dom.addClass(this.menuItems[menuItemIndex].elm, 'active');
	document.location = this.menuItems[menuItemIndex].elmLink.href;
}
CLM.MenuAnimator.prototype.moveArrowTo = function(menuItemIndex)
{
	this.moveArrowToItemIndex = menuItemIndex;
	
	var activeItem = 0;
	for (var i = 0; i < this.menuItems.length; i++) {
		if (SZN.Dom.hasClass(this.menuItems[i].elm, 'active')) {
			activeItem = i;
			SZN.Dom.removeClass(this.menuItems[i].elm, 'active');
		}
	}
	if (this.direction == 'vertical') {
		this.animCurPos = this.menuItems[activeItem].arrowPosY;
		this.animDestinationPos = this.menuItems[menuItemIndex].arrowPosY;
		this.adminSpeed = this.options.speed * 1000 / (Math.abs(this.animDestinationPos - this.animCurPos) / this.options.delta);
		if (this.animDestinationPos > this.animCurPos) {
			this.animateArrowMoveDown();
		}else {
			this.animateArrowMoveUp();
		}
	}else if (this.direction == 'horizontal') {
		this.animCurPos = this.menuItems[activeItem].arrowPosX;
		this.animDestinationPos = this.menuItems[menuItemIndex].arrowPosX;
		this.adminSpeed = this.options.speed * 1000 / (Math.abs(this.animDestinationPos - this.animCurPos) / this.options.delta);
		if (this.animDestinationPos > this.animCurPos) {
			this.animateArrowMoveRight();
		}else {
			this.animateArrowMoveLeft();
		}
	}
}


/*****************************/
/**
* MenuAnimator class
*/
CLM.MenuAnimatorV = SZN.ClassMaker.makeClass({
	NAME : "MenuAnimatorV",
	VERSION : "1.0",
	CLASS : "class",
	EXTEND: CLM.MenuAnimator
});

CLM.MenuAnimatorV.prototype.MenuAnimatorV = function(menu, menuBg, menuItemImgSrc, optionsObj)
{
	this.MenuAnimator('vertical', menu, menuBg, menuItemImgSrc, optionsObj);
	
	this.animateArrowMoveUp = SZN.bind(this, this.animateArrowMoveUp);
	this.animateArrowMoveDown = SZN.bind(this, this.animateArrowMoveDown);
}
CLM.MenuAnimatorV.prototype.moveArrowPosV = function(y)
{
	this.menuBg.style.background = 'transparent url(' + this.menuItemImgSrc + ') no-repeat scroll 1px ' + y + 'px';
}
CLM.MenuAnimatorV.prototype.animateArrowMoveUp = function()
{
	this.moveArrowPosV(this.animCurPos);
	if (this.animCurPos > this.animDestinationPos) {
		this.animCurPos -= this.options.delta;
		setTimeout(this.animateArrowMoveUp, this.adminSpeed);
	}else {
		this.arrowDestinationReached(this.moveArrowToItemIndex);
	}
}
CLM.MenuAnimatorV.prototype.animateArrowMoveDown = function()
{
	this.moveArrowPosV(this.animCurPos);
	if (this.animCurPos < this.animDestinationPos) {
		this.animCurPos += this.options.delta;
		setTimeout(this.animateArrowMoveDown, this.adminSpeed);
	}else {
		this.arrowDestinationReached(this.moveArrowToItemIndex);
	}
}


/*****************************/
/**
* MenuAnimator class
*/
CLM.MenuAnimatorH = SZN.ClassMaker.makeClass({
	NAME : "MenuAnimatorH",
	VERSION : "1.0",
	CLASS : "class",
	EXTEND: CLM.MenuAnimator
});


CLM.MenuAnimatorH.prototype.MenuAnimatorH = function(menu, menuBg, menuItemImgSrc, optionsObj)
{
	this.MenuAnimator('horizontal', menu, menuBg, menuItemImgSrc, optionsObj);
	
	this.animateArrowMoveLeft = SZN.bind(this, this.animateArrowMoveLeft);
	this.animateArrowMoveRight = SZN.bind(this, this.animateArrowMoveRight);
}
CLM.MenuAnimatorH.prototype.moveArrowPosH = function(x)
{
	this.menuBg.style.background = 'transparent url(' + this.menuItemImgSrc + ') no-repeat scroll ' + x + 'px ' + '29px';
}
CLM.MenuAnimatorH.prototype.animateArrowMoveLeft = function()
{
	this.moveArrowPosH(this.animCurPos);
	if (this.animCurPos > this.animDestinationPos) {
		this.animCurPos -= this.options.delta;
		setTimeout(this.animateArrowMoveLeft, this.adminSpeed);
	}else {
		this.arrowDestinationReached(this.moveArrowToItemIndex);
	}
}
CLM.MenuAnimatorH.prototype.animateArrowMoveRight = function()
{
	this.moveArrowPosH(this.animCurPos);
	if (this.animCurPos < this.animDestinationPos) {
		this.animCurPos += this.options.delta;
		setTimeout(this.animateArrowMoveRight, this.adminSpeed);
	}else {
		this.arrowDestinationReached(this.moveArrowToItemIndex);
	}
}


/**
* ThumbnailImage class
*/
CLM.MenuAnimator.MenuItem = SZN.ClassMaker.makeClass({
	NAME : "MenuItem",
	VERSION : "1.0",
	CLASS : "class"
});

CLM.MenuAnimator.MenuItem.prototype.$constructor = function(menuAnimator, index, elm)
{
	this.menuAnimator = menuAnimator;
	
	this.index = index;
	
	this.elm = elm;
	
	var menuPos = SZN.Dom.getBoxPosition(this.menuAnimator.menuBg);
	if (this.menuAnimator.direction == 'vertical') {
		this.arrowPosY = SZN.Dom.getBoxPosition(this.elm, this.menuAnimator.menuBg).top + 8;
		if (SZN.Browser.client != 'ie') {
			this.arrowPosY -= menuPos.top;
		}
	}else if (this.menuAnimator.direction == 'horizontal') {
		this.arrowPosX = SZN.Dom.getBoxPosition(this.elm, this.menuAnimator.menuBg).left + ((this.elm.clientWidth / 2) - (22 / 2)) - 5;
		if (SZN.Browser.client != 'ie') {
			this.arrowPosX -= menuPos.left;
		}
	}
	
	this.active = SZN.Dom.hasClass(this.elm, 'active');
	
	var menuItemLinks = elm.getElementsByTagName('a');
	if (menuItemLinks.length > 0) {
		this.elmLink = menuItemLinks[0];
		SZN.Events.addListener(this.elmLink, "click", this, "moveArrowTo", false, true);
	}else {
		throw new Error('CLM.MenuAnimator.MenuItem: invalid menu item - missing link element');
	}
}
CLM.MenuAnimator.MenuItem.prototype.moveArrowTo = function(e)
{
	if (e) {
		SZN.Events.cancelDef(e);
	}
	this.menuAnimator.moveArrowTo(this.index);
}

