﻿window.onload = function() {

    $('#images_list li:eq(0)').css('display', 'block');
    $('#images_container').wrap('<div id="carousel_container_wrap"></div>');
    $('#images_list').cycle({ pager: '#carousel_pager' });
    //$('#images_list').cycle();

    var maxImgHeight = 0;
    $('#images_list li').each(function() {
        if ($(this).height() > maxImgHeight) {
            maxImgHeight = $(this).height();
        }
    });

    $('#images_container').height(maxImgHeight + 10);

    $('#images_list').prepend('<div id="controls"><a id="view_smaller" style="display: none;" href="#">Smaller</a><a id="view_larger" href="#">Larger</a></div>');

    if ($('#images_li li').size() > 1) {
        $('#images_list').hover(
            function() { $('#controls').fadeIn(); },
            function() { $('#controls').fadeOut(); }
        );
    }

    $('#view_larger').click(function() {
        //alert("L click");
        $('#view_larger').fadeOut(function() {
            $('#view_smaller').fadeIn();
        });

        $('#images_container').animate({ 'width': '575px' }, "normal", function() {
            //if ($('#images_list img').size() > 1) {
            $('#images_container').height($('#images_container').height() + 200);
            //}
        });

        $('#images_container img').each(function() {
            $(this).attr('src', 'Image.ashx?ab=true&id=' + $(this).getUrlParam("id") + "&w=570&h=470");
        });

        return false;
    });

    if ($('#images_list li').size() > 15) {
        $('#view_larger').click();
    }

    $('#view_smaller').click(function() {
        //alert("S click");
        $('#view_smaller').fadeOut(function() {
            $('#view_larger').fadeIn();
        });

        $('#images_container').animate({ 'width': '310px' }, "normal");
        //if ($('#images_list img').size() > 1) {
        $('#images_container').height($('#images_container').height() - 200);
        //}

        $('#images_container img').each(function() {
            $(this).attr('src', 'Image.ashx?ab=true&id=' + $(this).getUrlParam("id") + "&w=310&h=280");
        });

        return false;
    });

    $('#images_container img').click(function() {
        if ($('#images_container').width() > 500) {
            $('#view_smaller').click();
        }
        else {
            $('#view_larger').click();
        }
    });
};

$(function() {

    $('a[rel="external"]', '#sharethis').click(function() {
        window.open($(this).attr('href'));
        return false;
    });

    var currentId = $(window.location.href).getUrlParam("id");
    var authenticated_class = "not_authenticated";

    if ($('#wysiwyg').length > 0) {
        authenticated_class = "authenticated";
    }

    $('#iframe_box').append($('<iframe id="iframe" scrolling="no" frameborder="0" class=' + authenticated_class + ' src="Login.aspx?ReturnUrl=/pendulum/Story.aspx?id=' + currentId + '"></iframe>'));

    if (document.referrer + "&refresh=true" == document.location.href) {
        window.location.href = window.location.href + '#last_comment';
    }

    if ($("#wysiwyg").length > 0) // check if wysiwyg element is on page
    {
        $('#wysiwyg').text('');
        $('#wysiwyg').wysiwyg();

        $("#submit_comment").click(function() {
            $("#submit_comment").attr("disabled", "disabled");
            $("#submit_comment").after('<img id="loading" src="http://www.elon.edu/e-net/images/indicator.gif" alt="loading" />');
            $.post("user/comment.ashx", { comment_name: $('#comment_name').val(), comment: CleanHtml($('#wysiwyg').val()) }, function(data) {

                if ($('#isFacebook').val() == 'true') {
                    $('#loading').hide();
                    alert('Your post has been submitted and is pending approval.');
                }
                else {
                    window.location.href = window.location.href.replace(/#comments_list/, '') + "&refresh=true";
                }
            });

            return false;
        });

        $('#comment_name').cookieBind();
    }

    var expandComments = $('<a id="expand_comments" href="#">Expand Comments [+]</a>').toggle(
         function() {
             $('#comments_list').appendTo('.span-15:first');
             //$('#comments_list').after($('#sidebar > form'));
             //$('#comments_list').after($('#iframe_box'));
             $(this).text('Minimize Comments [-]');
             window.location.href = window.location.href.replace(/#comments_list/, '') + '#comments_list';
         }, function() {
             $('#comments_list').appendTo('#sidebar');
             //$('#comments_list').before($('#sidebar > form'));
             //$('#comments_list').before($('#iframe_box'));
             $(this).text('Expand Comments [+]');
             window.location.href = window.location.href.replace(/#comments_list/, '') + '#comments_list';
         });

    $('#comments_list').prepend(expandComments);
}
    );

function CleanHtml(html) {
    html = html.replace(/<((?!br|span|i|b|a|img).|\n)*?>/ig, ''); // removes all tags except the ones listed
    //html = html.replace(/(<\w+).*?(\/?>)/g,'$1$2'); // removes all attributes
    return html;
}

function breakOut() {
    window.location.href = window.location.href.replace(/#comments_list/, '');
}
