/* Gallery */
function gallery(_obj) {
	
	// defaults options
	this.options = {
		hold: 'div.gallery-slide',
		duration: 700,
		slideElement: 1,
		autoRotation: false,
		effect: false,
		fadeEl: 'ul',
		switcher: 'ul > li',
		disableBtn: false,
		nextBtn: 'a.link-next, a.btn-next, a.next',
		prevBtn: 'a.link-prev, a.btn-prev, a.prev',
		circle: true,
		direction: false
	};
	for(key in _obj) this.options[key] = _obj[key];
	
	var _this = this;
	var _hold = $(_this.options.hold);
	var _speed = _this.options.duration;
	var _timer = _this.options.autoRotation;
	var _wrap = _hold.find(_this.options.fadeEl);
	var _el = _hold.find(_this.options.switcher);
	var _next = _hold.find(_this.options.nextBtn);
	var _prev = _hold.find(_this.options.prevBtn);
	var _count = _el.index(_el.filter(':last'));
	var _w = _el.outerWidth(true);
	var _h = _el.outerHeight(true);
	if (!_this.options.direction) {
		var _wrapHolderW = Math.ceil(_wrap.parent().width() / _w);
		if (((_wrapHolderW - 1) * _w + _w / 2) > _wrap.parent().width()) _wrapHolderW--;
	}
	else{
		var _wrapHolderW = Math.ceil(_wrap.parent().height()/_h);
		if (((_wrapHolderW-1)*_h + _h/2) > _wrap.parent().height()) _wrapHolderW--;
	}
	if (_timer) var _t;
	var _active = _el.index(_el.filter('.active:eq(0)'));
	if (_active < 0) _active = 0;
	var _last = _active;
	if (!_this.options.effect) var rew = _count - _wrapHolderW + 1;
	else var rew = _count;
	
	if (!_this.options.effect) {
		if (!_this.options.direction) _wrap.css({marginLeft: -(_w * _active)})
		else _wrap.css({marginTop: -(_h * _active)})
	}
	else {
		_wrap.css({
			opacity: 0
		}).removeClass('active').eq(_active).addClass('active').css({
			opacity: 1
		}).css('opacity', 'auto');
		_el.removeClass('active').eq(_active).addClass('active');
	}
	if (_this.options.disableBtn) {
		if (_count < _wrapHolderW) _next.addClass(_this.options.disableBtn);
		_prev.addClass(_this.options.disableBtn);
	}
	
	function fadeElement(){
		_wrap.eq(_last).animate({opacity:0}, {queue:false, duration: _speed});
		_wrap.removeClass('active').eq(_active).addClass('active').animate({
			opacity:1
		}, {queue:false, duration: _speed, complete: function(){
			$(this).css('opacity','auto');
		}});
		_el.removeClass('active').eq(_active).addClass('active');
		_last = _active;
	}
	function scrollEl(){
		if (!_this.options.direction) _wrap.animate({marginLeft: -(_w * _active)}, {queue:false, duration: _speed})
		else _wrap.animate({marginTop: -(_h * _active)}, {queue:false, duration: _speed})
	}
	function toPrepare(){
		if ((_active == rew) && _this.options.circle) _active = -_this.options.slideElement;
		for (var i = 0; i < _this.options.slideElement; i++){
			_active++;
			if (_active > rew) {
				_active--;
				if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
			}
		};
		if (_active == rew) if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
		if (!_this.options.effect) scrollEl();
		else fadeElement();
	}
	function runTimer(){
		_t = setInterval(function(){
			toPrepare();
		}, _timer);
	}
	_next.click(function(){
		flag=true;
		if(_t) clearTimeout(_t);
		if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.removeClass(_this.options.disableBtn);
		toPrepare();
		if (_timer) runTimer();
		return false;
	});
	_prev.click(function(){
		flag=true;
		if(_t) clearTimeout(_t);
		if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.removeClass(_this.options.disableBtn);
		if ((_active == 0) && _this.options.circle) _active = rew + _this.options.slideElement;
		for (var i = 0; i < _this.options.slideElement; i++){
			_active--;
			if (_active < 0) {
				_active++;
				if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
			}
		};
		if (_active == 0) if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
		if (!_this.options.effect) scrollEl();
		else fadeElement();
		if (_timer) runTimer();
		return false;
	});
	if (_this.options.effect) _el.click(function(){
		_active = _el.index($(this));
		if(_t) clearTimeout(_t);
		fadeElement();
		if (_timer) runTimer();
		if(_this.options.fadeEl != _this.options.switcher ){
			return false;
		}
	});
	if (_timer) runTimer();
	
	this.stop = function(){
		if (_t) clearTimeout(_t);
	}
	this.play = function(){
		if (_t) clearTimeout(_t);
		if (_timer) runTimer();
	}
}

