var homeSelected = 1;
var homeClicked = false;

function home_highlights(call, block) {
    var css_path = 'div.banners div.window';
    var interval = 10; // Segundos para cambiar el destacado
    var part = 984; // Pixeles de ancho de cada uno
    var total = $(css_path + ' div.slider').children('div.banner').length; // Numero de destacados
    var position = $(css_path).scrollLeft(); // Posicion horizontal
    var scrollTo = 0; // Posicion a la que haremos scroll
    var speed = 750; // Velocidad del scroll (ms)

    if (total == 1) {
        $('div.left_arrow').css('display', 'none')
        $('div.right_arrow').css('display', 'none')
        $('div.pages').css('display', 'none')
    } else {
        if (call == true) {
            // Primera llamada
            $('body.e1 div.pages div.page1').css('padding-right', '10px');
            $('div.left_arrow').css('display', 'none')
            setTimeout("home_highlights()", interval * 1000);
            return false
        }
    }

    if (total > 0) {
        if (block) {
            //console.log('block')
            if (block < 0) {
                block = total;
            }
            if (block > total) {
                block = 1;
            }
            scrollTo = (984 * (block - 1))
            homeSelected = (Math.round(scrollTo / part)) + 1;
            homeClicked = true;
            $(css_path).animate({scrollLeft: scrollTo}, speed);
        } else {
            if (position == ((Math.round(total * part)) - part)) {
                // Final, volvemos a 0
                //console.log('back to 0')
            } else {
                scrollTo = position + part;
                //console.log('going to... ' + scrollTo)
            }
            if (homeClicked == false) {
                //console.log('homeclicked == false')
                homeSelected = (Math.round(scrollTo / part)) + 1;
                if (scrollTo == 0) {
                    $(css_path).attr('scrollLeft', scrollTo);
                } else {
                    $(css_path).animate({scrollLeft: scrollTo}, speed);
                }
                setTimeout("home_highlights()", interval * 1000);
            }
        }
    }

    if (homeSelected == 1) {
        $('div.left_arrow').css('display', 'none')
        $('div.right_arrow').css('display', 'block')
    } else if (homeSelected == total) {
        $('div.left_arrow').css('display', 'block')
        $('div.right_arrow').css('display', 'none')
    } else {
        $('div.left_arrow').css('display', 'block')
        $('div.right_arrow').css('display', 'block')
    }

    $('body.e1 div.pages div.page').animate({'padding-right': '0'});
    $('body.e1 div.pages div.page' + homeSelected).animate({'padding-right': '10px'});

    //console.log(homeSelected)
}

function contactFormOverlay() {
    return $('div.contact_overlay_background').css('height', ($('.width').height() - 130) + 'px')
}

last_selected = null;
video_selected = null;
function contactForm(t, th) {
    if (t == "open" && !$('div.contact_overlay').is(':visible')) {
        if ($('div.leeloominai_lekatariba_lamina-tchai_ekbat_de_sebat').is(':visible')) {
            catalogOverlay('close');
        }
        last_selected = $('div.menu ul li.selected')
        last_selected.removeClass('selected')
        $('div.menu ul li:nth-child(6)').addClass('selected')
        $('div.contact_overlay').show('slide', {direction: 'up'})
        if ($('body.e5').length > 0) {
            video_selected = $('.e5 .videos .video .v:visible')
            video_selected.hide()
        }
        contactFormOverlay().show('slide', {direction: 'up'})
    } else if ((t == "open" || t == "close") && $('div.contact_overlay').is(':visible')) {
        last_selected.addClass('selected')
        $('div.menu ul li:nth-child(6)').removeClass('selected')
        $('div.contact_overlay').hide('slide', {direction: 'up'})
        contactFormOverlay().hide('slide', {direction: 'up'})
        if ($('body.e5').length > 0) {
            video_selected.show()
        }
    }
    return false;
}

function catalog_overlay() {
    return $('div.catalog_overlay_background').css('height', ($('.width').height() - 130) + 'px')
}

function catalogOverlay(t, th) {
    if (t == "open" && !$('div.catalog_overlay').is(':visible')) {
        if ($('div.contact_overlay').is(':visible')) {
            contactForm('close');
        }

        last_selected = $('div.menu ul li.selected')
        last_selected.removeClass('selected')
        $('div.menu ul li:nth-child(4)').addClass('selected')
        $('div.catalog_overlay').show('slide', {direction: 'up'})
        if ($('body.e5').length > 0) {
            video_selected = $('.e5 .videos .video .v:visible')
            video_selected.hide()
        }
        catalog_overlay().show('slide', {direction: 'up'})
    } else if ((t == "open" || t == "close") && $('div.catalog_overlay').is(':visible')) {
        last_selected.addClass('selected')
        $('div.menu ul li:nth-child(4)').removeClass('selected')
        $('div.catalog_overlay').hide('slide', {direction: 'up'})
        catalog_overlay().hide('slide', {direction: 'up'})
        if ($('body.e5').length > 0) {
            video_selected.show()
        }
    }
    return false;
}

function brandVideo(eid) {
    $('.e5 .videos .video .v').hide()
    $('.e5 .videos .list .item').removeClass('selected')
    $('.e5 .videos .video .video' + eid).show()
    $('.e5 .videos .list .item' + eid).addClass('selected')
    return false;
}

function changeOdd() {
    $('table.references tbody tr').removeClass('odd')
    $('table.references tbody tr:visible:odd').addClass('odd');
}

$(document).ready(function() {
    jQuery.fn.exists = function() {
        return jQuery(this).length > 0;
    }

    if ($('.product_detail').is(':visible')) {
        $('.product_detail .images .list div.image:first').addClass('show')
        $('.product_detail select#images').change(function() {
            var tab = $('.product_detail select#images option:selected').attr('class')

            if (tab) {
                $('.product_detail .tabs .tab').removeClass('active')
                $('.tab#' + tab).addClass('active')
                $('.product_detail table.references tbody tr').not("." + tab).hide();
                $('.product_detail table.references tbody tr.' + tab).show();
            }

            $('.product_detail .images .list .image').removeClass('show')
            $('.product_detail .images .list div.' + this.value).addClass('show')
        })
    }

    if ($('body.e5').exists()) {
        $('.e5 .videos .list .item:first').addClass('selected')
    }

    $("a.fbox").fancybox({
        'speedIn'        :    600,
        'speedOut'        :    200,
        'overlayShow'    :    true
    });
})

