/*
	created by Dimitar Stoyanov 
*/

function Partners(parentElem, childrenElem, length, interval, animation){
	//values
	this.parentId = parentElem;
	this.childrenClass = childrenElem;
	this.toShowLength = length;
	this.overallLength = 0;
	this.hiddenElems = new Array();
	this.shownElems = new Array();
	this.iterationElems = new Array();
	this.imageNames = new Array();
	this.timeInterval = interval;
	this.animationTime = animation;
	
	//methods	
	this.init = function(){
		var temp = this.overallLength;
		var name = this.childrenClass;
		var show = this.toShowLength;
		var tempIterator = 0;
		var imageNames = new Array();
		
		$(parentElem).children().each(function(){
			++temp;
			imageNames[tempIterator++] = $(this).children().attr('src');
		});
		
		tempIterator = 0;
		this.overallLength = temp;
		this.imageNames = imageNames;
		
		$(parentElem).children().each(function(){
			if (tempIterator < show) {
				$(this).addClass(name + tempIterator++).empty();
			}
			else {
				$(this).remove();
			}
		})
		
		this.imageNames.sort(function(){
			return 0.5 - Math.random();
		});
		
		for (var i = 0; i < this.toShowLength; ++i) {
			this.shownElems.push(this.imageNames.shift());
		}
		
		var shownElems = this.shownElems;
		this.hiddenElems = this.imageNames;
		var hiddenElems = this.hiddenElems;
		tempIterator = 0;
		
		$(this.parentId).children().each(function(){
			$(this).append('<img src="' + shownElems[tempIterator++] + '" alt="" />');
		});
	}

	this.animate = function(){
		parentId = this.parentId;
		childrenClass = this.childrenClass;
		toShowLength = this.toShowLength;
		overallLength = this.overallLength;
		hiddenElems = this.hiddenElems;
		shownElems = this.shownElems;
		iterationElems = this.iterationElems;
		timeInterval = this.timeInterval;
		animationTime = this.animationTime;
		var childrenOrder = new Array();

		for (var i = 0; i < toShowLength; ++i) {
			childrenOrder[i] = i;
		}

		childrenOrder.sort(function(){
			return 0.5 - Math.random();
		})

		setInterval(function(){
			$(parentId + ' .' + childrenClass + childrenOrder[0]).children().fadeOut(animation, function(){
				$(this).remove();
			});

			hiddenElems.push(shownElems.shift());
			shownElems.push(hiddenElems.shift());
			$(parentId + ' .' + childrenClass + childrenOrder[0]).append('<img src=' + shownElems[0] + ' alt="" />');
			$(parentId + ' .' + childrenClass + childrenOrder[0]).children().hide();
			//alert(parentId + ' .' + childrenClass + childrenOrder[0] + '<img src="' + shownElems[0] + '" alt=""');

			setTimeout(function() {
				$(parentId + ' .' + childrenClass + childrenOrder[0]).children().fadeIn(animation);
				childrenOrder.push(childrenOrder.shift());
			}, this.animation);

		}, timeInterval);
	}

	//initialization
	this.init();
	this.animate();
}
