window.addEvent('domready',function(){
	//clearFormFields({
	//	clearInputs: true,
	//	clearTextareas: true,
		//passwordFieldText: false,
		//addClassFocus: "focus",
		//filterClass: "default"
//	});
	initTabs();
	initDrop();
	// longWidth(); Commented out due to errors that were breaking the page. Not sure what it does.
	HSA_initScrollbars();
	initGallerys();
});

// Commented out due to errors that were breaking the page. Not sure what it does.
// function longWidth(){
// 	var sum = 0;
// 	$$('ul.long li').each(function(holder){
// 	sum += holder.getSize().x + parseInt(holder.getStyle('marginLeft')) + parseInt(holder.getStyle('marginRight'));
// 	});
// 	$$('ul.long')[0].setStyles({width:sum});
// }

function initGallerys(){
	$$('div.slideset').each(function(holder){
		new slideshow(holder);
	});
	$$('div.carousel').each(function(holder){
		new scrollGallery(holder);
	});
	$$('div.review-block div.visual').each(function(holder){
		new slideshow(holder,{
			slides:'ul.picture > li',
			pagingHolder:'ul.long',
			pagingTag:'li',
			autoPlay:false,
			effect:'fade'//fade, slideX, slideY,
		});
	});
}

function initDrop(){
	var li = $$("div.drop li");
	if (li.length) li[li.length-1].addClass('last');
}

function initTabs()
{
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabset") != -1)
		{
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";

					links[j].onclick = function ()
					{
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							for (var i = 0; i < this.tabs.length; i++)
							{
								var tab = document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1));
								if (tab)
								{
									tab.style.display = "none";
								}
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";
							return false;
						}
					}
				}
			}
		}
	}
}

function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}

