var scroll_duration = 6000;
var _timer;
var _i = 0;
var tab_a = [];
function initHPSlider(){
	var tab_holder = $('tab_holder');
	tab_a = $$('.tabset a.tab');
	var tabs = $$('#tab_holder .tab');
	var flag = false;
	if (tab_holder && tab_a.length == 3 && tabs.length == 3){
		tab_a.each(function(_e, _i){
			if (_e.className.indexOf('active')!=-1){
				act = _i;
			}
		})
		tabs.each(function(itm, indx){
			itm._mooeffect = new Fx.Tween($(itm), {duration:1000,
				onComplete: function(){
					if (tab_a[act].className.indexOf('active')!=-1) {;}
					else	{
						tab_a[act].className += ' active';
						if (sld_arr.length){
							sld_arr[0].parentNode.parentNode.scrollTop = (act)*47;
						}
					}
					
				}
			});
		})
		tab_a.each(function(_item, _index){
			_item._top = -_index*400;
			_item.onclick = function(){
				tabs.each(function(itm, indx){
					itm._mooeffect.start('top', (indx*400)-(_index*400));
				});
				tab_a.each(function(itm, indx){
					itm.className = itm.className.replace(' active', '');
					itm.className = itm.className.replace('active', '');
				});
				act = _index;
			}
		})
		tab_a[0].parentNode.parentNode.onclick = function(){
			if (_timer) clearTimeout(_timer);
		}
		_i = act;
		autoSlide(_i);
	}
	var slider_elements = $$('.tab-content .tab-content-box');
	if (slider_elements.length){
		slider_elements.each(function(itm, indx){
			itm._width = getInnerElementsWidth(itm, '.tab-item');
			itm._elm_count = getInnerElementsCount(itm, '.tab-item');
			itm._OEW = Math.round(itm._width/itm._elm_count);
			itm._moo = new Fx.Tween($(itm), {duration:300})
		});
	}
	
	var sld_arr = $$('.slider-fade-holder');
	if (sld_arr.length){
		sld_arr.each(function(itm, indx){
			itm._knb = itm.getElements('.fade-knob');
			if (itm._knb.length == 1 && slider_elements[indx]){
				initHSlider(itm, itm._knb[0], slider_elements[indx]);
			}
		});
		sld_arr[0].parentNode.parentNode.scrollTop = (act)*47;
	}
	
}
function autoSlide(_i){
	_i++;
	if (_i >= 3) {_i = 0;}
	tab_a[_i].onclick();
	_timer = setTimeout('autoSlide('+ _i +');', 6000);
}
function getInnerElementsWidth(_elem, kindof){
	var nodes = _elem.getElements(kindof);
	var _width = 0;
	if (nodes.length){
		nodes.each(function(_e, _i){
			_width += _e.scrollWidth;
		})
		return _width;
	}
	return 0;
}
function getInnerElementsCount(_elem, kindof){
	var nodes = _elem.getElements(kindof);
	var _count = 0;
	if (nodes.length){
		nodes.each(function(_e, _i){
			_count ++;
		})
		return _count;
	}
	return 0;
}
function initHSlider(sld_knb_hld, _knb, sbx){
	
	var mySlider = new CustomSlider(sld_knb_hld, _knb, {
	    range: [0, 100],
	    wheel: true,
	    snap: true,
	    onComplete: function(pos){
			sbx._np = getDiscretPos(sbx._width, sbx._OEW, pos);//Math.round(-((sbx._width - sbx._OEW*3)/100*pos));
			sbx._moo.start('left', -sbx._np*sbx._OEW);
			/*var _tmp = Math.round(285/sbx._OEW);
			this.knob.setStyle('left', sbx._np*_tmp);*/
	    }
	});
}
function getDiscretPos(w, ow, p){
	var sc = (w/ow-3);
	return Math.round(sc*p/100);
}
var CustomSlider = new Class({

	Implements: [Events, Options],

	options: {/*
		onChange: $empty,
		onComplete: $empty,*/
		onTick: function(position){
			if(this.options.snap) position = this.toPosition(this.step);
			this.knob.setStyle(this.property, position);
		},
		snap: false,
		offset: 0,
		t_btn_class: 'top',
		b_btn_class: 'bottom',
		range: false,
		alexandra: 50,
		wheel: false,
		steps: 100,
		mode: 'horizontal'
	},

	initialize: function(element, knob, options){
		this.setOptions(options);
		this.element = $(element);
		this.knob = $(knob);
		this.alexandra = this.options.alexandra;
		this.previousChange = this.previousEnd = this.step = -1;
		this.element.addEvent('mousedown', this.clickedElement.bind(this));
		
		if (this.options.wheel) this.element.addEvent('mousewheel', this.scrolledElement.bindWithEvent(this));
		var offset, limit = {}, modifiers = {'x': false, 'y': false};
		switch (this.options.mode){
			case 'vertical':
				this.axis = 'y';
				this.property = 'top';
				offset = 'offsetHeight';
				break;
			case 'horizontal':
				this.axis = 'x';
				this.property = 'left';
				offset = 'offsetWidth';
		}
		this.half = this.knob[offset] / 2;
		
		this.timer = null;
		
		this.full = this.element[offset] - this.knob[offset] + (this.options.offset * 2);
		this.min = $chk(this.options.range[0]) ? this.options.range[0] : 0;
		this.max = $chk(this.options.range[1]) ? this.options.range[1] : this.options.steps;
		this.range = this.max - this.min;
		this.steps = this.options.steps || this.full;
		this.stepSize = Math.abs(this.range) / this.steps;
		this.stepWidth = this.stepSize * this.full / Math.abs(this.range) ;

		this.knob.setStyle('position', 'relative').setStyle(this.property, - this.options.offset);
		modifiers[this.axis] = this.property;
		limit[this.axis] = [- this.options.offset, this.full - this.options.offset];
		this.drag = new Drag(this.knob, {
			snap: 0,
			limit: limit,
			modifiers: modifiers,
			onDrag: this.draggedKnob.bind(this),
			onStart: this.draggedKnob.bind(this),
			onComplete: function(){
				this.draggedKnob();
				this.end();
			}.bind(this)
		});
		if (this.options.snap) {
			this.drag.options.grid = Math.ceil(this.stepWidth);
			this.drag.options.limit[this.axis][1] = this.full;
		}
	},

	set: function(step){
		if (!((this.range > 0) ^ (step < this.min))) step = this.min;
		if (!((this.range > 0) ^ (step > this.max))) step = this.max;
		
		this.step = Math.round(step);
		this.checkStep();
		this.end();
		this.current_position = this.toPosition(this.step);
		this.fireEvent('tick', this.toPosition(this.step));
		return this;
	},

	clickedElement: function(event){
		var dir = this.range < 0 ? -1 : 1;
		var position = event.page[this.axis] - this.element.getPosition()[this.axis] - this.half;
		position = position.limit(-this.options.offset, this.full -this.options.offset);
		this.step = Math.round(this.min + dir * this.toStep(position));
		this.checkStep();
		this.end();
		this.fireEvent('tick', position);
	},
		
	scrolledElement: function(event){
		var mode = (this.options.mode == 'horizontal') ? (event.wheel < 0) : (event.wheel > 0);
		this.set(mode ? this.step - this.stepSize : this.step + this.stepSize);
		event.stop();
	},

	draggedKnob: function(){
		var dir = this.range < 0 ? -1 : 1;
		var position = this.drag.value.now[this.axis];
		position = position.limit(-this.options.offset, this.full -this.options.offset);
		this.step = Math.round(this.min + dir * this.toStep(position));
		this.checkStep();
	},

	checkStep: function(){
		if (this.previousChange != this.step){
			this.previousChange = this.step;
			this.fireEvent('change', this.step);
		}
	},

	end: function(){
		if (this.previousEnd !== this.step){
			this.previousEnd = this.step;
			this.fireEvent('complete', this.step + '');
		}
	},

	toStep: function(position){
		var step = (position + this.options.offset) * this.stepSize / this.full * this.steps;
		return this.options.steps ? Math.round(step -= step % this.stepSize) : step;
	},

	toPosition: function(step){
		return (this.full * Math.abs(this.min - step)) / (this.steps * this.stepSize) - this.options.offset;
	}

});

