$(document).ready( function() {
    var inAct = false;
    var elIndex = 0;
    var auto = true;
    $('.novaslider').mouseenter( function() {
        var left = $('.novaslider-cnt', this).position().left;
        var item = 0;
        if( left != 0 ) {
            item = Math.abs( left ) / $('.novaslider-item', this).width();
        }
        var text = $( $('.novaslider-item input', this)[item] ).val();
        $('.novaslider-descr span', this).html(text);
        $('.novaslider-left, .novaslider-right')
        .stop(true, false).animate({opacity: 1}, 300);
        $('.novaslider-descr', this)
        .stop(true, false).animate({opacity: 0.75}, 300);
    }).mouseleave( function() {
        $('.novaslider-left, .novaslider-right, .novaslider-descr', this)
        .stop(true, false).animate({opacity: 0}, 300);
    });
    $('.novaslider-left').click( function() {
        if( inAct ) {
            return false;
        }
        inAct = true;
        var self = $(this).parent();
        var cntWidth = $('.novaslider-item', self).width() * $('.novaslider-item', self).length;
        var left     = $('.novaslider-cnt', self).position().left;
        $('.novaslider-descr span', this).html(text);
        if( $.browser.msie && $.browser.version > 6 ) {
            left = left - 1;
        }
//        alert(cntWidth + left - $('.novaslider-item', self).length);
//        alert( $('.novaslider-item', self).width() );
        if( cntWidth + left <= $('.novaslider-item', self).width() ) {
            elIndex = 0;
            $('.novaslider-cnt', self).animate( {left: 0}, 300, function() {
                inAct = false;
            });
        } else {
            elIndex += 1
            $('.novaslider-cnt', self).animate( {left: left - $('.novaslider-item', self).width()}, 300, function() {
                inAct = false;
            });
        }
        var text = $( $('.novaslider-item input', self)[elIndex] ).val();
        $('.novaslider-descr span', self).fadeOut( 150, function() {
            $(this).html( text ).fadeIn( 150 );
        })
    });
    var sl = setInterval( function() {
        if( auto ) {
            $('.novaslider-left').click()
        }
    }, 2000 );
    var temp = null;
    $('.novaslider-right, .novaslider-left').click( function() {
        auto = false;
        try {
            clearTimeout( temp );
        } catch(e) {}
        temp = setTimeout( function() {
            auto = true;
        }, 2000)
    });
    $('.novaslider-right').click( function() {
        if( inAct ) {
            return false;
        }
        inAct = true;
        var self = $(this).parent();
        var cntWidth = $('.novaslider-item', self).width() * $('.novaslider-item', self).length;
        var left     = $('.novaslider-cnt', self).position().left;
        $('.novaslider-descr span', this).html(text);
        if( $.browser.msie && $.browser.version > 6 ) {
            left = left - 1;
        }
        if( left == 0 || left == -1 ) {
            $('.novaslider-cnt', self).animate( {left: $('.novaslider-item', self).width() - cntWidth}, 300, function() {
                inAct = false;
            });
            elIndex = $('.novaslider-item', self).length - 1;
        } else {
            elIndex -= 1;
            $('.novaslider-cnt', self).animate( {left: left + $('.novaslider-item', self).width()}, 300, function() {
                inAct = false;
            });
        }
        var text = $( $('.novaslider-item input', self)[elIndex] ).val();
        $('.novaslider-descr span', self).fadeOut( 150, function() {
            $(this).html( text ).fadeIn( 150 );
        })
    });

//    href
    $('.novaslider-item').click(function(){
        document.location = "/portfolio/";
    });
})