var slideshow = new Class({
	Implements : [Options, Events],
	options:{
		slides:'>div',
		nextBtn:false,
		prevBtn:false,
		pagingHolder:'div.switcher',
		pagingTag:'li',
		slidesActiveClass:'active',
		pagingActiveClass:'active',
		autoHeight:false,
		createPaging:false,
		autoPlay:true,
		dynamicLoad:false,
		imgAttr:'alt',
		effect:'slideX',//fade, slideX, slideY,
		startSlide:false,
		switchTime:5000,
		animSpeed:700
	},
	initialize:function(element, options) {
		this.mainHolder = $(element);
		this.setOptions(options);
		this.slides = this.mainHolder.getElements(this.options.slides);		
		if (this.options.nextBtn) this.nextBtn = this.mainHolder.getElement(this.options.nextBtn);
		if (this.options.prevBtn) this.prevBtn = this.mainHolder.getElement(this.options.prevBtn);
		
		this.previous = -1;
		this.loadingFrame = 1;
		this.busy = false;
		this.direction = 1;
		this.timer;
		this.pagingArray = new Array;
		this.loadArray = new Array;
		this.preloader = new Array;
		this.slidesParent = this.slides[0].getParent();
		this.slideW = this.slidesParent.getSize().x;
		this.slideH = this.slidesParent.getSize().y;
		this.autoPlay = this.options.autoPlay;
		
		this.initStartSlide();
		this.initPaging();
		this.setStyles();
		this.bindEvents();
		this.showSlide();
	},
	
	initStartSlide:function(){
		if (this.options.startSlide) this.current = this.options.startSlide
		else {
			var active = -1;
			for(var i = 0; i< this.slides.length-1; i++) {
				if (this.slides[i].hasClass(this.options.slidesActiveClass)) {
					active = i;
					break;						
				}
			}
			if (active != -1) this.current = active;
			else this.current = 0;
		}
	},
	
	initPaging:function(){
		this.pagingHolder = this.mainHolder.getElements(this.options.pagingHolder);
		
		if (this.options.createPaging) {
			this.pagingHolder.each(function(paging,i){
				paging.empty();
				var list = new Element('ul');
				var html = '';
				for (var i = 0; i < this.slides.length; i++) {
					html += '<li><a href="#">' + (i + 1) + '</a></li>';
				}
				list.innerHTML = html;
				list.inject(paging);
			}.bind(this));
		}
		
		this.pagingHolder.each(function(paging){
			this.pagingArray.push(paging.getElements(this.options.pagingTag))
		}.bind(this));
	},
	
	setStyles:function(){
		//loader
		if (this.options.dynamicLoad) {
			this.loader = new Element('div').addClass('loader');
			this.loaderDiv = new Element('div').inject(this.loader);
			this.loader.inject(this.slidesParent);
		}
		
		//slides
		this.slides.each(function(slide,i){
			if (this.options.effect == 'fade') {
				if (i != this.current) slide.setStyles({display:'none'});
				else slide.setStyles({display:'block'});
			} else if (this.options.effect == 'slideX'){
				if (i != this.current) slide.setStyles({display: 'none',left:-this.slideW});
				else slide.setStyles({display:'block',left:0});
			} else if (this.options.effect == 'slideY'){
				if (i != this.current) slide.setStyles({display:'none',top:-this.slideH});
				else slide.setStyles({display:'block',top:0});
			}
		}.bind(this));
		
		if (this.options.autoHeight) {
			this.slidesParent.setStyles({
				height:this.slides[this.current].getSize().y
			});
		}
	},
	
	bindEvents:function(){
		if (this.nextBtn) this.nextBtn.addEvent('click',function(){
			if (!this.busy) this.nextSlide();
			return false;
		}.bind(this));
		
		if (this.prevBtn) this.prevBtn.addEvent('click',function(){
			if (!this.busy) this.prevSlide();
			return false;
		}.bind(this));
		this.pagingArray.each(function(paging){
			paging.each(function(btn,i){
				btn.addEvent('click',function(){
					if (i != this.current && !this.busy) {
						this.previous = this.current;
						this.current = i;
						if (this.previous > i) this.direction = -1
						else this.direction = 1;
						this.showSlide();
					}
					return false;
				}.bind(this));
			}.bind(this));
		}.bind(this));
		
		if (this.options.dynamicLoad) this.loader.addEvent('click',this.abortLoading.bind(this));
	},
	
	nextSlide:function(){
		this.previous = this.current;
		if (this.current < this.slides.length-1) this.current++
		else this.current = 0;
		this.direction = 1;
		this.showSlide();
	},
	
	prevSlide:function(){
		this.previous = this.current;
		if (this.current > 0) this.current--
		else this.current = this.slides.length-1;
		this.direction = -1;
		this.showSlide();
	},
	
	showSlide:function(){
		if (this.previous == this.current) return; 
		var _current = this.current;
		this.busy = true;
		clearTimeout(this.timer);
		if (typeof this.loadArray[_current] != 'undefined' || !this.options.dynamicLoad) {
			//slide already loaded
			this.switchSlide();
		} else {
			//slide not loaded
			this.showLoading();
			var images = this.slides[this.current].getElements(this.options.dynamicLoad);
			if (images.length) {
				var counter = 0;
				images.each(function(img){
					var preloader = new Image;
					this.preloader.push(preloader);
					preloader.onload = function(){
						counter++;
						checkImages.apply(this);
					}.bind(this);
					preloader.onerror = function(){
						//ignore errors
						counter++;
						checkImages.apply(this);
					}.bind(this);
					preloader.src = img.getProperty(this.options.imgAttr);
				}.bind(this));
				
				function checkImages(){
					if (counter == images.length) {
						images.each(function(img){
							img.setProperty('src',img.getProperty(this.options.imgAttr));
						}.bind(this));
						successLoad.apply(this);
					}
				}
			} else successLoad.apply(this);
		}
		
		function successLoad(){
			this.loadArray[_current] = 1;
			this.hideLoading();
			this.switchSlide();
		}
	},
	
	switchSlide:function(){
		var obj = this;
		
		if (this.previous != -1) {
			var nextSlide = this.slides[this.current];
			var prevSlide = this.slides[this.previous];
			nextSlide.setStyles({display:'block'});
				
				
			if (this.options.effect == 'slideX'){
				this.slideW = this.slides[this.current].getSize().x;
				var nextFx = new Fx.Morph(nextSlide,{duration:this.options.animSpeed});
				var prevFx = new Fx.Morph(prevSlide,{duration:this.options.animSpeed,onComplete:callback.bind(this)});
				
				nextSlide.setStyles({left:this.slideW*this.direction})
				nextFx.start({left:0});
				prevFx.start({left:-this.slideW*this.direction});
			} else if (this.options.effect == 'slideY'){
				this.slideH = this.slides[this.current].getSize().y
				var nextFx = new Fx.Morph(nextSlide,{duration:this.options.animSpeed});
				var prevFx = new Fx.Morph(prevSlide,{duration:this.options.animSpeed,onComplete:callback.bind(this)});
				
				nextSlide.setStyles({top:this.slideH*this.direction});
				nextFx.start({top:0});
				prevFx.start({top:-this.slideH*this.direction});
			} else {
				var nextFx = new Fx.Morph(nextSlide,{duration:this.options.animSpeed,onComplete:function(){
					nextSlide.setStyles({opacity:'auto'});
				}});
				var prevFx = new Fx.Morph(prevSlide,{duration:this.options.animSpeed,onComplete:callback.bind(this)});
				nextSlide.setStyles({display:'block',opacity:0});
				nextFx.start({opacity:1});
				prevSlide.setStyles({opacity:1});
				prevFx.start({opacity:0});
			}
			
			if (this.options.autoHeight) {
				this.slideH = this.slides[this.current].getSize().y
				var parentFx = new Fx.Morph(this.slidesParent,{duration:this.options.animSpeed});
				parentFx.start({height:this.slideH});
			}
		} else {
			if (this.autoPlay) this.startAutoPlay();
			this.busy = false;
		}
		
		this.refreshStatus();
		
		function callback(){
			prevSlide.setStyles({display:'none'});
			if (this.autoPlay) this.startAutoPlay();
			this.busy = false;
		}
	},
	
	refreshStatus:function(){
		if (this.pagingArray.length) {
			this.pagingArray.each(function(paging){
				if (this.previous != -1) paging[this.previous].removeClass(this.options.pagingActiveClass);
				paging[this.current].addClass(this.options.pagingActiveClass);
			}.bind(this));
		}
		if (this.previous != -1) this.slides[this.previous].removeClass(this.options.pagingActiveClass);
		this.slides[this.current].addClass(this.options.pagingActiveClass);
	},
	
	showLoading:function(){
		this.loader.setStyles({display:'block'});
		clearInterval(this.loadingTimer);
		this.loadingTimer = setInterval(animateLoading.bind(this), 66);
		
		function animateLoading(){
			this.loaderDiv.setStyles({'top': this.loadingFrame * -40});
			this.loadingFrame = (this.loadingFrame + 1) % 12;
		}
	},
	
	hideLoading:function(){
		this.loader.setStyles({display:'none'});
		clearInterval(this.loadingTimer);
	},
	
	abortLoading:function(){
		this.busy = false;
		this.hideLoading();
		this.current = this.previous;
		for (var i = 0; i < this.preloader.length; i++) {
			this.preloader[i].onload = null;
			this.preloader[i].onerror = null;
		}
		if (this.autoPlay) this.startAutoPlay();
	},
	
	startAutoPlay:function(){
		clearTimeout(this.timer);
		this.timer = setTimeout(this.nextSlide.bind(this),this.options.switchTime);
	}
});

