window.DEBUG=true;eval(liligo.namespace);

var DirectoryList = new Application({	
	init: function() {
		this.dirIndex = $("dir-index");
		if (!$("directoryList") || !window.directories || !window.directories.length || !this.dirIndex) return;
		this.dirIndex.innerHTML = "";
		this.key = window.index; // optional range parameter in the url
		this.index = 0;
		
		this.keys = {};
		if (config.directoryType == "supplier") {
			this.template = new liligo.VTemplate("/seo/v3/common/templates/directory/supplier.vm", function (context) { 
var text = new liligo.__VT_StringCat(), _function = 'function', 
velocityCount = 0;
if (context.velocityCount) velocityCount=context.velocityCount;

if (context.showKey) {
text.push('<li class="key">');
text.push( context.elem.key);
text.push('</li>');
}
text.push('<li>	<a href="');
text.push( context.elem.url);
text.push('" title="');
text.push( context.elem.suppName);
text.push('">		<img src="/air/logo/');
text.push( context.elem.suppCode);
text.push('.gif" alt="');
text.push( context.elem.suppName);
text.push('" border="0" />	</a>	<span>		<a href="');
text.push( context.elem.url);
text.push('" title="');
text.push( context.elem.suppName);
text.push('">');
text.push( context.elem.suppName);
text.push('</a> :	</span>	<strong>');
text.push( context.elem.suppCountry);
text.push('</strong>');
if (context.elem.code) {
text.push(' (');
text.push( context.elem.code);
text.push(')');
}
text.push('</li>							');
return text.toString();
}
);
		} else {
			this.template = new liligo.VTemplate("/seo/v3/common/templates/directory/country.vm", function (context) { 
var text = new liligo.__VT_StringCat(), _function = 'function', 
velocityCount = 0;
if (context.velocityCount) velocityCount=context.velocityCount;

if (context.showKey) {
text.push('<li class="key">');
text.push( context.elem.key);
text.push('</li>');
}
text.push('<li><a href="');
text.push( context.elem.url);
text.push('" title="');
text.push( context.elem.urlname);
text.push('">');
text.push( context.elem.urlname);
text.push('</a>');
if (context.elem.code) {
text.push(' (');
text.push( context.elem.code);
text.push(')');
}
text.push('</li>');
return text.toString();
}
);			
		}

		// calc letter groups
		var currentKey;
		forEach(window.directories, function(directory) {
			if (directory.key != currentKey) {
				currentKey = directory.key;
				this.keys[currentKey] = 1;
			} else {
				this.keys[currentKey]++;
			}
		}, this);

		this.keyMode = true;
		if (this.keyMode) { // pager by letters

			// add key links
			var index = 0;
			forEach(this.keys, function(key, name) {
				if (this.key == name) this.index = index;
				var a = document.createElement("a");
				a.innerHTML = name;
				__Eo(a, "click", bind(this.setPage, this, index++));
				a.href = "javascript:void(0)";
				this.dirIndex.appendChild(a);
			}, this);

		} else { // pager by letter groups
			
			this.limit = 25;
			this.ranges = [];

			// calc ranges
			var currentRange = 0;
			forEach(this.keys, function(value, name) {
				var range = (this.ranges.length ? this.ranges[this.ranges.length - 1] : null);
				if (range && range.size < this.limit) {
					range.keys.push(name);
					range.size += value;
				} else {
					range = {};
					range.keys = [name];
					range.size = value;
					range.index = currentRange++;
					this.ranges.push(range);
				}
			}, this);
	
			// add range links
			forEach(this.ranges, function(range) {
				var a = document.createElement("a");
				a.innerHTML = (range.keys.length > 1) ? range.keys[0] +"-"+ range.keys[range.keys.length - 1] : range.keys[0];
				__Eo(a, "click", bind(this.setPage, this, range.index));
				a.href = "javascript:void(0)";
				this.dirIndex.appendChild(a);
			}, this);
		}

		if (config.directoryType == "supplier") this.setPage(this.index);
		else if ($("dir-left").innerHTML.match(/^\s+$/) && $("dir-right").innerHTML.match(/^\s+$/)) this.setPage(this.index);
	},
	
	setPage: function(index) {
		var links = this.dirIndex.getElementsByTagName("a");
		forEach(links, function(link) {
			Style.removeClass(link, "active");
		});
		
		var pageHTML = [];
		var currentKey;

		if (this.keyMode) {

			// populate keys
			var key = links[index].innerHTML;
			forEach(window.directories, function(directory) {
				if (directory.key == key) {
					pageHTML.push(this.setElem(directory, directory.key != currentKey));
					currentKey = key;
				}
			}, this);

		} else {

			// populate ranges
			forEach(this.ranges[index].keys, function(key) {
				forEach(window.directories, function(directory) {
					if (directory.key == key) {
						pageHTML.push(this.setElem(directory, directory.key != currentKey));
						currentKey = key;
					}
				}, this);
			}, this);
		}

		// split into columns
		$("dir-left").innerHTML = pageHTML.splice(0, Math.ceil(pageHTML.length / 2)).join("");
		$("dir-right").innerHTML = pageHTML.join("");

		Style.addClass(links[index], "active");
		Style.show("directoryList");
	},
	
	setElem: function(elem, showKey) {		
		return [
			this.template.process({
				elem : elem,
				showKey : showKey				
			})
		]
	}
});


