var jdvScrollInstance;
idModules = new Array();

function jdvAScroll(idModule, scrollDirection, transDuration, animationFun)
{
	this.mCount = 0;
	
	this.scrollDirection = new Array();
	this.transDuration = new Array();
	this.animationFun = new Array();
	//this.picArray = new Array();
	this.idModule = new Array();
	this.auto = new Array();
	this.scrolls = new Array();
	this.maxScroll = new Array();
	
	this.count   = new Array();
	this.prev    = new Array();
	this.current = new Array();
	this.next    = new Array();

	//this.curOpacity = new Array();
	
	this.initAScroll(idModule, scrollDirection, transDuration, animationFun);
	
}


jdvAScroll.prototype.initAScroll=function(idModule, scrollDirection, transDuration, animationFun)
{
	idModules[this.mCount] = idModule;

	this.scrollDirection[this.mCount] = scrollDirection;
	this.transDuration[this.mCount] = transDuration;
	this.animationFun[this.mCount] = animationFun;

	this.auto[this.mCount] = 0;
	
	this.count[this.mCount]   = 0;
	this.prev[this.mCount]    = 0;
	this.current[this.mCount] = 0;
	this.next[this.mCount]    = 1;

	//this.curOpacity = 0;
	
	this.scrolls[this.mCount] = new Fx.Scroll('jdv_ascroll'+idModules[this.mCount]+'_wrap', {
		wait: false,
		duration: this.transDuration[this.mCount],
		offset: {'x': 0, 'y': 0},
		transition: this.animationFun[this.mCount]
		});
	

	
	articleArray = $$('#jdv_ascroll'+idModules[this.mCount]+'_inner .jdv_ascroll_content');
	
	this.count[this.mCount] = articleArray.length;
	index = idModules.indexOf(parseInt(idModule),0);

	this.maxScroll[this.mCount] = 0;
	
	$('jdv_ascroll'+idModule+'_inner').style.left = '0px';
	
	for(i = 0; i < articleArray.length; i++)
	{
		if (this.scrollDirection[index] == 'horizontally'){
			slideEndX = $('slideEnd'+idModule).offsetLeft;
			widthWrap = $('jdv_ascroll'+idModule+'_wrap').offsetWidth;
			lastInsideX = slideEndX-$("jdv_ascroll"+idModule+"_content"+i).offsetLeft;
			
			if (lastInsideX > widthWrap){
				this.maxScroll[this.mCount] = i;
			}
		}else{
			slideEndY = $('slideEnd'+idModule).offsetTop;
			heightWrap = $('jdv_ascroll'+idModule+'_wrap').offsetHeight;
			lastInsideY = slideEndY-$("jdv_ascroll"+idModule+"_content"+i).offsetTop;
			
			if (lastInsideY > heightWrap){
				this.maxScroll[this.mCount] = i;
			}
		}

		if ($('jdv_ascroll_bar'+idModule))
		$("jdv_ascroll"+idModules[this.mCount]+"_link"+(i)).addEvent("click", function(event) {
		
			event = new Event(event).stop();
			
			start = "jdv_ascroll".length; 
			end = this.id.indexOf("_", start);
			
			idModule = this.id.substring(start, end);
					
			
			l = ("jdv_ascroll"+idModule+"_link").length;
			
			index = idModules.indexOf(parseInt(idModule),0);

			
			if (jdvScrollInstance.scrollDirection[index] == 'horizontally'){
				slideEndX = $('slideEnd'+idModule).offsetLeft;
				widthWrap = $('jdv_ascroll'+idModule+'_wrap').offsetWidth;
				lastInsideX = slideEndX-$("jdv_ascroll"+idModule+"_content"+this.id.substring(l)).offsetLeft;
				
			
				offsetX = $("jdv_ascroll"+idModule+"_content"+this.id.substring(l)).offsetLeft-(widthWrap-lastInsideX);
					
			
				if (lastInsideX > widthWrap){
					jdvScrollInstance.scrolls[index].toElement("jdv_ascroll"+idModule+"_content"+this.id.substring(l));
				
				}else{
					jdvScrollInstance.scrolls[index].scrollTo(offsetX, 0);
				}
			}else{
				slideEndY = $('slideEnd'+idModule).offsetTop;
				heightWrap = $('jdv_ascroll'+idModule+'_wrap').offsetHeight;
				lastInsideY = slideEndY-$("jdv_ascroll"+idModule+"_content"+this.id.substring(l)).offsetTop;
				
				
				
				offsetY = $("jdv_ascroll"+idModule+"_content"+this.id.substring(l)).offsetTop-(heightWrap-lastInsideY);

				if (lastInsideY > heightWrap){
					jdvScrollInstance.scrolls[index].toElement("jdv_ascroll"+idModule+"_content"+this.id.substring(l));
				
				}else{
					jdvScrollInstance.scrolls[index].scrollTo(0, offsetY);
				}
			}
			
			
			
			jdvScrollInstance.auto[index] = 0;
			
			jdvScrollInstance.current[index] = this.id.substring(l);

			jdvScrollInstance.setPrevNext(index);

		});

	}
	
	/* arrow button next */
	if ($("jdv_button_next"+idModules[this.mCount])) {
		$("jdv_button_next"+idModules[this.mCount]).addEvent("click", function(event) {
		
			event = new Event(event).stop();
			
			start = "jdv_button_next".length; 
			idModule = this.id.substring(start);
			
			index = idModules.indexOf(parseInt(idModule),0);
			
			jdvScrollInstance.auto[index] = 0;
			
			
			if (jdvScrollInstance.scrollDirection[index] == 'horizontally'){
				slideEndX = $('slideEnd'+idModule).offsetLeft;
				widthWrap = $('jdv_ascroll'+idModule+'_wrap').offsetWidth;
				lastInsideX = slideEndX-$("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetLeft;
				
				if (lastInsideX > widthWrap){
					offsetX = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetLeft;
					jdvScrollInstance.scrolls[index].scrollTo(offsetX, 0);
					
					jdvScrollInstance.current[index] = jdvScrollInstance.next[index];
					
				}else{
					offsetX = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetLeft-(widthWrap-lastInsideX);
					jdvScrollInstance.scrolls[index].scrollTo(offsetX, 0);
					
					
				}
				//alert(jdvScrollInstance.next[index]);
			}else{
				slideEndY = $('slideEnd'+idModule).offsetTop;
				heightWrap = $('jdv_ascroll'+idModule+'_wrap').offsetHeight;
				lastInsideY = slideEndY-$("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetTop;
				
				if (lastInsideY > heightWrap){
					//jdvScrollInstance.scrolls[index].toElement("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]);
					
					offsetY = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetTop;
					jdvScrollInstance.scrolls[index].scrollTo(0, offsetY);				
					
					jdvScrollInstance.current[index] = jdvScrollInstance.next[index];
					
				}else{
					offsetY = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetTop-(heightWrap-lastInsideY);
					jdvScrollInstance.scrolls[index].scrollTo(0, offsetY);
					
					
				}
				
			}
			//jdvScrollInstance.current[index] = jdvScrollInstance.next[index];
			jdvScrollInstance.setPrevNext(index);
		});
	}
	
	/* arrow button prev */
	if ($("jdv_button_prev"+idModules[this.mCount])) {
		$("jdv_button_prev"+idModules[this.mCount]).addEvent("click", function(event) {
		
		
			event = new Event(event).stop();
			
			start = "jdv_button_prev".length; 
			
			idModule = this.id.substring(start);
			
			index = idModules.indexOf(parseInt(idModule),0);
			jdvScrollInstance.auto[index] = 0;
			
			if (jdvScrollInstance.scrollDirection[index] == 'horizontally'){
				slideEndX = $('slideEnd'+idModule).offsetLeft;
				widthWrap = $('jdv_ascroll'+idModule+'_wrap').offsetWidth;
				lastInsideX = slideEndX-$("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.prev[index]).offsetLeft;
				
				if (lastInsideX > widthWrap){
					//jdvScrollInstance.scrolls[index].toElement("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.prev[index]);
					
					offsetX = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.prev[index]).offsetLeft;
					jdvScrollInstance.scrolls[index].scrollTo(offsetX, 0);
					
					jdvScrollInstance.current[index] = jdvScrollInstance.prev[index];
					
				}else{
					
					offsetX = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.maxScroll[index]).offsetLeft;
					jdvScrollInstance.scrolls[index].scrollTo(offsetX, 0);
					jdvScrollInstance.current[index] = jdvScrollInstance.maxScroll[index]-1;
					
				}
			}else{
				slideEndY = $('slideEnd'+idModule).offsetTop;
				heightWrap = $('jdv_ascroll'+idModule+'_wrap').offsetHeight;
				lastInsideY = slideEndY-$("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.prev[index]).offsetTop;
				
				if (lastInsideY > heightWrap){
					//jdvScrollInstance.scrolls[index].toElement("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.prev[index]);
					
					offsetY = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.prev[index]).offsetTop;
					jdvScrollInstance.scrolls[index].scrollTo(0, offsetY);
					jdvScrollInstance.current[index] = jdvScrollInstance.prev[index];
					
				}else{//alert(jdvScrollInstance.maxScroll[index]);
					//offsetY = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.prev[index]).offsetTop-(heightWrap-lastInsideY);
					offsetY = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.maxScroll[index]).offsetTop;
					jdvScrollInstance.scrolls[index].scrollTo(0, offsetY);

					jdvScrollInstance.current[index] = jdvScrollInstance.maxScroll[index]-1;
					
				}
			}
			//jdvScrollInstance.current[index] = jdvScrollInstance.prev[index];
			jdvScrollInstance.setPrevNext(index);
		});
	}
	
	this.mCount++;
}


