var scrollerArray= [];

function scrolleradd(name, width, offsetHeight){
scrollerArray [scrollerArray.length]=new scroller(name,width, offsetHeight) ;
}
function scrollerget(name){
 var obj=null;
 for (var i =0; i<scrollerArray.length; i++) {
  if (scrollerArray[i].name== name)   { obj= scrollerArray[i]; break; }
 }
 return obj;
}

	function pixel(pxVal) {
		var s_px = new String(pxVal);
		if (s_px.indexOf('px',0) > 0)
			return s_px.substr(0, s_px.indexOf('px',0)) - 0;
		else
			return pxVal - 0;
	}

function scroller(name,width){
	var m_scrollTop=0;
	var m_visible=true;
	var m_iScrollDistDef = 2;
	var m_iScrollSpeed = 30;
	var m_iScrollDist = m_iScrollDistDef;
	var m_iNxt = null;
	var m_iCur = false;
	var m_visibleheight =100;
	this.name=name;
	this.width=width;
	this.hide=p_hide;
	this.scrollup=p_scrollup;
	this.scrolldown=p_scrolldown;
	this.scroll=p_scroll;
	this.stopscroll=p_stopscroll;
	this.window_onresize=p_window_onresize;
	this.getElement=p_getElement;
	this.move=p_move;
	this.toggle = p_toggle;
	this.setvisibility = p_SetVisibility;
	//this.toggle2=p_toggle2;

	function p_getElement(suffix){
		return document.getElementById(this.name + suffix);
	}


	function p_window_onresize() {

        var scrlayer=this.getElement('scrlayer');
		if (scrlayer != null) {
		    p_setheight(scrlayer);
	        m_visibleheight=pixel(scrlayer.offsetHeight);
    	}

		var curTop = this.move(0);
		m_ScrollTop = curTop;
		return false;
	}

	function p_move(dist) {
		div = this.getElement('content');
		contentHeight = pixel(div.offsetHeight);
		var new_top = pixel(div.style.top) + dist;
		if (new_top < m_visibleheight-contentHeight ) new_top = m_visibleheight-contentHeight;
		if (new_top > 0) new_top = 0;
		if (new_top<0){
		    this.getElement('up').style['display']='block';
		}else{
            this.getElement('up').style['display']='none';
		}
		if (m_visibleheight-new_top-contentHeight<0 && m_visibleheight<contentHeight){
		    this.getElement('down').style['display']='block';
		}else{
            this.getElement('down').style['display']='none';
		}
		div.style.top = new_top + 'px';
    		return new_top;
	}

	function alignMenu(offset) {
		if (offset > 0) curTop = moveUp(offset);
		else curTop = moveDown(-offset);
		ScrollTop = curTop;
	}

    function p_scrollup() {
		try{
			this.move(m_iScrollDist);
			var me=this;
        	m_iNxt = setTimeout(function(){me.scrollup();}, m_iScrollSpeed);
			m_iScrollDist+=m_iScrollDistDef;
			m_iCur = true;
		}catch(e){
		}
		return false;
	}


    function p_hide() {
		try{
		    var div=this.getElement('');
        	div.style.width = '0px';
  			m_visible=false;
       	    var divtab=this.getElement('tab');
       	    divtab.className='hidden';

  	        if (this.name=='basket'){
  	            if(this.getElement('promo')!=null){
	                this.getElement('promo').style.width = this.width + 'px';
	            }
    	        this.getElement('summary').style.display = 'block';
	        }

		}catch(e){
		}
		return false;
	}

//	function p_toggle2(wcat,wInfo, catHide) {
//
//		//this.toggle();
//		 var w2=pixel(document.getElementById('category').style['width']);
//
//		if (catHide==1) {
//		    wInfo=wInfo +(wcat);
//		    document.getElementById('category').style['width']='0px';
//		}else {
//		    document.getElementById('category').style['width']=wcat +'px';
//		}
//		document.getElementById('InfoFrame').style['width']=wInfo +'px';
//		}

    function p_toggle() {
		try{
		    var div=this.getElement('');
			var w=pixel(div.offsetWidth);
		    if (m_visible==true){
			    w=Math.floor(w*.9-1);
    	    }else{
			    w=Math.floor(w*1.1+1);
            }
			var timeout=2;
			if(w<=0){
			   m_visible=false;
			   w=0;
			   timeout=0;
		    }
			if(w>=this.width){
			   m_visible=true;
			   w=this.width;
			   timeout=0;
		    }
        	div.style.width = w + 'px';
        	if (pixel(div.offsetWidth)!=w){
  			   m_visible=false;
			   timeout=0;
          	}

  	        if (this.name=='basket'){
	            if(this.getElement('promo')!=null){
	                this.getElement('promo').style.width = this.width-w + 'px';
	            }
	        }

			var me=this;
        	if (timeout>0){
        	  	setTimeout(function(){me.toggle();}, timeout);
        	}else{
        	    var divtab=this.getElement('tab');
        	    divtab.className=m_visible==true ? 'visible':'hidden';
        	    if (this.name=='basket'){
        	        this.getElement('summary').style.display = m_visible == true ? 'none' : 'block';
        	        // The below code doesn't work in v3 yet. Meant for an extended summary when hovering over the minimized basket
//        	        if (m_visible != true) {
//        	            tempObj = $('bitems');
//        	            var items = '';
//        	            if (tempObj != null) {
//        	                items = tempObj.innerHTML;
//        	                tempObj.innerHTML = '';
//        	            }
//        	            tempObj = $('summarybitems');
//        	            if (tempObj != null) {
//        	                tempObj.innerHTML = items;
//        	            }
//        	            tempObj = $('basketAddItem');
//        	            if (tempObj != null) {
//        	                items = tempObj.innerHTML;
//        	                tempObj = $('summarybasketAddItem');
//        	                if (tempObj != null) {
//        	                    tempObj.innerHTML = items;
//        	                }
//        	            }
//        	        } else {
//        	            tempObj = $('summarybitems');
//        	            var items = '';
//        	            if (tempObj != null) {
//        	                items = tempObj.innerHTML;
//        	                tempObj.innerHTML = '';
//        	            }
//        	            tempObj = $('bitems');
//        	            if (tempObj != null) {
//        	                tempObj.innerHTML = items;
//        	            }
//        	            tempObj = $('summarybasketAddItem');
//        	            if (tempObj != null) {
//        	                items = tempObj.innerHTML;
//        	                tempObj = $('basketAddItem');
//        	                if (tempObj != null) {
//        	                    tempObj.innerHTML = items;
//        	                }
//        	            }
//        	        }
                     neotekscript('/main/control.aspx?ajax=1&action=loginps&ptype=18&pval=' + (m_visible==true ? '1':'0'));
           	    }else{
                      neotekscript('/main/control.aspx?ajax=1&action=loginps&ptype=19&pval=' + (m_visible==true ? '1':'0'));
          	    }
        	}
		}catch(ex){
		    alert(ex);
		}
		return false;
	}

	function p_scrolldown() {
		try{
			this.move(-m_iScrollDist)
			var me=this;
			m_iNxt = setTimeout(function(){me.scrolldown();},m_iScrollSpeed);
			m_iScrollDist+=m_iScrollDistDef;
			m_iCur = true;
		}catch(e){
		}
		return false;
	}

	function p_scroll(delta) {
		try{
			this.move(delta);
		}catch(e){
		}
		m_iScrollDist+=delta;
		return false;
	}

	function p_stopscroll() {
		try{
			if(m_iCur){
				clearTimeout(m_iNxt);
			}
			m_iScrollDist = m_iScrollDistDef;
			m_iCur = false;
		}catch(e){
		}
		return false;
    }

    function p_SetVisibility(state) {
        var div = this.getElement('');
        var divtab = this.getElement('tab');
        if (this.name == 'basket' && $('basketsummary') != null) {
            divtab = $('basketsummary');
        }
        
        if (state == 1) { // Visible and Opened
            div.style.display = 'block';
            divtab.style.display = (divtab.id != 'basketsummary') ? 'block' : 'none';
            m_visible = true;
        }
        else if (state == 2) { // Visible but mimimized
            div.style.display = 'block';
            div.style.width = '0px';
            divtab.style.display = 'block';
            m_visible = false;
        } else { // Hide
            div.style.display = 'none';
            divtab.style.display = 'none';
            m_visible = false;
        }
    }
}




