/* Author: Linea Web Graffiti 2000

*/

var _fnNextSlide = null;
var _fnStartSlide = null;
var _fnTestAnimate = null;

$(document).ready(function () {

	if(!($('#InEvidence_Preview img').length)){
		$('#InEvidence_Preview span').addClass('no-image');
	}
	
    // Run Matt Kersley's jQuery Responsive menu plugin (see plugins.js)
    if ($.fn.mobileMenu) {
        $('nav[role="navigation"] ul').mobileMenu({
            switchWidth: 965,                   // width (in px to switch at)
            topOptionText: 'MENU\'',            // first option text
            indentString: '&nbsp;&nbsp;&nbsp;'  // string for indenting nested items
        });
    }

    if ($.fn.mobileMenu) {
        $('#navLeft ul').mobileMenu({
            switchWidth: 965,                   // width (in px to switch at)
            topOptionText: 'SUB MENU\'',        // first option text
            indentString: '&nbsp;&nbsp;&nbsp;'  // string for indenting nested items
        });
    }
    
    if ($('#recaptcha_widget').length) {
        var arr = $('#recaptcha_widget').find('div a img');
        arr.eq(0).attr('src', '/Master/Phoenix/images/captcha.png');
    }

    // Run Mathias Bynens jQuery placeholder plugin (see plugins.js)
    if ($.fn.placeholder) {
        $('input, textarea').placeholder();
    }

    var _sliderTimer = null;
    var _sliderPause = false;
    var _sliderAnimate = false;

    var productList = $('<ul id="productsList"></ul>');
    var productCount = 0;
    $('.flexslider a').each(function (index) {
        var $obj = $(this);

        $obj.mouseenter(function () {
            _sliderPause = true;
            clearTimeout(_sliderTimer);
        });
        $obj.mouseleave(function () {
            _sliderPause = false;
            _fnStartSlider();
        });

        var productHref = $obj.attr('href');

        var productTitle = $obj.find('img').attr('alt').toLowerCase();
        var productControl = $('<li>', {
            'class': 'prod' + (++productCount),
            html: '<a href="' + productHref + '">' + productTitle + '</a>',
            mouseenter: function () {
                _fnTestAnimate(index);
            },
            mouseleave: function () {
                _sliderPause = false;
                _fnStartSlider();
            }
        });
        productList.append(productControl);
    });
    $('#productsPreview > div').append(productList);
    var $controls = $('#productsPreview li');

    var $slides = $('.flexslider li');
    /*$('.flexslider').css({ 'max-height': 539, 'max-width': 824 });*/
    $slides.css({ 'opacity': 0, 'display': 'none' });

    // first show
    $slides.eq(0).css({ 'opacity': 1, 'display': 'block' });
    $controls.eq(0).addClass('active');

    var _slideTimeout = 2000;
    var _slideDuration = 600;

    var _currentSlide = 0;
    var _totalSlide = $slides.length;

    _fnTestAnimate = function (index) {
        if (_sliderAnimate) {
            setTimeout(function () { _fnTestAnimate(index); }, 200);
        }
        else {
            _fnNextSlide(index);
            _sliderPause = true;
            clearTimeout(_sliderTimer);
        }
    }

    _fnNextSlide = function () {

        // se pausa o animazione in corso return
        if (_sliderPause || _sliderAnimate) {
            return;
        }

        // controllo se forzare la index
        var _forceIndex = false;
        if (arguments.length > 0) {
            _forceIndex = true;
        }

        // mark animate
        _sliderAnimate = true;

        // get next index
        var _nextSlide = _forceIndex ? arguments[0] : ((_currentSlide + 1) % _totalSlide);

        // animate slide
        $slides.eq(_currentSlide).clearQueue().animate({ opacity: 0 }, _slideDuration, function () {
            $(this).css({ 'display': 'none' });
        });
        $slides.eq(_nextSlide).clearQueue().css({ 'display': 'block' }).animate({ opacity: 1 }, _slideDuration, function () {
            _currentSlide = _nextSlide;
            _sliderAnimate = false;
        });

        // select active control
        $controls.eq(_currentSlide).removeClass('active');
        $controls.eq(_nextSlide).addClass('active');

        // repeat function
        if (!_forceIndex) {
            _sliderTimer = setTimeout(function () {
                _fnNextSlide();
            }, _slideTimeout);
        }

    }

    _fnStartSlider = function () {
        _sliderTimer = setTimeout(function () {
            _fnNextSlide();
        }, _slideTimeout);
    }

});

$(window).load(function () {

    _fnStartSlider();

});