var scrollGallery = new Class({
	Implements : [Options, Events],
	options:{
		holder:'div.carousel-holder', 
		slider:'>ul',
		slides:'>li',
		prevBtn:'span.prev',
		nextBtn:'span.next',
		pagingHolder:'div.switcher',
		pagingBtn:'a',
		createPaging:false,
		circleSlide:true,
		switchTime:1000,
		slideSpeed:500,
		autoSlide:false,
		startSlide:false,
		horizontal:true,
		step:1
	},
	initialize:function(element,options){
		// elems
		this.setOptions(options);
		this.mainHolder = $(element);
		this.holder = this.mainHolder.getElement(this.options.holder);
		this.slider = this.holder.getElement(this.options.slider)
		this.slides = this.slider.getElements(this.options.slides);
		this.prevBtn = this.mainHolder.getElement(this.options.prevBtn);
		this.nextBtn = this.mainHolder.getElement(this.options.nextBtn);
		//variables
		if (this.options.horizontal) this.slideSize = this.slides[0].getSize().x + parseInt(this.slides[0].getStyle('marginLeft'))+parseInt(this.slides[0].getStyle('marginRight'));
		else this.slideSize = this.slides[0].getSize().y + parseInt(this.slides[0].getStyle('marginTop'))+parseInt(this.slides[0].getStyle('marginBottom'));
		if (this.options.horizontal) this.visibleStep = Math.round(this.holder.getSize().x/this.slideSize);
		else this.visibleStep = Math.round(this.holder.getSize().y/this.slideSize);
		this.options.step ? this.step = this.options.step : this.step = this.visibleStep;
		this.options.startSlide ? this.current = Math.floor(this.options.startSlide/this.step) : this.current = 0;
		this.max = this.slides.length - this.visibleStep;
		this.stepCount = Math.ceil((this.slides.length-this.visibleStep)/this.step)+1;
		this.Fx = new Fx.Morph(this.slider,{duration:this.options.slideSpeed, onComplete:function(){
			this.autoPlay();
		}.bind(this)});
		
		this.current = -1;
		this.previous = -1;
		//paging
		this.pagingHolder = this.mainHolder.getElements(this.options.pagingHolder);
		if (this.pagingHolder.length) this.pagingLength = Math.ceil(this.slides.length / this.step);
		
		if (this.pagingHolder.length) {
			this.pagingBtn = new Array;
			if (this.options.createPaging) {
				this.pagingHolder.each(function(obj,i){
					obj.empty();
					var list = new Element('ul');
					var html = '';
					for (var i = 0; i < this.pagingLength; i++) html += '<li><a href="#">' + (i + 1) + '</a></li>';
					list.innerHTML = html;
					list.inject(obj);
				}.bind(this));
			}
			
			this.pagingHolder.each(function(obj,i){
				this.pagingBtn[i] = obj.getElements(this.options.pagingBtn); 
				var stepCount = 0;
				this.pagingBtn[i].each(function(obj,j){
					var index = stepCount;
					obj.addEvent('click',function(){
						if (index != this.current) {
							this.showSlide(index);
						}
						return false;
					}.bind(this));
					stepCount += this.step;
				}.bind(this));
			}.bind(this));
		}
		
		//next & prev
		if (this.nextBtn) this.nextBtn.addEvent('click',function(){
			this.nextSlide();
			return false;
		}.bind(this));
		if (this.prevBtn) this.prevBtn.addEvent('click',function(){
			this.prevSlide();
			return false;
		}.bind(this));
		
		this.showSlide(this.options.startSlide,true);
		this.autoPlay();
	},
	nextSlide:function(){
		if (this.options.circleSlide && this.current == this.stepCount - 1) {
			this.previous = this.current;
			this.current = 0;
			this.move();
		}
		else if (this.current < this.stepCount - 1) {
				this.previous = this.current++
				this.move();
			}
	},
	prevSlide:function(){
		if (this.options.circleSlide && this.current == 0) {
			this.previous = this.current;
			this.current = this.stepCount - 1;
			this.move();
		}
		else if (this.current > 0) {
			this.previous = this.current--;
			this.move();
		}
	},
	move:function(instant){
		clearTimeout(this.timer);
		var direction = this.direction;
		var offset;
		if (this.step * this.current < this.max) offset = (this.step*this.current)*this.slideSize;
		else offset = this.max * this.slideSize;
		if (instant) {
			if (this.options.horizontal) this.slider.setStyles({marginLeft:-offset});
			else this.slider.setStyles({marginTop:-offset});
		}
		else {
			this.Fx.cancel();
			if (this.options.horizontal) this.Fx.start({marginLeft:-offset});
			else this.Fx.start({marginTop:-offset});
		}
		this.refreshStatus();
	},
	showSlide:function(nextInd,instant){
		var nextCount = Math.floor(nextInd/this.step);
		if (nextCount != this.current) {
			this.previous = this.current;
			this.current = nextCount;
			if (instant) this.move(instant);
			else this.move();
		}
	},
	refreshStatus:function(){
		if (this.pagingHolder.length) {
			this.pagingBtn.each(function(obj){
				if (this.previous != -1) obj[this.previous].removeClass('active');
				obj[this.current].addClass('active');
			}.bind(this));
		}
	},
	autoPlay:function(){
		if (this.options.autoSlide) {
			clearTimeout(this.timer);
			this.timer = setTimeout(this.nextSlide.bind(this),this.options.switchTime);
		}
	}
});

