function setImgSize() {
    $("img").each(function(i) {

        if (this.width > 480)
            this.style.width = '475px';
    });
}
function setUpErrorLogin() {
    $(".loginBox input").addClass("error");
}

function clearErrorLogin() {
    $(".loginBox input").removeClass("error");
}

function editCommentAttachEvent() {
    $("img[alt=Edit]").click(function() {
        var dlg = $("<div/>").dialog({
            autoOpen:false,
            resizable: false,
            draggable: true,
            modal : true,
            width:500,
            title: "Edit Comment",
            overlay: {
                backgroundColor: '#000',
                opacity: 0.3
            },
            buttons: {
                "Save": function() {

                    $.ajax( {
                        url : "/auth/editComment.secure",
                        data : {
                            id : $(this).find("input[name=thisID]").val(),
                            body : $(this).find("textarea").val().replace(/\n/g,"<br/>")
                        },
                        type : "POST",
                        beforeSend : function() {
                            $("#sm_olay").css( {
                                'height' :'100%',
                                'width' :'100%',
                                'position' :'fixed',
                                'left' :'0px',
                                'top' :'0px',
                                'z-index' :'1000',
                                'opacity' :5 / 100
                            });
                            $("#sm_olay").show();
                        },
                        success : function(response) {

                            $("#sm_olay").hide();
                            $(dlg).dialog('destroy').remove();
                        }
                    });

                    $("input[value="+$(this).find("input[name=thisID]").val()+"]").parent().parent().find("span.commentBody").html($(this).find("textarea").val().replace(/\n/g,"<br/>"));
                }
            }
        });
        
        var comment = $(this).parent().parent().parent().
                find("span.commentBody");

        $("<textarea cols='63' rows='12'>" + comment.html().replace(/<br\/?>/g,"\n") + "</textarea>").append(
                "<input type='hidden' name='thisID' value='" + $(this).parent().parent().
                        find("input[name=commentID]").val() + "'").appendTo(dlg);
        dlg.dialog('open');
    });
}

function editGNoteAttachEvent() {
    $("img[alt=Edit]").click(function() {
        var dlg = $("<div/>").dialog({
            autoOpen:false,
            resizable: false,
            draggable: true,
            modal : true,
            width:500,
            title: "Edit Guest Note",
            overlay: {
                backgroundColor: '#000',
                opacity: 0.3
            },
            buttons: {
                "Save": function() {

                    $.ajax( {
                        url : "/auth/editGNote.secure",
                        data : {
                            id : $(this).find("input[name=thisID]").val(),
                            body : $(this).find("textarea").val().replace(/\n/g,"<br/>")
                        },
                        type : "POST",
                        beforeSend : function() {
                            $("#sm_olay").css( {
                                'height' :'100%',
                                'width' :'100%',
                                'position' :'fixed',
                                'left' :'0px',
                                'top' :'0px',
                                'z-index' :'1000',
                                'opacity' :5 / 100
                            });
                            $("#sm_olay").show();
                        },
                        success : function(response) {

                            $("#sm_olay").hide();
                            $(dlg).dialog('destroy').remove();
                        }
                    });

                    $("input[value="+$(this).find("input[name=thisID]").val()+"]").parent().parent().find("div.newsBody").html($(this).find("textarea").val().replace(/\n/g,"<br/>"));
                }
            }
        });

        var comment = $(this).parent().parent().parent().
                find("div.newsBody");

        $("<textarea cols='63' rows='12'>" + comment.html().replace(/<br\/?>/g,"\n") + "</textarea>").append(
                "<input type='hidden' name='thisID' value='" + $(this).parent().parent().
                        find("input[name=gNoteID]").val() + "'").appendTo(dlg);
        dlg.dialog('open');
    });
}

function attachLoginDialog() {
     $("#openLoginWindow").click(function() {
        clearErrorLogin();
        dialog.dialog('open');
        return false;
    });
}

var dialog;

$(document).ready(function() {
    dialog = $("<div/>").dialog({
        autoOpen:false,
        resizable: false,
        draggable: true,
        modal : true,
        title: "Press.cv.ua Account Login",
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        },
        buttons: {
            "Login": function() {
                $(this).find("form").submit();
            }
        }
    });
    $(".loginBox").appendTo(dialog);
    attachLoginDialog();
    setImgSize();
});



