jQuery.noConflict();
jQuery(document).ready(function($){

    $("#menu_2").hover(function(){
            $(this).find(".lower").slideToggle(0);
            $(this).toggleClass("active");
            $(this).find("ul").toggleClass("active");
            return false;
    },
    function(){
            $(this).find(".lower").slideToggle(0);
            $(this).toggleClass("active");
            $(this).find("ul").toggleClass("active");
            return false;
    }
    );
    $("#menu_3").hover(function(){
            $(this).find(".lower").slideToggle(0);
            $(this).toggleClass("active");
            $(this).find("ul").toggleClass("active");
            return false;

    },
    function(){
            $(this).find(".lower").slideToggle(0);
            $(this).toggleClass("active");
            $(this).find("ul").toggleClass("active");
            return false;
    }
    );
    $("#menu_4").hover(function(){
            $(this).find(".lower").slideToggle(0);
            $(this).toggleClass("active");
            $(this).find("ul").toggleClass("active");
            return false;
    },
    function(){
            $(this).find(".lower").slideToggle(0);
            $(this).toggleClass("active");
            $(this).find("ul").toggleClass("active");
            return false;
    }
    );
    $("#menu_5").hover(function(){
            $(this).find(".lower").slideToggle(0);
            $(this).toggleClass("active");
            $(this).find("ul").toggleClass("active");
            return false;
    },
    function(){
            $(this).find(".lower").slideToggle(0);
            $(this).toggleClass("active");
            $(this).find("ul").toggleClass("active");
            return false;
    }
    );


    $(".lower").click(function(){
        window.location = $(this).parent().find("a:first").attr("href");
    });


    $(".photo_date").find("a:first").click(function(){
        $(this).parent().parent().toggleClass("hide");
        $(this).parent().parent().next().toggleClass("hide").find("textarea").focus();

    });

    $(".photo_date").find("a:last").click(function(){
        var _photo_id = $(this).attr("photo_id");
        var _photoalbum_id = $(this).attr("photoalbum_id");
        var _this = $(this);
        
        if(confirm("Удалить фотографию?"))
        {
            $.ajax({
                url:"action_save_delete_photo.php",
                data:{photo_id:_photo_id,photoalbum_id:_photoalbum_id,action:"delete"},
                dataType:"html",
                type:"post",
                success:function(data){                    
                    if(data=="ошибка")
                    _this.parent().find("small").text("Требуется авторизация");
                    else
                    if(data=="вы не можете редактировать чужой фотоальбом")
                        _this.parent().find("small").text("ошибка");
                    else
                    {
                        _this.parent().find("small").text("Фотография удалена");
                        
                        _this.parent().parent().addClass("hide");
                        //<a><img></a>
                        _this.parent().parent().prev().addClass("hide");
                        //separator
                        _this.parent().parent().next().next().addClass("hide");
                        
                        
                    }
                }
            });
        }
    });

    $(".text_photo_descr").find("a").click(function(){
        var text = $(this).parent().find("textarea").val();
        var _photo_id = $(this).attr("photo_id");
        var _photoalbum_id = $(this).attr("photoalbum_id");
        var _this = $(this);        

        $.ajax({
            url: "action_save_delete_photo.php",
            data: {photo_id:_photo_id,photoalbum_id:_photoalbum_id,new_text:text,action:"save"},
            dataType: "html",
            type: "post",
            beforeSend: function(){
                           
                       },
            success: function (data) {                               
                //_this.parent().prev().find("small").val(data);
                _this.parent().toggleClass("hide");

                if(data=="ошибка")
                    _this.parent().prev().find("small").text("Требуется авторизация");
                else
                if(data=="вы не можете редактировать чужой фотоальбом")
                    _this.parent().prev().find("small").text("ошибка");
                else
                    _this.parent().prev().find("small").text("Изменения сохранены");
                
                _this.parent().prev().toggleClass("hide");
                _this.parent().prev().find("p.descr").html(nl2br(data,true));
                
                
            }
         });
    });

    $(".close_window").click(function(){
        $("#photoalbum_window").toggleClass("hide");        
    });

    $(".show_window").click(function(){
        var _photoalbum_id = $(this).attr("photoalbum_id");
        
        $("#photoalbum_window").toggleClass("hide");
        
        //data: {object_type:_object_type,object_id:_object_id,photoalbum_id:_photoalbum_id,selected_object_type:_selected_object_type,selected_object_id:_selected_object_id,selected_photoalbum_id:_selected_photoalbum_id},
        
        $.ajax({
            url: "/useradmin/show_photos.php",
            data: {photoalbum_id:_photoalbum_id,type:"action"},            
            dataType: "html",
            beforeSend: function(){
                            $("#state").html("Загрузка...");
                       },
            success: function (data) {
                $("#state").html("Готово.");                
                $("#photoalbum_content").html(data);
            }
        });
    });       

photoalbum_edit_object_typef();
});

