$( document ).ready( function( ){
    $( ".deleteItem" ).click( function( ){
        if( !confirm( "Czy na pewno chcesz usunąć wybrany element?" ) )
            return false;
    } );


    $( "#messageReceiver" ).autocomplete( "message/users",
                                          { minChars:2,
                                            matchSubset:1,
                                            matchContains:1,
                                            cacheLength:10,
                                            selectOnly:1 } );

    $( "#imgMultiList a img" ).click( function( ){
        var src = new String( $( this ).attr( "src" ) );
        var parent = $( this ).parent( );

        if( parent.attr( "class" ) != "thickbox" )
        {
            if( src.indexOf( "youtube.com" ) === -1 )
            {
                src = src.replace( 'multilist', 'mnormall' );
                src = src.replace( 'multi', 'mnormall' );
                parent.attr( "href", src ).addClass( "thickbox" );
            }
            else
            {
                var href = $( this ).parent( ).attr( "href" );
                parent.attr( "href", href + "/flash?height=380&width=560" ).addClass( "thickbox" );
            }

            tb_init('a.thickbox');
        }
    } );

    $( "form.styled div.note a" ).click( function( ){
        var note = new String( $( this ).attr( "class" ) );
        note = note.substr( 1, 1 );

        $( this ).parent( ).find( "input" ).val( note );
        return false;
    } );

    var pos = -79;

    $("div.note a").mouseover(function(){
        var note = new String( $( this ).attr( "class" ) );
        note = note.substr( 1, 1 );
        
        $(this).parent( ).removeAttr( "class" ).addClass( "note" ).addClass( "note" + note );
    });

    $("div.note a").mouseout(function(){
        $(this).parent( ).removeAttr( "class" ).addClass( "note" );

        var positions = [];
        positions[ 1 ] = -64;
        positions[ 2 ] = -48;
        positions[ 3 ] = -32;
        positions[ 4 ] = -16;
        positions[ 5 ] = 0;

        pos = positions[ $( this ).parent( ).find( "input" ).val( ) ];

        $(this).parent().css( "background-position", "0px "+ pos +"px" );
    });

    $("div.note a").mouseout();

    $( ".js_add_note a" ).click( function( ){
        var href = new String( $( this ).attr( "href" ) );

        if( href.indexOf( "opinia" ) != -1 )
        {
            $.get( href , {}, function( data ){ alert( data ); } );
            return false;
        }
    } );

    if( typeof pid != "undefined" && typeof lin != "undefined" && lin > 0 )
    {
        var edit_modes = {  url             : "perfumes/addOption",
                            params          : "pid=" + pid,
                            cancel_button   : "<input type=\"submit\" class=\"submit inplace_cancel\" value=\"Anuluj\"></input>",
                            save_button     : "<input type=\"submit\" class=\"submit inplace_save\" value=\"Zapisz\"></input>",
                            saving_text     : "Zapisuje dane...",
                            show_buttons    : true,
                            element_id      : $( this ).attr( "id" )};

        var local =  {  default_text    : "Wybrane perfumy nie posiadają jeszcze opisu. Kliknij, aby go dodać.",
                        field_type      : "textarea",
                        textarea_cols   : 50 };

        var tmp = {};
        $.extend( tmp, edit_modes, local );

        $( ".product_desc" ).editInPlace( tmp );

        local =  {  default_text    : "Wybrane perfumy nie posiadają jeszcze nut zapachowych. Kliknij, aby je dodać." };
        tmp = {}
        $.extend( tmp, edit_modes, local );
        $( ".product_info_value" ).editInPlace( tmp );
    }

    $( "#searchvalue" ).autocomplete( "perfumes/names" );

} );