
/* Functions used in OW mosaics */
/*
ow.mosaic = {

    init:   function() {
        
        
    }

}
*/

var viewer = new Array();
            
function init() {
    
    /* Identify all the mosaics on the web page */
    
    var mosaics = getElementsByClassName( 'mosaic-widget' );
    var id = null;
    for ( var i = 0; i < mosaics.length; i++ ) {
        id = mosaics[i].id;
        viewer[id] = new Seadragon.Viewer( id + '-zoom' );
        viewer[id].openDzi("/6br/images/6br-mosaic.dzi");
        viewer[id].addEventListener( 'animationfinish', onAnimationFinish );
    }
    
}
 
function forwardImage( iframe ) {
    var image = iframe.contentWindow.document.body.innerHTML; 
    if ( image.length > 0 ) {
        var id = getParentByClass( iframe, 'mosaic-widget' ).id;
        ow.ajax.url( '/widget/mosaic-widget?do=describe&id=' + id +
                     '&my-image=' + image );
    }
}

/*  Uploads image from persons' computer ... just makes the uploading
    widget visible and then submits the form    */
    
function uploadMyImage( where ) {
    
    var widget = getParentByClass( where, 'option' );
    var updating = getElementsByClassName( 'updating', null, widget );

    if ( updating != '')
        updating[0].className = removeClass( updating[0].className, 'hidden' );
}

function removePictogram( where, pictoId ) {
    
    var id = getParentByClass( where, 'mosaic-widget' ).id;
    ow.ajax.url( '/widget/mosaic-widget?do=remove&id=' + id + '&picto-id=' +
                 pictoId );
}
    
function startJS() {
    
    /*  Unfortunately need to set display to none for Mozilla browser,
        which causes music to stop abruptly */
    
    var browser = navigator.appName.toLowerCase();
    var version = navigator.appVersion.toLowerCase();
    // alert( browser + ' - ' + version );
    var displayNone = true;    
    if ( browser.indexOf( 'microsoft' ) != -1 ||
         browser.indexOf( 'opera' ) != -1 )
        displayNone = false;
    else if ( browser.indexOf( 'netscape' ) != -1 &&
              version.indexOf( 'safari' ) != -1 )
        displayNone = false;
    if ( displayNone == true ) {
        var animation = document.getElementById( 'animation' );
        animation.style.display = 'none';
    }
    
    /* Set mosaic to be displayed */
    
    var reveal = document.getElementById( 'reveal' )
    reveal.style.visibility = 'visible'; 
}
    
function cancel( where ) {
    var id = getParentByClass( where, 'mosaic-widget' ).id;
    viewer[id].openDzi( '/6br/images/6br-mosaic.dzi' ); 
    ow.ajax.url( '/widget/mosaic-widget?do=cancel&id=' + id );
}
    
function upload( where ) {
    var id = getParentByClass( where, 'mosaic-widget' ).id;
    viewer[id].close();
    ow.ajax.url( '/widget/mosaic-widget?do=upload&id=' + id );
}
Seadragon.Utils.addEvent( window, "load", init );

function onAnimationFinish( thisViewer ) {
    var id = getParentByClass( thisViewer.drawer.elmt, 'mosaic-widget' ).id;
    var bounds = viewer[id].viewport.getBounds();
    var scaling = viewer[id].source.dimensions.x;
    bounds.x = Math.round( scaling * bounds.x );
    bounds.y = Math.round( scaling * bounds.y );
    bounds.width = Math.round( scaling * bounds.width );
    bounds.height = Math.round( scaling * bounds.height );
    ow.ajax.url( '/widget/mosaic-widget?do=meta&id=' + id + '&area=' + 
                 bounds.x + '+' + bounds.y + '+' + bounds.width + '+' + 
                 bounds.height );
}

