/**
 * kosciuszko foundation javascript include
 * Copyright 2009 Fund for the City of New York
 * 
 * This is free software. Use and distribution are subject to the terms
 * of the FCNY Open Source Software License. See license.txt, or
 * http://fred.fcny.org/fcny-ossl.txt
 */

// slides object
var slides = { "version":"1.0" }

slides.showindex = 0;
slides.activeslides = {};
slides.slides = {};

slides.showslide = function ( id ) {
  var slide = $(id);
  if ( !slide ) return;
  // which show?
  var showindex = this.slides[ id ];
  if ( !showindex ) {
    log("Got slide but no registered show for it!");
    return;
  }
  if ( this.activeslides[ showindex ] ) {
    this.activeslides[ showindex ].style.display = 'none';
    signal( this.activeslides[ showindex ], "inactive" );
  }
  this.activeslides[ showindex ] = slide;
  this.activeslides[ showindex ].style.display = 'block';
  var videoid = getNodeAttribute( slide, "videoid" );
  if ( videoid ) {
    log("Playing",videoid);
    window.setTimeout( "$('flowplayer"+videoid+"').DoPlay()", 1000 );
  }
  signal( this.activeslides[ showindex ], "active" )
}

slides.activeIcon = function ( e ) {
  var src = e.src();
  var icon = $( "t_"+src.id )
  addElementClass( icon, "active" );
  //log("Activated",src.id,"icon",icon.id);
}

slides.inactiveIcon = function ( e ) {
  var src = e.src();
  var icon = $( "t_"+src.id )
  removeElementClass( icon, "active" );
  //log("Deactivated",src.id);
}

slides.clickhandler = function ( e ) {
  if ( e.mouse().button["left"]==true ) {
    e.stop();
    var target = e.src();
    if ( target.tagName!="A" ) return;
    target.blur();
    var id = target.hash.substr(1);
    this.showslide( id );
  }
}

slides.navhandler = function ( e ) {
  var target = e.target();
  if ( target.tagName!="A" ) return;
  if ( hasElementClass( target, "pagenav") ) return;
  e.stop();
  target.blur();
  var id = target.hash.substr(1);
  this.showslide( id );
}

slides.init = function( slideshow ) {
  this.showindex++;
  log("Init for slideshow",this.showindex,"with slideshow",slideshow);
  // find slidenav links
  iterateElementsByTagAndClassName ( "div", "slidenav", slideshow, function( navs, i ) {
    //log( "Found slidenav",navs[i], slides.activeslides );
    connect( navs[i], "onclick", slides, "navhandler" );
  });
  // find slideholder links
  iterateElementsByTagAndClassName ( "a", "slideclick", slideshow, function( navs, i ) {
    //log( "Found slidenav",navs[i], slides.activeslides );
    connect( navs[i], "onclick", slides, "clickhandler" );
  });

  // set activeslide
  this.activeslides[ this.showindex ] = false;
  // map slides to this slideshow
  var allslides = getElementsByTagAndClassName( "div", "slide", slideshow );
  var mylength = allslides.length;
  for ( var i=0; i < mylength; i++ ) {
    this.slides[ allslides[i].id ] = this.showindex;
    if ( hasElementClass( allslides[i], "cover") ) {
      this.activeslides[ this.showindex ] = allslides[i];
    }
  }
  
  if ( $('Browser') ) {
    iterateElementsByTagAndClassName ( "a", "slidenav", $('Browser'), function( navs, i ) {
      //log( "Found browser slidenav",navs[i], slides.activeslides );
      connect( navs[i], "onclick", slides, "clickhandler" );
      // find slide
      var id = navs[i].hash.substr(1);
      //alert(id);
      //log("Found icon for",id);
      if ( $(id) ) {
        connect( $(id), "active", slides, "activeIcon" );
        connect( $(id), "inactive", slides, "inactiveIcon" );
      }
      if ( i==0 ) {
        addElementClass( navs[i].parentNode, "active" );
      }
      
    });
  }
}