;(function($){$.fn.wheel=function(a){return this[a?"bind":"trigger"]("wheel",a)};$.event.special.wheel={setup:function(){$.event.add(this,b,wheelHandler,{})},teardown:function(){$.event.remove(this,b,wheelHandler)}};var b=!$.browser.mozilla?"mousewheel":"DOMMouseScroll"+($.browser.version<"1.9"?" mousemove":"");function wheelHandler(a){switch(a.type){case"mousemove":return $.extend(a.data,{clientX:a.clientX,clientY:a.clientY,pageX:a.pageX,pageY:a.pageY});case"DOMMouseScroll":$.extend(a,a.data);a.delta=-a.detail/3;break;case"mousewheel":a.delta=a.wheelDelta/120;if($.browser.opera)a.delta*=-1;break}a.type="wheel";return $.event.handle.call(this,a,a.delta)}})(jQuery);

jQuery.fn.crawlLine = function(_options){
	// defaults options
	var _options = jQuery.extend({
		speed:2,
		crawElement:'.craw-line',
		textElement:'ul',
		hoverClass:'viewText'
	},_options);
	
	return this.each(function(){
		var _THIS = jQuery(this);
		var _el = $(_options.crawElement, _THIS).css('position','relative');
		var _text = $(_options.textElement, _THIS);
		var _clone = _text.css({
			'whiteSpace': 'nowrap'
		}).clone();
		var _elWidth = 0;
		var _k = 1;
		
		// set parametrs *******************************************************
		var _textWidth = 0;
		_text.each(function(){
			_textWidth += $(this).outerWidth(true);
		});
		var _duration = _textWidth*50 / _options.speed;
		_el.append(_clone);
		_el.css('width',_textWidth*2);
		
	    var animate = function() {
			_el.animate({left:-_textWidth}, {queue:false, duration:_duration*_k, easing:'linear', complete:function(){
				_el.css('left','0');
				_k=1;
				animate();
			}})
	    }
	    animate();
		
	    _THIS.hover(function() {
			_el.stop();
			_THIS.addClass(_options.hoverClass);
	    }, function(){
			_THIS.removeClass(_options.hoverClass);
			_k = (_textWidth + parseInt(_el.css('left')))/_textWidth;
			animate();
	    })
		_THIS.bind('wheel',function(event,delta){
			var _marginScroll;
			if (delta<0) {
				_marginScroll = parseInt(_el.css('left')) - 20;
				_el.animate({left:_marginScroll}, {queue:false, duration:100, easing:'linear', complete:function(){
					_k = (_textWidth + parseInt(_el.css('left')))/_textWidth;
				}});
			} else {
				_marginScroll = parseInt(_el.css('left')) + 20;
				if (_marginScroll > 0) _marginScroll = 0;
				_el.animate({left:_marginScroll}, {queue:false, duration:100, easing:'linear', complete:function(){
					_k = (_textWidth + parseInt(_el.css('left')))/_textWidth;
				}});
			}
			return false;
		});
	});
}

$(document).ready(function(){
	gal = new gallery({
		hold: '.gallery-wrapper',
		duration: 250,
		autoRotation: 50000,
		fadeEl: 'ul.gallery > li',
		switcher: '.thumbnails li',
		effect: 'fade'
	});
	
	$('.scroller').crawlLine({crawElement:'.craw-line'});
});

eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('9 t(a,b){g(3=0;3<a.h;3++){k c=[],d=[],n=[],e=[],4=[],l=[];8(a[3].5(\'.\')!=-1||a[3].5(\'#\')==-1){8(a[3].5(\'>\')!=-1){c[3]=a[3].f(a[3].5(\'>\')+2);a[3]=a[3].f(0,a[3].5(\'>\')-1)}8(a[3].5(\'.\')!=-1){d[3]=a[3].f(a[3].5(\'.\')+1);a[3]=a[3].f(0,a[3].5(\'.\'))}n[3]=a[3];8(!d[3])d[3]=\'\';8(c[3]){l[3]=r.o(n[3]);g(k j=0;j<l[3].h;j++){8(l[3][j].6.5(d[3])!=-1){4[3]=l[3][j].o(c[3]);g(k i=0;i<4[3].h;i++){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}}}s{4[3]=r.o(n[3]);g(k i=0;i<4[3].h;i++){8(4[3][i].6.5(d[3])!=-1){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}}}s 8(a[3].5(\'#\')!=-1){8(a[3].5(\'>\')!=-1){c[3]=a[3].f(a[3].5(\'>\')+2);a[3]=a[3].f(0,a[3].5(\'>\')-1)}a[3]=a[3].m(\'#\',\'\');e[3]=r.u(a[3]);8(e[3]){8(c[3]){4[3]=e[3].o(c[3]);g(k i=0;i<4[3].h;i++){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}s{e[3].p=9(){7.6+=\' \'+b};e[3].q=9(){7.6=7.6.m(b,\'\')}}}}}}',31,31,'|||_hoverItem|_hoverElement|indexOf|className|this|if|function||||_class|_id|substr|for|length|||var|_parent|replace|_tag|getElementsByTagName|onmouseover|onmouseout|document|else|hoverForIE6|getElementById'.split('|'),0,{}));
function ieHover() {
	hoverForIE6(['#nav > li','ul.menu > li', 'table > tr','ul.list > li'], 'hover');
}
if (window.attachEvent && !window.opera){
	window.attachEvent("onload", ieHover);
}

function initPage()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
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.filter) o.filter = "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)) {
				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);
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);
