$(document).ready( function() {
	
  // fire up all doc viewer widgets on a page
  $('.docViewerWidget').each( function(e) {
    // to find the group id for this page, strip docViewer_ from the div id
    var groupId = $(this).attr('id').replace(/docViewer_/, '');
    var docViewer = new FullDocViewer( groupId );
    docViewer.startWidget();
  })

})	


function getGroup() {
  return $('.docViewerWidget').attr('id').replace(/docViewer_/, '');
}

function activateDocument( scribd_id, access_key )  {
  // load the scribd viewer
  var scribd_doc = scribd.Document.getDoc( scribd_id, access_key );
  scribd_doc.addParam( 'jsapi_version', 1 );
  scribd_doc.write( 'embedded_flash' );

  // determine the index of the doc ..
  var docs = document['docViewer'].docs;
  for ( var i = 0; i < docs.length; i++ ) {
    if ( docs[i].scribd_id.toString() == scribd_id.toString() ) {
      document['docViewer'].coverView.setIndex( i );
      break;
    }
  }
}

function imageDoubleClicked( name, url, index ) {
  var docViewer = document['docViewer'];	
  activateDocument( docViewer.docs[index].scribd_id, docViewer.docs[index].access_key );
}

function imageLoadComplete() {
   setTimeout("document['docViewer'].trigger()", 500);
}


function FullDocViewer( group ) {
  
	
  this.group = group;
  this.target = '#docViewer_' + this.group;
  this.docs = [];
  this.coverView = null;
  this.carouselContainerId = 'docViewer_' + this.group + '_carouselContainer'
  
  this.startWidget = function() { 
    // register self
    document['docViewer'] = this;
    
    $(this.target).html("Loading Documents ...");
    
    // go get the document records for this page, apply the harvest() function to them.
    $.getJSON('/docs.json?group=' + group, function(data, textStatus) { document['docViewer'].dataCallback( data ); } );
    
  }
  
  this.trigger = function() {
    // load document into viewer
    var creds = $(".docViewerDefault").attr('id').split("_");
    activateDocument( creds[0], creds[1] );

    for ( var i = 0; i < this.docs.length; i++ ) {
       if ( this.docs[i].scribd_id == creds[0] ) {
          this.coverView.setIndex( i );
          break;
       }
    }

  }

  
  this.dataCallback = function( docs ) {

    // run through each item in the document set, and do what needs doing
    for ( var i = 0; i < docs.length; i += 1 ) {
      var d = docs[i].document;
      this.docs.push( d );
    }

    // populate some targets for the data gatherer
    $(this.target).html( '<table><tr><td colspan="2"><div id="' + this.carouselContainerId + '"></div></td></tr><td width="600"><div id="embedded_flash"><a href="http://www.scribd.com"></a></div><br/></td><td valign="top"><ul></ul></td></tr></table>' );

    // set up some stylin for the list and whatnot
    // $(this.target + ' ul').css('list-style-image', 'url(/d/App_Themes/iBelong/Images/ibn-searchresult-document.png)' );

    // list out the documents from the CMS
    for ( var i = 0; i < this.docs.length; i++ ) {

      var id_key = this.docs[i].scribd_id + '_' + this.docs[i].access_key;

      // set up the link to the document
      $(this.target + ' ul').append('<li><a id="doc_' + id_key + '" href="#">' + this.docs[i].name + '</a></li>');

      // load doc on click
      $('#doc_' + id_key ).click( function(e) { 
        var creds = $(this).attr('id').split('_');
	activateDocument( creds[1], creds[2] );
	return false; 
      });
    }

    // more stylin' on the list items
    $(this.target + ' li>a').css('font-weight', 'bold');
    
    this.coverView = new CoverView();
    this.coverView.thisInstanceName = this.carouselContainerId;
    this.coverView.flashPreferences( 980, 400, this.carouselContainerId )
    this.coverView.setDataSource( 'http://4-h.org/docs.json?group=' + this.group );
    this.coverView.init();
    
  }

}
