﻿/// <reference path="~/desktopmodules/intevista/js/jquery-1.3.2.min.vsdoc.js"/>

$(window).load(function () {
    formatListings();
});

function formatListings() {
    $(".iv_left a img").each(function () {
        var l = $(this).closest(".iv_left");
        var r = l.siblings(".iv_right");
        r.css("margin-left", (l.width() + 10) + "px");
    });
}

$('head').append($('<script class="dynamic" type="text/javascript" src="/DesktopModules/Intevista/public/Comments/comments.js"></script>'));

$(document).ready(function () {

    $.ajaxSetup({ cache: false });

    $("abbr.timeago").timeago();

    $(".iv_facebook").click(function () {
        var href = $(this).closest("div").attr("href");
        var name = $(this).closest(".iv_right").find(".iv_header").find("a span").html();
        window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(href) + '&t=' + encodeURIComponent(name), 'sharer', 'toolbar=0,status=0,width=626,height=436');
        return false;
    });

    $(".iv_public_listings_index .iv_items").each(function () {
        var item = $(this);
        var img = item.find(".iv_right .iv_header img");
        img.qtip({ content: { url: "/desktopmodules/intevista/public/listings/social.aspx", data: { listingkey: img.attr("key")} }, position: { adjust: { screen: true }, corner: { target: "rightTop"} }, show: { when: "click", solo: true }, hide: "unfocus", style: { width: { min: 100 }, border: { width: 1, radius: 5}} });
    });

    $("div[published=false]").closest("div.iv_right").find("div.iv_header").prepend("<img src='/desktopmodules/intevista/img/icons/exclamation.png' />");

    $(".iv_info").each(function (index, obj) {
        if ($(obj).children().length != 0)
            $(obj).css("margin-top", "12px");
    });

    $(".iv_buttons").each(function (index, obj) {
        if ($(obj).children().length != 1)
            $(obj).css("margin-top", "12px");
    });

    $("a[defaulttabid=0]").attr("href", "javascript:void(0);");
    $(".iv_buttons .iv_details[defaulttabid!=0]").live("click", function () { window.location = $(this).attr("href"); });
    $(".iv_buttons .iv_action_enabled").live("click", function () { window.open($(this).attr("href")); });
    $(".iv_buttons .iv_schedule").siblings(".iv_action_enabled, .iv_action_disabled").hide();

    $(".iv_schedule").live("click", function (e) {
        var oCalendar = $(this).closest(".iv_items").next();
        if ($(this).hasClass("calendarvisible"))
            oCalendar.hide();
        else {
            oCalendar.show();
            oCalendar.find("div:first").fullCalendar("render");
        }

        $(this).toggleClass("calendarvisible");
        return false;
    });

    $(".iv_edit").live("click", function (e) {
        window.location = $(this).attr("edit");
        e.preventDefault();
        return false;
    });


    $("input[type=button].iv_rsvp").live("click", function (e) {
        var oEvent = $("a[href='/desktopmodules/intevista/public/listings/schedule.aspx?" + $(this).attr("qs") + "']").parent("div");
        var exe = $(this).attr("execute");
        var sib = $(this).siblings(".iv_rsvpmessage");
        var but = $(this);
        $.get("/desktopmodules/intevista/public/listings/rsvp.aspx?" + $(this).attr("qs"), null, function (e) {
            if (e == "true") {
                if (exe != "") {
                    window.location = exe;
                    //                    var frm = "<form target='_blank' action='" + exe + "' id='rsvpform'></form>";
                    //                    $("body").append(frm);
                    //                    $("#rsvpform").submit().remove();
                }
                oEvent.addClass("iv_calendarevent_rsvp");
                sib.show();
                but.hide();
            }
        });

        e.preventDefault();
        return false;
    });
});

function CreateCommentForm(listingKey, instanceKey) {
    var containerId = '#listing-' + listingKey;
    if (instanceKey != null) containerId += '-' + instanceKey;
    var container = $(containerId);

    var commentForm = IV.Comments.CreateForm(listingKey, instanceKey, {
        onsuccess: function () {
            LoadComments(listingKey, instanceKey, null, true);
            IV.Comments.MinimizeCommentForm(commentForm);
        }
    });

    $('.iv_comment_form', container).replaceWith(commentForm);

    commentForm.hide();

    IV.Comments.MinimizeCommentForm(commentForm);
}

function LoadComments(listingKey, instanceKey, count, isAdd) {
    var containerId = '#listing-' + listingKey;
    if (instanceKey != null) containerId += '-' + instanceKey;
    var container = $(containerId);
    var commentsContainer = $('.iv_comment_list', container);
    var commentsForm = container.find('.iv_comment_form');
    IV.Comments.LoadComments(commentsContainer, listingKey, instanceKey, { count: count,
        onloaded: function (commentsData) {
            // Size the comments div...
            if ($('.iv_comments', container).width() > IV.Comments.Width)
                $('.iv_comments', container).width(IV.Comments.Width);

            commentsForm.show();

            var socialLinkContainer = container.find('.iv_social_links');
            $('.iv_comments_show', socialLinkContainer).remove();

            var comments = container.find('.iv_comments');
            if (comments.html() != null) {
                var viewCommentsLink = $('<a title="Show comments" href="javascript: void(0);" class="iv_comments_show">' + commentsData.TotalCount + '</a>');
                viewCommentsLink.click(function() {
                    if (commentsContainer.children().length == 0)
                        LoadComments(listingKey, instanceKey);
                    else
                        commentsContainer.empty();
                });
                socialLinkContainer.append(viewCommentsLink);
            }
            
            if (isAdd) {
                var commentElement = $('.iv_comment:last', container);
                if (!isScrolledIntoView(commentElement)) {
                    var anchor = commentElement.children('a:first');
                    if (anchor.length > 0) window.location = '#' + anchor.attr('name');
                }
            }
        },
        onremoved: function () {
            LoadComments(listingKey, instanceKey);
        }
    });
}
