function initScrollGallery() {
	$('div.gallery, div.gallery-area > div').each(function(){
		var _circleSlide = true;
		var _slideSpeed = 1000;
		var _gallery = $(this);
		var _btnPrev = _gallery.find('a.btn-prev');
		var _btnNext = _gallery.find('a.btn-next');
		var _holder = _gallery;
		var _slider = _holder.find('ul');
		var _slideItems = _slider.find('li');

		var _scrollWidth = _holder.width();
		var _sumWidth = 0;
		_slideItems.each(function(){_sumWidth+=$(this).outerWidth(true)});
		var _step = 0;
		var _stepCount = Math.ceil(_sumWidth / _scrollWidth);
		var _offset;
		var _maxOffset = -(_sumWidth-_scrollWidth);

		_btnPrev.click(function(){
			prevSlide();
			return false;
		});
		_btnNext.click(function(){
			nextSlide();
			return false;
		});

		function prevSlide() {
			if(_step > 0) {
				_step--;
				if(_step == 0) _offset = 0;
				else _offset = -_step*_scrollWidth;
				animateSlide();
			} else {
				if(_circleSlide) {
					_step = _stepCount-1;
					_offset = _maxOffset;
					animateSlide();
				}
			}
		}
		function nextSlide() {
			if(_step < _stepCount-1) {
				_step++;
				if(_step == _stepCount-1) _offset = _maxOffset;
				else _offset = -_step*_scrollWidth;
				animateSlide();
			} else {
				if(_circleSlide) {
					_step = 0;
					_offset = -_step*_scrollWidth;
					animateSlide();
				}
			}
		}
		function animateSlide() {
			_slider.animate({marginLeft:_offset},{duration:_slideSpeed,queue:false});
		}
	});
}

function initSlideShow() {
	$('div.product-section').slideShow({
		numElementLink:'div.gallery-area ul a',
		slideEl:'div.slideset > div.slide',
		autoSlideShow:false,
		switchTime:3000,
		duration:650
	});
}

function initPopups() {
	$(function(){
		$('a.open-popup').simpleLightbox({
			faderOpacity: 0.75,
			faderBackground: '#000',
			closeLink:'a.close'
		});
	});
}

function initOpenClose() {
	$('.collapsed-text').each(function(){
		var _opener = $(this).find('.text-opener');
		var _oDots = _opener.find('em');
		var _oDef = _opener.find('span');
		var _oClose = _opener.find('strong');

		var _hidden =  $(this).find('.hidden-text');
		_opener.click(function(){
			if(_hidden.is(':visible')) {
				_hidden.hide();
				_oDots.show();
				_oDef.show();
				_oClose.hide();
			} else {
				_hidden.show();
				_oDots.hide();
				_oDef.hide();
				_oClose.show();
			}
			return false;
		});
	});
}

// page init
$(document).ready(function() {
	initSlideShow();
	initPopups();
	initOpenClose();
});
$(window).bind("load", function() {
	VSA_initScrollbars();
	initScrollGallery();
});

