﻿$(document).ready(function () {
    initNavIndexes();
    initSlide();
    clearFormFields({
        clearInputs:true,
        clearTextareas:true,
        passwordFieldText:true,
        addClassFocus:"focus",
        filterClass:"default"
    });
    //initValidation();
    initFade();
    initCrowLine();
    initTabs();
});
function initNavIndexes() {
    var nav = document.getElementById("step");
    if (nav) {
        var lis = nav.getElementsByTagName("li");
        for (var i = 0; i < lis.length; i++) {
            //lis[i].style.zIndex = i+1;
            lis[i].style.zIndex = lis.length - i;
        }
    }
}
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.id == 'searchKey') 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.id == 'searchKey') {
                        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)) {
                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";
    }
}
function initTabs() {
    $('.tab-content img').show();
    jQuery('.tabset-holder-inner').NVTabs({
        opener:'.tabset-inner > li > h2 > a',
        tabBox:'div.tab-content-inner'
    });
}
jQuery.fn.NVTabs = function (_options) {
    var _options = jQuery.extend({
        opener:'.tabset > li > h2 > a',
        tabBox:'div.tab-content',
        fadeTab:false,
        slideTab:false,
        autoRotate:false,
        swichTime:5000,
        animationSpeed:500,
        eventSwich:'click',
        activeClass:'active'
    }, _options);

    return this.each(function () {
        var _this = jQuery(this);
        var _opener = jQuery(_options.opener, _this);
        var _tabBox = jQuery(_options.tabBox, _this);
        var _activeClass = _options.activeClass;
        var _fadeTab = _options.fadeTab;
        var _slideTab = _options.slideTab;
        var _swichTime = _options.swichTime;
        var _animationSpeed = _options.animationSpeed;
        var _autoRotate = _options.autoRotate;
        var _eventSwich = _options.eventSwich;
        if (!_tabBox.length) return;
        var _tabCount = _tabBox.length;
        var ind = 0;
        var prevInd = ind;
        var _tabInfo, timer;
        _opener.each(function (i, el) {
            if (_opener.eq(i).hasClass(_activeClass)) ind = i;
            prevInd = ind;
        })
        if (_fadeTab) {
            _this.css({
                position:'relative',
                height:_opener.parents('ul').outerHeight(true) + _tabBox.eq(ind).outerHeight(true)
            })
            _tabBox.css({
                position:'absolute',
                top:_opener.parents('ul').outerHeight(true),
                left:0,
                opacity:0
            });
            _tabBox.eq(ind).css({
                opacity:1
            })
        }
        else if (_slideTab) {
            _tabInfo = jQuery('<div class="tabinfo-wrapper"></div>');
            _tabInfo.appendTo(_this);
            _this.css({
                width:_this.parent().width()
            });
            _tabBox.css({
                position:'absolute',
                top:0
            });
            _tabBox.show().each(function (i, el) {
                jQuery(this).appendTo(_tabInfo);
                jQuery(this).css({
                    left:jQuery(this).outerWidth(true) * i
                });
            });
            _tabInfo.css({
                position:'relative',
                width:_tabBox.outerWidth(true) * _tabCount,
                left:-_tabBox.eq(ind).outerWidth(true) * ind,
                height:_tabBox.eq(ind).outerHeight(true)
            });
            _tabInfo.animate({
                height:_tabBox.eq(ind).outerHeight(true)
            }, { queue:false, duration:100 });
        }
        else {
            _tabBox.hide().eq(ind).show();
        }
        function nextTab() {
            prevInd = ind;
            if (ind < _tabCount - 1) ind++;
            else ind = 0;
            swichTab();
        }

        function prevTab() {
            prevInd = ind;
            if (ind > 0) ind--;
            else ind = _tabCount - 1;
            swichTab();
        }

        function swichTab() {
            if (_fadeTab) {
                _tabBox.eq(prevInd).animate({
                    opacity:0
                }, { queue:false, duration:_animationSpeed });
                _tabBox.eq(ind).show().animate({
                    opacity:1
                }, { queue:false, duration:_animationSpeed });
                _this.animate({
                    height:_opener.parents('ul').outerHeight(true) + _tabBox.eq(ind).outerHeight(true)
                }, { queue:false, duration:250 })
            }
            else if (_slideTab) {
                _tabInfo.animate({
                    left:-_tabBox.eq(ind).outerWidth(true) * ind,
                    height:_tabBox.eq(ind).outerHeight(true)
                }, { queue:false, duration:_animationSpeed })
            }
            else {
                _tabBox.hide().eq(ind).show();
            }
            _opener.removeClass(_activeClass).eq(ind).addClass(_activeClass);
        }

        function autoSwich() {
            if (_autoRotate) {
                if (timer) clearTimeout(timer);
                timer = setTimeout(function () {
                    nextTab();
                    autoSwich();
                }, _swichTime)
            }
        }

        _opener.bind(_eventSwich, function () {
            prevInd = ind;
            ind = _opener.index(this);
            if (!jQuery(this).hasClass(_activeClass)) {
                swichTab();
            }
            return false;
        });
        _this.hover(
            function () {
                if (timer) clearTimeout(timer);
            },
            function () {
                if (timer) clearTimeout(timer);
                autoSwich();
            }
        )
        autoSwich();
    });
}
function initSlide() {
    jQuery('div.gallery-top').scrollGallery({
        sliderHolder:'div.gallery-top-frame',
        slider:'>ul',
        slides:'>li',
        btnPrev:'a.link-prev',
        btnNext:'a.link-next',
        vertical:true
    });
    jQuery('div.gallery-new-holder').scrollGallery({
        sliderHolder:'div.gallery-new-mask',
        slider:'>ul',
        slides:'>li',
        btnPrev:'a.link-prev',
        btnNext:'a.link-next'
    });
    jQuery('div.gallery').scrollGallery({
        sliderHolder:'div.gallery-frame',
        slider:'>ul',
        slides:'>li',
        pagerLinks:'.switch a',
        btnPrev:'a.prev',
        btnNext:'a.next'
    });
}
jQuery.fn.scrollGallery = function (_options) {
    var _options = jQuery.extend({
        sliderHolder:'>div',
        slider:'>ul',
        slides:'>li',
        pagerLinks:'div.pager a',
        btnPrev:'a.link-prev',
        btnNext:'a.link-next',
        activeClass:'active',
        disabledClass:'disabled',
        generatePagination:'div.pg-holder',
        curNum:'em.scur-num',
        allNum:'em.sall-num',
        circleSlide:true,
        pauseClass:'gallery-paused',
        pauseButton:'none',
        pauseOnHover:true,
        autoRotation:false,
        stopAfterClick:false,
        switchTime:5000,
        duration:650,
        easing:'swing',
        event:'click',
        afterInit:false,
        vertical:false,
        step:false,
        startElement:0
    }, _options);

    return this.each(function () {
        var _this = jQuery(this);
        var _sliderHolder = jQuery(_options.sliderHolder, _this);
        var _slider = jQuery(_options.slider, _sliderHolder);
        var _slides = jQuery(_options.slides, _slider);
        var _btnPrev = jQuery(_options.btnPrev, _this);
        var _btnNext = jQuery(_options.btnNext, _this);
        var _pagerLinks = jQuery(_options.pagerLinks, _this);
        var _generatePagination = jQuery(_options.generatePagination, _this);
        var _curNum = jQuery(_options.curNum, _this);
        var _allNum = jQuery(_options.allNum, _this);
        var _pauseButton = jQuery(_options.pauseButton, _this);
        var _pauseOnHover = _options.pauseOnHover;
        var _pauseClass = _options.pauseClass;
        var _autoRotation = _options.autoRotation;
        var _activeClass = _options.activeClass;
        var _disabledClass = _options.disabledClass;
        var _easing = _options.easing;
        var _duration = _options.duration;
        var _switchTime = _options.switchTime;
        var _controlEvent = _options.event;
        var _step = _options.step;
        var _vertical = _options.vertical;
        var _circleSlide = _options.circleSlide;
        var _stopAfterClick = _options.stopAfterClick;
        var _afterInit = _options.afterInit;

        if (!_slides.length) return;
        var _currentStep = _options.startElement;
        var _sumWidth = 0;
        var _sumHeight = 0;
        var _hover = false;
        var _stepWidth;
        var _stepHeight;
        var _stepCount;
        var _offset;
        var _timer;

        _slides.each(function () {
            _sumWidth += $(this).outerWidth(true);
            _sumHeight += $(this).outerHeight(true);
        });
        if (_vertical) {
            _slider.css({ height:_sumHeight})
        }
        else {
            _slider.css({ width:_sumWidth})
        }
        ;

        function recalcOffsets() {
            if (_vertical) {
                if (_step) {
                    _stepHeight = _slides.eq(_currentStep).outerHeight(true);
                    _stepCount = Math.ceil((_sumHeight - _sliderHolder.height()) / _stepHeight) + 1;
                    _offset = -_stepHeight * _currentStep;
                } else {
                    _stepHeight = _sliderHolder.height();
                    _stepCount = Math.ceil(_sumHeight / _stepHeight);
                    _offset = -_stepHeight * _currentStep;
                    if (_offset < _stepHeight - _sumHeight) _offset = _stepHeight - _sumHeight;
                }
            } else {
                if (_step) {
                    _stepWidth = _slides.eq(_currentStep).outerWidth(true) * _step;
                    _stepCount = Math.ceil((_sumWidth - _sliderHolder.width()) / _stepWidth) + 1;
                    _offset = -_stepWidth * _currentStep;
                    if (_offset < _sliderHolder.width() - _sumWidth) _offset = _sliderHolder.width() - _sumWidth;
                } else {
                    _stepWidth = _sliderHolder.width();
                    _stepCount = Math.ceil(_sumWidth / _stepWidth);
                    _offset = -_stepWidth * _currentStep;
                    if (_offset < _stepWidth - _sumWidth) _offset = _stepWidth - _sumWidth;
                }
            }
        }

        if (_btnPrev.length) {
            _btnPrev.bind(_controlEvent, function () {
                if (_stopAfterClick) stopAutoSlide();
                prevSlide();
                return false;
            });
        }
        if (_btnNext.length) {
            _btnNext.bind(_controlEvent, function () {
                if (_stopAfterClick) stopAutoSlide();
                nextSlide();
                return false;
            });
        }
        if (_generatePagination.length) {
            _generatePagination.empty();
            recalcOffsets();
            var _list = $('<ul />');
            for (var i = 0; i < _stepCount; i++) $('<li><a href="#">' + (i + 1) + '</a></li>').appendTo(_list);
            _list.appendTo(_generatePagination);
            _pagerLinks = _list.children();
        }
        if (_pagerLinks.length) {
            _pagerLinks.each(function (_ind) {
                jQuery(this).bind(_controlEvent, function () {
                    if (_currentStep != _ind) {
                        if (_stopAfterClick) stopAutoSlide();
                        _currentStep = _ind;
                        switchSlide();
                    }
                    return false;
                });
            });
        }
        function prevSlide() {
            recalcOffsets();
            if (_currentStep > 0) _currentStep--;
            else if (_circleSlide) _currentStep = _stepCount - 1;
            switchSlide();
        }

        function nextSlide() {
            recalcOffsets();
            if (_currentStep < _stepCount - 1) _currentStep++;
            else if (_circleSlide) _currentStep = 0;
            switchSlide();
        }

        function refreshStatus() {
            if (_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentStep).addClass(_activeClass);
            if (!_circleSlide) {
                _btnPrev.removeClass(_disabledClass);
                _btnNext.removeClass(_disabledClass);
                if (_currentStep == 0) _btnPrev.addClass(_disabledClass);
                if (_currentStep == _stepCount - 1) _btnNext.addClass(_disabledClass);
            }
            if (_curNum.length) _curNum.text(_currentStep + 1);
            if (_allNum.length) _allNum.text(_stepCount);
        }

        function switchSlide() {
            recalcOffsets();
            if (_vertical) _slider.animate({marginTop:_offset}, {duration:_duration, queue:false, easing:_easing});
            else _slider.animate({marginLeft:_offset}, {duration:_duration, queue:false, easing:_easing});
            refreshStatus();
            autoSlide();
        }

        function stopAutoSlide() {
            if (_timer) clearTimeout(_timer);
            _autoRotation = false;
        }

        function autoSlide() {
            if (!_autoRotation || _hover) return;
            if (_timer) clearTimeout(_timer);
            _timer = setTimeout(nextSlide, _switchTime + _duration);
        }

        if (_pauseOnHover) {
            _this.hover(function () {
                _hover = true;
                if (_timer) clearTimeout(_timer);
            }, function () {
                _hover = false;
                autoSlide();
            });
        }

        recalcOffsets();
        refreshStatus();
        autoSlide();
        if (_vertical) {
            _slider.css({ marginTop:_offset });
        }
        else _slider.css({ marginLeft:_offset });

        if (_pauseButton.length) {
            _pauseButton.click(function () {
                if (_this.hasClass(_pauseClass)) {
                    _this.removeClass(_pauseClass);
                    _autoRotation = true;
                    autoSlide();
                } else {
                    _this.addClass(_pauseClass);
                    stopAutoSlide();
                }
                return false;
            });
        }
        if (_afterInit && typeof _afterInit === 'function') _afterInit(_this, _slides);
    });
}

