/**
 * javascript for lupavision
 */

var $ = YAHOO.util.Dom.get ;

YAHOO.namespace('Lupa') ;

YAHOO.Lupa = {

 gridImageHeight: 0,
 gridImageWidth: 0,
 
 /**
  * initialisation function
  */
 init: function() {
    // scale images within any element of class 'large-image' to fit onscreen at load time
    YAHOO.Lupa.writeEmailAddress() ;
    YAHOO.Lupa.imageTweaks() ;
  },

 /**
  *
  *
  */
 imageTweaks: function() {
    YAHOO.Lupa.scaleImageElements = YAHOO.util.Dom.getElementsByClassName('large-image','div') ;
    if ( YAHOO.Lupa.scaleImageElements.length > 0 ) {
      YAHOO.util.Dom.batch(YAHOO.Lupa.scaleImageElements, YAHOO.Lupa.scaleLargeImages) ;
    }
    YAHOO.Lupa.right75divs = YAHOO.util.Dom.getElementsByClassName('right75', 'div') ;
    if ( YAHOO.Lupa.right75divs.length > 0 ) {
      reg = YAHOO.util.Dom.getRegion(YAHOO.Lupa.right75divs[0]) ;
      viewportWidth = YAHOO.util.Dom.getViewportWidth() ;
      margin = 100 ;
      divWidth = viewportWidth - ( reg.left + margin ) ;
      YAHOO.util.Dom.setStyle( YAHOO.Lupa.right75divs, 'width', divWidth + 'px') ;
    }
    // clean up illustration-grid images to line up nicely
    YAHOO.Lupa.arrangeGridImages() ;
    YAHOO.Lupa.setImageClasses() ;
  },

 /**
  * batch process potentially large images
  */
 scaleLargeImages: function(el) {
    YAHOO.Lupa.contentImages = el.getElementsByTagName('img') ;
    YAHOO.util.Dom.batch(YAHOO.Lupa.contentImages, YAHOO.Lupa.scaleIfLarge) ;
  },

 growRight75Div: function(el) {
  },

 scaleTo: function(el, oReg) {
    iReg = YAHOO.util.Dom.getRegion(el) ;
    console.log(iReg);
    console.log(oReg);
  },

 /**
  * detect which images are too large to fit in a single window,
  * and scale them down to suit
  */
 scaleIfLarge: function(img) {
    var vW = YAHOO.util.Dom.getViewportWidth() ;
    var vH = YAHOO.util.Dom.getViewportHeight() ;
    var xPos = YAHOO.util.Dom.getX(img) ;
    var yPos = YAHOO.util.Dom.getY(img) ;
    var xSize = parseInt(YAHOO.util.Dom.getStyle(img, 'width')) ;
    var ySize = parseInt(YAHOO.util.Dom.getStyle(img, 'height')) ;
    // first try and fit it horizontally
    if ( ( vW - xPos ) < xSize ) {
      img.originalWidth = xSize + 'px' ;
      YAHOO.util.Dom.setStyle(img, 'width', vW - xPos + 'px' ) ;
      YAHOO.util.Dom.setStyle(img, 'cursor', 'pointer') ;
      YAHOO.util.Dom.setStyle(img, 'cursor', 'hand') ;
      YAHOO.util.Event.on(img, 'click', YAHOO.Lupa.restoreImageSize) ;
    }
    // then try and fit it vertically
    if ( ( vH - yPos ) < ySize ) {
      img.originalHeight = ySize + 'px' ;
      YAHOO.util.Dom.setStyle(img, 'height', vH - yPos + 'px' ) ;
      YAHOO.util.Dom.setStyle(img, 'cursor', 'pointer') ;
      YAHOO.util.Dom.setStyle(img, 'cursor', 'hand') ;
      YAHOO.util.Event.on(img, 'click', YAHOO.Lupa.restoreImageSize) ;
    }
  },

 /**
  */
 arrangeGridImages: function() {
    YAHOO.Lupa.illustrationGrid = $( 'illustration-grid' ) ;
    if ( YAHOO.Lupa.illustrationGrid ) {
      YAHOO.Lupa.gridImages = YAHOO.Lupa.illustrationGrid.getElementsByTagName('img') ;
      if ( YAHOO.Lupa.gridImages.length > 0 ) {
	YAHOO.util.Dom.batch( YAHOO.Lupa.gridImages, YAHOO.Lupa.getGridImageHeightAndWidth) ;
//	alert( [ YAHOO.Lupa.gridImageHeight, YAHOO.Lupa.gridImageWidth ] ) ;
	YAHOO.util.Dom.batch( YAHOO.Lupa.gridImages, YAHOO.Lupa.arrangeGridImage) ;
      }
    }
  },

 getGridImageHeightAndWidth: function(img) {
    var imgHeight = parseInt( YAHOO.util.Dom.getStyle(img, 'height') ) ;
    var imgWidth = parseInt( YAHOO.util.Dom.getStyle(img, 'width') ) ;
    if ( imgHeight > YAHOO.Lupa.gridImageHeight ) {
      YAHOO.Lupa.gridImageHeight = imgHeight ;
    }
    if ( imgWidth > YAHOO.Lupa.gridImageWidth ) {
      YAHOO.Lupa.gridImageWidth = imgWidth ;
    }
  },

 arrangeGridImage: function(img) {
    var imgHeight = parseInt( YAHOO.util.Dom.getStyle(img, 'height' ) ) ;
    if ( imgHeight < YAHOO.Lupa.gridImageHeight ) {
      var padT = ( YAHOO.Lupa.gridImageHeight - imgHeight ) / 2 ;
      var padB = YAHOO.Lupa.gridImageHeight - ( imgHeight + padT ) ;
      YAHOO.util.Dom.setStyle(img, 'margin-top', padT + 'px') ;
      YAHOO.util.Dom.setStyle(img, 'margin-bottom', padB + 'px') ;
    }
    // we just use margin: x auto to centre it ... horizontally
    /*
    var imgWidth = parseInt( YAHOO.util.Dom.getStyle(img, 'width' ) ) ;
    if ( imgWidth < YAHOO.Lupa.gridImageWidth ) {
      var padL = ( YAHOO.Lupa.gridImageWidth - imgWidth ) / 2 ;
      var padR = YAHOO.Lupa.gridImageWidth - ( imgWidth + padT ) ;
      YAHOO.util.Dom.setStyle(img, 'margin-left', padL + 'px') ;
      YAHOO.util.Dom.setStyle(img, 'margin-right', padR + 'px') ;
    }
    */
  },

 /**
  * could be a toggle, but it's 2am, so i'll leave this here
  */
 restoreImageSize: function() {
    if ( typeof(this.originalWidth) != 'undefined' ) {
      YAHOO.util.Dom.setStyle(this, 'width', this.originalWidth) ;
    }
    if ( typeof(this.originalHeight) != 'undefined' ) {
      YAHOO.util.Dom.setStyle(this, 'height', this.originalHeight) ;
    }
  },

  setImageClasses: function() {
    var c = YAHOO.util.Dom.get('content') ;
    var theImages = c.getElementsByTagName('img') ;
    for ( var i = 0 ; i < theImages.length ; i++ ) {
      YAHOO.Lupa.setImageClass(theImages[i]) ;
    }
  },

  setImageClass: function(myImage) {
    if ( myImage.align == 'right' ) {
      YAHOO.util.Dom.addClass(myImage, 'alignright') ;
    }
    if ( myImage.align == 'left' ) {
      YAHOO.util.Dom.addClass(myImage, 'alignleft') ;
    }
  },

  writeEmailAddress: function() {
    var ea = YAHOO.util.Dom.getElementsByClassName('email-address') ;
    var parts = [ 'nz', '.', 'co', '.', 'timelessflooring', '@', 'damiah' ] ;
    var address = '' ;
    for ( i = parts.length-1 ; i>=0 ; i-- ) {
      address += parts[i] ;
    }
    for ( i =0 ; i < ea.length ; i++ ) {
      if ( ea[i].innerHTML != '' ) {
        text = ea[i].innerHTML ;
      } else {
        text = address ;
      }
      ea[i].innerHTML = "<a href='mailto:" + address + "'>" + text + "</a>" ;
    }
  }
  
 

} ;

YAHOO.util.Event.on(window, 'load', YAHOO.Lupa.init) ;