slides.factory = function() {
  var shows = getElementsByTagAndClassName( "div", "show", $("Canvas") );
  var showslength = shows.length;
  for ( var s=0; s < showslength; s++ ) {
    slides.init( shows[s] );
  }
  if ( $('Homeshow') ) {
    slides.init( $('Homeshow') );
  }
  // show a different slide if requested
  if ( window.location.hash ) {
    var id = window.location.hash.substr(1);
    var isslide = $(id);
    if ( isslide && hasElementClass( isslide, "slide" ) ) {
      log("Showing slide on this page",id);
      this.showslide( id );
    }
  }
}

connect( window, "ondomload", slides, "factory" );

// lightbox for previews
var lightbox = { "version": "1.0", "counter": 0 }

lightbox.iconClick = function( e ) {
  e.stop();
  var preview = e.src();
  var lb_id = getNodeAttribute( preview, "lightbox_id" );
  var rawimage = $( "lightbox_" + lb_id );
  if ( rawimage.lightbox===undefined ) {
    if ( !rawimage.width ) {
      var width = rawimage.currentStyle.width.substring( 0, rawimage.currentStyle.width.indexOf("px") );
      var height = rawimage.currentStyle.height.substring( 0, rawimage.currentStyle.height.indexOf("px") );
    }
    else {
      var width = rawimage.width;
      var height = rawimage.height;
    }
    rawimage.lightbox = { "w": width, "h": height };
  }
  else {
    var width = rawimage.lightbox.w;
    var height = rawimage.lightbox.h;
  }
  // get viewport size, and resize image (reduce only) to fit
  var winsize = getViewportDimensions();
  // take 16 off for scrollbars
  winsize.w = winsize.w - 16;
  if ( winsize.w < 1024 ) {
    winsize.h = winsize.h - 16;
  }
  var targetw = winsize.w - 20;
  var targeth = winsize.h - 20;
  if ( width > targetw ) {
    var ratio = targetw / width;
    width = targetw;
    height = Math.floor( height * ratio );
  }
  if ( height > targeth ) {
    var ratio = targeth / height;
    height = targeth;
    width = Math.floor( width * ratio );
  }
  // set position in window
  if ( window.pageYOffset===undefined ) {
    var scrolledto = document.documentElement.scrollTop;
  }
  else {
    var scrolledto = window.pageYOffset;
  }
  var posx = ( winsize.w - width ) / 2;
  if ( posx < 10 ) posx = 10;
  var posy = ( winsize.h - height ) / 3;
  if ( posy < 10 ) posy = 10;
  posy = posy + scrolledto;
  log( "Winsize", winsize, "scrolled to", scrolledto, "rawimage size", rawimage.lightbox.w,"x",rawimage.lightbox.h,"final position", posx,"x",posy, "final size",width,"x",height );
  rawimage.style.left = posx+"px";
  rawimage.style.top = posy+"px";
  rawimage.style.width = width+"px";
  rawimage.style.height = height+"px";
  if ( rawimage && hasElementClass( rawimage, "loaded" ) ) {
    addElementClass( rawimage, "active" );
    addElementClass( $("top"), "lightbox" );
    //ScrollTo( rawimage );
    log("click",preview,"lb_id", lb_id);
  }
  else {
    log( "no rawimage or not loaded" );
  }
}

lightbox.rawimageClick = function( e ) {
  e.stop();
  var rawimage = e.src();
  var origid = getNodeAttribute( rawimage, "original_id" );
  var orig = $( origid );
  removeElementClass( $("top"), "lightbox" );
  //ScrollTo( orig );
  removeElementClass( rawimage, "active" );
}