var HSA_scrollAreas = new Array();
var HSA_default_imagesPath = "images";
var HSA_default_btnLeftImage = "button-left.gif";
var HSA_default_btnRightImage = "button-right.gif";
var HSA_default_scrollStep = 5;
var HSA_default_wheelSensitivity = 10;
var HSA_default_scrollbarPosition = 'bottom'; //'top';//'bottom';'inline';
var HSA_default_scrollButtonWidth = 22;
var HSA_default_scrollbarHeight = 22;
var HSA_resizeTimer = 200;
var HSA_touchFlag = isTouchDevice(); // true/false to move scroll with scrollable body

function HSA_initScrollbars() {
	if(!document.body.children) return;
	var scrollElements = HSA_getElements("hscrollable", "DIV", document, "class");
	for (var i=0; i<scrollElements.length; i++)
	{
		HSA_scrollAreas[i] = new HScrollArea(i, scrollElements[i]);
	}
}

function isTouchDevice() {
	try {
		document.createEvent("TouchEvent");
		return true;
	} catch (e) {
		return false;
	}
}

function touchHandler(event){
	var touches = event.changedTouches, first = touches[0], type = "";
	switch(event.type) {
		case "touchstart": type = "mousedown"; break;
		case "touchmove":  type = "mousemove"; break;
		case "touchend":   type = "mouseup"; break;
		default: return;
	}
	var simulatedEvent = document.createEvent("MouseEvent");
	simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, false, false, false, false, 0/*left*/, null);
	first.target.dispatchEvent(simulatedEvent);
	event.preventDefault();
}

