var cursorImagenes=2;
var scrollHorizontalFlag=true;
var scrollVerticalFlag=true;

function updateContador(){
    $('#imgActual').text(cursorImagenes-1);
    $('#totalImg').text($('.activeProject .project').length);
}

function centerFlechas(){
    $('#previousProject').width($('#previousProject img').width()*5);
    $('#nextProject').width($('#nextProject img').width()*5);
    $('#previousProject').height($('#previousProject img').height()*5);
    $('#nextProject').height($('#nextProject img').height()*5);
    $('#previousPicture').width($('#previousPicture img').width()*5);  
    $('#nextPicture').width($('#nextPicture img').width()*5);  
    $('#previousPicture').height($('#previousPicture img').height()*5);
    $('#nextPicture').height($('#nextPicture img').height()*5);
    $('#previousProject img').css('left',$('#previousProject').width()/2-$('#previousProject img').width()/2);
    $('#nextProject img').css('left',$('#nextProject').width()/2-$('#nextProject img').width()/2);
    $('#previousPicture img').css('top',$('#previousPicture').height()/2-$('#previousPicture img').height()/2);
    $('#nextPicture img').css('top',$('#nextPicture').height()/2-$('#nextPicture img').height()/2);
    $('#previousProject').css('left',$(window).width()/2-$('#previousProject').width()/2-3);
    $('#nextProject').css('left',$(window).width()/2-$('#nextProject').width()/2-3);
    $('#previousPicture').css('top',$(window).height()/2-$('#previousPicture').height()/2-3);
    $('#nextPicture').css('top',$(window).height()/2-$('#nextPicture').height()/2-3);
}

function formatImage(start){
    $('.activeProject .projectImage').each(function(){
        if($(this).filter('iframe').length){
            var condicion=location.href.indexOf('home')>-1||location.href.indexOf('masters')>-1||location.href=='http://eugeniquitllet.com/'||location.href=='http://eugeniquitllet.com/'||location.href=='http://www.eugeniquitllet.com'||location.href=='http://www.eugeniquitllet.com/';
            if(start){
                $(this).css('width','auto');
                $(this).css('height','auto');
                $(this).width(265);
            }
            var heightIframe=$(this).height();
            var widthIframe=$(this).width();
            var ratioIframe=heightIframe/widthIframe;
            $(this).height($(this).parents('.imageContainer').height());
            var difIframe=$(this).height()/heightIframe;
            var widthFinalIframe=$(this).width()*difIframe;
            $(this).width(widthFinalIframe);
        }else{
            $(this).height($(this).parents('.imageContainer').height());
            $(this).parent().append('<div style="position:absolute;top:0;left:0;right:0;bottom:0;z-index:3;"></div>');
        }
        $(this).css('margin-top',$(this).parents('.imageContainer').height()/2-$(this).height()/2);
    });
}

function fadeOutText(data){
    var contador=$('#contador');
    $('.clientContainer').fadeOut(500,function(){
        $('.clientContainer').html(data.client);
        $('.clientContainer').fadeIn(1500);
    });
    $('.projectDescription').fadeOut(500,function(){
        $('.projectDescription').html(data.description);
        $('.projectDescription').append(contador);
        $('.projectDescription').fadeIn(1500);
        updateContador();
    });
}

function fadeInText(){
    $('.clientContainer').fadeIn(3000);
    $('.projectDescription').fadeIn(3000,function(){
        $('#logoPie').mouseleave();
    });
}

function dimensions(start){
    $('.project').width($(window).width());
    $('.project').height($(window).height());
    if($(window).width()*$('.activeProject .projectImage').length>$('#projectViewer').width()){
        $('#projectViewer').width($(window).width()*$('.activeProject .projectImage').length);
    }
    $('#projectViewer').height($(window).height()*$('.singleProjectWrapper').length);
    $('.singleProjectWrapper').height($(window).height());
    $('.clientContainer').css('left',$(window).width()/2-$('.clientContainer').width()/2);
    formatImage(start);
    $('.activeProject .imageContainer').each(function(){
        $(this).css('left',$('.project').width()/2-$(this).width()/2);       
    });
    $('#projectWrapper').css('visibility','visible');
}

function checkFlechas(){
    if(cursorImagenes==2){
        $('#previousPicture').hide(500);
    }else{
        $('#previousPicture').show(500);
    }
    if(cursorImagenes==$('.activeProject .projectImage').length+1){
        $('#nextPicture').hide(500);
    }else{
        $('#nextPicture').show(500);
    }
    if(punteroPosts==0){
        $('#previousProject').hide(500);
    }else{
        $('#previousProject').show(500);
    }
    if(punteroPosts==postsArray.length-1){
        $('#nextProject').hide(500);
    }else{
        $('#nextProject').show(500);
    }
    centerFlechas();
}

