var jdvIScrollInstance;
idISModules = new Array();

function jdvIScroll(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.initIScroll(idModule, scrollDirection, transDuration, animationFun);
	
}


jdvIScroll.prototype.initIScroll=function(idModule, scrollDirection, transDuration, animationFun)
{
	idISModules[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_iscroll'+idISModules[this.mCount]+'_wrap', {
		wait: false,
		duration: this.transDuration[this.mCount],
		offset: {'x': 0, 'y': 0},
		transition: this.animationFun[this.mCount]
		});
	

	
	picArray = document.getElementById('jdv_iscroll'+idISModules[this.mCount]+'_inner').getElementsByTagName("img");
	this.count[this.mCount] = picArray.length;
	index = idISModules.indexOf(parseInt(idModule),0);

	this.maxScroll[this.mCount] = 0;
	
	$('jdv_iscroll'+idModule+'_inner').style.left = '0px';
	
	for(i = 0; i < picArray.length; i++)
	{

		if (this.scrollDirection[index] == 'horizontally'){
			slideEndX = $('slideEnd'+idModule).offsetLeft;
			widthWrap = $('jdv_iscroll'+idModule+'_wrap').offsetWidth;
			lastInsideX = slideEndX-$("jdv_iscroll"+idModule+"_content"+i).offsetLeft;
			
			if (lastInsideX > widthWrap){
				this.maxScroll[this.mCount] = i;
			}
		}else{
			slideEndY = $('slideEnd'+idModule).offsetTop;
			heightWrap = $('jdv_iscroll'+idModule+'_wrap').offsetHeight;
			lastInsideY = slideEndY-$("jdv_iscroll"+idModule+"_content"+i).offsetTop;
			
			if (lastInsideY > heightWrap){
				this.maxScroll[this.mCount] = i;
			}
		}
		
		/* slideEnd = $('slideEnd'+idModule).offsetLeft;
		widthWrap = $('jdv_iscroll'+idModule+'_wrap').offsetWidth;
		lastInside = slideEnd-$("jdv_iscroll"+idModule+"_content"+i).offsetLeft; 

		if (lastInside > widthWrap){
			this.maxScroll[this.mCount] = i;
		}
		*/
		
		if ($('jdv_iscroll_bar'+idModule))
		$("jdv_iscroll"+idISModules[this.mCount]+"_link"+(i)).addEvent("click", function(event) {
		
			event = new Event(event).stop();
			
			start = "jdv_iscroll".length; 
			end = this.id.indexOf("_", start);
			
			idModule = this.id.substring(start, end);
					
					
			l = ("jdv_iscroll"+idModule+"_link").length;
			
			index = idISModules.indexOf(parseInt(idModule),0);
				
			if (jdvIScrollInstance.scrollDirection[index] == 'horizontally'){
				slideEndX = $('slideEnd'+idModule).offsetLeft;
				widthWrap = $('jdv_iscroll'+idModule+'_wrap').offsetWidth;
				lastInsideX = slideEndX-$("jdv_iscroll"+idModule+"_content"+this.id.substring(l)).offsetLeft;
				offsetX = $("jdv_iscroll"+idModule+"_content"+this.id.substring(l)).offsetLeft-(widthWrap-lastInsideX);
				
				if (lastInsideX > widthWrap){
					jdvIScrollInstance.scrolls[index].toElement("jdv_iscroll"+idModule+"_content"+this.id.substring(l));
				}else{
					
					jdvIScrollInstance.scrolls[index].scrollTo(offsetX, 0);

				}
			
			}else{
				slideEndY = $('slideEnd'+idModule).offsetTop;
				heightWrap = $('jdv_iscroll'+idModule+'_wrap').offsetHeight;
				lastInsideY = slideEndY-$("jdv_iscroll"+idModule+"_content"+this.id.substring(l)).offsetTop;
				
				
				
				offsetY = $("jdv_iscroll"+idModule+"_content"+this.id.substring(l)).offsetTop-(heightWrap-lastInsideY);
			
				//alert(slideEndY);
				if (lastInsideY > heightWrap){
					jdvIScrollInstance.scrolls[index].toElement("jdv_iscroll"+idModule+"_content"+this.id.substring(l));
				
				}else{
					jdvIScrollInstance.scrolls[index].scrollTo(0, offsetY);
				}
			}
			
			jdvIScrollInstance.auto[index] = 0;
			
			jdvIScrollInstance.current[index] = this.id.substring(l);

			jdvIScrollInstance.setPrevNextImage(index);

		});

	}
	
	
	if ($("jdv_button_next"+idISModules[this.mCount])) {
		$("jdv_button_next"+idISModules[this.mCount]).addEvent("click", function(event) {
		
			event = new Event(event).stop();
			
			start = "jdv_button_next".length; 
			idModule = this.id.substring(start);
			
			index = idISModules.indexOf(parseInt(idModule),0);
			
			jdvIScrollInstance.auto[index] = 0;
			
			if (jdvIScrollInstance.scrollDirection[index] == 'horizontally'){

				slideEndX = $('slideEnd'+idModule).offsetLeft;
				widthWrap = $('jdv_iscroll'+idModule+'_wrap').offsetWidth;
				lastInsideX = slideEndX-$("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetLeft;
				if (lastInsideX > widthWrap){
					//jdvIScrollInstance.scrolls[index].toElement("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]);
					
					offsetX = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetLeft;
					jdvIScrollInstance.scrolls[index].scrollTo(offsetX, 0);
					
					jdvIScrollInstance.current[index] = jdvIScrollInstance.next[index];
					
				}else{
					offsetX = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetLeft-(widthWrap-lastInsideX);
					jdvIScrollInstance.scrolls[index].scrollTo(offsetX, 0);
					jdvIScrollInstance.current[index] = jdvIScrollInstance.count[index] - 1;
				}
			}else{
				slideEndY = $('slideEnd'+idModule).offsetTop;
				heightWrap = $('jdv_iscroll'+idModule+'_wrap').offsetHeight;
				lastInsideY = slideEndY-$("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetTop;
				
				if (lastInsideY > heightWrap){
					//jdvIScrollInstance.scrolls[index].toElement("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]);
					
					offsetY = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetTop;
					jdvIScrollInstance.scrolls[index].scrollTo(0, offsetY);				
					
					jdvIScrollInstance.current[index] = jdvIScrollInstance.next[index];
					
				}else{
					offsetY = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetTop-(heightWrap-lastInsideY);
					jdvIScrollInstance.scrolls[index].scrollTo(0, offsetY);
					jdvIScrollInstance.current[index] = jdvIScrollInstance.count[index] - 1;
					
				}
			}
			jdvIScrollInstance.setPrevNextImage(index);
		});
	}
	
	
	if ($("jdv_button_prev"+idISModules[this.mCount])) {
		$("jdv_button_prev"+idISModules[this.mCount]).addEvent("click", function(event) {
		
		
			event = new Event(event).stop();
			
			start = "jdv_button_prev".length; 
			
			idModule = this.id.substring(start);
			
			index = idISModules.indexOf(parseInt(idModule),0);
			jdvIScrollInstance.auto[index] = 0;
			
			if (jdvIScrollInstance.scrollDirection[index] == 'horizontally'){
			
				slideEnd = $('slideEnd'+idModule).offsetLeft;
				widthWrap = $('jdv_iscroll'+idModule+'_wrap').offsetWidth;
				

				lastInside = slideEnd-$("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.current[index]).offsetLeft;

				if (lastInside > widthWrap){

					offsetX = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.prev[index]).offsetLeft;
					jdvIScrollInstance.scrolls[index].scrollTo(offsetX, 0);

					jdvIScrollInstance.current[index] = jdvIScrollInstance.prev[index];
					
				}else{
					offsetX = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.maxScroll[index]).offsetLeft;

					jdvIScrollInstance.scrolls[index].scrollTo(offsetX, 0);

					jdvIScrollInstance.current[index] = jdvIScrollInstance.maxScroll[index];
				}
			}
			else{
				slideEndY = $('slideEnd'+idModule).offsetTop;
				heightWrap = $('jdv_iscroll'+idModule+'_wrap').offsetHeight;
				lastInsideY = slideEndY-$("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.prev[index]).offsetTop;
				
				if (lastInsideY > heightWrap){
					
					offsetY = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.prev[index]).offsetTop;
					jdvIScrollInstance.scrolls[index].scrollTo(0, offsetY);
					jdvIScrollInstance.current[index] = jdvIScrollInstance.prev[index];
					
				}else{
					offsetY = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.maxScroll[index]).offsetTop;
					jdvIScrollInstance.scrolls[index].scrollTo(0, offsetY);

					jdvIScrollInstance.current[index] = jdvIScrollInstance.maxScroll[index];
					
				}
			}
			
			jdvIScrollInstance.setPrevNextImage(index);
		});
	}
	
	this.mCount++;
}