function HScrollArea(index, elem) //constructor
{
	this.index = index;
	this.element = elem;

	var attr = this.element.getAttribute("imagesPath");
	this.imagesPath = attr ? attr : HSA_default_imagesPath;

	attr = this.element.getAttribute("btnLeftImage");
	this.btnLeftImage = attr ? attr : HSA_default_btnLeftImage;

	attr = this.element.getAttribute("btnRightImage");
	this.btnRightImage = attr ? attr : HSA_default_btnRightImage;

	attr = Number(this.element.getAttribute("scrollStep"));
	this.scrollStep = attr ? attr : HSA_default_scrollStep;

	attr = Number(this.element.getAttribute("wheelSensitivity"));
	this.wheelSensitivity = attr ? attr : HSA_default_wheelSensitivity;

	attr = this.element.getAttribute("scrollbarPosition");
	this.scrollbarPosition = attr ? attr : HSA_default_scrollbarPosition;
	
	attr = this.element.getAttribute("scrollButtonWidth");
	this.scrollButtonWidth = attr ? attr : HSA_default_scrollButtonWidth;

	attr = this.element.getAttribute("scrollbarHeight");
	this.scrollbarHeight = attr ? attr : HSA_default_scrollbarHeight;

	this.scrolling = false;

	this.iOffsetX = 0;
	this.scrollWidth = 0;
	this.scrollContent = null;
	this.scrollbar = null;
	this.scrollleft = null;
	this.scrollright = null;
	this.scrollslider = null;
	this.scroll = null;
	this.enableScrollbar = false;
	this.scrollFactor = 1;
	this.scrollingLimit = 0;
	this.leftPosition = 0;

	//functions declaration
	this.init = HSA_init;
	this.scrollLeft = HSA_scrollLeft;
	this.scrollRight = HSA_scrollRight;
	this.createScrollBar = HSA_createScrollBar;
	this.scrollIt = HSA_scrollIt;

	this.init();
}

function HSA_init() {
	this.scrollContent = document.createElement("DIV");
	this.scrollContent.style.position = "absolute";
	this.scrollContent.style.overflow = "hidden";
	this.scrollContent.style.width = this.element.offsetWidth + "px";
	this.scrollContent.style.height = this.element.offsetHeight + "px";
	
	while(this.element.childNodes.length) this.scrollContent.appendChild(this.element.childNodes[0]);

	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;
	if(document.all && !window.opera) {
		this.element.onmouseenter = function(){_this.element.over = true;};
		this.element.onmouseleave = function(){_this.element.over = false;}
	} else {
		this.element.onmouseover = function(){_this.element.over = true;};
		this.element.onmouseout = function(){_this.element.over = false;}
	}

	if (document.all)
	{
		this.element.onscroll = HSA_handleOnScroll;
		this.element.onresize = HSA_handleResize;
	}
	else
	{
		window.onresize = HSA_handleResize;
	}
	
	this.createScrollBar();
	if (window.addEventListener) {
		/** DOMMouseScroll is for mozilla. */
		this.element.addEventListener('DOMMouseScroll', HSA_handleMouseWheel, false);
	}
	/** IE/Opera. */
	this.element.onmousewheel = document.onmousewheel = HSA_handleMouseWheel;

	// move content by touch
	if(HSA_touchFlag) {
		_this.scrollContent.onmousedown = function(e) {
			var startX = e.pageX-getRealLeft(_this.scrollContent);
			var origLeft = _this.scrollContent.scrollLeft;
			_this.scrollContent.onmousemove = function(e) {
				var moveX = e.pageX-getRealLeft(_this.scrollContent);
				var iNewX = origLeft-(moveX-startX);
				if(iNewX < 0) iNewX = 0;
				if(iNewX > _this.scrollContent.scrollWidth) iNewX = _this.scrollContent.scrollWidth;
				_this.scrollContent.scrollLeft = iNewX;
				_this.scrollslider.style.left =  1 / _this.scrollFactor * Math.abs(_this.scrollContent.scrollLeft) + _this.scrollButtonWidth + "px";
			}
		}
		_this.scrollContent.onmouseup = function(e) {
			_this.scrollContent.onmousemove = null;
		}
		this.scrollContent.addEventListener("touchstart", touchHandler, true);
		this.scrollContent.addEventListener("touchmove", touchHandler, true);
		this.scrollContent.addEventListener("touchend", touchHandler, true);
	}
}