function initValidation() {
    var _errorClass = 'error-row';
    var _regEmail = /([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/;
    jQuery('form.contact-form').each(function () {
        var _form = jQuery(this);
        jQuery('input', jQuery(this)).focus(function () {
            jQuery(this).parents('div').eq(0).removeClass(_errorClass);
        });
        function checkFields() {

            var _flag = false;
            _form.find('.' + _errorClass).removeClass(_errorClass);

            // fields validation
            _form.find('.request').each(function () {
                if (jQuery(this).val() == '' || jQuery(this).val() == jQuery(this).attr('title')) addError(jQuery(this));
            });
            _form.find('.request-email').each(function () {
                if (!_regEmail.test(jQuery(this).val()) || jQuery(this).val() == '' || jQuery(this).val() == jQuery(this).attr('title')) addError(jQuery(this));
            });
            // error class adding
            function addError(_obj) {
                _obj.parents('div').eq(0).addClass(_errorClass);
                _flag = true;
            }

            return _flag;
        }

        // catch form submit event
        _form.bind('submit', function () {
            if (checkFields()) {
                return false;
            }
        });
    });
}
function initFade() {
    jQuery('.tabset-holder').gallery({
        listOfSlides:'.tab-holder > .tab-content',
        switcher:'.tabset > li',
        autoHeight:true,
        event:'mouseover',
        autoRotation:5000,
        effect:'fade'
    });
}
(function ($) {
    $.fn.gallery = function (options) {
        return new Gallery(this.get(0), options);
    };
    function Gallery(context, options) {
        this.init(context, options);
    }

    ;
    Gallery.prototype = {
        options:{},
        init:function (context, options) {
            this.options = $.extend({
                infinite:false,
                duration:700,
                slideElement:1,
                autoRotation:false,
                effect:false,
                listOfSlides:'ul > li',
                switcher:false,
                disableBtn:false,
                nextBtn:'a.link-next, a.btn-next, a.next',
                prevBtn:'a.link-prev, a.btn-prev, a.prev',
                circle:true,
                direction:false,
                event:'click',
                IE:false,
                autoHeight:false
            }, options || {});
            var _el = $(context).find(this.options.listOfSlides);
            if (this.options.effect) this.list = _el;
            else this.list = _el.parent();
            this.switcher = $(context).find(this.options.switcher);
            this.nextBtn = $(context).find(this.options.nextBtn);
            this.prevBtn = $(context).find(this.options.prevBtn);
            this.count = _el.index(_el.filter(':last'));

            if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
            else this.active = _el.index(_el.filter('.active:eq(0)'));
            if (this.active < 0) this.active = 0;
            this.last = this.active;

            this.woh = _el.outerWidth(true);
            if (!this.options.direction) this.installDirections(this.list.parent().width());
            else {
                this.woh = _el.outerHeight(true);
                this.installDirections(this.list.parent().height());
            }

            if (!this.options.effect) {
                this.rew = this.count - this.wrapHolderW + 1;
                if (!this.options.direction) this.anim = '{marginLeft: -(this.woh * this.active)}';
                else this.anim = '{marginTop: -(this.woh * this.active)}';
                eval('this.list.css(' + this.anim + ')');
            }
            else {
                this.rew = this.count;

                this.list.css({
                    opacity:0,
                    position:'absolute',
                    top:0,
                    left:0
                }).removeClass('active').eq(this.active).addClass('active').css({opacity:1}).css('opacity', 'auto');
                this.list.parent().css({
                    position:'relative'
                })
                this.switcher.removeClass('active').eq(this.active).addClass('active');
                if (this.options.autoHeight) this.list.parent().css({height:this.list.eq(this.active).outerHeight()});
            }
            this.flag = true;
            if (this.options.infinite) {
                this.count++;
                this.active += this.count;
                this.list.append(_el.clone());
                this.list.append(_el.clone());
                eval('this.list.css(' + this.anim + ')');
            }

            this.initEvent(this, this.nextBtn, true);
            this.initEvent(this, this.prevBtn, false);
            if (this.options.disableBtn) this.initDisableBtn();
            if (this.options.autoRotation) this.runTimer(this);
            if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
        },
        initDisableBtn:function () {
            this.prevBtn.removeClass('prev-' + this.options.disableBtn);
            this.nextBtn.removeClass('next-' + this.options.disableBtn);
            if (this.active == 0 || this.count + 1 == this.wrapHolderW) this.prevBtn.addClass('prev-' + this.options.disableBtn);
            if (this.active == 0 && this.count == 1 || this.count + 1 <= this.wrapHolderW) this.nextBtn.addClass('next-' + this.options.disableBtn);
            if (this.active == this.rew) this.nextBtn.addClass('next-' + this.options.disableBtn);
        },
        installDirections:function (temp) {
            this.wrapHolderW = Math.ceil(temp / this.woh);
            if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderW--;
        },
        fadeElement:function () {
            if ($.browser.msie && this.options.IE) {
                this.list.eq(this.last).css({opacity:0});
                this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
            }
            else {
                this.list.eq(this.last).animate({opacity:0}, {queue:false, duration:this.options.duration});
                this.list.removeClass('active').eq(this.active).addClass('active').animate({
                    opacity:1
                }, {queue:false, duration:this.options.duration, complete:function () {
                    $(this).css('opacity', 'auto');
                }});
            }
            if (this.options.autoHeight) this.list.parent().animate({height:this.list.eq(this.active).outerHeight()}, {queue:false, duration:this.options.duration});
            if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
            this.last = this.active;
        },
        scrollElement:function ($this) {
            if (!$this.options.infinite) eval('$this.list.animate(' + $this.anim + ', {queue:false, duration: $this.options.duration});');
            else eval('$this.list.animate(' + $this.anim + ', $this.options.duration, function(){ $this.flag = true });');
            if ($this.options.switcher) $this.switcher.removeClass('active').eq($this.active / $this.options.slideElement).addClass('active');
        },
        runTimer:function ($this) {
            if ($this._t) clearTimeout($this._t);
            $this._t = setInterval(function () {
                if ($this.options.infinite) $this.flag = false;
                $this.toPrepare($this, true);
            }, this.options.autoRotation);
        },
        initEventSwitcher:function ($this, el) {
            el.bind($this.options.event, function () {
                $this.active = $this.switcher.index($(this)) * $this.options.slideElement;
                if ($this._t) clearTimeout($this._t);
                if ($this.options.disableBtn) $this.initDisableBtn();
                if (!$this.options.effect) $this.scrollElement($this);
                else $this.fadeElement();
                if ($this.options.autoRotation) $this.runTimer($this);
                return false;
            });
        },
        initEvent:function ($this, addEventEl, dir) {
            addEventEl.bind($this.options.event, function () {
                if ($this.flag) {
                    if ($this.options.infinite) $this.flag = false;
                    if ($this._t) clearTimeout($this._t);
                    $this.toPrepare($this, dir);
                    if ($this.options.autoRotation) $this.runTimer($this);
                }
                return false;
            });
        },
        toPrepare:function ($this, side) {
            if (!$this.options.infinite) {
                if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
                if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
                for (var i = 0; i < $this.options.slideElement; i++) {
                    if (side) {
                        if ($this.active + 1 <= $this.rew) $this.active++;
                    }
                    else {
                        if ($this.active - 1 >= 0) $this.active--;
                    }
                }
                ;
            }
            else {
                if ($this.active >= $this.count + $this.count && side) $this.active -= $this.count;
                if ($this.active <= $this.count - 1 && !side) $this.active += $this.count;
                eval('$this.list.css(' + $this.anim + ')');
                if (side) $this.active += $this.options.slideElement;
                else $this.active -= $this.options.slideElement;
            }
            if (this.options.disableBtn) this.initDisableBtn();
            if (!$this.options.effect) $this.scrollElement($this);
            else $this.fadeElement();
        },
        stop:function () {
            if (this._t) clearTimeout(this._t);
        },
        play:function () {
            if (this._t) clearTimeout(this._t);
            if (this.options.autoRotation) this.runTimer(this);
        }
    }
}(jQuery));

function initCrowLine() {
    jQuery('.partner-logo').crawlLine({
        crawElement:'>ul',
        textElement:'>li',
        imageCenter:true
    });
}

;
(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:'div',
        textElement:'p',
        hoverClass:'viewText',
        imageCenter:false
    }, _options);

    return this.each(function () {
        var _THIS = jQuery(this);
        var _el = $(_options.crawElement, _THIS).css('position', 'relative');
        var _text = $(_options.textElement, _el);
        var _clone = _text.css('whiteSpace', 'nowrap').clone();
        var _elWidth = 0;
        var _k = 1;
        var _imageCenter = _options.imageCenter;

        // set parametrs *******************************************************
        var _textWidth = 0;
        _text.each(function () {
            if (_imageCenter) {
                jQuery(this).children().css({
                    marginTop:(jQuery(this).parent('ul').height() - jQuery(this).children().outerHeight(true)) / 2
                })
            }
            _textWidth += jQuery(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;
        });
    });
}