lightbox.rawImageLoad = function( e ) {
  e.stop();
  var rawimage = e.src();
  if ( !rawimage.complete ) {
    // not complete
    window.setTimeout('lightbox.rawImagePostLoad("'+rawimage.id+'")', 1000);
  }
  else {
    lightbox.rawImagePostLoad( rawimage.id );
  }
}

lightbox.rawImagePostLoad = function( rawimageid ) {
  var rawimage = $( rawimageid );
  if ( !rawimage.width ) {
    var width = rawimage.currentStyle.width.substring( 0, rawimage.currentStyle.width.indexOf("px") );
    var height = rawimage.currentStyle.height.substring( 0, rawimage.currentStyle.height.indexOf("px") );
  }
  else {
    var width = rawimage.width;
    var height = rawimage.height;
  }
  // check to see that rawimage is bigger than original in width or height
  var origid = getNodeAttribute( rawimage, "original_id" );
  var orig = $( origid );
  if ( !getNodeAttribute( orig, "title" ) ) {
    setNodeAttribute( orig, "title", "Click to view full size." );
  }
  addElementClass( rawimage, "loaded" );
  connect( rawimage, "onclick", lightbox, "rawimageClick" );
}

lightbox.attach = function( ele ) {
  // make sure preview is last namepart
  var names = ele.src.split( "/" );
  var lastname = names.pop();
  if ( !hasElementClass(ele, "nolightbox") && !hasElementClass(ele.parentNode, "quote") ) {
    // connect onclick and add lightbox class
    lightbox.counter ++;
    if ( !ele.id ) {
      ele.id = "lightbox_original_" + lightbox.counter;
      //log("Assigned id to images",i,",",ele.id);
    }
    ele.lightboxclick = connect( ele, "onclick", lightbox, "iconClick" );
    addElementClass( ele, "lightbox" );
    setNodeAttribute( ele, "lightbox_id", lightbox.counter );
    var rawname = names.join("/") + "/poster.jpg";
    var rawimage = IMG( { "id": "lightbox_"+lightbox.counter, "src": rawname, "class": "rawimage", "title": "Click again to close image", "original_id": ele.id } );
    connect( rawimage, "onload", lightbox, "rawImageLoad" );
    $("top").appendChild( rawimage );
  }
}

lightbox.init = function() {
  iterateElementsByTagAndClassName ( "img", null, $("Object"), function( images, i ) {
    if ( images[i].src.indexOf( "preview" ) > -1 ) {
      lightbox.attach( images[i] );
    }
    else if ( images[i].src.indexOf( "thumbnail" ) > -1 ) {
      if ( !hasElementClass(images[i].parentNode, "slideclick") ) {
        lightbox.attach( images[i] );
      }
    }
    else if ( images[i].src.indexOf( "icon" ) > -1 ) {
      lightbox.attach( images[i] );
    }
  });
  log("Lightbox set up for",lightbox.counter,"images");
}

connect( window, "onload", lightbox, "init" );

var translucent = { "version": "1.0" }
// takes a translucent element and puts an opaque overlay in front of it.
translucent.make = function( ele ) {
  var opaque = ele.cloneNode( true );
  removeElementClass( opaque, "translucent" );
  opaque.style.backgroundColor = "transparent";
  opaque.style.opacity = "1";
  ele.parentNode.appendChild( opaque );
}
translucent.init = function() {
  iterateElementsByTagAndClassName ( "div", "translucent", $("Canvas"), function( divs, i ) {
    translucent.make( divs[i] );
  });
}
connect( window, "onload", translucent, "init" );

var boxnav = { "version": "1.0" }
boxnav.init = function() {
  iterateElementsByTagAndClassName ( "div", "boxnav", $("SiteNav"), function( divs, i ) {
    var href = getNodeAttribute( divs[i], "href" );
    if ( href ) {
      divs[i].style.cursor = "pointer";
      divs[i].href = href;
      connect( divs[i], "onclick", function( e ){ var src = e.src(); window.location=src.href; });
    }
  });
}
connect( window, "onload", boxnav, "init" );
