﻿// JavaScript Document
function scrollBarHoriz_obj(){
	var ME = this
	var DIVS = this.getElementsByTagName("BUTTON")
	var leftButton = DIVS.leftButton
	var rightButton = DIVS.rightButton
	var scrollButton = DIVS.scrollButton
	var myID = ME.id
	
	var myMouseOverStatus = 0
	var minLeft = leftButton.offsetLeft +  leftButton.offsetWidth+10
	var maxLeft = rightButton.offsetLeft - scrollButton.offsetWidth -10
	var clickOnScrollButton  = false
	var clickOnLeftRightButton = false
	
	var myTargetID = myID.split('_')[1]
	var myTarget = window.document.getElementById(myTargetID)
	var myTargetInnerDiv = myTarget.getElementsByTagName("DIV")[0]
	var myTargetMaxScroll = 0
	var s
	var myBodyWidth = 790
	var myBodyHeight = 490;
	var weelDir = window.document.body.dir.toLowerCase()=="rtl"? -1:1
	
	ME.unselectable = "on"
	rightButton.unselectable = "on"
	leftButton.unselectable = "on"
	scrollButton.unselectable = "on"
	scrollButton.onfocus = function(){
		scrollButton.blur()
	}
	leftButton.onfocus = function(){
		leftButton.blur()
	}
	rightButton.onfocus = function(){
		rightButton.blur()
	}
	ME.onmouseover = function(){
		myMouseOverStatus = 1
	}
	ME.onmouseout = function(){
		/*var s
		myMouseOverStatus = 2
		s = setTimeout(myMouseOutStatusTimer, 700)	*/	
	}
	myMouseOutStatusTimer = function(){
		if(myMouseOverStatus == 2 ){
			myMouseOverStatus = 0
			onBodyMouseUp()
		}		
	}
	function onBodyMouseUp(){
		 clickOnScrollButton  = false
		 clickOnLeftRightButton = false
		 window.document.body.onmousemove = ""
		
	}
	/*********************** Scroll Button Behavior *******************************/
	scrollButtonBehavior =function(){
		var xClickPoz,yClickPoz
		var clickPoz
		
	
		scrollButton.onmousedown=function(e){
			
			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				myBodyWidth = window.innerWidth;
				myBodyHeight = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				//IE 6+ in 'standards compliant mode'
				myBodyWidth = document.documentElement.clientWidth;
				myBodyHeight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				myBodyWidth = document.body.clientWidth;
				myBodyHeight = document.body.clientHeight;
			}
				if(window.event) e = window.event
				xClickPoz = e.clientX
				yClickPoz = e.clientY
				clickPoz = this.offsetLeft
				clickOnScrollButton = true
				window.document.body.onmousemove=onBodymousemove
				
				//window.document.onmouseover=myMouseOutStatusTimer
			}
		
		onMouseUpArr[onMouseUpArr.length]= onBodyMouseUp
		onBodymousemove = function(e){
			var scrollCoefficient
			
			if (clickOnScrollButton && myMouseOverStatus > 0){
				if(window.event) e = window.event
				
				//window.document.title=myBodyWidth + "  "+e.clientY
				newPoz =  clickPoz + ( (xClickPoz -  e.clientX)*-1 )
				if (newPoz>maxLeft)newPoz = maxLeft
				if (newPoz<minLeft)newPoz = minLeft
					scrollButton.style.left = newPoz.toString() + "px" 
					scrollCoefficient = (newPoz-minLeft)/(maxLeft-minLeft)
					if(bowserType=="firefox" && window.document.body.dir.toLowerCase()=="rtl"){
						myTarget.scrollLeft =  (scrollCoefficient * myTargetMaxScroll)- (myTargetInnerDiv.offsetWidth - myTarget.offsetWidth)
					}else{
						myTarget.scrollLeft =scrollCoefficient * myTargetMaxScroll
					}
				if (e.clientX<25 || e.clientX>myBodyWidth-5){window.document.body.onmousemove=""; return false}
				if (e.clientY<25 || e.clientY>myBodyHeight-30){window.document.body.onmousemove=""; return false}
			}
			//window.document.title= myBodyHeight +" --- "+e.clientY
		}
		
		
		
	
	}
	scrollButton.startMe = scrollButtonBehavior
	scrollButton.startMe()
	/*********************** END  Scroll Button Behavior *******************************/
	/***********************   Scroll Target Listener  *******************************/
	var scrlLeft
	var stage2innerProportions 
	function myTargetListener(){
		scrlLeft = Math.abs(myTarget.scrollLeft)
		if(bowserType=="firefox" && window.document.body.dir.toLowerCase()=="rtl") scrlLeft = myTargetInnerDiv.offsetWidth- myTarget.offsetWidth -scrlLeft
		stage2innerProportions = myTargetInnerDiv.offsetWidth/  myTarget.offsetWidth * 0.1
		
		minLeft = leftButton.offsetLeft +  leftButton.offsetWidth -5
		maxLeft = rightButton.offsetLeft - scrollButton.offsetWidth +5
		if(clickOnScrollButton) return false
		//window.document.title= myTarget.scrollLeft
		if(myTarget.offsetWidth > myTargetInnerDiv.offsetWidth){
			scrollButton.style.visibility = "hidden"
			
			
		}else{
			
			scrollButton.style.visibility = "visible"
			myTargetMaxScroll =  myTargetInnerDiv.offsetWidth - myTarget.offsetWidth 
			var scrollCoefficient = scrlLeft / myTargetMaxScroll
			if (scrollCoefficient>1) scrollCoefficient = 1
			newPoz = (maxLeft-minLeft) * scrollCoefficient + leftButton.offsetWidth-5
			scrollButton.style.left = newPoz.toString() + "px"
		}
		
		
	}
	setInterval(myTargetListener, 70)	
	/***********************  End Scroll Target Listener  *******************************/
	/***********************  Target Wheel Scroll   *******************************/
	
	
	function weelFunction(e){
		
		if(!anyScrollMouseOver && !popupImag){
			if (!e) e = window.event;
			if (clickOnScrollButton)onBodyMouseUp()
			//prompt("",bowserType)
			if(e.detail){
				switch (bowserType){
					case 'firefox':
						myTarget.scrollLeft = e.detail  > 0 ? myTarget.scrollLeft -  50*-weelDir: myTarget.scrollLeft + 50*-weelDir
					break;
					default:
						myTarget.scrollLeft = e.detail  > 0 ? myTarget.scrollLeft +  50*weelDir: myTarget.scrollLeft -  50*weelDir
				}
			}else if(e.wheelDelta){
				switch (bowserType){
					case 'chrome':
						myTarget.scrollLeft = e.wheelDelta>0 ? myTarget.scrollLeft + 50*-weelDir : myTarget.scrollLeft - 50*-weelDir
					break;
					default:
						myTarget.scrollLeft = e.wheelDelta<0 ? myTarget.scrollLeft + 50*weelDir : myTarget.scrollLeft - 50*weelDir
					break;
				}
			}
		}
		return false;
		
		
	} 
	window.document.onmousewheel = weelFunction
	//ME.onmousewheel =myTarget.onmousewheel
	if (window.addEventListener) {
			window.document.addEventListener('DOMMouseScroll', weelFunction , false);
		/*myTarget.addEventListener('DOMMouseScroll', myTarget.onmousewheel , false);
		ME.addEventListener('DOMMouseScroll', myTarget.onmousewheel , false);*/
	}
	
	/***********************  End Target Wheel Scroll  *******************************/
	/***********************   Start scroll Up/Down Button  *******************************/
	function f_scrollRight(){
		if(clickOnLeftRightButton){
			myTarget.scrollLeft = myTarget.scrollLeft - (8 * (1+stage2innerProportions))
			clearTimeout(s)
			s= setTimeout(f_scrollRight,15)
			
		}
	}
	leftButton.onmousedown = function(){
		clickOnLeftRightButton = true
		f_scrollRight()
	}
	function f_scrollLeft(){
		if(clickOnLeftRightButton){
			myTarget.scrollLeft = myTarget.scrollLeft +( 8 * (1+stage2innerProportions))
			clearTimeout(s)
			s= setTimeout(f_scrollLeft,15)
			//window.document.title= stage2innerProportions
		}
	}
	rightButton.onmousedown = function(){
		clickOnLeftRightButton = true
		f_scrollLeft()
	}
	ME.scrllExternal = function(e){
		
		if(window.event) e = window.event
		if(!popupImag){
			switch(e.keyCode){
				case 37:
				//Left
					clickOnLeftRightButton = true
					f_scrollRight()
					
				break;
				case 39:
				//rigth
					clickOnLeftRightButton = true
					f_scrollLeft()
				
				break;
				
			
			}
			if (clickOnLeftRightButton){
				window.document.onkeyup = function(){
					onBodyMouseUp()
					window.document.onkeyup = ""
					
				}
			}
		}
		
	}
	if (window.document.getElementById("glob_leftButton")){
		window.document.getElementById("glob_leftButton").onmouseover=function(){
			
			
		}
		window.document.getElementById("glob_leftButton").onmouseout=function(){
			if(!clickOnScrollButton){
				clickOnLeftRightButton = false
				
				
			}
			
		
		}
		
		
		
	}
	if (window.document.getElementById("glob_rightButton")){
		window.document.getElementById("glob_rightButton").onmouseover=function(){
			if(!clickOnScrollButton){
				clickOnLeftRightButton = true
				f_scrollRight()
				
			}
			
		}
		window.document.getElementById("glob_rightButton").onmouseout=function(){
			if(!clickOnScrollButton){
				clickOnLeftRightButton = false
				
				
			}
			
		
		}
		
		
		
	}
	ME.public_scrllLeft = function(){
		if(!clickOnScrollButton){
				clickOnLeftRightButton = true
				f_scrollLeft()
				return true
		}else{
			return false	
		}
		
		
	}
	ME.public_scrllRight = function(){
		if(!clickOnScrollButton){
				clickOnLeftRightButton = true
				f_scrollRight()
				return true
		}else{
			return false	
		}
		
	}
	ME.public_stop= function(){
		clickOnLeftRightButton = false
		
	}
	
	
}