function HSA_createScrollBar()
{
	if (this.scrollbar != null)
	{
		this.element.removeChild(this.scrollbar);
		this.scrollbar = null;
	}
	
	if (this.scrollContent.scrollWidth <= this.scrollContent.offsetWidth)
		this.enableScrollbar = false;
	else if (this.element.offsetWidth > 2*this.scrollButtonWidth)
		this.enableScrollbar = true;
	else
		this.enableScrollbar = false;
	
	if (this.scrollContent.scrollWidth - Math.abs(this.scrollContent.scrollLeft) < this.element.offsetWidth)
		this.scrollContent.style.left = 0;

	if (this.enableScrollbar)
	{
		this.scrollbar = document.createElement("DIV");
		this.element.appendChild(this.scrollbar);		
		this.scrollbar.style.position = "absolute";
		this.scrollbar.style.left = "0px";
		this.scrollbar.style.width = this.element.offsetWidth+"px";
		this.scrollbar.style.height = this.scrollbarHeight + "px";

		this.scrollbar.className = 'hscroll-bar';

		if(this.scrollbarHeight != this.scrollbar.offsetHeight)
		{
			this.scrollbarHeight = this.scrollbar.offsetWidth;
		}
		
		this.scrollbarHeight = this.scrollbar.offsetHeight;

		if(this.scrollbarPosition == 'top')
		{
			this.scrollContent.style.top = this.scrollbarHeight + 5 + "px";			
			this.scrollContent.style.height = this.element.offsetHeight - this.scrollbarHeight - 5 + "px";
		}
		else if (this.scrollbarPosition == 'bottom')
		{
			this.scrollbar.style.top = this.element.offsetHeight - this.scrollbarHeight  + "px";
			this.scrollContent.style.height = this.element.offsetHeight - this.scrollbarHeight - 5 + "px";
		}

		//create scroll up button
		this.scrollleft = document.createElement("DIV");
		this.scrollleft.index = this.index;
		this.scrollleft.onmousedown = HSA_handleBtnLeftMouseRight;
		this.scrollleft.onmouseup = HSA_handleBtnLeftMouseLeft;
		this.scrollleft.onmouseout = HSA_handleBtnLeftMouseOut;
		
		if(HSA_touchFlag) {
			this.scrollleft.addEventListener("touchstart", touchHandler, true);
			this.scrollleft.addEventListener("touchend", touchHandler, true);
		}
		
		this.scrollleft.style.position = "absolute";
		this.scrollleft.style.left = "0px";
		this.scrollleft.style.width = this.scrollButtonWidth + "px";		
		this.scrollleft.style.height = this.scrollbarHeight + "px";
		
		
		this.scrollleft.innerHTML = '<img src="' + this.imagesPath + '/' + this.btnLeftImage + '" border="0"/>';
		this.scrollbar.appendChild(this.scrollleft);

		this.scrollleft.className = 'hscroll-left';

		if(this.scrollButtonWidth != this.scrollleft.offsetWidth)
		{
			this.scrollButtonWidth = this.scrollleft.offsetWidth;
		}
		
		//create scroll down button
		this.scrollright = document.createElement("DIV");
		this.scrollright.index = this.index;
		this.scrollright.onmousedown = HSA_handleBtnRightMouseRight;
		this.scrollright.onmouseup = HSA_handleBtnRightMouseLeft;
		this.scrollright.onmouseout = HSA_handleBtnRightMouseOut;
		
		if(HSA_touchFlag) {
			this.scrollright.addEventListener("touchstart", touchHandler, true);
			this.scrollright.addEventListener("touchend", touchHandler, true);
		}
		
		this.scrollright.style.position = "absolute";
		this.scrollright.style.left =  this.scrollbar.offsetWidth - this.scrollButtonWidth + "px";
		this.scrollright.style.height = this.scrollbarHeight + "px";
		this.scrollright.innerHTML = '<img src="' + this.imagesPath + '/' + this.btnRightImage + '" border="0"/>';
		this.scrollbar.appendChild(this.scrollright);

		this.scrollright.className = 'hscroll-right';


		//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.left = this.scrollButtonWidth + "px";
		this.scroll.style.height = this.scrollbarHeight + "px";
		
		var h = this.scrollbar.offsetWidth - 2*this.scrollButtonWidth;
		this.scroll.style.width = ((h > 0) ? h : 0) + "px";
		
		this.scroll.innerHTML = '';
		this.scroll.onclick = HSA_handleScrollbarClick;
		this.scrollbar.appendChild(this.scroll);
		this.scroll.style.overflow = "hidden";

		this.scroll.className = "hscroll-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-left"></div><div class="scroll-bar-right"></div></div>';
		this.scrollbar.appendChild(this.scrollslider);
		
		this.subscrollslider = document.getElementById("scrollslider"+this.index);		
		this.subscrollslider.style.width = Math.round((this.scrollContent.offsetWidth/this.scrollContent.scrollWidth)*(this.scrollbar.offsetWidth - 2*this.scrollButtonWidth)) + "px";
		
		this.scrollslider.className = "hscroll-slider";
		
		this.scrollWidth = this.scrollbar.offsetWidth - 2*this.scrollButtonWidth - this.scrollslider.offsetWidth;
		this.scrollFactor = (this.scrollContent.scrollWidth - this.scrollContent.offsetWidth)/this.scrollWidth;
		this.leftPosition = getRealLeft(this.scrollbar) + this.scrollButtonWidth;
		/* this.scrollbarWidth = this.scrollbar.offsetWidth - 2*this.scrollButtonWidth - this.scrollslider.offsetWidth; */

		this.scrollslider.style.left = /* 1 / this.scrollFactor * Math.abs(this.scrollContent.offsetTop) +*/ this.scrollButtonWidth + "px";
		this.scrollslider.style.height = "100%";
		this.scrollslider.onmousedown = HSA_handleSliderMouseDown;
		if(HSA_touchFlag) {
			this.scrollslider.addEventListener("touchstart", touchHandler, true);
		}
		if (document.all)
			this.scrollslider.onmouseup = HSA_handleSliderMouseLeft;
	}
	else
		this.scrollContent.style.height = this.element.offsetHeight + "px";
}