jdvIScroll.prototype.autoIScroll=function(idModule, transDelay)
//function autoIScroll(idModule, transDelay)
{

	index = idISModules.indexOf(parseInt(idModule),0);
	
	
	if (jdvIScrollInstance.auto[index]) {
		
		if (jdvIScrollInstance.scrollDirection[index] == 'horizontally'){

			slideEndX = $('slideEnd'+idModule).offsetLeft;
			widthWrap = $('jdv_iscroll'+idModule+'_wrap').offsetWidth;
			lastInsideX = slideEndX-$("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetLeft;
				
			if (lastInsideX > widthWrap){
				offsetX = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetLeft;
				jdvIScrollInstance.scrolls[index].scrollTo(offsetX, 0);
			
			}else{
				offsetX = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetLeft-(widthWrap-lastInsideX);
				jdvIScrollInstance.scrolls[index].scrollTo(offsetX, 0);
			}
		}else{
			slideEndY = $('slideEnd'+idModule).offsetTop;
			heightWrap = $('jdv_iscroll'+idModule+'_wrap').offsetHeight;
			lastInsideY = slideEndY-$("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetTop;
			
			if (lastInsideY > heightWrap){
				
				offsetY = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetTop;
				jdvIScrollInstance.scrolls[index].scrollTo(0, offsetY);
				
				
				jdvIScrollInstance.current[index] = jdvIScrollInstance.next[index];
				
			}else{
				offsetY = $("jdv_iscroll"+idModule+"_content"+jdvIScrollInstance.next[index]).offsetTop-(heightWrap-lastInsideY);
				jdvIScrollInstance.scrolls[index].scrollTo(0, offsetY);
				
			}
		}
		
		
		jdvIScrollInstance.current[index] = jdvIScrollInstance.next[index];
		jdvIScrollInstance.setPrevNextImage(index);
		setTimeout('jdvIScrollInstance.autoIScroll('+idModule+', '+transDelay+')', transDelay);
	}
}


jdvIScroll.prototype.setPrevNextImage=function(index)
{
// setPrevNextImage (index) {

	jdvIScrollInstance.current[index] = parseInt(jdvIScrollInstance.current[index]);
	jdvIScrollInstance.prev[index] = parseInt(jdvIScrollInstance.prev[index]);
	jdvIScrollInstance.next[index] = parseInt(jdvIScrollInstance.next[index]);
	
	if (jdvIScrollInstance.current[index] != 0) {
		jdvIScrollInstance.prev[index] = jdvIScrollInstance.current[index] - 1;
	} else {
		jdvIScrollInstance.prev[index] = jdvIScrollInstance.current[index];
	}

	if (jdvIScrollInstance.current[index] != (jdvIScrollInstance.count[index] - 1)) {
		jdvIScrollInstance.next[index] = jdvIScrollInstance.current[index] + 1;
	} else {
		//jdvIScrollInstance.next[index] = jdvIScrollInstance.current[index];
		jdvIScrollInstance.next[index] = 0;
	}	
}
