// Here we attach functions to resize text to the anchors on story detail pages.
$(document).ready(function () {
    $("a#txtResizeMinus").click(function () {
        txtResizeSmaller();
    });
    $("a#txtResizePlus").click(function () {
        txtResizeLarger();
    });

    $("#featured_assets").hover(

    function () {
        highlightMorePhotos(1);
    }, function () {
        highlightMorePhotos(0);
    });
    $("#more_photos").hover(

    function () {
        highlightMorePhotos(1);
    }, function () {
        highlightMorePhotos(0);
    });

    setupCycleSlideShow();
});

// This function simply fades the backgound color of the More Photos div
//   in and out depending on the action passed, 1 == fade color in, 0 == out


function highlightMorePhotos(act) {
    if (act == 1) {
        $("#morePhotos").css("background-color", "#FFFFCC");
        $("#morePhotos").css("border", "1px dashed #E0E0E0");
    }
    if (act == 0) {
        $("#morePhotos").css("background-color", "white");
        $("#morePhotos").css("border", "none");
    }
}

function txtResizeSmaller() {
    var currentTxtSize = $("#story_text_top > p").css("font-size");
    var newTxtSize = currentTxtSize.split("px");

    var currentTxtLineHeight = $("#story_text_top > p").css("line-height");
    var newTxtLineHeight = currentTxtLineHeight.split("px");

    if (parseInt(newTxtSize[0]) <= 8) {
        $("img#txtResizeMinus").css("display", "none");
        $("img#txtResizeMinusFade").css("display", "inline");
    } else {
        currentTxtSize = parseInt(newTxtSize[0]) - 2;
        currentTxtLineHeight = parseFloat(newTxtLineHeight[0]) - 2;
        if (parseInt(currentTxtSize) <= 8) {
            $("img#txtResizeMinus").css("display", "none");
            $("img#txtResizeMinusFade").css("display", "inline");
        }

        currentTxtSize = currentTxtSize + "px";
        currentTxtLineHeight = currentTxtLineHeight + "px";
    }

    $("img#txtResizePlus").css("display", "inline");
    $("img#txtResizePlusFade").css("display", "none");

    $("#story_text_top > p").css("font-size", currentTxtSize);
    $("#story_text_top > p").css("line-height", currentTxtLineHeight);
    $("#story_text_remaining > p").css("font-size", currentTxtSize);
    $("#story_text_remaining > p").css("line-height", currentTxtLineHeight);
}

function txtResizeLarger() {
    var currentTxtSize = $("#story_text_top > p").css("font-size");
    var newTxtSize = currentTxtSize.split("px");

    var currentTxtLineHeight = $("#story_text_top > p").css("line-height");
    var newTxtLineHeight = currentTxtLineHeight.split("px");

    if (parseInt(newTxtSize[0]) >= 24) {
        $("img#txtResizePlus").css("display", "none");
        $("img#txtResizePlusFade").css("display", "inline");
    } else {
        currentTxtSize = parseInt(newTxtSize[0]) + 2;
        currentTxtLineHeight = parseFloat(newTxtLineHeight[0]) + 2;
        if (parseInt(currentTxtSize) >= 24) {
            $("img#txtResizePlus").css("display", "none");
            $("img#txtResizePlusFade").css("display", "inline");
        }

        currentTxtSize = currentTxtSize + "px";
        currentTxtLineHeight = currentTxtLineHeight + "px";
    }

    $("img#txtResizeMinus").css("display", "inline");
    $("img#txtResizeMinusFade").css("display", "none");

    $("#story_text_top > p").css("font-size", currentTxtSize);
    $("#story_text_top > p").css("line-height", currentTxtLineHeight);
    $("#story_text_remaining > p").css("font-size", currentTxtSize);
    $("#story_text_remaining > p").css("line-height", currentTxtLineHeight);
}




// This function sets up the way images cycle on the story detail pages.


function setupCycleSlideShow() {
    var imageCount = $('#cycleSlides div.slide').length;

    if (imageCount == 0) return;
    else if (imageCount == 1) {
        var h = $('#cycleSlides div.slide').height();
        $('#cycleSlides').height(h);
    } else {

        $("a#morePhotos").toggle(

        function () {
            $("a#morePhotos").html("CLICK TO HIDE PHOTOS");
            $("ul#cycleNav").slideDown();
        }, function () {
            $("a#morePhotos").html("CLICK FOR MORE PHOTOS");
            $("ul#cycleNav").slideUp();
        });

        $('#cycleSlideShow').hover(

        function () {
            $('#cycleControls').fadeIn();
        }, function () {
            $('#cycleControls').fadeOut();
        });

        $('#cycleSlides').cycle({
            fx: 'fade',
            speed: 500,
            timeout: 10000,
            after: resizeCycleSlideShow,
            next: "#cycleNext",
            prev: "#cyclePrev",
            pager: "#cycleNav",
            pagerAnchorBuilder: function (idx, slide) {
                return '#cycleNav li:eq(' + (idx) + ') a';
            }
        });

        $('#cycleSlides').cycle('pause');
    }
}

function resizeCycleSlideShow() {
    // This resizes the image container after the image has changed.
    var h = $(this).height();
    $('#cycleSlides').height(h);

    // set the width of the panel to the width of the image.
    //var w = $(this).find ('img').width ();
    //$(this).width (w);
}

// This function converts all our anchors with the .media class to be embedded instead of
//   just links.  Currently this is not really needed, but I abstracted it thinking that
//   down the road affiliates may want to customize this functionality.


function setupInlineMedia() {
    $('.media').media();
}


