function photo_scroller_left( obj_id )
{
    $( '#' + obj_id + '-slider' ).slider( 'moveTo' , '-=10' ); 
}

function photo_scroller_right( obj_id )
{
    $( '#' + obj_id + '-slider' ).slider( 'moveTo' , '+=10' );
} 
  
function photo_scroller_slide( obj, e, ui )
{
  
    iframe_document( obj ).body.scrollLeft = ( ui.value * obj.percent );
}                       

function iframe_document( obj )
{
    if( window.opera )
        return document.frames[ obj.id ].document;
    
    if( document.all ) 
        return obj.contentWindow.document;
    
    return obj.contentDocument;
}


function iframe_window( obj )
{
    if( window.opera )
        return document.frames[ obj.id ];
    
    return obj.contentWindow;
}

function photo_scroller_resize( obj )
{
    var frame_content_w   = $( iframe_document( obj ).getElementById( 'content' ) ).width();
    var frame_w           = $( obj ).width();
    var frame_scroll      = document.all ? iframe_document( obj ).body.offsetLeft : iframe_document( obj ).body.scrollLeft;
    var percentage        = ( frame_w / frame_content_w ) * 100;
    var slider_exp        = '#' + obj.id + '-slider';
    var scroller_w        = $( slider_exp ).width();
    var frame_slidearea_w = frame_content_w <= frame_w 
                                ? frame_slidearea_w = 1
                                : frame_content_w - frame_w + 10;
                                
    obj.percent = frame_slidearea_w / 100;
    
    if( percentage >= 100 )
        percentage = 100;
    
    $( slider_exp + ' .ui-slider-handle').css('width', percentage + '%' );
  
}

function photo_scroller_init()
{
    var iframe = this;   
    
    $( window ).resize( function( ){ 
        iframe_document( iframe ).photo_scroller_img = document.getElementById( iframe.id + '-img' );
        photo_scroller_resize( iframe ); 
    } )
    
    $( '#' + iframe.id + '-slider' ).slider( { 'slide': function( e, ui ){ photo_scroller_slide( iframe, e, ui ); } } );
    
    iframe_document( iframe ).photo_scroller_img = document.getElementById( iframe.id + '-img' );
    iframe_window( iframe ).photo_scroller_select( iframe_document( iframe ).getElementsByTagName( 'A' ).item( 0 ) );
    photo_scroller_resize( iframe );
}

function photo_scroller_select( arg )
{
    if(!arg) return false;

    if( arg.tagName )
        var node = arg;
    else
        var node = this;

    //edit here
        var itm = node.getElementsByTagName( 'IMG' ).item(0);

        document.photo_scroller_img.alt = itm.title;
        document.photo_scroller_img.title = itm.title;
        document.photo_scroller_img.src = itm.alt;

         if(parent.document.getElementById('USER_toolBox')) {
           var tb = parent.document.getElementById('USER_toolBox');
       
           tb.innerHTML  = "<input type='button' value='Редактировать' onclick=\"wGo('photos.editByUser','target:"+itm.id+"')\" style='float:right;' \/> ";
           tb.innerHTML += "<input type='button' value='Удалить' onclick=\"if(window.confirm('Удалить?')) wGo('photos.removeByUser','target:"+itm.id+"')\" \/> ";
        } 

    // enought! ;)
    
    
    if( document.selected_photo )
        $( document.selected_photo ).removeClass( 'selected' );
        
    document.selected_photo = node;
    $( node ).addClass( 'selected' );
    
    return false;
}
