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

///
function postListItemOver(id){
    $(id+" .overlay").stop().animate({
        opacity: 0
    }, 150);
    $(id+" .info").stop().animate({
        top: 130
    }, 150, "easeInOutQuad");
}

function postListItemOut(id){
    $(id+" .overlay").stop().animate({
        opacity: 1
    }, 200);
    $(id+" .info").stop().animate({
        top: 160
    }, 80, "easeInCirc");
}


function showGalleryPopup(postSlug){
    $("body").append("<div class='galleryBG' id='galleryOverlay' style='display:none'><a href='#' onclick='closeGalleryPopup()'>CLOSE</a></div>");
    var overlay = $("#galleryOverlay");
    overlay.height($(document).height());
    overlay.fadeIn();
    $.get('/gallery/'+postSlug, function(data){
        overlay.append(data);
        startSlideShow("#galleryOverlay #slides", "");
        $("#galleryOverlay #slides").css('backgroundColor','black');
    })
}

function closeGalleryPopup(){
    //$("#galleryOverlay").remove();
}

function startSlideShow(containerName, popupLink, showZoom){
    var container = $(containerName);
    container.css("cursor", "pointer")
    var tmpHtml = container.html();
    container.html("<div class='innerContainer'></div>");
    var innerContainer = $(containerName+" .innerContainer");
    oldInnerContainer = innerContainer;
    innerContainer.html(tmpHtml);
    var maxWidth = 0;
    //loop children
    innerContainer.children().each(function(i){
        maxWidth += $(this).width()+20;
        if(showZoom){
            $(this).append("<a class='zoom' href='"+popupLink+i+"/"+$(window).height()+"'><div class='zoomInner'>&nbsp;</div></a>");
            var zoom = $(this).find(".zoom");
            zoom.css("left", 0);
            zoom.css("top", 0);
            zoom.css("width", $(this).width());
            zoom.css("height", $(this).height())
            zoom.css("opacity",0);
            
            var zoomInner = zoom.find(".zoomInner");
            zoomInner.css("left", Math.round($(this).width()*.5-zoomInner.height()*.5));
            zoomInner.css("top", Math.round($(this).parent().parent().height()*.5-zoomInner.height()*.5));
        }
    });
    maxWidth += 50;
    innerContainer.width(maxWidth);
    
    if(container.width()>innerContainer.width()){
        container.find(".last").removeClass("last");
    }
    
    var moving = false;
    nextSlide = function(){
        if(moving)
            return;
        if(innerContainer.children().size()<2)
            return;
        if(container.width()>innerContainer.width())
            return;
        moving = true;
        var next = $(innerContainer.children()[0]);
        var curWidth = next.width()+20;
        var nextClone = next.clone();
        
        innerContainer.width(innerContainer.width()+curWidth);
        innerContainer.append(nextClone);
        if(popupLink != ""){
            //mouseOut(nextClone);
            //mouseOver($(innerContainer.children()[1]));
        }
        
        if(nextClone.hasClass("clickNextSlide")){
            //nextClone.click(function(){nextSlide()});
            if(popupLink != ""){
                nextClone.mouseover(function(){mouseOver($(this))});
                nextClone.mouseout(function(){mouseOut($(this))});
            }
        }
        
        innerContainer.animate({
            marginLeft: -curWidth
        }, 500, "easeOutSine", function(){//curWidth*.7
            next.remove();
            innerContainer.width(innerContainer.width()-curWidth);
            curMargin = 0;
            innerContainer.css("marginLeft", 0);
            moving = false;
        })
    };
    
    //add nextButton
    container.append("<a class='next')'>&nbsp;</a>");
    var nextBtn = container.find(".next");
    if(container.width()>=innerContainer.width()){
        nextBtn.hide();
    }else{
        nextBtn.css("opacity", 0);
    }
    nextBtn.click(function(){nextSlide();$(this).css("opacity", 1);});
    nextBtn.css("top",Math.round(container.height()*.5-nextBtn.height()*.5));
    nextBtn.mouseover(function(){
        if(container.width()<innerContainer.width()){
            $(this).stop();$(this).css("opacity",1);
        }
    });

    function positionNextBtn(){
        nextBtn.css("left",container.width()-nextBtn.width());
    }
    positionNextBtn();
    $(window).resize(function(){positionNextBtn()});
    
    mouseOver = function(el){
        if(container.width()<innerContainer.width()){
            nextBtn.stop().animate({
                opacity: 1
            }, 450);
        }
        if(showZoom){
            el.find(".zoom").stop().animate({
                opacity: 1
            }, 450);
        }
    }
        
    mouseOut = function(el){
        if(showZoom){
            el.find(".zoom").stop().animate({
                opacity: 0
            }, 250);
        }
        nextBtn.stop().animate({
            opacity: 0
        }, 250);
    }
    
    innerContainer.find(".clickNextSlide").each(function(){
        /*if($($(this).children()[0]).is('img'))
            $($(this).children()[0]).click(function(){nextSlide()});
        else    
            $(this).click(function(){nextSlide()});
        if(popupLink != ""){*/
            $(this).mouseover(function(){mouseOver($(this))});
            $(this).mouseout(function(){mouseOut($(this))});
        //}
    })
}

function checkSize(height, link, offset){
    if($(window).height()!=height)
        window.location = link+offset+"/"+$(window).height()
}