function getNewProject(up){
    $('#logoPie').mouseenter();
    scrollVerticalFlag=false;
    $.post('/post-controller',{
        postId:postsArray[punteroPosts]
    },function(data){
        $('.activeProject').removeClass('activeProject');
        if(!downloadPosts[punteroPosts]){
            if(up){
                $('#projectViewer').prepend(data.project);
            }else{
                $('#projectViewer').append(data.project);
            }
            downloadPosts[punteroPosts]=true;
        }else{
            $('#singleProject'+data.idPost).addClass('activeProject');
        }
        var elem=$('#singleProject'+data.idPost);
        $(elem).find('.imageContainer').first().imagesLoaded(function(){
            dimensions(true);
            fadeOutText(data);
            cursorImagenes=2;
            elem.css('visibility','hidden');
            $('#projectWrapper').scrollTo(elem,{
                axis:'y',
                duration:1000,
                onAfter:function(){
                    $('#projectWrapper').scrollTo($('.activeProject #project2'),{
                        axis:'x',
                        duration:1,
                        onAfter:function(){
                            elem.css('visibility','visible');
                            $('#logoPie').mouseleave();
                            history.pushState(null,'','/projects/'+data.slug);
                            updateContador();
                        }
                    });
                    checkFlechas();
                    scrollVerticalFlag=true;
                }
            });
        });
    },'json');
}

function fakeVerticalScroll(){
    var elem=$('.activeProject');
    $('#projectWrapper').scrollTo(elem,{
        axis:'y',
        duration:0,
        onAfter:function(){
            $('#projectWrapper').scrollTo($('.activeProject #project'+cursorImagenes),{
                axis:'x',
                duration:0
            });
            scrollVerticalFlag=true;
        }
    });
}

function events(){
    $('body').on('click','#previousPicture',function(){
        if(cursorImagenes==2||!scrollHorizontalFlag)
            return;
        scrollHorizontalFlag=false;
        cursorImagenes--;
        var elem=$('.activeProject #project'+cursorImagenes);
        updateContador();
        $('#projectWrapper').scrollTo(elem,{
            axis:'x',
            duration:1000,
            onAfter:function(){
                checkFlechas();
                scrollHorizontalFlag=true;
            }
        });
    });
    $('body').on('click','#nextPicture',function(){
        if(cursorImagenes==$('.activeProject .projectImage').length+1||!scrollHorizontalFlag)
            return;
        scrollHorizontalFlag=false;
        cursorImagenes++;
        var elem=$('.activeProject #project'+cursorImagenes);
        updateContador();
        $('#projectWrapper').scrollTo(elem,{
            axis:'x',
            duration:1000,
            onAfter:function(){
                checkFlechas();
                scrollHorizontalFlag=true;
            }
        });
    });
    $('body').on('click','#previousProject',function(){
        if(punteroPosts==0||!scrollVerticalFlag)
            return;
        punteroPosts--;
        getNewProject();
    });
    $('body').on('click','#nextProject',function(){
        if(punteroPosts==postsArray.length-1||!scrollVerticalFlag)
            return
        punteroPosts++;
        getNewProject();
    });
    $('body').hammer().bind('swipe drag',function(event){
        event.preventDefault();
        if(event.direction=='left'){
            $('#nextPicture').click();
        }else if(event.direction=='right'){
            $('#previousPicture').click();
        }else if(event.direction=='down'){
            $('#previousProject').click();
        }else if(event.direction=='up'){
            $('#nextProject').click();
        }
    });
    $('.projectDescription').hammer().bind('swipe drag',function(event){
        event.stopPropagation();
    });
    $(window).keydown(function(event){
        if(event.which==37){
            $('#previousPicture').click();
        }else if(event.which==39){
            $('#nextPicture').click();
        }else if(event.which==38){
            $('#previousProject').click();
        }else if(event.which==40){
            $('#nextProject').click();
        }
    });
    $(window).bind('resize',function(){
        centerFlechas();
        dimensions(true);
        fakeVerticalScroll();
    });
    $('.flecha').hover(function(){
        $(this).animate({
            opacity:1
        },400);
    },function(){
        $(this).animate({
            opacity:0.35 
        },400);
    });
}

$(document).ready(function(){
    if($('body').attr('id')!='news'){
        $('#projectWrapper').scrollTo($('.activeProject #project2'),{
            axis:'x',
            duration:1
        });
    }
    $('#content').imagesLoaded(function(){
        dimensions(true);
        updateContador();
        fadeInText();
    });
    checkFlechas();
    events();
});	