dojo.require('dojox.image');
var cl = {};

function setActiveMenu( id ) {
	var elem = dojo.byId('menuItem'+id);
	if (elem == null) return false;
	while ( (elem.nodeName.toLowerCase() != 'body') && (elem.id != 'menu') ) {
		if ( (elem.nodeName.toLowerCase() == 'li') || (elem.nodeName.toLowerCase() == 'td') ) dojo.addClass(elem, 'active');
		elem = elem.parentNode;
	}
	elem = dojo.byId('menuBranch'+id);
	if (elem == null) return false;
	while ( (elem.nodeName.toLowerCase() != 'body') && (elem.id != 'sidebar') ) {
		if (elem.nodeName.toLowerCase() == 'li') dojo.addClass(elem, 'active');
		elem = elem.parentNode;
	}
}

function validateSubmit() {
	var result = true;
	var elem = dojo.byId('inputc1');
	dojo.removeClass(elem, 'error');
	if ( (elem == null) || (elem.value == '')) { dojo.addClass(elem, 'error'); result = false; }
	elem = dojo.byId('inputc2');
	dojo.removeClass(elem, 'error');
	if ( (elem == null) || (elem.value == '') || (!elem.value.match(/^[^\s]+@[^\s]+\.[^\s]+$/)) ) { dojo.addClass(elem, 'error'); result = false; }
	elem = dojo.byId('inputc3');
	dojo.removeClass(elem, 'error');
	if ( (elem == null) || (elem.value == '') ) { dojo.addClass(elem, 'error'); result = false; }
	elem = dojo.byId('inputc4');
	dojo.removeClass(elem, 'error');
	if ( (elem == null) || (elem.value == '') || (!elem.value.match(/^\d+$/)) ) { dojo.addClass(elem, 'error'); result = false; }
	return result;
}

cl.gallery = function (ident, images, names, size, delay) {
	this.ident = ident;
    this.imgs = images;
    this.names = names;
    this.size = size;
    this.delay = delay;
    this.timeoutId = 0;
    this.iterator = 0;
    this.max = images.length;
    this.img = null;
    this.name = null;
    this.init = function () {
    	var _1 = this;
    	var size = this.size;
    	dojo.addOnLoad(function(){
    		var array = [];
    		dojo.forEach(images,
			    function(item) {
			        array.push("/media/generator/get-image/method/0/size/"+size+"/image/"+item);
			    }
			);
    	    dojox.image.preload(array);
    	    _1.delayAnim();
    	});
    }
    this.next = function () {
    	if (!this.img) this.img = dojo.byId('galleryImg'+ident);
    	if (!this.name) this.name = dojo.byId('galleryName'+ident);
    	var mi_id = this.imgs[this.nextIter()];
    	var name = this.names[this.iterator];
    	this.changeItem(mi_id, name);
    	if (this.timeoutId) this.delayAnim();
    };
    this.prew = function () {
    	if (!this.img) this.img = dojo.byId('galleryImg'+ident);
    	if (!this.name) this.name = dojo.byId('galleryName'+ident);
    	var mi_id = this.imgs[this.prewIter()];
    	var name = this.names[this.iterator];
    	this.changeItem(mi_id, name);
    	this.stopAnim();
    };
    this.stopAnim = function () {
    	if (this.timeoutId) {
    		clearTimeout ( this.timeoutId );
    		this.timeoutId = 0;
    	}
    }
    this.delayAnim = function () {
    	var _1 = this;
    	if (!this.delay) return false;
    	if (this.timeoutId) clearTimeout ( this.timeoutId );
    	this.timeoutId = setTimeout ( function() {
    		_1.next();
    		_1.delayAnim();
    	}, this.delay);
    };
    this.changeItem = function (mi_id, name) {
    	var image = this.img;
    	var nameCont = this.name;
    	var size = this.size;
    	var position = " ("+(this.iterator+1)+"/"+this.max+")";
    	var fadeArgs = {
	        node: image,
	        duration: 500,
	        onEnd: function () {
    			image.src = "/media/generator/get-image/method/0/size/"+size+"/image/"+mi_id;
    			image.alt = name;
    			nameCont.innerHTML = name + position;
    			nameCont.href = "/media/client/one-item/mi_id/"+mi_id;
	    		var fadeArgs2 = {
	    		        node: image,
	    		        duration: 500,
	    		        onEnd: function () {
	    				}
	    		};
	    		dojo.fadeIn(fadeArgs2).play();
	    	}
	    };
	    dojo.fadeOut(fadeArgs).play();
    };
    this.nextIter = function () {
    	this.iterator++;
    	if (this.iterator >= this.max) this.iterator = 0;
    	return this.iterator;
    }
    this.prewIter = function () {
    	this.iterator--;
    	if (this.iterator < 0) this.iterator = this.max - 1;
    	return this.iterator;
    }
    // initialize object (preload images)
    this.init();
}

