$(function(){
		$("#scroll").easydrag();		
		 SetVariable();
	});
	var toPosition = 0;
	var IFramePageHeight = 0;
	var IFrameHeight = 0;	
	var timeOutId;
	var bFirstTimeSetVariable = true;
	var IEBigger = 0;	
	var ScrollHeight=0;
	var MIN_SCROLL_HEIGHT = 20;
	var SCROLL_TOP_BOTTOM_PIC_HEIGHT = 14
	var MaxScrollDown = 0;
	var MovQuotiety = 1;	
	var MouseDownTimeOutId;
	var MouseUpTimeOutId;
	var ScrollImgTop="scrollTop";
	var ScrollImgBt="scrollBt";
	var IsClicked = false;

	function CalculateAndScroll(num)
	{
		toPosition = num*MovQuotiety;
		SetScroll(toPosition,false);
	}	
	
	function ClickAndScroll(num)
	{
	    IsClicked = true;
        toPosition = toPosition+(num*MovQuotiety);
        SetScroll(toPosition,true);		
	}

	function StillDown()
	{
	    //$("#bb").val("stilldown"+toPosition);
	    //alert("still down 1");
	    IsClicked = true;
		toPosition = toPosition+(4*MovQuotiety);
		//alert("still down 2");
		SetScroll(toPosition,true);
		//alert("still down 3");
		MouseDownTimeOutId = window.setTimeout("StillDown()", 100);		
	}
	function ClearStillDown()
	{
	    IsClicked = false;
		clearTimeout(MouseDownTimeOutId);
	}
	function StillUp()
	{
	    IsClicked = true;
		toPosition = toPosition+(-4*MovQuotiety);
		SetScroll(toPosition,true)
		MouseUpTimeOutId = window.setTimeout("StillUp()", 100);
	}
	function ClearStillUp()
	{
	    IsClicked = false;
		clearTimeout(MouseUpTimeOutId);
	}

    function ResetScrollPosition()
    {        
//        IFramePageHeight = 0;
//        IFrameHeight = 0;	
//        
//        bFirstTimeSetVariable = true;
//        IEBigger = 0;	
//        ScrollHeight=0;
//        MIN_SCROLL_HEIGHT = 20;
//        SCROLL_TOP_BOTTOM_PIC_HEIGHT = 14
//        MaxScrollDown = 0;
//        MovQuotiety = 1;
        //IsClicked = false;	
        
        toPosition = 0;
        document.getElementById(ScrollDivID).style.position="absolute";
		document.getElementById(ScrollDivID).style.top="0px";
		
		//reset the iframe content page's scroll at 0
		MovIFramePageTo(0);
    } 
    
	function SetScroll(num,fromClick)
	{			
	    //document.getElementById("aa").value=num;
		if(toPosition<0)
		{
			toPosition = 0;
		}
		else if(toPosition>(IFramePageHeight-IFrameHeight))
		{
			toPosition = IFramePageHeight-IFrameHeight;			
		}	
		MovIFramePageTo(toPosition);
		if(fromClick)
		{				
			document.getElementById(ScrollDivID).style.position="absolute";
			document.getElementById(ScrollDivID).style.top=toPosition/MovQuotiety + "px";
		}
	}

	function MovIFramePageTo(PositionY)
	{ 
	   if (document.getElementById){ 
		  news = document.getElementById(IFRAME_NAME); 
	   } 
	   else{ 
		  eval('news = "news";'); 
	   } 
	   if(window.navigator.userAgent.indexOf("MSIE")>=1)//ie
	   {
		   //alert("ie");
		   //return;
			eval(IFRAME_NAME).scrollTo(0,toPosition);
	   }
	   else if (news.contentDocument)//firefox
	   {
			news.contentWindow.scrollTo(0,PositionY); 
	   } 
	   else if (news.document)//other
	   {
			news.document.scrollTo(0,PositionY); 
	   } 
	}

	function GetIFrameHeight() 
	{
	    try
        {
		    if (window.navigator.userAgent.indexOf("MSIE")>=1)
		    {
			    return document.frames[IFRAME_NAME].document.body.scrollHeight-IEBigger;
		    }
		    else if(window.navigator.userAgent.indexOf("Firefox")>=1) 
		    {   //alert("firefox");
			    return document.getElementById(IFRAME_NAME).contentDocument.documentElement.scrollHeight;
			    //return document.frames[IFRAME_NAME].document.documentElement.scrollHeight;
		    }
		    else
		    {
		        if(IFramePageHeight==0)
		        {
			        return document.getElementById(IFRAME_NAME).contentDocument.documentElement.clientHeight;
			    }
			    else
			    {
			        return document.getElementById(IFRAME_NAME).contentDocument.body.clientHeight;
			    }
		    }		
		}
        catch(err)
        {
            //do nothing
        }
	}
	
	function SetVariable()
	{		
		//alert('aa');
		IFramePageHeight = GetIFrameHeight();
		IFrameHeight = document.getElementById(IFRAME_NAME).height;		

		if(IFramePageHeight==0)
		{			
			timeOutId = window.setTimeout("SetVariable()", 100);
		}
		else if(!bFirstTimeSetVariable)
		{
			clearTimeout(timeOutId);
			if (window.navigator.userAgent.indexOf("MSIE")>=1)
		    {
		        //alert("ie");
			    IFramePageHeight = document.frames[IFRAME_NAME].document.body.scrollHeight-IEBigger;
		    }
		    else
		    {
			    IFramePageHeight = document.getElementById(IFRAME_NAME).contentDocument.body.clientHeight;
			}
		}
		bFirstTimeSetVariable = false;		
		
		//alert(IsClicked);
		// set the iFrame's scroll height
	    if(IsClicked)
	    {
	        //alert("aa");
	        IsClicked = false;
	        return false;    
	    }
		SetScrollHeight();	
	}

    function SetScrollVisible(Visible)
    {
        if(Visible)
        {
            document.getElementById(ScrollDivID).style.display = "" ;
            document.getElementById(ScrollImgID).style.display = "" ;
            
            if(document.getElementById(ScrollImgTop))
            {
                document.getElementById(ScrollImgTop).style.display= "";
                document.getElementById(ScrollImgBt).style.display="";
            }
		}
		else
		{
		    document.getElementById(ScrollDivID).style.display = "none" ;
	        document.getElementById(ScrollImgID).style.display = "none" ;

            if(document.getElementById(ScrollImgTop))
            {
	            document.getElementById(ScrollImgTop).style.display= "none";
	            document.getElementById(ScrollImgBt).style.display="none";
	        }
		}
    }
    
	function SetScrollHeight()
	{	    
		// the iframe content already loaded
		if(IFramePageHeight>0)
		{
			var barHeight = Math.round(ScrollOriginalHeight*(ScrollOriginalHeight/IFramePageHeight));			
			document.getElementById(ScrollDivID).width = 16;
			document.getElementById(ScrollImgID).width = 16;
					
			//alert("barHeight"+barHeight)
			if(barHeight>=ScrollOriginalHeight)
			{			   
				 SetScrollVisible(false);
			}
			else
			{
			    SetScrollVisible(true);
			    		
				ResetScrollPosition();			
				
				if(barHeight<MIN_SCROLL_HEIGHT)
				{
					MaxScrollDown = ScrollOriginalHeight - MIN_SCROLL_HEIGHT;
					document.getElementById(ScrollDivID).height = MIN_SCROLL_HEIGHT;
					document.getElementById(ScrollImgID).height = MIN_SCROLL_HEIGHT-SCROLL_TOP_BOTTOM_PIC_HEIGHT;				
				}
				else
				{
					MaxScrollDown = ScrollOriginalHeight - barHeight;
					document.getElementById(ScrollDivID).height = barHeight;
					document.getElementById(ScrollImgID).height = barHeight-SCROLL_TOP_BOTTOM_PIC_HEIGHT;
				}				
				MovQuotiety = (IFramePageHeight-IFrameHeight)/MaxScrollDown;
			}
		}
	}