﻿$(document).ready(function() {

    var blogCommentsOpen = false;

    // TO MAKE NON-JS VERSION
    $("#rightcolmenu a").attr("href", "javascript:void(0);");
    $("#rightcolmenu li").removeClass("selected");

    // NEWSLETTER BUTTON TEXT
    $(".newsletterBtn").attr("value", "");

    // RIGHT COLUMN SWITCH
    var currentVis = $("#rightcolmenu ul li:first").text();
    $("#rightcol .rightcolwrapper").hide();
    $("#rightcol .rightcolwrapper:first").show();
    $("#rightcolmenu ul li:first").addClass("selected");
    var animating = false;

    $("#rightcolmenu a").click(function() {
        if (animating == false) {
            if ($(this).text() != currentVis) {
                animating = true;
                currentVis = $(this).text();
                $("#rightcolmenu li").removeClass("selected");
                $(this).parent().addClass("selected");

                var rightcol = "#rightcol_" + $(this).text()

                $(".rightcolwrapper").fadeOut(1000, function() {
                    $(rightcol).animate({
                        opacity: 1
                    }, 500, function() {
                        $(rightcol).fadeIn(1000, function() {
                            animating = false;
                        });
                    });
                });
            }
        }
    });

    // INPUT BOXES
    $("input[type=text], textarea").focus(function() {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
            $(this).css("color", "#616161");
        }
    });

    $("input[type=text], textarea").blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
            $(this).css("color", "#cccccc");
        }
    });

    // SHARE ICONS
    $("#share a").hover(function() {
        $("#share h2 span").text("on " + $(this).find("img").attr("title"));
    },
    function() {
        $("#share h2 span").text("");
    });

    // HOVER MENUS
    // remove alt and title attributes
    $(".mainitem .image img").removeAttr("alt").removeAttr("title");
    $(".mainitem .image").hoverIntent(function() {
        var $featureHoverMenu = $(this).find(".featurehovermenu");
        var $topOffset = $(this).height() - ($featureHoverMenu.height() + parseInt($featureHoverMenu.css("padding-top")) + parseInt($featureHoverMenu.css("padding-bottom")));
        $(this).find(".featurehovermenu").animate({ top: $topOffset }, { queue: false, duration: 160 });
    },
    function() {
        $(this).find(".featurehovermenu").animate({ top: $(this).height() }, { queue: false, duration: 160 });
    });


    $(".smallitem .image img").removeAttr("alt").removeAttr("title");
    $(".smallitem .image").hoverIntent(function() {
        $(this).find(".teaserhovermenu").fadeIn();
    },
    function() {
        $(this).find(".teaserhovermenu").fadeOut();
    });


    // COMMENTS SCROLLING

    $(".commentsDown").click(function() {
        $(".scrollableComments").scrollTo('+=150px', '0', 800);
    });
    $(".commentsUp").click(function() {
        $(".scrollableComments").scrollTo('-=150px', '0', 800);
    });


    // PAGE SCROLLING
    if ($('#pagenumberContainer').length > 0) {
        $('#pagenumberContainer').serialScroll({
            items: '#allPageNumbers div',
            prev: '.prevarrow a',
            next: '.nextarrow a',
            offset: 0, //when scrolling to photo, stop 230 before reaching it (from the left)
            start: 0, //as we are centering it, start at the 2nd
            duration: 400,
            force: true,
            stop: true,
            lock: false,
            cycle: false,
            onAfter: function(elem) {
                if (elem.innerHTML == $("#allPageNumbers").children().size()) {
                    $(".nextarrow a img").attr("src", "/images/arrow_right_disabled.gif");
                } else {
                    $(".nextarrow a img").attr("src", "/images/arrow_right_enabled.gif");
                }
                if (elem.innerHTML == 1) {
                    $(".prevarrow a img").attr("src", "/images/arrow_left_disabled.gif");
                } else {
                    $(".prevarrow a img").attr("src", "/images/arrow_left_enabled.gif");
                }

            }
        });
    }
    if ($('#categoryTeaserArea').length > 0) {
        $('#categoryTeaserArea').serialScroll({
            items: '#allTeasers .teasercol',
            prev: '.prevarrow a',
            next: '.nextarrow a',
            offset: 0,
            start: 0, //as we are centering it, start at the 2nd
            duration: 400,
            force: false,
            stop: true,
            lock: false,
            cycle: false,
            step: 2

        });
    }


    // PROJECT IMAGES

    var totalPhotos = $(".hiddenPhotoData .lightbox").length;
    var currentindex = parseFloat($(".currentPhotoId").html());
    var titles = new Array();
    var paths = new Array();
    var counter = 0;
    $(".hiddenPhotoData .lightbox").each(function() {
        titles[counter] = $(this).find("img").attr("title");
        paths[counter] = $(this).attr("href");
        counter = counter + 1;
    });
    $(".hiddenPhotoData .lightbox:eq(" + currentindex + ")").removeClass("lightbox");

    $(".nextButton").click(function() {
        if (currentindex + 1 < totalPhotos) {
            currentindex = parseFloat(currentindex) + 1;
        } else {
            currentindex = "0";
        }

        changePhoto(currentindex);

    });

    $(".prevButton").click(function() {
        if (parseFloat(currentindex) == 0) {
            currentindex = totalPhotos - 1;
        } else {
            currentindex = currentindex - 1;
        }

        changePhoto(currentindex);

    });

    function changePhoto(i) {

        $(".currentPhotoId").html(i);
        $(".mainitem .image .description h2").text(titles[parseFloat(i)]);
        $(".mainImage").attr("src", paths[parseFloat(i)].replace("fullsize", "inpage"));
        $(".featurehovermenu .zoom a").attr("href", paths[parseFloat(i)]);
        $(".hiddenPhotoData a").addClass("lightbox");
        $(".hiddenPhotoData .lightbox:eq(" + i + ")").removeClass("lightbox");
        $(".lightbox").lightBox();
    }


    // LIGHTBOXES
    if ($(".lightbox").length > 0) {
        $(".lightbox").lightBox();
    }

    // IE6 WARNING
    if ($.browser.msie == true && $.browser.version == "6.0") {
        var warningBox = $("<div/>").attr("id", "ie6Warning").addClass("popupBox");
        warningBox.load("ie6warning.html");
        warningBox.prependTo(document.body);
        var overlay = $("<div/>").attr("id", "overlay");
        var bodyHeight = document.body.offsetHeight;
        var bodyWidth = document.body.offsetWidth;
        var warningBoxHeight = warningBox.height();
        var warningBoxWidth = warningBox.width();

        warningBox.css({
            "top": (bodyHeight / 2) - (warningBoxHeight / 2),
            "left": (bodyWidth / 2) - (warningBoxWidth / 2)
        });
        overlay.css({
            "height": bodyHeight,
            "width": bodyWidth
        });

        overlay.prependTo(document.body);
    }

    // SMALL ITEM SHARE LINKS

    $(".shareLink").click(function() {

        var url = window.location.href;
        var urlparts = url.split('/');
        var host = urlparts[2];
        if ($(this).hasClass("mainItemShareLink")) {
            var url = "http://" + host + $(this).closest(".mainitem").find("h1 a").attr("href");
            var title = $(this).closest(".mainitem").find("h1 a").text();
        } else {
            var url = "http://" + host + $(this).closest(".teaserhovermenu").find(".overlay a").attr("href");
            var title = $(this).closest(".smallitem").find("h2 a").text();
        }
        var twitterstatus = (title + ":" + "+" + url).replace(/ /g, "+");
        $("#shareThisPopUp h1 span").text(title);
        popUp = $("#shareThisPopUp");
        overlay = $("#shareOverlay");
        var bodyHeight = document.body.offsetHeight;
        var cssHeight = bodyHeight
        if (document.documentElement.clientHeight > bodyHeight) {
            cssHeight = document.documentElement.clientHeight;
        }
        var bodyWidth = document.body.offsetWidth;
        var popUpHeight = popUp.height();
        var popUpWidth = popUp.width();

        // clear share icons
        $(".popUpShareIcons").html("");

        // reload share icons from template
        var iconsfile = "http://" + host + "/templates/item_share_icons.txt"

        $.get(iconsfile, function(data) {
            data = data.replace(/##url##/g, url);
            data = data.replace(/##twittersharestatus##/g, twitterstatus);
            data = data.replace(/##description##/g, title);
            data = data.replace(/a href/g, "a target=\"_blank\" href");
            $(".popUpShareIcons").html(data);
        });


        popUp.css({
            "top": (bodyHeight / 2) - (popUpHeight / 2),
            "left": (bodyWidth / 2) - (popUpWidth / 2)
        });
        overlay.css({
            "height": cssHeight,
            "width": bodyWidth,
            "opacity": "0.75"
        });

        overlay.fadeIn();
        popUp.fadeIn();

    });


    // EXTERNAL LINK HANDLING
    if ($("#blogcontent").length <= 0) {
        $("a").each(function() {
            if ($(this).attr("href").indexOf("http://") > -1 || $(this).attr("href").indexOf("https://") > -1) {
                $(this).attr("target", "_blank");
            }
        });
    }

    // BLOG COMMENTS
    $("#comments").hide();
    $(".commentsLink").click(function() {
        if (blogCommentsOpen == true) {
            $("#comments").slideUp(function() {
                adjustShadowHeight()
            });
            $(".commentsLink a").removeAttr("class");
            $(".commentsLink a").addClass("arrowDown");
            blogCommentsOpen = false;
        } else {
            $("#comments").slideDown("slow", function() {
                adjustShadowHeight()
            });
            $(".commentsLink a").removeAttr("class");
            $(".commentsLink a").addClass("arrowUp");
            blogCommentsOpen = true;
        }
    });

    // BLOG SHADOW HEIGHT
    if ($("#blogcontent").length > 0) {
        adjustShadowHeight();
        adjustColumnHeights();
    }

});

// POPUP CLOSE BUTTON   
function closeThis(id) {
    document.getElementById(id).style.display="none";
    if (document.getElementById("overlay")) {
        document.getElementById("overlay").style.display="none";
    }
    if (document.getElementById("shareOverlay")) {
        document.getElementById("shareOverlay").style.display="none";
         $.get("http://www.mathayward.com/templates/item_share_icons.txt", function(data) {
            $(".popUpShareIcons").html(data);
        });
    }
}

function adjustShadowHeight() {
    var shadowHeight = parseFloat($("#blogcontent").height()) + parseFloat($("#blogcontent").css("marginTop"));
    //var rightColHeight = parseFloat($("#blogcontent").height()) - parseFloat($("#blogrightcol").css("paddingBottom"));
    
    $("#leftshadow, #rightshadow").css("height", shadowHeight + "px");

    //$("#blogrightcol").css("height", rightColHeight + "px");
}

function adjustColumnHeights() {
    var leftColHeight = parseFloat($("#leftcol").height());
    var blogContentHeight = parseFloat($("#blogcontent").height()) + parseFloat($("#blogcontent").css("marginTop"));
    
    var rightColHeight = parseFloat($("#blogrightcol").height()) + parseFloat($("#blogrightcol").css("paddingBottom"));
    var maxHeight = Math.max(leftColHeight, blogContentHeight, rightColHeight);
    $("#blogcontent").css("height", maxHeight + "px");
    $("#blogrightcol").css("height", maxHeight - parseFloat($("#blogrightcol").css("paddingBottom")) + "px");
}