jQuery.fn.styleCode = function () {
    return this.each(function () {
        var c = jQuery(this).find(".entry pre");
        c.data("isStyled") != true && c.wrap('<div class="codebg"><div class="codetop"></div></div>').data("isStyled", true)
    })
};
jQuery.fn.collapse = function () {
    return this.each(function () {
        jQuery(this).stop().animate({
            opacity: 0,
            width: 0,
            marginRight: 0,
            padding: 0
        }, 350)
    })
};
jQuery.fn.expand = function (c) {
    return this.each(function () {
        jQuery(this).stop().animate({
            opacity: 1,
            width: c,
            marginRight: "18px",
            padding: "0 1px"
        }, 350)
    })
};
$(function () {
    function c() {
        $("#profile a").parent().parent().addClass("profileactive");
        $("#fullscreen").css("display", "block").animate({
            opacity: 0.5
        }, 200);
        $("#profileview").slideDown(550, function () {
            $(this).find(".closeprofile").fadeIn(200)
        });
        $("#header").animate({
            marginTop: "320px"
        }, 550)
    }
    function f(a) {
        if (a < 1) a = i;
        else if (a > i) a = 1;
        $("#focusarea").find("li:visible").fadeOut("slow").end().find("li:nth-child(" + a + ")").fadeIn("slow");
        $markers.find("a.active").removeClass("active").end().find("li:nth-child(" + a + ") a").addClass("active")
    }
    function j() {
        var a = 2 + $markers.find("a.active").parent().index();
        f(a);
        g = setTimeout(j, k)
    }
    function l(a) {
        a = a.toLowerCase();
        a == "everything" ? $("#thumbnails li").expand(m) : $("#thumbnails li").not("." + a).collapse().end().filter("." + a).expand(m)
    }
    function n() {
        $("#content .tag-post").each(function () {
            var a = $(this).offset().top - 10,
                b = $(this).outerHeight(),
                d = $(window).scrollTop(),
                p = $(this).find(".postInfo").outerHeight(true),
                q = $(this).find(".postInfo").offset().left;
            if (a - d < 0 && a - d > -b && a - d < 10 + p - b) $(this).find(".postInfo").data("fixed", "false").css({
                position: "absolute",
                right: "0px",
                left: "auto",
                bottom: "10px",
                top: "auto"
            });
            else a - d < 0 && a - d > -b ? $(this).find(".postInfo").data("fixed", "true").css({
                position: "fixed",
                left: q + "px",
                bottom: "auto",
                top: "10px"
            }) : $(this).find(".postInfo").data("fixed", "false").css({
                position: "absolute",
                right: "0px",
                left: "auto",
                bottom: "auto",
                top: "0px"
            });
            if ($(this).find(".postInfo").data("fixed") == "true") {
                a = $(this).offset().left + $(this).find(".entry").outerWidth(false) - $(window).scrollLeft();
                $(this).find(".postInfo").css({
                    left: a + o + "px"
                });
                console.log($(window).scrollLeft() + " and " + a)
            }
        })
    }
    var r = [];
    $.preLoadImages = function () {
        for (var a = arguments.length; a--;) {
            var b = document.createElement("img");
            b.src = arguments[a];
            r.push(b)
        }
    };
    $.preLoadImages("../img/photo.jpg");
    $("#contact a").mouseenter(function () {
        $("#contactinfo").text($(this).text()).fadeIn("fast");
        $(this).stop().parent().stop().animate({
            paddingTop: "7px"
        }, 150).end().animate({
            opacity: 1
        }, 100)
    }).mouseleave(function () {
        $(this).stop().parent().stop().animate({
            paddingTop: "10px"
        }, 150).end().animate({
            opacity: 0.5
        }, 100)
    });
    $("#contact").mouseleave(function () {
        $("#contactinfo").fadeOut("fast")
    });
    $("#fullscreen").css("opacity", 0);
    $("#profile a").click(function (a) {
        a.preventDefault();
        c()
    });
    $("#sprofile").click(function (a) {
        a.preventDefault();
        $("html,body").animate({
            scrollTop: 0
        }, 550);
        c()
    });
    $("#fullscreen, #profileview .closeprofile").click(function (a) {
        a.preventDefault();
        $("#menu").removeClass("profileactive");
        $("#fullscreen").delay(200).fadeOut("slow");
        $("#profileview").slideUp(550).find(".closeprofile").fadeOut(200);
        $("#header").animate({
            marginTop: "0px"
        }, 550)
    });
    $("#focusarea").find("li").hide().end().find("li:first").show();
    $markers = $("#feature ul.markers");
    var i = $("#focusarea").children().length,
        k = 7E3,
        g = setTimeout(j, k);
    $("#focusnext, #focusprev").click(function (a) {
        a.preventDefault();
        clearTimeout(g);
        a = 2 * $(this).hasClass("nextslide") + $markers.find("a.active").parent().index();
        f(a)
    });
    $markers.delegate("a", "click", function (a) {
        a.preventDefault();
        clearTimeout(g);
        f($(this).parent().index() + 1)
    });
    $("#thumbnails li").mouseenter(function () {
        $(this).find("img").stop().animate({
            opacity: 0.8
        }, 300).end().find(".viewcasestudy").fadeIn("fast")
    }).mouseleave(function () {
        $(this).find("img").stop().animate({
            opacity: 1
        }, 150).end().find(".viewcasestudy").fadeOut("fast")
    });
    $("#filter a").click(function (a) {
        a.preventDefault();
        $(this).parent().parent().find("li a").removeClass("active").end().end().end().addClass("active");
        window.location.hash = "/" + $(this).text().replace(" ", "-");
        l($(this).text().replace(" ", "-"))
    });
    var m = $("#thumbnails li:first").width(),
        e = window.location.hash;
    if (e != "") {
        e = e.split("/");
        l(e[1]);
        $("#filter").find(".active").removeClass("active").end().find(".f" + e[1].toLowerCase()).addClass("active")
    }
    $("#content").styleCode();
    $("div.tag-video").click(function () {
        $(this).find("span.date").fadeOut("slow")
    });
    n();
    $(window).scroll(function () {
        n()
    });
    var o = 16;
    $(window).resize(function () {
        $("#content .tag-post").each(function () {
            if ($(this).find(".postInfo").data("fixed") == "true") {
                var a = $(this).offset().left + $(this).find(".entry").outerWidth(false) - $(window).scrollLeft();
                $(this).find(".postInfo").css({
                    left: a + o + "px"
                });
                console.log($(window).scrollLeft() + " and " + a)
            }
        })
    });
    $("#content").delegate("div.tag-post div.postInfo .shortURL", "click", function () {
        if ($.browser.msie) {
            var a = document.body.createTextRange();
            a.moveToElementText(this);
            a.select()
        } else if ($.browser.mozilla || $.browser.opera) {
            var b = window.getSelection();
            a = document.createRange();
            a.selectNodeContents(this);
            b.removeAllRanges();
            b.addRange(a)
        } else if ($.browser.webkit) {
            b = window.getSelection();
            b.setBaseAndExtent(this, 0, this, 1)
        }
    });
});