function HSA_handleBtnLeftMouseRight()
{
	var sa = HSA_scrollAreas[this.index];
	sa.scrolling = true;
	sa.scrollLeft();
}

function HSA_handleBtnLeftMouseLeft()
{
	HSA_scrollAreas[this.index].scrolling = false;
}

function HSA_handleBtnLeftMouseOut()
{
	HSA_scrollAreas[this.index].scrolling = false;
}

function HSA_handleBtnRightMouseRight()
{
	var sa = HSA_scrollAreas[this.index];
	sa.scrolling = true;
	sa.scrollRight();
}

function HSA_handleBtnRightMouseLeft()
{
	HSA_scrollAreas[this.index].scrolling = false;
}

function HSA_handleBtnRightMouseOut()
{
	HSA_scrollAreas[this.index].scrolling = false;
}

function HSA_scrollIt()
{
	this.scrollContent.scrollLeft = this.scrollFactor * ((this.scrollslider.offsetLeft + this.scrollslider.offsetWidth/2) - this.scrollButtonWidth - this.scrollslider.offsetWidth/2);
}

function HSA_scrollLeft()
{
	if (this.scrollingLimit > 0)
	{
		this.scrollingLimit--;
		if (this.scrollingLimit == 0) this.scrolling = false;
	}
	if (!this.scrolling) return;
	if ( this.scrollContent.scrollLeft - this.scrollStep > 0)
	{
		this.scrollContent.scrollLeft -= this.scrollStep;
		this.scrollslider.style.left = 1 / this.scrollFactor * Math.abs(this.scrollContent.scrollLeft) + this.scrollButtonWidth + "px";
	}
	else
	{
		this.scrollContent.scrollLeft = "0";
		this.scrollslider.style.left = this.scrollButtonWidth + "px";
		return;
	}
	setTimeout("HSA_Ext_scrollLeft(" + this.index + ")", 30);
}

function HSA_Ext_scrollLeft(index)
{
	HSA_scrollAreas[index].scrollLeft();
}

function HSA_scrollRight()
{
	if (this.scrollingLimit > 0)
	{
		this.scrollingLimit--;
		if (this.scrollingLimit == 0) this.scrolling = false;
	}
	if (!this.scrolling) return;


	this.scrollContent.scrollLeft += this.scrollStep;
	this.scrollslider.style.left =  1 / this.scrollFactor * Math.abs(this.scrollContent.scrollLeft) + this.scrollButtonWidth + "px";

	if (this.scrollContent.scrollLeft >= (this.scrollContent.scrollWidth - this.scrollContent.offsetWidth))
	{
		this.scrollContent.scrollLeft = (this.scrollContent.scrollWidth - this.scrollContent.offsetWidth);
		this.scrollslider.style.left = this.scrollbar.offsetWidth - this.scrollButtonWidth - this.scrollslider.offsetWidth + "px";
		return;
	}

	setTimeout("HSA_Ext_scrollRight(" + this.index + ")", 30);
}

function HSA_Ext_scrollRight(index)
{
	HSA_scrollAreas[index].scrollRight();
}

function HSA_handleMouseMove(evt)
{
	var sa = HSA_scrollAreas[((document.all && !window.opera) ? this.index : document.documentElement.scrollAreaIndex)];
	var posy = 0;
	if (!evt) var evt = window.event;
	
	if (evt.pageX)
		posy = evt.pageX;
	else if (evt.clientX)
		posy = evt.clientX;
			
		if (document.all && !window.opera)
		{
			if(!document.addEventListener) {
				posy += document.documentElement.scrollLeft;
			}
		}

	var iNewY = posy - sa.iOffsetX - sa.leftPosition;
		iNewY += sa.scrollButtonWidth;
		
	if (iNewY < sa.scrollButtonWidth)
		iNewY = sa.scrollButtonWidth;
	if (iNewY > (sa.scrollbar.offsetWidth - sa.scrollButtonWidth) - sa.scrollslider.offsetWidth)
		iNewY = (sa.scrollbar.offsetWidth - sa.scrollButtonWidth) - sa.scrollslider.offsetWidth;

	sa.scrollslider.style.left = iNewY + "px";

	sa.scrollIt();
}

