function findPos(obj) {
    var curleft = 0;
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
        while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

function closePopups() {
    $(".popup-a").css("visibility", "hidden");
    $(".popup-b").css("visibility", "hidden");
    $(".popup-lists").css("visibility", "hidden");
    $(".view-media").css("display", "none");
}

function showReply(reply_obj, item_id, item_type, id, type, story_item_id, link_name) {
    $('#reply_pop_id').val(id);
    $('#reply_pop_type').val(type);
    $('#reply_item_id').val(item_id);
    $('#reply_item_type').val(item_type);
    $('#reply_story_item_id').val(story_item_id);
    $('#reply_link_name').val(link_name);
    $('#reply-form-action').attr("action", $('#add-comment-action').attr("action"));
    $('#reply_ref').val($('#ref').val());
    var offset = $(document).width() - 1008 + 55;
    var offset2 = ($(reply_obj).offset().left - 476) * 2;
    xx = $(document).width() - $(reply_obj).offset().left - $('#reply-form').width() - offset + offset2;
    yy = $(reply_obj).offset().top - 98;
    var pos = findPos(reply_obj);
    $('#reply-form').css("left", pos[0] + 50 + "px");
    $('#reply-form').css("top", pos[1] - 100 + "px");
    $('#reply-form').css("visibility", "visible");
    $('#reply-form').css("display", "");
}

function monitorPopupListsClick(e) {
    var evt = (e) ? e : event;
    var theElem = evt.srcElement ? evt.srcElement : evt.target;
    var cnt = 1;
    var current_element = $(theElem);
    while (cnt < 10) {
        if ($(current_element).attr('class') == 'popup-lists' || $(current_element).attr('class') == 'popup-a' || $(current_element).attr('class') == 'popup-b' || $(current_element).attr('class') == 'view-media') {
            return true;
        }
        current_element = $(current_element).parent();
        ++cnt;
    }
    document.onclick = '';
    closePopups();
    return true;
}
$(document).ready(function () {
    $.post('/ajax/read_topic/', {
        url: location.href
    });
    /*
    $('a.i-like').each(function (i, v) {
        var that = $(this);
        var id = parseInt(that.attr('id').substring(5));
        try {
            if (my_stories_read.indexOf(id) != -1) {
                $(this).attr('class', $(this).attr('class').replace('star-off', 'star-on'));
            }
        } catch(err) {
            return false;
        }
    });
    */
    $('a.remove-read').click(function () {
        $.post($(this).attr("href"), {
            url: $(this).attr("href")
        });
        $("#" + this.parentNode.getAttribute("id")).hide("slow");
        return false;
    });
    $("a.more_link").click(function () {
        var a = new Image();
        a.src = '/ajax/read/' + $(this.offsetParent).attr('id').substr('3', $(this.offsetParent).attr('id').length) + '/0/';
        return true;
    });
    $(".photos-link").click(function () {
        closePopups();
        $(this.offsetParent).children("div.view-media").css("display", "block");
        document.onclick = monitorPopupListsClick;
        return false;
    });
    $(".view-media a.close").click(function () {
        $(this.offsetParent).css("display", "none");
        document.onclick = '';
        return false;
    });
    $(".popup-a a.close").click(function () {
        $(this.offsetParent).css("visibility", "hidden");
        document.onclick = '';
        return false;
    });
    $(".popup-b a.close").click(function () {
        $(this.offsetParent).css("visibility", "hidden");
        document.onclick = '';
        return false;
    });
    $(".popup-lists a.close").click(function () {
        $(this.offsetParent).css("visibility", "hidden");
        document.onclick = '';
        return false;
    });
    $('a.view-all-more-link').each(function () {
        $(this).click(function () {
            var pos = findPos(this);
            closePopups();
            $("#view-all-" + this.id).css("visibility", "visible");
            $("#view-all-" + this.id).css("position", "absolute");
            $("#view-all-" + this.id).css("top", pos[1] - ($("#view-all-" + this.id).height() / 2) + 35 + "px");
            $("#view-all-" + this.id).css("left", pos[0] - 630 + "px");
            document.onclick = monitorPopupListsClick;
            return false;
        });
    });
    $("span.show_cluster_image_grid a").click(function () {
        $('#' + this.id + '_link').toggle();
        return false;
    });
    if ($("input.dummy-button") !== null) {
        $("input.dummy-button").css("display", "none");
    }
    $("input.submit").click(function () {
        $(this).attr("disabled", "true");
        $(this).parents("form")[0].submit();
        return false;
    });
    $("a.score").click(function () {
        closePopups();
        var pos = findPos(this);
        var score_span = $(this).parent().next('.view-score');
        $(score_span).css("visibility", "visible");
        $(score_span).css("top", "0px");
        var score_total = $(this).html();
        document.onclick = monitorPopupListsClick;
        return false;
    });
    $("#add-topic-topic-").click(function () {
        var topic_list = window.x;
        var new_topic = $("#add-topic-topic").val();
        for (var i = 0; i < topic_list.length; i++) {
            if (new_topic.toLowerCase() == topic_list[i].t.toLowerCase()) {
                var current_topics = $(".checkbox_list li input");
                for (var j = 0; j < current_topics.length; j++) {
                    if (current_topics.eq(j).val() == topic_list[i].tid) {
                        $(".add-topic-note").html('<img src="/includes/images/ico/action_stop.gif" style="vertical-align:middle;" /> Topic Already Exists');
                        return false;
                    }
                }
                $(".checkbox_list").append("<li><input type='checkbox' name='topics[]' value='" + topic_list[i].tid + "' checked='checked' /> " + new_topic + "</li>");
                $(".add-topic-note").html('<img src="/includes/images/ico/icon_accept.gif" style="vertical-align:middle;" /> Topic Added');
                $(".add-topic-topic").val('').focus();
                return false;
            }
        }
        $(".add-topic-note").html('<img src="/includes/images/ico/action_stop.gif" style="vertical-align:middle;" /> We did not recognize this topic, please try a different one');
        return false;
    });
    $("#add-topic").click(function () {
        closePopups();
        var pos = findPos(this);
        $("#add-topic-form").css("visibility", "visible");
        $("#add-topic-form").css("position", "absolute");
        $("#add-topic-form").css("top", pos[1] - 100 + "px");
        $("#add-topic-form").css("left", pos[0] - 390 + "px");
        return false;
    });
    var reg_form = $('#register-form')[0];
    var done_once = false;
    $(".popup a.close-register").click(function () {
        $.unblockUI();
        closePopups();
        return false;
    });
    $(".clear").focus(function () {
        if (this.defaultValue === "") {
            this.defaultValue = this.value;
        }
        if (this.value == this.defaultValue) {
            this.value = '';
        }
    });
    $(".clear").blur(function () {
        if (this.value === "") {
            this.value = this.defaultValue;
        }
    });
    $(".reply").click(function () {
        var classes = this.className;
        classes = classes.substr(classes.indexOf('reply-val'), classes.length);
        var class_ary = classes.split("-");
        showReply(this, class_ary[4], class_ary[6], class_ary[2], class_ary[3], class_ary[5], $(this).attr('rel'));
        return false;
    });
});
