function selectType($link){
    $type=$current_type=$link.attr("id");
    if($link.hasClass("sel")) return false;
    $("#main .inner").fadeOut(200, function(){
        $(".thumb").each(function($i){
            if($(this).hasClass($type) || $type=="all") $(this).show();
            else $(this).hide();
        });
    });
    $("#main .inner").fadeIn(200);
    if($link.hasClass("sel")) return;
    $link.addClass("sel").siblings().removeClass("sel");
}

function selectPic($pid, $from){
    if($from=="left") $start_pos="-620px";
    else $start_pos="100%";
    $("#big-photo").attr({
        src: $("#"+$pid).attr("href"),
        alt: $pid
    }).css("left", $start_pos).load(function(){
        $("#block").fadeIn(200);
        alignBigPic();
    });
    return false;
}


function alignBigPic(){
    $bodyh=$(window).height();
    $bodyw=$(document).width();
    $imgh=$("#big-photo").height();
    $imgw=$("#big-photo").width();
            
    $("#big-photo").stop(1, 0).animate({
        left: $bodyw/2-$imgw/2-10,
        top: $bodyh/2-$imgh/2-10
    }, 200);
    
    $("#arrow-left, #arrow-right").css({
        top: $bodyh/2-50
    });
}

function blockAlign($block){
    $bodyh=$(window).height();
    $bodyw=$(document).width();
    $bh=$block.innerHeight();
    $bw=$block.innerWidth();
            
    $block.stop(1, 0).animate({
        left: $bodyw/2-$bw/2,
        top: $bodyh/2-$bh/2
    }, 200);
}


$(document).ready(function(){
    $current_type="";
    selectType($("#people"));
    
    $("#big-photo").css("max-height", $(window).height()-50);
    
    $("#filter li").click(function(){
        selectType($(this));
    });
    
    $("#about").click(function(){
        $("#about-info").show();
        $("#block2").fadeIn(200);
        blockAlign($("#about-info"));
        return false;
    });
    
    $("#contact1, #contact2").click(function(){
        $("#contact-info").show();
        $("#block2").fadeIn(200);
        blockAlign($("#contact-info"));
        return false;
    });
    
    $(".close-button").click(function(){
        $(this).parent().hide();
        $("#block2").fadeOut(200);
    }).hover(function(){
        $(this).fadeTo(0, 0.6);
    }, function(){
        $(this).fadeTo(0, 1);
    });
    
    $(".thumb").addClass("all");
    
    //увеличение фоток
    $(".thumb").click(function(){
        selectPic($(this).attr("id"), "left");
        return false;
    });
    
    $("#close-popup").click(function(){
        $("#block").fadeOut(200);
        $(".thumb").removeClass("open");
    }).hover(function(){
        $(this).fadeTo(0, 0.6);
    }, function(){
        $(this).fadeTo(0, 1);
    });;
    
    //стрелки
    $("#arrow-left, #arrow-right").fadeTo(0, 0.5);
    $("#arrow-left, #arrow-right").hover(function(){
        $(this).stop(1, 1).fadeTo(100, 1);
    }, function(){
        $(this).stop(1, 1).fadeTo(100, 0.5);
    });
    
    //листалка
    $("#arrow-right").click(function(){
        $next=$("#"+$("#big-photo").attr("alt")).nextAll("a:visible");
        if(!$next.is(":last")) $next=$(".thumb:visible:first");
        $("#big-photo").stop(1, 1).animate({
            left: "100%"
        }, 200, "linear", function(){
            selectPic($next.attr("id"), "left");
        });
        return false;
    });
    
    $("#arrow-left").click(function(){
        $next=$("#"+$("#big-photo").attr("alt")).prevAll("a:visible");
        if(!$next.is(":first")) $next=$(".thumb:visible:last");
        $("#big-photo").stop(1, 1).animate({
            left: "-620px"
        }, 200, "linear", function(){
            selectPic($next.attr("id"), "right");
        });
        return false;
    });
    
    //при ресайзе окна
    $(window).resize(function(){
        if($("#block").is(":visible")) alignBigPic();
        if($("#about-info").is(":visible")) blockAlign($("#about-info"));
        $("#big-photo").css("max-height", $(window).height()-50);
    });
    
    
    
    //admin
    $(".photo-thumb img.preview").click(function(){
        if($(this).hasClass("sel")) {
            $(".popup-form").fadeOut(200);
            $("br.temp").remove();
            $("img").removeClass("sel");
            return false;
        }
        $("img").removeClass("sel");
        $(this).addClass("sel");
        $("br.temp").remove();
        $(".popup-form").stop(1, 1).fadeOut(200);
        $(this).parents(".photo-thumb").before("<br class='cl temp' />").after("<br class='cl temp' />").find(".popup-form").stop(1, 1).fadeIn(200);
        return false;
    })
    
    $("#show-form").click(function(){
        $f=$("#add-form");
        if($f.is(":visible")) {
            $f.stop(1, 1).fadeOut(200);
            $(this).text("Добавить фотку");
        } else {
            $f.stop(1, 1).fadeIn(200);
            $(this).text("Скрыть форму");
        }
    });
});