function HSA_handleSliderMouseDown(evt)
{
	if (!(document.uniqueID && document.compatMode && !window.XMLHttpRequest))
	{
		document.onselectstart = function() { return false; }
		document.onmousedown = function() { return false; }
	}
	var sa = HSA_scrollAreas[this.index];
	if (document.all && !window.opera)
	{
		sa.scrollslider.setCapture()
		sa.iOffsetX = event.offsetX;
		sa.scrollslider.onmousemove = HSA_handleMouseMove;
		if(HSA_touchFlag) {
			sa.scrollslider.addEventListener("touchmove", touchHandler, true);
		}
	}
	else
	{
		if(window.opera)
		{
			sa.iOffsetX = event.offsetX;
		}
		else
		{
			sa.iOffsetX = evt.layerX;
		}
		document.documentElement.scrollAreaIndex = sa.index;
		document.documentElement.addEventListener("mousemove", HSA_handleMouseMove, true);
		document.documentElement.addEventListener("mouseup", HSA_handleSliderMouseLeft, true);
		if(HSA_touchFlag) {
			document.documentElement.addEventListener("touchmove", touchHandler, true);
			document.documentElement.addEventListener("touchend", touchHandler, true);
		}
	}
}

function HSA_handleSliderMouseLeft()
{
	if (!(document.uniqueID && document.compatMode && !window.XMLHttpRequest))
	{
		document.onmousedown = null;
		document.onselectstart = null;
	}
	if (document.all && !window.opera)
	{
		var sa = HSA_scrollAreas[this.index];
		sa.scrollslider.onmousemove = null;
		sa.scrollslider.releaseCapture();
		sa.scrollIt();
	}
	else
	{
		var sa = HSA_scrollAreas[document.documentElement.scrollAreaIndex];
		document.documentElement.removeEventListener("mousemove", HSA_handleMouseMove, true);
		document.documentElement.removeEventListener("mouseup", HSA_handleSliderMouseLeft, true);
		if(HSA_touchFlag) {
			document.documentElement.removeEventListener("touchmove", touchHandler, true);
			document.documentElement.removeEventListener("touchend", touchHandler, true);
		}
		sa.scrollIt();
	}
}

function HSA_handleResize()
{
	if (HSA_resizeTimer)
	{
		clearTimeout(HSA_resizeTimer);
		HSA_resizeTimer = 0;
	}
	HSA_resizeTimer = setTimeout("HSA_performResizeEvent()", 100);
}

function HSA_performResizeEvent()
{
	for (var i=0; i<HSA_scrollAreas.length; i++)
		HSA_scrollAreas[i].createScrollBar();
}

function HSA_handleMouseWheel(event)
{
	if (this.index != null) {
		var sa = HSA_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.scrollLeft();
			} else {
				sa.scrollRight();
			}
			if (event.preventDefault) {
				event.preventDefault();
			}
			event.returnValue = false;
		}
	}
}

function HSA_handleSelectStart()
{
	event.returnValue = false;
}

function HSA_handleScrollbarClick(evt)
{
	var sa = HSA_scrollAreas[this.index];
	var offsetX = (document.all ? event.offsetX : evt.layerX);
	
	if (offsetX < (sa.scrollButtonWidth + sa.scrollslider.offsetWidth/2))
		sa.scrollslider.style.left = sa.scrollButtonWidth + "px";
	else if (offsetX > (sa.scrollbar.offsetWidth - sa.scrollButtonWidth - sa.scrollslider.offsetWidth))
		sa.scrollslider.style.left = sa.scrollbar.offsetWidth - sa.scrollButtonWidth - sa.scrollslider.offsetWidth + "px";
	else
	{
		sa.scrollslider.style.left = offsetX + sa.scrollButtonWidth - sa.scrollslider.offsetWidth/2 + "px";
	}
	sa.scrollIt();
}

function HSA_handleOnScroll()
{
	event.srcElement.doScroll("pageLeft");
}

//--- common functions ----

function HSA_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;
}

function getRealLeft(elem)
{
	var nLeft = 0;
	if(elem)
	{
		do
		{
			nLeft += elem.offsetLeft - elem.scrollLeft;
			elem = elem.offsetParent;
		}
		while(elem)
	}
	return nLeft;
}