jdvAScroll.prototype.autoScroll=function(idModule, transDelay)
//function autoScroll(idModule, transDelay)
{

	index = idModules.indexOf(parseInt(idModule),0);
	//alert(idModule);
	
	if (jdvScrollInstance.auto[index]) {

		if (jdvScrollInstance.scrollDirection[index] == 'horizontally'){
				slideEndX = $('slideEnd'+idModule).offsetLeft;
				widthWrap = $('jdv_ascroll'+idModule+'_wrap').offsetWidth;
				lastInsideX = slideEndX-$("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetLeft;
				
				if (lastInsideX > widthWrap){
					//jdvScrollInstance.scrolls[index].toElement("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]);
					
					offsetX = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetLeft;
					jdvScrollInstance.scrolls[index].scrollTo(offsetX, 0);
					
					
					jdvScrollInstance.current[index] = jdvScrollInstance.next[index];
					
				}else{
					offsetX = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetLeft-(widthWrap-lastInsideX);
					jdvScrollInstance.scrolls[index].scrollTo(offsetX, 0);
					
				}
			}else{
				
				slideEndY = $('slideEnd'+idModule).offsetTop;
				heightWrap = $('jdv_ascroll'+idModule+'_wrap').offsetHeight;
				lastInsideY = slideEndY-$("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetTop;
				
				if (lastInsideY > heightWrap){
					//jdvScrollInstance.scrolls[index].toElement("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]);
					
					offsetY = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetTop;
					jdvScrollInstance.scrolls[index].scrollTo(0, offsetY);
					
					
					jdvScrollInstance.current[index] = jdvScrollInstance.next[index];
					
				}else{
					offsetY = $("jdv_ascroll"+idModule+"_content"+jdvScrollInstance.next[index]).offsetTop-(heightWrap-lastInsideY);
					jdvScrollInstance.scrolls[index].scrollTo(0, offsetY);
					
				}
			}
		
		
		jdvScrollInstance.current[index] = jdvScrollInstance.next[index];
		jdvScrollInstance.setPrevNext(index);
		setTimeout('jdvScrollInstance.autoScroll('+idModule+', '+transDelay+')', transDelay);
	}
}


jdvAScroll.prototype.setPrevNext=function(index)
{
// setPrevNext (index) {

	jdvScrollInstance.current[index] = parseInt(jdvScrollInstance.current[index]);
	jdvScrollInstance.prev[index] = parseInt(jdvScrollInstance.prev[index]);
	jdvScrollInstance.next[index] = parseInt(jdvScrollInstance.next[index]);
	
	if (jdvScrollInstance.current[index] != 0) {
		jdvScrollInstance.prev[index] = jdvScrollInstance.current[index] - 1;
	} else {
		jdvScrollInstance.prev[index] = jdvScrollInstance.current[index];
	}

	if (jdvScrollInstance.current[index] != (jdvScrollInstance.count[index] - 1)) {
		jdvScrollInstance.next[index] = jdvScrollInstance.current[index] + 1;
	} else {
		//jdvScrollInstance.next[index] = jdvScrollInstance.current[index];
		jdvScrollInstance.next[index] = 0;
	}	
	
	//alert(jdvScrollInstance.current[index]);
	//alert(jdvScrollInstance.prev[index]);
}

