var hdjMpuScroller= jQuery.noConflict();

/*
Thumbnail scroller jQuery plugin
Author: malihu [http://manos.malihu.gr]
Homepage: manos.malihu.gr/jquery-thumbnail-scroller
*/
(function(hdjMpuScroller){  
 hdjMpuScroller.fn.thumbnailScroller=function(options){  
	var defaults={ //default options
		scrollerType:"hoverPrecise", //values: "hoverPrecise", "hoverAccelerate", "clickButtons"
		scrollerOrientation:"horizontal", //values: "horizontal", "vertical"
		//scrollEasing:"easeOutCirc", //easing type
		scrollEasing:"easeInOutQuint",
		scrollEasingAmount:800, //value: milliseconds
		acceleration:2, //value: integer
		scrollSpeed:600, //value: milliseconds
		noScrollCenterSpace:0, //value: pixels
		autoScrolling:0, //value: integer
		autoScrollingSpeed:8000, //value: milliseconds
		autoScrollingEasing:"easeInOutQuint", //easing type
		autoScrollingDelay:2500 //value: milliseconds
	};
	var options=hdjMpuScroller.extend(defaults,options);
    return this.each(function(){ 
		//cache vars
		var hdjImgthis=hdjMpuScroller(this);
		var hdjImgscrollerContainer=hdjImgthis.children(".mpuScrollerjTscrollerContainer");
		var hdjImgscroller=hdjImgthis.children(".mpuScrollerjTscrollerContainer").children(".mpuScrollerjTscroller");
		var hdjImgscrollerNextButton=hdjImgthis.children(".mpuScrollerjTscrollerNextButton");
		var hdjImgscrollerPrevButton=hdjImgthis.children(".mpuScrollerjTscrollerPrevButton");
		//set scroller width
		if(options.scrollerOrientation=="horizontal"){
			
			//hdjImgscrollerContainer.css("width",999999); 
			  hdjImgscrollerContainer.css("width",hdjImgthis.width()*(Math.ceil(hdjImgscroller.children().size()/3))); 
			var totalWidth=hdjImgscroller.outerWidth(true);
			hdjImgscrollerContainer.css("width",totalWidth);
		}else{
			var totalWidth=hdjImgscroller.outerWidth(true);
		}
		var totalHeight=hdjImgscroller.outerHeight(true); //scroller height
		//do the scrolling
		if(totalWidth>hdjImgthis.width() || totalHeight>hdjImgthis.height()){ //needs scrolling		
			var pos;
			var mouseCoords;
			var mouseCoordsY;
			if(options.scrollerType=="hoverAccelerate"){ //type hoverAccelerate
				var animTimer;
				var interval=8;
				hdjImgthis.hover(function(){ //mouse over
					hdjImgthis.mousemove(function(e){
						pos=findPos(this);
						mouseCoords=(e.pageX-pos[1]);
						mouseCoordsY=(e.pageY-pos[0]);
					});
					clearInterval(animTimer);
					animTimer = setInterval(Scrolling,interval);
				},function(){  //mouse out
					clearInterval(animTimer);
					hdjImgscroller.stop();
				});
				hdjImgscrollerPrevButton.add(hdjImgscrollerNextButton).hide(); //hide buttons
			}else if(options.scrollerType=="clickButtons"){
				ClickScrolling();
			}else{ //type hoverPrecise
				pos=findPos(this);
				hdjImgthis.mousemove(function(e){
					mouseCoords=(e.pageX-pos[1]);
					mouseCoordsY=(e.pageY-pos[0]);
					var mousePercentX=mouseCoords/hdjImgthis.width(); if(mousePercentX>1){mousePercentX=1;}
					var mousePercentY=mouseCoordsY/hdjImgthis.height(); if(mousePercentY>1){mousePercentY=1;}
					var destX=Math.round(-((totalWidth-hdjImgthis.width())*(mousePercentX)));
					var destY=Math.round(-((totalHeight-hdjImgthis.height())*(mousePercentY)));
					hdjImgscroller.stop(true,false).animate({left:destX,top:destY},options.scrollEasingAmount,options.scrollEasing); 
				});
				hdjImgscrollerPrevButton.add(hdjImgscrollerNextButton).hide(); //hide buttons
			}
			//auto scrolling
			if(options.autoScrolling>0){
				AutoScrolling();
			}
		} else {
			//no scrolling needed
			hdjImgscrollerPrevButton.add(hdjImgscrollerNextButton).hide(); //hide buttons
		}
		//"hoverAccelerate" scrolling fn
		var scrollerPos;
		var scrollerPosY;
		function Scrolling(){
			if((mouseCoords<hdjImgthis.width()/2) && (hdjImgscroller.position().left>=0)){
				hdjImgscroller.stop(true,true).css("left",0); 
			}else if((mouseCoords>hdjImgthis.width()/2) && (hdjImgscroller.position().left<=-(totalWidth-hdjImgthis.width()))){
				hdjImgscroller.stop(true,true).css("left",-(totalWidth-hdjImgthis.width())); 
			}else{
				if((mouseCoords<=(hdjImgthis.width()/2)-options.noScrollCenterSpace) || (mouseCoords>=(hdjImgthis.width()/2)+options.noScrollCenterSpace)){
					scrollerPos=Math.round(Math.cos((mouseCoords/hdjImgthis.width())*Math.PI)*(interval+options.acceleration));
					hdjImgscroller.stop(true,true).animate({left:"+="+scrollerPos},interval,"linear"); 
				}else{
					hdjImgscroller.stop(true,true);
				}
			}
			if((mouseCoordsY<hdjImgthis.height()/2) && (hdjImgscroller.position().top>=0)){
				hdjImgscroller.stop(true,true).css("top",0); 
			}else if((mouseCoordsY>hdjImgthis.height()/2) && (hdjImgscroller.position().top<=-(totalHeight-hdjImgthis.height()))){
				hdjImgscroller.stop(true,true).css("top",-(totalHeight-hdjImgthis.height())); 
			}else{
				if((mouseCoordsY<=(hdjImgthis.height()/2)-options.noScrollCenterSpace) || (mouseCoordsY>=(hdjImgthis.height()/2)+options.noScrollCenterSpace)){
					scrollerPosY=Math.cos((mouseCoordsY/hdjImgthis.height())*Math.PI)*(interval+options.acceleration);
					hdjImgscroller.stop(true,true).animate({top:"+="+scrollerPosY},interval,"linear"); 
				}else{
					hdjImgscroller.stop(true,true);
				}
			}
		}
		//auto scrolling fn
		var autoScrollingCount=0;
		function AutoScrolling(){
			hdjImgscroller.delay(options.autoScrollingDelay).animate({left:-(totalWidth-hdjImgthis.width()),top:-(totalHeight-hdjImgthis.height())},options.autoScrollingSpeed,options.autoScrollingEasing,function(){
				hdjImgscroller.animate({left:0,top:0},options.autoScrollingSpeed,options.autoScrollingEasing,function(){
					autoScrollingCount++;
					if(options.autoScrolling>1 && options.autoScrolling!=autoScrollingCount){
						AutoScrolling();
					}
				});
			});
		}
		//click scrolling fn
		function ClickScrolling(){
			hdjImgscrollerPrevButton.hide();
			if(totalWidth<=hdjImgthis.width())
			{
				hdjImgscrollerNextButton.hide();
			}
			else
			{
				hdjImgscrollerNextButton.show();
			}
			hdjImgscrollerNextButton.click(function(e){ //next button
			
				e.preventDefault(); 
				var posX=hdjImgscroller.position().left;
				var diffX=totalWidth+(posX-hdjImgthis.width());
				var posY=hdjImgscroller.position().top;
				var diffY=totalHeight+(posY-hdjImgthis.height());
				hdjImgscrollerPrevButton.stop().show("fast");
				if(options.scrollerOrientation=="horizontal"){
					if(diffX>=hdjImgthis.width()){
						hdjImgscroller.stop().animate({left:"-="+hdjImgthis.width()},options.scrollSpeed,options.scrollEasing,function(){
							if(diffX==hdjImgthis.width()){
								hdjImgscrollerNextButton.stop().hide("fast");
							}
						});
					} else {
						hdjImgscrollerNextButton.stop().hide("fast");
						hdjImgscroller.stop().animate({left:hdjImgthis.width()-totalWidth},options.scrollSpeed,options.scrollEasing);
					}
				}else{
					if(diffY>=hdjImgthis.height()){
						hdjImgscroller.stop().animate({top:"-="+hdjImgthis.height()},options.scrollSpeed,options.scrollEasing,function(){
							if(diffY==hdjImgthis.height()){
								hdjImgscrollerNextButton.stop().hide("fast");
							}
						});
					} else {
						hdjImgscrollerNextButton.stop().hide("fast");
						hdjImgscroller.stop().animate({top:hdjImgthis.height()-totalHeight},options.scrollSpeed,options.scrollEasing);
					}
				}
			});
			hdjImgscrollerPrevButton.click(function(e){ //previous button
		        
				e.preventDefault();
				
				var posX=hdjImgscroller.position().left;
				var diffX=totalWidth+(posX-hdjImgthis.width());
				var posY=hdjImgscroller.position().top;
				var diffY=totalHeight+(posY-hdjImgthis.height());
				hdjImgscrollerNextButton.stop().show("fast");
				if(options.scrollerOrientation=="horizontal"){
					if(posX+hdjImgthis.width()<=0){
						
						hdjImgscroller.stop().animate({left:"+="+hdjImgthis.width()},options.scrollSpeed,options.scrollEasing,function(){
							if(posX+hdjImgthis.width()==0){
									
								hdjImgscrollerPrevButton.stop().hide("fast");
							}
						});
					} else {
						
						hdjImgscrollerPrevButton.stop().hide("fast");
						hdjImgscroller.stop().animate({left:0},options.scrollSpeed,options.scrollEasing);
					}
				}else{
					if(posY+hdjImgthis.height()<=0){
						hdjImgscroller.stop().animate({top:"+="+hdjImgthis.height()},options.scrollSpeed,options.scrollEasing,function(){
							if(posY+hdjImgthis.height()==0){
								hdjImgscrollerPrevButton.stop().hide("fast");
							}
						});
					} else {
						hdjImgscrollerPrevButton.stop().hide("fast");
						hdjImgscroller.stop().animate({top:0},options.scrollSpeed,options.scrollEasing);
					}
				}
			});
		}
	});  
 };  
 
 
	
	
 hdjMpuScroller(document).ready(
  function() {
	/*  hdj('div.tmpSlideshowControlsPopUP').hide();*/
	 hdjMpuScroller("#tS2").thumbnailScroller
 ({ 
		scrollerType:"clickButtons", 
		scrollerOrientation:"horizontal", 
		scrollSpeed:2, 
		scrollEasing:"easeInOutQuint", 
		scrollEasingAmount:600, 
		acceleration:1, 
		scrollSpeed:800, 
		noScrollCenterSpace:10, 
		autoScrolling:0, 
		autoScrollingSpeed:2000, 
		autoScrollingEasing:"easeInOutQuint", 
		autoScrollingDelay:500 
		
	});
  
  }
);
})(jQuery); 
//global js functions
//find element Position
function findPos(obj){
	var curleft=curtop=0;
	if (obj.offsetParent){
		curleft=obj.offsetLeft
		curtop=obj.offsetTop
		while(obj=obj.offsetParent){
			curleft+=obj.offsetLeft
			curtop+=obj.offsetTop
		}
	}
	return [curtop,curleft];
}