var Drag = new Class({

	Implements: [Events, Options],

	options: {/*
		onBeforeStart: $empty,
		onStart: $empty,
		onDrag: $empty,
		onCancel: $empty,
		onComplete: $empty,*/
		snap: 6,
		unit: 'px',
		grid: false,
		style: true,
		limit: false,
		handle: false,
		invert: false,
		preventDefault: false,
		modifiers: {x: 'left', y: 'top'}
	},

	initialize: function(){
		var params = Array.link(arguments, {'options': Object.type, 'element': $defined});
		this.element = $(params.element);
		this.document = this.element.getDocument();
		this.setOptions(params.options || {});
		var htype = $type(this.options.handle);
		this.handles = (htype == 'array' || htype == 'collection') ? $$(this.options.handle) : $(this.options.handle) || this.element;
		this.mouse = {'now': {}, 'pos': {}};
		this.value = {'start': {}, 'now': {}};
		
		this.selection = (Browser.Engine.trident) ? 'selectstart' : 'mousedown';
		
		this.bound = {
			start: this.start.bind(this),
			check: this.check.bind(this),
			drag: this.drag.bind(this),
			stop: this.stop.bind(this),
			cancel: this.cancel.bind(this),
			eventStop: $lambda(false)
		};
		this.attach();
	},

	attach: function(){
		this.handles.addEvent('mousedown', this.bound.start);
		return this;
	},

	detach: function(){
		this.handles.removeEvent('mousedown', this.bound.start);
		return this;
	},

	start: function(event){
		if (this.options.preventDefault) event.preventDefault();
		this.fireEvent('beforeStart', this.element);
		this.mouse.start = event.page;
		var limit = this.options.limit;
		this.limit = {'x': [], 'y': []};
		for (var z in this.options.modifiers){
			if (!this.options.modifiers[z]) continue;
			if (this.options.style) this.value.now[z] = this.element.getStyle(this.options.modifiers[z]).toInt();
			else this.value.now[z] = this.element[this.options.modifiers[z]];
			if (this.options.invert) this.value.now[z] *= -1;
			this.mouse.pos[z] = event.page[z] - this.value.now[z];
			if (limit && limit[z]){
				for (var i = 2; i--; i){
					if ($chk(limit[z][i])) this.limit[z][i] = $lambda(limit[z][i])();
				}
			}
		}
		if ($type(this.options.grid) == 'number') this.options.grid = {'x': this.options.grid, 'y': this.options.grid};
		this.document.addEvents({mousemove: this.bound.check, mouseup: this.bound.cancel});
		this.document.addEvent(this.selection, this.bound.eventStop);
	},

	check: function(event){
		if (this.options.preventDefault) event.preventDefault();
		var distance = Math.round(Math.sqrt(Math.pow(event.page.x - this.mouse.start.x, 2) + Math.pow(event.page.y - this.mouse.start.y, 2)));
		if (distance > this.options.snap){
			this.cancel();
			this.document.addEvents({
				mousemove: this.bound.drag,
				mouseup: this.bound.stop
			});
			this.fireEvent('start', this.element).fireEvent('snap', this.element);
		}
	},

	drag: function(event){
		if (this.options.preventDefault) event.preventDefault();
		this.mouse.now = event.page;
		for (var z in this.options.modifiers){
			if (!this.options.modifiers[z]) continue;
			this.value.now[z] = this.mouse.now[z] - this.mouse.pos[z];
			if (this.options.invert) this.value.now[z] *= -1;
			if (this.options.limit && this.limit[z]){
				if ($chk(this.limit[z][1]) && (this.value.now[z] > this.limit[z][1])){
					this.value.now[z] = this.limit[z][1];
				} else if ($chk(this.limit[z][0]) && (this.value.now[z] < this.limit[z][0])){
					this.value.now[z] = this.limit[z][0];
				}
			}
			if (this.options.grid[z]) this.value.now[z] -= (this.value.now[z] % this.options.grid[z]);
			if (this.options.style) this.element.setStyle(this.options.modifiers[z], this.value.now[z] + this.options.unit);
			else this.element[this.options.modifiers[z]] = this.value.now[z];
		}
		this.fireEvent('drag', this.element);
	},

	cancel: function(event){
		this.document.removeEvent('mousemove', this.bound.check);
		this.document.removeEvent('mouseup', this.bound.cancel);
		if (event){
			this.document.removeEvent(this.selection, this.bound.eventStop);
			this.fireEvent('cancel', this.element);
		}
	},

	stop: function(event){
		this.document.removeEvent(this.selection, this.bound.eventStop);
		this.document.removeEvent('mousemove', this.bound.drag);
		this.document.removeEvent('mouseup', this.bound.stop);
		if (event) this.fireEvent('complete', this.element);
	}

});


if (window.addEventListener)
	window.addEventListener("load", initHPSlider, false);
else if (window.attachEvent && !window.opera)
	window.attachEvent("onload", initHPSlider);
