var posicionX;
var posicionY;

$(document).bind('mousemove', function(e){
    posicionX = e.pageX;
    posicionY = e.pageY
});

$(document).ready(function(){
    $.preloadCssImages();

    // ANIMACION DEL MENU
    $('ul#menu a').mouseenter(function(){
        if($(this).parent().hasClass('selected')){}
        else{$(this).find('span:eq(0)').animate({marginTop: '30px'}, 300);}
        });
    $('ul#menu a').mouseleave(function(){
        if($(this).parent().hasClass('selected')){}
        else{
            $(this).find('span:eq(0)').stop(true, false);
            $(this).find('span:eq(0)').animate({
                marginTop:'0px'
            },100);
        }
    });
    $('ul#menu a span').click(function(){
        $(this).parent().trigger('click');
    });
    

    //COLORES DEL SORTEO

    // ANIMACION DEL POPUP
    if($('div.popup-sorteo').length > 0){
        //animacion al entrar
        $('div.popup-sorteo').animate({left:'190px'},500,function(){
                $('div.popup-sorteo').animate({left:'170px'},200);
            })
        //boton cerrar
        $('div.popup-sorteo p.cerrar a').click(function(){
            $('div.popup-sorteo').hide();
        });
    }

    // ANIMACION DE QUIENES SOMOS
    if($('div.quienes-somos').length > 0){
        $('div.bg-negro').animate({right: '200px'},1000, function(){
            $('div.bg-negro').animate({right: '30px'},500);
        });
    }

    // PREGUNTAS FRECUENTES
    if($('div.faq').length > 0){
        $('div.bg-negro').animate({left: '20%'},1000, function(){
            $('div.bg-negro').animate({left: '25%'},500);
        });
        var timer;

        $('#scroll-top').mouseenter(function(){
            timer = setInterval(function(){
                if($('div.bg-negro h1').css('marginTop') == '0px'){
                         clearInterval(timer);
                         return;
                }else{
                    $('div.bg-negro h1').animate({marginTop:'+=5px'},0);
                }
            },35);
        });
        $('#scroll-top').mouseleave(function(){clearInterval(timer)});

        $('#scroll-bottom').mouseenter(function(){
            timer = setInterval(function(){
                if($('div.bg-negro h1').css('marginTop') == '-650px'){
                     clearInterval(timer);
                     return;
                    }
                $('div.bg-negro h1').animate({marginTop:'-=5px'},0)
            },35);
        });
        $('#scroll-bottom').mouseleave(function(){clearInterval(timer)});
    }

    //ANIMACION DE COTICE SUS MUEBLES
    if($('div.cotice').length > 0){
        $('div.bg-negro').animate({left: '0%'},500,function(){
            $('div.bg-negro form').fadeIn(1000);
        });
    }

    //ANIMACION DE CONTACTO
    if($('div.contacto').length > 0){
        $('div.distribuidor h2').fadeIn(750,function(){
            $('div.distribuidor ul').animate({left: '0%'},750,function(){
                $('div.puntos').fadeIn(750);
            });
        });
    }

    //ANIMACION NEWS
    if($('div.news').length > 0){
        $('div.bg-negro').animate({left: '0%'},1000, function(){$('div.bg-negro p').fadeIn(300);});
    }

    //ANIMACION INFO TECNICA
    if($('div.info').length > 0){
        $('ul.infomenu a:eq(0)').click(function(){animateInfo($(this), 180, 145);});
        $('ul.infomenu a:eq(1)').click(function(){animateInfo($(this), 205, 132);});
        $('ul.infomenu a:eq(2)').click(function(){animateInfo($(this), 265, 102);});
        $('ul.infomenu a:eq(3)').click(function(){animateInfo($(this), 205, 132);});
        $('ul.infomenu a:eq(4)').click(function(){animateInfo($(this), 350, 60);});
        $('ul.infomenu a:eq(5)').click(function(){animateInfo($(this), 180, 145);});
        
        $('ul.infomenu a:eq(0)').trigger('click');
        $('div.bg-negro').slideDown(500,function(){$('ul.infomenu').show()});
        $(window).bind('hashchange', function(){
                var hash = window.location.hash || '#caracteristicas';
                var link = $('a[href='+hash+']');
                if(link.length > 0)
                    link.trigger('click');
            });
            $(window).trigger("hashchange");
    }

    //ANIMACION PRODUCTOS
    if($('div.productos').length > 0){

        //THUMBS
            //on click de los thumbs
        $('div.thumbs a').click(function(event){
           var url = $(this).attr('href').substr(1);
           $('div.thumbs li').removeClass('selected');
           $(this).parent().addClass('selected');
           $('div.imagenes').empty().load(url+'.php');
        })

        $(window).bind('hashchange', function(){
                var hash = window.location.hash || '#mesadabanio';
                //console.info(hash);
                var link = $('a[href='+hash+']');
                if(link.length > 0)
                    link.trigger('click');
            });
            $(window).trigger("hashchange");

            //scrolling thumbs
        var scrollthumb;
        var li = $('div.thumbs li:eq(0)');
        $('div.thumbs ul').mouseenter(function(e){
            // TOP ENTRE 128 Y 200
            // BOTTOM ENTRE 425 y 496
            scrollthumb = setInterval(function(){
                $('div.thumbs ul li:eq(0)');
                if(posicionY > 128 && posicionY < 200){
                    if($(li).css('marginTop') != '0px')
                    $(li).animate({marginTop:'+=10px'},80);
                }else if(posicionY > 425 && posicionY < 496){
                    if($(li).css('marginTop') != '-110px')
                    $(li).animate({marginTop:'-=10px'},80);
                }else{
                    
                }
            },100);
        });

        $('div.thumbs ul').mouseleave(function(e){
            clearInterval(scrollthumb);
        });

        //IMAGENES
        var scrollImagenes;
        $('div.imagenes').mouseenter(function(e){
            //posicionX  //imagen de 703px de ancho
            scrollImagenes = setInterval(function(){
                var ul = $('div.imagenes ul');
                var offset = $('div.imagenes').offset().left;
                var distancia = posicionX - offset;
                $('div.thumbs ul li:eq(0)');
                var margen = parseInt($(ul).css('marginLeft'));
                var tope = $('div.imagenes img').length - 1;
                tope = tope * 703 * (-1);

                if(distancia > 0 && distancia < 130){
                    if(margen < 0)
                    $(ul).animate({marginLeft:'+=30px'},80);
                }else if(distancia > 573 && distancia < 703){
                    if(margen > tope)
                    $(ul).animate({marginLeft:'-=30px'},80);
                }else{

                }
            },100);
        });
        //MOUSE LEAVE DE IMAGENES
        $('div.imagenes').mouseleave(function(e){
            clearInterval(scrollImagenes);
        });
    }
});

function thumbScroll(event){
    
}

function animateInfo(boton, alto, posicion){
    var padre = $(boton).parent();
    var indice = $(padre).index();
    if($(padre).hasClass('selected')) return;

    $(padre).parent().find('li').removeClass('selected');
    $(padre).addClass('selected');

    $('ul.desarrollo > li').removeClass('selected').hide();
    $('ul.desarrollo > li:eq('+indice+')').addClass('selected').show();

    $('ul.desarrollo').animate({height: alto+'px'},500);
    $('div.bg-negro').animate({top: posicion+'px'},500);
}