function delete_photo(photo_id)
{    
    jQuery.ajax({
            url: "/useradmin/action_editphoto.php",
            data: {photo_id:photo_id,action:"remove"},
            dataType: "html",
            beforeSend: function(){
                            jQuery("#state").html("Удаляю...");
                       },
            success: function (data) {
                if(data==1)
                    {
                        jQuery("#state").html("Фотография удалена.");
                        jQuery("#photo_"+photo_id).parent().addClass("hide");
                    }
                else
                {
                        jQuery("#state").html("Не удалось удалить фотографию.");
                }
            }
         });
}

function photoalbum_edit_object_typef()
{
    var val = jQuery("#photoalbum_edit_object_type").val();
    var _not_city = 0;
    if(val != 2) _not_city = 1;
        jQuery.ajax({
            url: "/request_object_from_db.php",
            data: {object_type:val,not_city:_not_city},
            dataType: "html",
            beforeSend: function(){
                            jQuery("#photoalbum_country").html("<option value=\"0\"> Загрузка... </option>");
                       },
            success: function (data) {
                jQuery("#photoalbum_country").html("<option value=\"0\"> Выберите страну </option>");
                jQuery("#photoalbum_country").append(data);

                jQuery("#photoalbum_city").html("<option value=\"0\"> -- </option>");
                jQuery("#photoalbum_hotel").html("<option value=\"0\"> -- </option>");


                jQuery("#hotel_tr").addClass("null_option");
                jQuery("#city_tr").addClass("null_option");

                if(val==2) jQuery("#hotel_tr").addClass("val2_null_option");
                if(val==3) jQuery("#hotel_tr").removeClass("val2_null_option");
                
            }
         });
}

function photoalbum_countryf()
{
        var val = jQuery("#photoalbum_country").val();
        jQuery.ajax({
            url: "/request_object_from_db.php",
            data: {country:val},
            dataType: "html",
            beforeSend: function(){
                            jQuery("#photoalbum_city").html("<option value=\"0\"> Загрузка... </option>");
                       },
            success: function (data) {

                jQuery("#photoalbum_city").html("<option value=\"0\"> Выберите город </option>");
                jQuery("#photoalbum_city").append(data);
                jQuery("#city_tr").removeClass("null_option");
               
                jQuery("#photoalbum_hotel").html("<option  value=\"0\"> -- </option>");
                jQuery("#hotel_tr").addClass("null_option");
            }
         });
}

function photoalbum_cityf()
{
    var val = jQuery("#photoalbum_city").val();
    jQuery.ajax({
        url: "/request_object_from_db.php",
        data: {city:val},
        dataType: "html",
        beforeSend: function(){
                        jQuery("#photoalbum_hotel").html("<option value=\"0\"> Загрузка... </option>");
                   },
        success: function (data) {
            if(jQuery("#photoalbum_hotel").attr("disabled"))
                {
                    jQuery("#photoalbum_hotel").html("<option value=\"0\"> -- </option>");
                }
            else
                jQuery("#photoalbum_hotel").html("<option value=\"0\"> Выберите отель </option>");
                jQuery("#photoalbum_hotel").append(data);
                jQuery("#hotel_tr").removeClass("null_option");
        }
     });
}


//аналог php функции nl2br
  function nl2br (str, is_xhtml) {
    var breakTag = '';

    breakTag = '<br />';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }

    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}