/* lightbox */
jQuery.fn.simpleLightbox = function(_options){
	// defaults options
	var _options = jQuery.extend({
		lightboxContentBlock: '.lightbox',
		faderOpacity: 0.8,
		faderBackground: '#ffffff',
		closeLink:'a.close-btn, a.cancela',
		href:true,
		onClick: null
	},_options);

	return this.each(function(i, _this){
		var _this = jQuery(_this);
		if (!_options.href)
			_this.lightboxContentBlock = _options.lightboxContentBlock;
		else _this.lightboxContentBlock = _this.attr('href');
		if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {
			_this.faderOpacity = _options.faderOpacity;
			_this.faderBackground = _options.faderBackground;
			_this.closeLink = _options.closeLink;
			var _fader;
			var _lightbox = $(_this.lightboxContentBlock);
			if (!jQuery('div.lightbox-fader').length)
				_fader = $('body').append('<div class="lightbox-fader"></div>');

			_fader = jQuery('div.lightbox-fader');
			_lightbox.css({
				'zIndex':999
			});
			_fader.css({
				opacity:_this.faderOpacity,
				backgroundColor:_this.faderBackground,
				display:'none',
				position:'absolute',
				top:0,
				left:0,
				zIndex:998,
				textIndent: -9999
			}).text('$nbsp');
			_lightbox.shownFlag = false;
			_this.click(function(){
				if (jQuery.isFunction(_options.onClick)) {
					_options.onClick.apply(_this);
				}
				_lightbox.shownFlag = true;
				_lightbox.hide();
				jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				_fader.fadeIn(300, function(){
					_lightbox.fadeIn(400);
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				});
				return false;
			});
			jQuery(_this.closeLink).click(function(){
				_lightbox.fadeOut(400, function(){
					_fader.fadeOut(300);
					_scroll = false;
				});
				return false;
			});
			_fader.click(function(){
				_lightbox.fadeOut(400, function(){
					_fader.fadeOut(300);
				});
				return false;
			});
			var _scroll = false;
			jQuery.fn.simpleLightbox.positionLightbox = function (_lbox) {
				if(!_lbox.shownFlag) return false;
				var _height = 0;
				var _width = 0;
				var _minWidth = $('body > div:eq(0)').outerWidth();
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				} else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				var _thisHeight = _lbox.outerHeight();
				var _page = $('body > div:eq(0)');
				if (_lbox.length) {
					if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
					if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
					if (_height > _thisHeight) {
						if ($.browser.msie && $.browser.version < 7) {
							_lbox.css({
								position:'absolute',
								top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
							});
						} else {
							_lbox.css({
								position:'fixed',
								top: ((_height - _lbox.outerHeight()) / 2)+"px"
							});
						}
					}
					else {
						var _fh = parseInt(_fader.css('height'));
						if (!_scroll) {
						if (_fh - _thisHeight > parseInt($(document).scrollTop())) {
								_fh = parseInt($(document).scrollTop())
								_scroll = _fh;
							} else {
								_scroll = _fh - _thisHeight;
							}
						}
						_lbox.css({
							position:'absolute',
							top: _scroll
						});
					}
					if (_width > _lbox.outerWidth()) _lbox.css({left:((_width - _lbox.outerWidth()) / 2 + 10) + "px"});
					else _lbox.css({position:'absolute',left: 0});
				}
			}

			jQuery(window).resize(function(){
				if (_lightbox.is(':visible'))
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			jQuery(window).scroll(function(){
				if (_lightbox.is(':visible'))
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});

			jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				$(document).keydown(function (e) {
				if (!e) evt = window.event;
				if (e.keyCode == 27) {
					_lightbox.fadeOut(400, function(){
						_fader.fadeOut(300);
					});
				}
			});
		}
	});
}

/* slideshow */
jQuery.fn.slideShow = function(_options){
	// defaults options
	var _options = jQuery.extend({
		slideEl:'div.img-hold img',
		linkNext:'a.next',
		linkPrev:'a.prev',
		linkPause:'a.pause',
		numElementLink:'div.nav li a',
		duration:500,
		autoSlideShow:false,
		switchTime:3000,
		event:'click',
		currentEl:'#footer span.cur',
		allEl:'#footer span.all'
	},_options);

	return this.each(function(){
		var _THIS = jQuery(this);
		var _slideEl = $(_options.slideEl, _THIS);
		var _elTagName = _options.slideEl;
		var _linkNext = $(_options.linkNext, _THIS).length ? $(_options.linkNext, _THIS) : false;
		var _linkPrev = $(_options.linkPrev, _THIS).length ? $(_options.linkPrev, _THIS) : false;
		var _linkPause = $(_options.linkPause, _THIS).length ? $(_options.linkPause, _THIS) : false;
		var _numElementLink = $(_options.numElementLink, _THIS).length ? $(_options.numElementLink, _THIS) : false;
		var _numElement = $(_options.numElementLink, _THIS).length ? _numElementLink.parent() : false;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _numElActive, _n, _timer = false, _hover = false;

		if (!_slideEl.filter('.active').length)
				_slideEl.eq(0).addClass('active');

		$(_slideEl).not(".active").css('display','none');

		if ($(_numElement).length) {activeNumEl()}

		if (_options.autoSlideShow) {
				_timer = setTimeout(function(){nextEl()},_switchTime);
				contentHover();
		}
		if (_linkNext) {
				$(_linkNext).click(function(){
						if (_timer) {
								clearTimeout(_timer);
								_timer = setTimeout(function(){nextEl()},_switchTime);
						}
						if ($(_slideEl).filter(".active").next().is(_elTagName)) {
							$(_slideEl).filter(".active").next().addClass("next");
						} else {
							$(_slideEl).eq(0).addClass("next");
						}
						fadeElement();
						activeNumEl();
						return false;
				});
		}
		if (_linkPrev) {
				$(_linkPrev).click(function(){
						if (_timer) {
								clearTimeout(_timer);
								_timer = setTimeout(function(){nextEl()},_switchTime);
						}
						if ($(_slideEl).filter(".active").prev().is(_elTagName)) {
							$(_slideEl).filter(".active").prev().addClass("next");
						} else {
							$(_slideEl).filter(":last-child").addClass("next");
						}
						fadeElement();
						activeNumEl();
						return false;
				});
		}
		if (_linkPause) {
				if (!_options.autoSlideShow) {
					_linkPause.addClass('play');
					_options.autoSlideShow = true;
					if (!_hover) contentHover();
				}
				$(_linkPause).click(function(){
						if (!_linkPause.is('.play')) {
							clearTimeout(_timer);
							_linkPause.addClass('play');
						} else {
							_linkPause.removeClass('play');
							_timer = setTimeout(function(){nextEl()},_switchTime);
						}
						return false;
				});
		}
		if (_numElementLink) {
				$(_numElementLink).bind(_options.event, function(){
						if (_timer) {
								clearTimeout(_timer);
								_timer = setTimeout(function(){nextEl()},_switchTime);
						}
						_n = _numElement.index($(this).parent());
						if (!$(_slideEl).eq(_n).hasClass("active")){
							$(_slideEl).eq(_n).addClass("next");
							fadeElement();
							activeNumEl();
						}
						return false;
				});
		}
		if (_options.currentEl || _options.allEl) {
				_THIS.all = _slideEl.length;
				$(_options.allEl, _THIS).html(_THIS.all);
				function currentNum() {
					var _index = _slideEl.index(_slideEl.filter('.active')) + 1;
					$(_options.currentEl, _THIS).html(_index);
				}
				currentNum();
		}
		function contentHover() {
			_hover = true;
			_slideEl.hover(function() {
					if (_timer) clearTimeout(_timer);
			}, function(){
					if (_linkPause && !_linkPause.is('.play'))
						_timer = setTimeout(function(){nextEl()},_switchTime);
					else if (!_linkPause)
						_timer = setTimeout(function(){nextEl()},_switchTime);
			});
		}
		function nextEl(){
				if ($(_slideEl).filter(".active").next().is(_elTagName)){
					$(_slideEl).filter(".active").next().addClass("next");
				} else {
					$(_slideEl).eq(0).addClass("next");
				}
				fadeElement();
				activeNumEl();
				_timer = setTimeout(function(){nextEl()},_switchTime);
		};
		function activeNumEl() {
				$(_numElement).removeClass("active");
				_numElActive = $(_slideEl).index($(_slideEl).filter(".active")[0]);
				$(_numElement).eq(_numElActive).addClass("active");
		};
		function fadeElement(){
				$(_slideEl).filter(".active").fadeOut(_duration).removeClass("active");
				$(_slideEl).filter(".next").fadeIn(_duration).addClass("active").removeClass("next");
				if (_options.currentEl || _options.allEl) currentNum();
		};
	});
}

/* custom scroll */
var VSA_scrollAreas = new Array();
var VSA_default_imagesPath = "/templates/interspar/resources/images";
var VSA_default_btnUpImage = "btn-scroll-up.gif";
var VSA_default_btnDownImage = "btn-scroll-down.gif";
var VSA_default_scrollStep = 5;
var VSA_default_wheelSensitivity = 10;
var VSA_default_scrollbarPosition = 'right';//'left','right','inline';
var VSA_default_scrollButtonHeight = 21;
var VSA_default_scrollbarWidth = 19;
var VSA_resizeTimer = 2000;

function VSA_initScrollbars()
{
	var scrollElements = VSA_getElements("scrollable", "DIV", document, "class");
	for (var i=0; i<scrollElements.length; i++)
	{
		VSA_scrollAreas[i] = new ScrollArea(i, scrollElements[i]);
	} 
}

function ScrollArea(index, elem) //constructor
{
	this.index = index;
	this.element = elem;

	var attr = this.element.getAttribute("imagesPath");
	this.imagesPath = attr ? attr : VSA_default_imagesPath;

	attr = this.element.getAttribute("btnUpImage");
	this.btnUpImage = attr ? attr : VSA_default_btnUpImage;

	attr = this.element.getAttribute("btnDownImage");
	this.btnDownImage = attr ? attr : VSA_default_btnDownImage;

	attr = Number(this.element.getAttribute("scrollStep"));
	this.scrollStep = attr ? attr : VSA_default_scrollStep;

	attr = Number(this.element.getAttribute("wheelSensitivity"));
	this.wheelSensitivity = attr ? attr : VSA_default_wheelSensitivity;

	attr = this.element.getAttribute("scrollbarPosition");
	this.scrollbarPosition = attr ? attr : VSA_default_scrollbarPosition;

	attr = this.element.getAttribute("scrollButtonHeight");
	this.scrollButtonHeight = attr ? attr : VSA_default_scrollButtonHeight;

	attr = this.element.getAttribute("scrollbarWidth");
	this.scrollbarWidth = attr ? attr : VSA_default_scrollbarWidth;

	this.scrolling = false;

	this.iOffsetY = 0;
	this.scrollHeight = 0;
	this.scrollContent = null;
	this.scrollbar = null;
	this.scrollup = null;
	this.scrolldown = null;
	this.scrollslider = null;
	this.scroll = null;
	this.enableScrollbar = false;
	this.scrollFactor = 1;
	this.scrollingLimit = 0;
	this.topPosition = 0;

	//functions declaration
	this.init = VSA_init;
	this.scrollUp = VSA_scrollUp;
	this.scrollDown = VSA_scrollDown;
	this.createScrollBar = VSA_createScrollBar;
	this.scrollIt = VSA_scrollIt;

	this.init();
}

function VSA_init()
{
	this.scrollContent = document.createElement("DIV");
	this.scrollContent.style.position = "absolute";
	this.scrollContent.innerHTML = this.element.innerHTML;
	this.scrollContent.style.overflow = "hidden";

	this.scrollContent.style.width = this.element.offsetWidth + "px";
	this.scrollContent.style.height = this.element.offsetHeight + "px";


	this.element.innerHTML = "";
	this.element.style.overflow = "hidden";
	this.element.style.display = "block";
	this.element.style.visibility = "visible";
	this.element.style.position = "relative";
	this.element.appendChild(this.scrollContent);

	this.scrollContent.className = 'scroll-content';

	this.element.index = this.index;
	this.element.over = false;

	var _this = this;
	this.element.onmouseover = function(){
		_this.element.over = true;
	};
	this.element.onmouseout = function(){
		_this.element.over = false;
	}

	if (document.all)
	{
		this.element.onscroll = VSA_handleOnScroll;
		this.element.onresize = VSA_handleResize;
	}
	else
	{
		window.onresize = VSA_handleResize;
	}

	this.createScrollBar();

	if (window.addEventListener)
		/** DOMMouseScroll is for mozilla. */
		this.element.addEventListener('DOMMouseScroll', VSA_handleMouseWheel, false);
	/** IE/Opera. */
	this.element.onmousewheel = document.onmousewheel = VSA_handleMouseWheel;

}

function VSA_createScrollBar()
{

	if (this.scrollbar != null)
	{
		this.element.removeChild(this.scrollbar);
		this.scrollbar = null;
	}

	if (this.scrollContent.scrollHeight <= this.scrollContent.offsetHeight)
		this.enableScrollbar = false;
	else if (this.element.offsetHeight > 2*this.scrollButtonHeight)
		this.enableScrollbar = true;
	else
		this.enableScrollbar = false;

	if (this.scrollContent.scrollHeight - Math.abs(this.scrollContent.scrollTop) < this.element.offsetHeight)
		this.scrollContent.style.top = 0;

	if (this.enableScrollbar)
	{

		this.scrollbar = document.createElement("DIV");
		this.element.appendChild(this.scrollbar);
		this.scrollbar.style.position = "absolute";
		this.scrollbar.style.top = "0px";
		this.scrollbar.style.height = this.element.offsetHeight+"px";
		this.scrollbar.style.width = this.scrollbarWidth + "px";

		this.scrollbar.className = 'vscroll-bar';

		if(this.scrollbarWidth != this.scrollbar.offsetWidth)
		{
			this.scrollbarWidth = this.scrollbar.offsetHeight;
		}

		this.scrollbarWidth = this.scrollbar.offsetWidth;

		if(this.scrollbarPosition == 'left')
		{
			this.scrollContent.style.left = this.scrollbarWidth + 5 + "px";
			this.scrollContent.style.width = this.element.offsetWidth - this.scrollbarWidth - 5 + "px";
		}
		else if(this.scrollbarPosition == 'right')
		{
			this.scrollbar.style.left = this.element.offsetWidth - this.scrollbarWidth  + "px";
			this.scrollContent.style.width = this.element.offsetWidth - this.scrollbarWidth - 5 + "px";
		}

		//create scroll up button
		this.scrollup = document.createElement("DIV");
		this.scrollup.index = this.index;
		this.scrollup.onmousedown = VSA_handleBtnUpMouseDown;
		this.scrollup.onmouseup = VSA_handleBtnUpMouseUp;
		this.scrollup.onmouseout = VSA_handleBtnUpMouseOut;
		this.scrollup.style.position = "absolute";
		this.scrollup.style.top = "0px";
		this.scrollup.style.left = "0px";
		this.scrollup.style.height = this.scrollButtonHeight + "px";
		this.scrollup.style.width = this.scrollbarWidth + "px";

		this.scrollup.innerHTML = '<img src="' + this.imagesPath + '/' + this.btnUpImage + '" border="0"/>';
		this.scrollbar.appendChild(this.scrollup);

		this.scrollup.className = 'vscroll-up';

		if(this.scrollButtonHeight != this.scrollup.offsetHeight)
		{
			this.scrollButtonHeight = this.scrollup.offsetHeight;
		}

		//create scroll down button
		this.scrolldown = document.createElement("DIV");
		this.scrolldown.index = this.index;
		this.scrolldown.onmousedown = VSA_handleBtnDownMouseDown;
		this.scrolldown.onmouseup = VSA_handleBtnDownMouseUp;
		this.scrolldown.onmouseout = VSA_handleBtnDownMouseOut;
		this.scrolldown.style.position = "absolute";
		this.scrolldown.style.left = "0px";
		this.scrolldown.style.top =  this.scrollbar.offsetHeight - this.scrollButtonHeight + "px";
		this.scrolldown.style.width = this.scrollbarWidth + "px";
		this.scrolldown.innerHTML = '<img src="' + this.imagesPath + '/' + this.btnDownImage + '" border="0"/>';
		this.scrollbar.appendChild(this.scrolldown);

		this.scrolldown.className = 'vscroll-down';

		//create scroll
		this.scroll = document.createElement("DIV");
		this.scroll.index = this.index;
		this.scroll.style.position = "absolute";
		this.scroll.style.zIndex = 0;
		this.scroll.style.textAlign = "center";
		this.scroll.style.top = this.scrollButtonHeight + "px";
		this.scroll.style.left = "0px";
		this.scroll.style.width = this.scrollbarWidth + "px";

		var h = this.scrollbar.offsetHeight - 2*this.scrollButtonHeight;
		this.scroll.style.height = ((h > 0) ? h : 0) + "px";

		this.scroll.innerHTML = '';
		this.scroll.onclick = VSA_handleScrollbarClick;
		this.scrollbar.appendChild(this.scroll);
		this.scroll.style.overflow = "hidden";

		this.scroll.className = "vscroll-line";

		//create slider
		this.scrollslider = document.createElement("DIV");
		this.scrollslider.index = this.index;
		this.scrollslider.style.position = "absolute";
		this.scrollslider.style.zIndex = 1000;
		this.scrollslider.style.textAlign = "center";
		this.scrollslider.innerHTML = '<div id="scrollslider' + this.index + '" style="padding:0;margin:0;"><div class="scroll-bar-top"></div><div class="scroll-bar-bottom"></div></div>';
		this.scrollbar.appendChild(this.scrollslider);

		this.subscrollslider = document.getElementById("scrollslider"+this.index);
		this.subscrollslider.style.height = Math.round((this.scrollContent.offsetHeight/this.scrollContent.scrollHeight)*(this.scrollbar.offsetHeight - 2*this.scrollButtonHeight)) + "px";

		this.scrollslider.className = "vscroll-slider";

		this.scrollHeight = this.scrollbar.offsetHeight - 2*this.scrollButtonHeight - this.scrollslider.offsetHeight;
		
		// andreas korat
		// fix for ie 7
		if(jQuery.browser.msie && jQuery.browser.version == 7)
		{
			var height = 0;
			
			jQuery(this.scrollContent).children().each(
			function(el)
			{
			  height += jQuery(this).outerHeight(true);
			}
			);
			this.scrollFactor = (height - this.scrollContent.offsetHeight)/this.scrollHeight;
		}
		else
		{
			this.scrollFactor = (this.scrollContent.scrollHeight - this.scrollContent.offsetHeight)/this.scrollHeight;
		}
		
		this.topPosition = getRealTop(this.scrollbar) + this.scrollButtonHeight;
		/* this.scrollbarHeight = this.scrollbar.offsetHeight - 2*this.scrollButtonHeight - this.scrollslider.offsetHeight; */

		this.scrollslider.style.top = 1 / this.scrollFactor * Math.abs(this.scrollContent.scrollTop) + this.scrollButtonHeight + "px";
		this.scrollslider.style.left = "0px";
		this.scrollslider.style.width = "100%";
		this.scrollslider.onmousedown = VSA_handleSliderMouseDown;
		if (document.all)
			this.scrollslider.onmouseup = VSA_handleSliderMouseUp;
	}
	else
		this.scrollContent.style.width = this.element.offsetWidth + "px";
}

function VSA_handleBtnUpMouseDown()
{
	var sa = VSA_scrollAreas[this.index];
	sa.scrolling = true;
	sa.scrollUp();
}

function VSA_handleBtnUpMouseUp()
{
	VSA_scrollAreas[this.index].scrolling = false;
}

function VSA_handleBtnUpMouseOut()
{
	VSA_scrollAreas[this.index].scrolling = false;
}

function VSA_handleBtnDownMouseDown()
{
	var sa = VSA_scrollAreas[this.index];
	sa.scrolling = true;
	sa.scrollDown();
}

function VSA_handleBtnDownMouseUp()
{
	VSA_scrollAreas[this.index].scrolling = false;
}

function VSA_handleBtnDownMouseOut()
{
	VSA_scrollAreas[this.index].scrolling = false;
}

function VSA_scrollIt()
{
	this.scrollContent.scrollTop = this.scrollFactor * ((this.scrollslider.offsetTop + this.scrollslider.offsetHeight/2) - this.scrollButtonHeight - this.scrollslider.offsetHeight/2);
}

function VSA_scrollUp()
{
	if (this.scrollingLimit > 0)
	{
		this.scrollingLimit--;
		if (this.scrollingLimit == 0) this.scrolling = false;
	}
	if (!this.scrolling) return;
	if ( this.scrollContent.scrollTop - this.scrollStep > 0)
	{
		this.scrollContent.scrollTop -= this.scrollStep;
		this.scrollslider.style.top = 1 / this.scrollFactor * Math.abs(this.scrollContent.scrollTop) + this.scrollButtonHeight + "px";
	}
	else
	{
		this.scrollContent.scrollTop = "0";
		this.scrollslider.style.top = this.scrollButtonHeight + "px";
		return;
	}
	setTimeout("VSA_Ext_scrollUp(" + this.index + ")", 30);
}

function VSA_Ext_scrollUp(index)
{
	VSA_scrollAreas[index].scrollUp();
}

function VSA_scrollDown()
{
	if (this.scrollingLimit > 0)
	{
		this.scrollingLimit--;
		if (this.scrollingLimit == 0) this.scrolling = false;
	}
	if (!this.scrolling) return;


	this.scrollContent.scrollTop += this.scrollStep;
	this.scrollslider.style.top =  1 / this.scrollFactor * Math.abs(this.scrollContent.scrollTop) + this.scrollButtonHeight + "px";

	if (this.scrollContent.scrollTop >= (this.scrollContent.scrollHeight - this.scrollContent.offsetHeight))
	{
		this.scrollContent.scrollTop = (this.scrollContent.scrollHeight - this.scrollContent.offsetHeight);
		this.scrollslider.style.top = this.scrollbar.offsetHeight - this.scrollButtonHeight - this.scrollslider.offsetHeight + "px";
		return;
	}

	setTimeout("VSA_Ext_scrollDown(" + this.index + ")", 30);
}

function VSA_Ext_scrollDown(index)
{
	VSA_scrollAreas[index].scrollDown();
}

function VSA_handleMouseMove(evt)
{
	var sa = VSA_scrollAreas[((document.all && !window.opera) ? this.index : document.documentElement.scrollAreaIndex)];
	var posy = 0;
	if (!evt) var evt = window.event;

	if (evt.pageY)
		posy = evt.pageY;
	else if (evt.clientY)
		posy = evt.clientY;

		if (document.all && !window.opera)
		{
			posy += document.documentElement.scrollTop;
		}

	var iNewY = posy - sa.iOffsetY - sa.topPosition;
		iNewY += sa.scrollButtonHeight;

	if (iNewY < sa.scrollButtonHeight)
		iNewY = sa.scrollButtonHeight;
	if (iNewY > (sa.scrollbar.offsetHeight - sa.scrollButtonHeight) - sa.scrollslider.offsetHeight)
		iNewY = (sa.scrollbar.offsetHeight - sa.scrollButtonHeight) - sa.scrollslider.offsetHeight;

	sa.scrollslider.style.top = iNewY + "px";
	sa.scrollIt();
}

function VSA_handleSliderMouseDown(evt)
{
	if (!(document.uniqueID && document.compatMode && !window.XMLHttpRequest))
	{
		document.onselectstart = function() { return false; }
		document.onmousedown = function() { return false; }
	}

	var sa = VSA_scrollAreas[this.index];
	if (document.all && !window.opera)
	{
		sa.scrollslider.setCapture()
		sa.iOffsetY = event.offsetY;
		sa.scrollslider.onmousemove = VSA_handleMouseMove;
	}
	else
	{
		if(window.opera)
		{
			sa.iOffsetY = event.offsetY;
		}
		else
		{
			sa.iOffsetY = evt.layerY;
		}
		document.documentElement.scrollAreaIndex = sa.index;
		document.documentElement.addEventListener("mousemove", VSA_handleMouseMove, true);
		document.documentElement.addEventListener("mouseup", VSA_handleSliderMouseUp, true);
	}
	return false;
}

function VSA_handleSliderMouseUp()
{
	if (!(document.uniqueID && document.compatMode && !window.XMLHttpRequest))
	{
		document.onmousedown = null;
		document.onselectstart = null;
	}

	if (document.all && !window.opera)
	{
		var sa = VSA_scrollAreas[this.index];
		sa.scrollslider.onmousemove = null;
		sa.scrollslider.releaseCapture();
		sa.scrollIt();
	}
	else
	{
		var sa = VSA_scrollAreas[document.documentElement.scrollAreaIndex];
		document.documentElement.removeEventListener("mousemove", VSA_handleMouseMove, true);
		document.documentElement.removeEventListener("mouseup", VSA_handleSliderMouseUp, true);
		sa.scrollIt();
	}
	return false;
}

function VSA_handleResize()
{
	if (VSA_resizeTimer)
	{
		clearTimeout(VSA_resizeTimer);
		VSA_resizeTimer = 0;
	}
	VSA_resizeTimer = setTimeout("VSA_performResizeEvent()", 100);
}

function VSA_performResizeEvent()
{
	for (var i=0; i<VSA_scrollAreas.length; i++)
		VSA_scrollAreas[i].createScrollBar();
}
function VSA_handleMouseWheel(event){
	if (this.index != null) {
		var sa = VSA_scrollAreas[this.index];

		if (sa.scrollbar == null) return;
		sa.scrolling = true;
		sa.scrollingLimit = sa.wheelSensitivity;

		var delta = 0;
		if (!event) /* For IE. */
			event = window.event;
		if (event.wheelDelta) { /* IE/Opera. */
			delta = event.wheelDelta/120;

		/*if (window.opera)
		delta = -delta;*/
		} else if (event.detail) { /** Mozilla case. */
			delta = -event.detail/3;
		}
		if (delta && sa.element.over) {
			if (delta > 0)
				sa.scrollUp();
				else
			sa.scrollDown();
			if (event.preventDefault)
				event.preventDefault();
			event.returnValue = false;
		}
	}
}

function VSA_handleSelectStart()
{
	event.returnValue = false;
}

function VSA_handleScrollbarClick(evt)
{
	var sa = VSA_scrollAreas[this.index];
	var offsetY = (document.all ? event.offsetY : evt.layerY);

	if (offsetY < (sa.scrollButtonHeight + sa.scrollslider.offsetHeight/2))
		sa.scrollslider.style.top = sa.scrollButtonHeight + "px";
	else if (offsetY > (sa.scrollbar.offsetHeight - sa.scrollButtonHeight - sa.scrollslider.offsetHeight))
		sa.scrollslider.style.top = sa.scrollbar.offsetHeight - sa.scrollButtonHeight - sa.scrollslider.offsetHeight + "px";
	else
	{
		sa.scrollslider.style.top = offsetY + sa.scrollButtonHeight - sa.scrollslider.offsetHeight/2 + "px";
	}
	sa.scrollIt();
}

function VSA_handleOnScroll()
{
	//event.srcElement.doScroll("pageUp");
}

//--- common functions ----

function VSA_getElements(attrValue, tagName, ownerNode, attrName) //get Elements By Attribute Name
{
	if (!tagName) tagName = "*";
	if (!ownerNode) ownerNode = document;
	if (!attrName) attrName = "name";
	var result = [];
	var nl = ownerNode.getElementsByTagName(tagName);
	for (var i=0; i<nl.length; i++)
	{
	//	if (nl.item(i).getAttribute(attrName) == attrValue)
//		result.push(nl.item(i));
		if (nl.item(i).className.indexOf(attrValue) != -1)
		result.push(nl.item(i));
	}
	return result;
}

var _ie = false;
if(navigator.userAgent.toLowerCase().indexOf("msie") != -1 && navigator.userAgent.toLowerCase().indexOf("opera") == -1) _ie = true;

function getRealTop(elem)
{
	if(_ie) {
		var nTop = 0;
		if(elem)
		{
			do
			{
				nTop += elem.offsetTop - elem.scrollTop;
				elem = elem.offsetParent;
			}
			while(elem)
		}
		return nTop;
	}
	else {
		var posTop = 0;
		while (elem.offsetParent) {
			posTop += elem.offsetTop;
			elem = elem.offsetParent;
		}
		var _res = new Object();
		return posTop;
	}
}
