var commonImg = new String(location.pathname);
if (commonImg.indexOf("/") != 0) { commonImg = "/" + commonImg }
commonImg = commonImg.substr(0, commonImg.indexOf("/design/") + "/design/".length) + "common/img/";
function categoryTitle(conf) {
	this.category    = "";
	this.imageLength = 0;
	this.interval    = 5000;
	this.step        = 5;
	this.duration    = 30;

	this.init(conf);
}
categoryTitle.prototype = {
	_backToHome : (function(){
		var preload = BAPreloadImage(commonImg + "back_to_home.png");

		var node = document.createElementBA("img");
		node.setAttributeBA("src", preload.src);
		node.setAttributeBA("alt", "Back to Home");

		var link = document.createElementBA("a");
		link.appendClassNameBA("back");
		link.appendChildBA(node);

		if (BA.ua.isWinIE && BA.ua.revision < 7) {
			node.setAttributeBA("src", commonImg + "transparent.gif");
			node.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + preload.src + "');"
		}

		return link;
	})(),
	_image : (function(){
		var img = document.createElementBA("img");
		img.setAttributeBA("id", "shapeImage");
		img.setAttributeBA("src", commonImg + "shape_01.png");
		return img;
	})(),
	init : function(conf) {
		if (conf.category)    this.category    = conf.category;
		if (conf.imageLength) this.imageLength = conf.imageLength;
		if (conf.interval)    this.interval    = conf.interval;
		if (conf.step)        this.step        = conf.step;
		if (conf.duration)    this.duration    = conf.duration;

		var sitetitle = document.createElementBA("img");
		sitetitle.setAttributeBA("src", commonImg + "sitetitle_" + this.category +".png");
		sitetitle.appendClassNameBA("title");

		if (BA.ua.isWinIE && BA.ua.revision < 7) {
			sitetitle.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + sitetitle.getAttributeBA("src") + "');"
			sitetitle.setAttributeBA("src", commonImg + "transparent.gif");
		}

		var shape = document.createElementBA("div");
		shape.appendClassNameBA("shape");
		shape.appendChildBA(this._image);

		BAAddOnload(function(){
			var link = document.getElementsByTagNameBA("link");
			var indexTitle = "";
			for (var i = 0, n = link.length; i < n; i++) {
				if (link[i].getAttributeBA("rel").indexOf("index") != -1) {
					this._backToHome.setAttributeBA("title", link[i].getAttributeBA("title") || "");
					this._backToHome.setAttributeBA("href",  link[i].getAttributeBA("href")  || "");
					break;
				}
			}

			var container = document.getElementByIdBA("category-title");
			sitetitle.setAttributeBA("alt", container.getElementsByTagNameBA("img")[0].getAttributeBA("alt"));

			container.removeAllChildrenBA();
			container.appendChildBA(sitetitle);
			container.appendChildBA(this._backToHome);
			container.appendChildBA(shape);

			this.crossFader = new BAImageCrossFader(this._image);
			this.crossFader.setParam(this.step, this.duration);

			this.start();
		}, this);
	},
	start : function() {
		var switcher = new BAImageSwitcher(this._image.getAttributeBA("id"));
		for (var i = 1, n = this.imageLength; i < n; i++) {
			var num = i;
			if ((new String(i)).length < 2) {
				num = "0" + i;
			}
			var src = this._image.getAttributeBA("src").replace(/[0-9]+\.png/, num + ".png");
			switcher.addImage(src);
		}
		switcher.startRotateRandomly(this.interval);
		switcher.enableCrossFader(this.step, this.duration);
	}
}

