/*
Copyright (c) 2007, Parosweb O.E. All rights reserved.
version: 2007-10-22
*/

////////////////////////////////////////////////////////////////////////////////
// PW.gallery //////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
PW.gallery = function( sID, sCID, oConf, oStyles ) {
  this.sID = sID ? sID : 'pw_gallery';
  this.sCID = sCID ? sCID : false;
	this.oConf = { 'relative':false,
	               'imagepath':false,
                 'images':[],
                 'link':this.sID+'_link',
                 'thumbsperpage':7,
                 'width':530,
                 'height':583,
                 'mainH':450,
                 'thumbsH':74,
                 'thumbsHmod':5,
                 'imgWmod':2,
                 'thumbsdisplayed':true,
                 'imagetimeout':2500,
                 'thumbstimeout':5000,
                 'playinterval':5000,
                 'portraitsize':[ [320,481],[41,62] ],
                 'landscapesize':[ [481,320],[62,41] ],
                 'quadraticsize':[ [350,350],[50,50] ],
								 'classname':this.sID+'_class',
								 'mediapath':'/PW/media/main-1/',
                 'noimg':'pw-gallery-noimg.gif',
                 'nothumb':'pw-gallery-nothumb.gif',
								 'maskConf':{},
								 'maskStyles':{},
                 'loadConf':{   'relative':false, 
                                'anim':false, 
                                'content':'&nbsp;' },
                 'loadStyles':{ 'background-color':false, 
                                'border':false,
                                'z-index':200 },
                 'loadIConf':{   'relative':true,
                                 'anim':false,
                                 'content':'&nbsp;' },
                 'loadIStyles':{ 'background-color':false, 
                                 'border':false,
                                 'z-index':200 },
                 'loadTConf':{   'relative':true,
                                 'anim':false,
                                 'content':'&nbsp',
                                 'width':'205px',
                                 'height':'10px',
                                 'left':'50%',
                                 'top':'50%' },
                 'loadTStyles':{ 'background-color':false, 
                                 'border':false, 
                                 'z-index':200 }
                };
  if ( oConf ) { for ( i in oConf ) { this.oConf[ i ] = oConf[ i ]; } }
	if ( !this.oConf.maskConf.relative ) { this.oConf.maskConf.relative = this.oConf.relative; }
	this.oStyles = { 'z-index':201 };
  if ( oStyles ) { for ( i in oStyles ) { this.oStyles[ i ] = oStyles[ i ]; } }

	this.oGallery = false;
	this.oLoad = false;
	this.oMask = false;
  this.oImage = false;
  this.oThumbnails = false;
  this.shown  = new $U.CustomEvent( 'shown', this );
  this.hidden = new $U.CustomEvent( 'hidden', this );
  this.bshown  = new $U.CustomEvent( 'bshown', this );
  this.bhidden = new $U.CustomEvent( 'bhidden', this );
  this.iloaded = new $U.CustomEvent( 'iloaded', this );
  this.tloaded = new $U.CustomEvent( 'tloaded', this );
};
// PW.gallery.init /////////////////////////////////////////////////////////////
PW.gallery.prototype.init = function() {
  var o = this;
  o.aThumbnails = [];
  for ( i in o.oConf.images ) { 
	  if ( o.oConf.imagepath ) { o.oConf.images[i] = o.oConf.imagepath + o.oConf.images[i] }
    o.aThumbnails[i] = o.oConf.images[i].replace( /\.jpg/,'-tn.jpg' );
  }
  o.iPage = 1;
  o.iIndex = 0;
  o.bNavActive = false;
  o.oKLnext = PW.util.addkey( 39, function(){ if ( o.bPlaying ) { o.pause(); } o.next(); }, o );
  o.oKLprev = PW.util.addkey( 37, function(){ if ( o.bPlaying ) { o.pause(); } o.prev(); }, o );
  o.oKLup   = PW.util.addkey( 38, function(){ if ( !o.oConf.thumbsdisplayed ) { o.toggleThumbs(); } }, o );
  o.oKLdown = PW.util.addkey( 40, function(){ if ( o.oConf.thumbsdisplayed ) { o.toggleThumbs(); } }, o );
  o.oKLplay = PW.util.addkey( 32, function(){ if ( o.bPlaying ) { o.pause(); } else { o.play(); } }, o );
  o.oKLhide = PW.util.addkey( 27, function(){ o.hide(); }, o );
	$E.on( o.oConf.link, 'click', o.show, o, true );
};
// PW.gallery.create ///////////////////////////////////////////////////////////
PW.gallery.prototype.create = function() {
  var o = this;
  var oConf = {};
  var aConf = ['relative','top','left','bottom','right','fixX','fixY','anim','animspeed'];
  for ( i in aConf ) {
    if ( o.oConf[ aConf[i] ] || !isNaN( o.oConf[ aConf[i] ] ) ) {
      oConf[ aConf[i] ] = o.oConf[ aConf[i] ];
    }
  }
  oConf.width   = o.oConf.width+'px';
  oConf.height  = o.oConf.height+'px';
  oConf.content = '';
  o.oGallery = new PW.panel( o.sID, o.sCID, oConf, o.oStyles );
  o.oGallery.init();
  $D.addClass( o.oGallery.oObj, o.oConf.classname );
  // head
  o.oGallery.oHead = PW.util.createDiv( o.sID+'_head' );
  PW.util.appendLink( o.oGallery.oHead, false, o.sID+'_close', false, 'close' );
  o.oGallery.oObj.appendChild( o.oGallery.oHead );
  $E.addListener( o.oGallery.oHead, 'click', o.hide, o, true );
  // main
  o.oGallery.oMain = PW.util.createDiv( o.sID+'_main' );
  $D.setStyle( o.oGallery.oMain, 'height', o.oConf.mainH+'px' );
  o.oGallery.oObj.appendChild( o.oGallery.oMain );
  // image
  o.oGallery.oImage = PW.util.createDiv( o.sID+'_img', 'img' );
  o.oGallery.oMain.appendChild( o.oGallery.oImage );
  o.oLoadImg = new PW.load( o.sID+'_loadimg', o.sID+'_main', 
                            o.oConf.loadIConf, o.oConf.loadIStyles );
  // thumbs
  o.oGallery.oThumbs = PW.util.createDiv( o.sID+'_thumbs' );
  o.oGallery.oObj.appendChild( o.oGallery.oThumbs );
  // nav
  o.oGallery.oNav = PW.util.createDiv( o.sID+'_nav' );
  PW.util.appendLink( o.oGallery.oNav, false, o.sID+'_nav_play', 'play', 'play' );
  PW.util.appendLink( o.oGallery.oNav, false, o.sID+'_nav_prev', 'prev', 'prev' );
  PW.util.appendLink( o.oGallery.oNav, false, o.sID+'_nav_next', 'next', 'next' );
  o.oGallery.oStatus = PW.util.createDiv( o.sID+'_status' );
  o.oGallery.oNumbers = PW.util.createDiv( o.sID+'_numbers' );
  o.oGallery.oMenu = PW.util.createDiv( o.sID+'_menu' );
  o.oGallery.oStatus.appendChild( o.oGallery.oNumbers );
  o.oGallery.oStatus.appendChild( o.oGallery.oMenu );
  o.oGallery.oNav.appendChild( o.oGallery.oStatus );
  o.oGallery.oThumbs.appendChild( o.oGallery.oNav );
  // thumbs main
  o.oGallery.oThumbsMain = PW.util.createDiv( o.sID+'_thumbs_main' );
  PW.util.appendLink( o.oGallery.oThumbsMain, false, o.sID+'_page_prev', 'prev', 'prev' );
  PW.util.appendLink( o.oGallery.oThumbsMain, false, o.sID+'_page_next', 'next', 'next' );
  $D.setStyle( o.oGallery.oThumbsMain, 'height', o.oConf.thumbsH+'px' );
  o.oGallery.oThumbs.appendChild( o.oGallery.oThumbsMain );
  o.oLoadThumbs = new PW.load( o.sID+'_loadthumbs', o.sID+'_thumbs_main', 
                               o.oConf.loadTConf, o.oConf.loadTStyles );
  //thumbs images
  o.oGallery.oThumbsImages = PW.util.createDiv( o.sID+'_thumbs_images' );
  o.oGallery.oThumbsMain.appendChild( o.oGallery.oThumbsImages );
  // thumbs close
  o.oGallery.oThumbsFoot = PW.util.createDiv( o.sID+'_thumbs_foot' );
  PW.util.appendLink( o.oGallery.oThumbsFoot, false, o.sID+'_thumbs_close', false, 'collapse' );
  o.oGallery.oThumbs.appendChild( o.oGallery.oThumbsFoot );
};
// PW.gallery.showImage //////////////////////////////////////////////////////
PW.gallery.prototype.showImage = function( i ) {
  var o = this;
  o.iIndex = i;
  o.setNumbers();
  var m = o.oConf.thumbsperpage*(o.iPage - 1);
  if ( !m ) { m=0; }
  o.oImage = new PW.image( o.oConf.images[ o.iIndex+m ], o.oConf.imagetimeout );
  o.oImage.load.subscribe( function() {
    o.oImage.load.unsubscribeAll();
    if ( o.oImage.bLoaded ) {
      var dimension = o.oImage.sDimension ? o.oImage.sDimension+'size' : 'quadraticsize';
      o.oImage.iWidth = o.oConf[dimension][0][0] ? o.oConf[dimension][0][0] : o.oImage.iWidth;
      o.oImage.iHeight = o.oConf[dimension][0][1] ? o.oConf[dimension][0][1] : o.oImage.iHeight;
      o.oGallery.oImage.width = o.oImage.iWidth;
      o.oGallery.oImage.height = o.oImage.iHeight;
    } else {
      o.oGallery.oImage.removeAttribute( 'width' );
      o.oGallery.oImage.removeAttribute( 'height' );
    }
    o.oGallery.oImage.src = o.oImage.sURL;
    o.centerImg();
    o.iloaded.fire();
    if ( !o.bInit ) { o.oLoadImg.hide(); }
  }, o, true );
  o.oImage.timeout.subscribe( function() {
    o.oImage.timeout.unsubscribeAll();
    o.oGallery.oImage.src = o.oConf.mediapath + o.oConf.noimg;
    o.centerImg();
    o.iloaded.fire();
    if ( !o.bInit ) { o.oLoadImg.hide(); }
  }, o, true );
  if ( !o.bInit ) {
    o.oLoadImg.show();
  }
  o.oTOImage = setTimeout( function() {
    o.oImage.preload();
  }, 500 );
};
// PW.gallery.centerImg ////////////////////////////////////////////////////////
PW.gallery.prototype.centerImg = function() {
  var o = this;
  var iOW, iOH;
  if ( o.oImage.bLoaded ) {
    iOW = o.oImage.iWidth;
    iOH = o.oImage.iHeight;
  } else {
    iOW = o.oGallery.oImage.width;
    iOH = o.oGallery.oImage.height;
  }
  var iW = iOW + o.oConf.imgWmod;
  var iH = iOH + ( o.oConf.imgHmod ? o.oConf.imgHmod : o.oConf.imgWmod );
  var iTop = parseInt( ( o.oConf.mainH - iOH ) / 2, 10 );
  var iLeft = parseInt( ( o.oConf.width - iW ) / 2, 10 );
  PW.util.setStyles( o.oGallery.oImage, { 'top':iTop+'px', 'left':iLeft+'px' } );
};
// PW.gallery.createThumbnails /////////////////////////////////////////////////
PW.gallery.prototype.showThumbnails = function( pi ) {
  var o = this;
  var aThumbnails = [];
  var m = o.oConf.thumbsperpage*(pi - 1);
  if ( !m ) { m=0; }
  for ( var i=0; i<o.oConf.thumbsperpage; i++ ) {
    var j = i+m;
    if ( j > o.aThumbnails.length - 1 ) { break; }
    aThumbnails[i] = o.aThumbnails[j];
  }
  o.oThumbnails = new PW.images( aThumbnails, o.oConf.thumbstimeout, true );
  if ( !o.oConf.thumbsdisplayed ) {
    o.oThumbnails.iCount = aThumbnails.length;
    o.oGallery.oThumbsImages.innerHTML = '';
    o.tloaded.fire();
    return;
  }
  o.oThumbnails.load.subscribe( function() {
    o.oThumbnails.load.unsubscribeAll();
    var oThumbnails = o.oThumbnails.oImages;
    var code = '';
    o.oThumbnails.iCount = 0;
    for ( var i=0; i<o.oConf.thumbsperpage; i++ ) {
      if ( !oThumbnails[i] ) { break; }
      o.oThumbnails.iCount++;
      var dimension, w, h;
      if ( oThumbnails[i].sDimension ) {
        dimension = oThumbnails[i].sDimension ? oThumbnails[i].sDimension : 'quadratic';
        var d = dimension+'size';
        w = o.oConf[d][1][0] ? o.oConf[d][1][0] : oThumbnails[i].iWidth;
        h = o.oConf[d][1][1] ? o.oConf[d][1][1] : oThumbnails[i].iHeight;
      } else {
        oThumbnails[i].sURL = o.oConf.mediapath + o.oConf.nothumb;
        dimension = 'landscape';
        w = o.oConf.landscapesize[1][0] ? o.oConf.landscapesize[1][0] : '';
        h = o.oConf.landscapesize[1][1] ? o.oConf.landscapesize[1][1] : '';
      }
      code += '<td id="' +o.sID+'_td_'+i +'">';
      code += '<a href="javascript:void(0);" id="'+o.sID+'_ta_'+i+'" class="'+dimension+'">';
      code += '<img src="'+oThumbnails[i].sURL+'" id="'+o.sID+'_ti_'+i+'" ';
      code += 'width="'+w+'" height="'+h+'" \>';
      code += '</a></td>';
    }
    code = '<table><tr>'+code+'</tr></table>';
    setTimeout( function() {
      o.oGallery.oThumbsImages.innerHTML = code;
      for ( var i=0; i<o.oConf.thumbsperpage; i++ ) {
        if ( !oThumbnails[i] ) { break; }
        $E.on( o.sID+'_ta_'+i, 'click', o.thumbclick, o, true );
      }
      o.tloaded.fire();
      if ( !o.bInit ) { o.oLoadThumbs.hide(); }
    }, 900 );
  }, o, true );
  if ( !o.bInit ) { 
    o.oGallery.oThumbsImages.innerHTML = '';
    o.oLoadThumbs.show();
  }
  o.oTOThumbs = setTimeout( function() {
    o.oThumbnails.preload();
  }, 700 );
};
// PW.gallery.show /////////////////////////////////////////////////////////////
PW.gallery.prototype.show = function() {
  var o = this;
	if ( !o.oMask ) {
		o.oMask = new PW.mask( o.sID+'_mask', o.sCID, o.oConf.maskConf, o.oConf.maskStyles );
	}
	if ( !o.oLoad ) {
		o.oLoad = new PW.load( o.sID+'_load', o.sCID, o.oConf.loadConf, o.oConf.loadStyles );
	}
  if ( !o.oGallery ) {
    o.bInit = true;
    o.iloaded.subscribe( function() {
      o.iloaded.unsubscribeAll();
      o.showThumbnails( o.iPage );
    }, o, true );
    o.tloaded.subscribe( function() {
      o.tloaded.unsubscribeAll();
      o.setCurrentThumbnail( o.iIndex );
      setTimeout( function() { 
        if ( !o.oConf.thumbsdisplayed ) { 
          o.oConf.thumbsdisplayed = true;
          o.toggleThumbs();
        }
        o.oGallery.show();
      }, 500 );
    }, o, true );
    o.create();
    o.showImage( o.iIndex );
  }
	o.oMask.shown.subscribe( function() {
		o.oMask.shown.unsubscribeAll();
		if ( o.bInit ) {
      o.bInit = false;
      o.oLoad.show();
    } else {
      o.oGallery.show();
    }
  }, o, true );
  o.oGallery.shown.subscribe( function() {
    o.oGallery.shown.unsubscribeAll();
    if( o.oLoad.oLoad.bDisplayed ) { o.oLoad.hide(); }
		$E.addListener( o.oMask.oMask.oObj, 'click', o.hide, o, true );
    o.startNav();
    o.shown.fire();
  }, o, true );
  o.bshown.fire();
	o.oMask.show();
};
// PW.gallery.hide /////////////////////////////////////////////////////////////
PW.gallery.prototype.hide = function() {
  var o = this;
  if ( o.bPlaying ) { o.pause(); }
  o.stopNav();
	$E.removeListener( o.oMask.oMask.oObj, 'click', o.hide );
	o.oGallery.hidden.subscribe( function() {
		o.oGallery.hidden.unsubscribeAll();
		o.oMask.hide();
  }, o, true );
	o.oMask.hidden.subscribe( function() {
	  o.oMask.hidden.unsubscribeAll();
		o.hidden.fire();
	}, o, true );
	o.bhidden.fire();
	o.oGallery.hide();
};
// PW.gallery.setCurrentThumbnail //////////////////////////////////////////////
PW.gallery.prototype.setCurrentThumbnail = function( j ) {
  var o = this;
  for ( var i=0; i<o.oConf.thumbsperpage; i++ ) {
    if ( i > o.aThumbnails.length - 1 ) { break; }
    $D.removeClass( o.sID+'_td_'+i, 'current' );
  }
  $D.addClass( o.sID+'_td_'+j, 'current' );
};
// PW.gallery.setNumbers ///////////////////////////////////////////////////////
PW.gallery.prototype.setNumbers = function() {
  var o = this;
  var code = '';
  code += o.iIndex + ( o.oConf.thumbsperpage*(o.iPage - 1) ) + 1;
  code += ' / ' + o.oConf.images.length;
  o.oGallery.oNumbers.innerHTML = code;
};
// PW.gallery.thumbclick ///////////////////////////////////////////////////////
PW.gallery.prototype.thumbclick = function( e ) {
  var o = this;
  if ( e ) { 
    $E.stopEvent( e );
    $E.getTarget( e ).blur();
  }
  if ( !o.bNavActive ) { return; }
  if ( o.bPlaying ) { o.pause(); }
  if ( o.oTOImage ) { clearTimeout( o.oTOImage ); }
  var oE = $E.getTarget( e );
  var id = oE.id.split('_');
  o.iIndex = parseInt( id[ id.length - 1 ], 10 );
  o.setCurrentThumbnail( o.iIndex );
  o.showImage( o.iIndex );
};
// PW.gallery.next /////////////////////////////////////////////////////////////
PW.gallery.prototype.next = function( e ) {
  var o = this;
  if ( e ) { 
    $E.stopEvent( e );
    $E.getTarget( e ).blur();
    if ( o.bPlaying ) { o.pause(); }
  }
  if ( !o.bNavActive ) { return; }
  if ( o.oTOImage ) { clearTimeout( o.oTOImage ); }
  var m = o.oConf.thumbsperpage * (o.iPage - 1);
  if ( !m ) { m=0; }
  if ( o.iIndex==o.oConf.thumbsperpage - 1 || o.iIndex+m==o.aThumbnails.length - 1 ) {
    o.nextPage();
  } else {
    o.iIndex += 1;
    o.setCurrentThumbnail( o.iIndex );
    o.showImage( o.iIndex );
  }
};
// PW.gallery.prev /////////////////////////////////////////////////////////////
PW.gallery.prototype.prev = function( e ) {
  var o = this;
  if ( e ) { 
    $E.stopEvent( e );
    $E.getTarget( e ).blur();
  }
  if ( !o.bNavActive ) { return; }
  if ( o.bPlaying ) { o.pause(); }
  if ( o.oTOImage ) { clearTimeout( o.oTOImage ); }
  var m = o.oConf.thumbsperpage * (o.iPage - 1);
  if ( !m ) { m=0; }
  if ( o.iIndex===0 ) {
    o.prevPage();
  } else {
    o.iIndex -= 1;
    o.setCurrentThumbnail( o.iIndex );
    o.showImage( o.iIndex );
  }
};
// PW.gallery.nextPage /////////////////////////////////////////////////////////
PW.gallery.prototype.nextPage = function( e ) {
  var o = this;
  if ( e ) { 
    $E.stopEvent( e );
    $E.getTarget( e ).blur();
    if ( o.bPlaying ) { o.pause(); }
  }
  if ( !o.bNavActive ) { return; }
  o.stopNav( true );
  if ( o.oTOThumbs ) { clearTimeout( o.oTOThumbs ); }
  if ( o.aThumbnails.length/o.oConf.thumbsperpage<=o.iPage ) {
    o.iPage = 1;
  } else {
    o.iPage += 1;
  }
  o.iIndex = 0;
  if ( !o.oConf.thumbsdisplayed || o.bPlaying ) {
    o.iloaded.subscribe( function() {
      o.iloaded.unsubscribeAll();
      o.showThumbnails( o.iPage );
    }, o, true );
    o.tloaded.subscribe( function() {
      o.tloaded.unsubscribeAll();
      o.setCurrentThumbnail( o.iIndex );
      o.startNav( true );
    }, o, true );
    o.showImage( o.iIndex );
  } else {
    o.tloaded.subscribe( function() {
      o.tloaded.unsubscribeAll();
      o.setCurrentThumbnail( o.iIndex );
      o.showImage( o.iIndex );
      o.startNav( true );
    }, o, true );
    o.showThumbnails( o.iPage );
  }
};
// PW.gallery.prevPage /////////////////////////////////////////////////////////
PW.gallery.prototype.prevPage = function( e ) {
  var o = this;
  if ( e ) { 
    $E.stopEvent( e );
    $E.getTarget( e ).blur();
    if ( o.bPlaying ) { o.pause(); }
  }
  if ( !o.bNavActive ) { return; }
  o.stopNav( true );
  if ( o.oTOThumbs ) { clearTimeout( o.oTOThumbs ); }
  if ( o.iPage==1 ) {
    o.iPage = parseInt( o.aThumbnails.length/o.oConf.thumbsperpage, 10 );
    if ( o.aThumbnails.length%o.oConf.thumbsperpage>0 ) { o.iPage+=1; }
  } else {
    o.iPage -= 1;
  }
  var m = o.oConf.thumbsperpage*(o.iPage - 1);
  if ( !m ) { m=0; }
  o.oThumbnails.iCount = 0;
  for ( var i=0; i<o.oConf.thumbsperpage; i++ ) {
    var j = i+m;
    if ( j > o.aThumbnails.length - 1 ) { break; }
    o.oThumbnails.iCount++;
  }
  o.iIndex = o.oThumbnails.iCount - 1;
  if ( !o.oConf.thumbsdisplayed ) {
    o.iloaded.subscribe( function() {
      o.iloaded.unsubscribeAll();
      o.showThumbnails( o.iPage );
    }, o, true );
    o.tloaded.subscribe( function() {
      o.tloaded.unsubscribeAll();
      o.setCurrentThumbnail( o.iIndex );
      o.startNav( true );
    }, o, true );
    o.showImage( o.iIndex );
  } else {
    o.tloaded.subscribe( function() {
      o.tloaded.unsubscribeAll();
      o.setCurrentThumbnail( o.iIndex );
      o.showImage( o.iIndex );
      o.startNav( true );
    }, o, true );
    o.showThumbnails( o.iPage );
  }
};
// PW.gallery.toggleThumbs /////////////////////////////////////////////////////
PW.gallery.prototype.toggleThumbs = function( e ) {
  var o = this;
  if ( e ) { 
    $E.stopEvent( e );
    $E.getTarget( e ).blur();
  }
  if ( !o.bNavActive ) { return; }
  if ( o.bPlaying ) { o.pause(); }
  var dsp = 'none';
  if ( o.oConf.thumbsdisplayed ) {
    o.oConf.mainH = o.oConf.mainH + o.oConf.thumbsH + o.oConf.thumbsHmod;
    o.oConf.thumbsdisplayed = false;
  } else {
    o.stopNav( true );
    o.oConf.mainH = o.oConf.mainH - o.oConf.thumbsH - o.oConf.thumbsHmod;
    dsp = 'block';
    o.oConf.thumbsdisplayed = true;
    o.tloaded.subscribe( function() {
      o.tloaded.unsubscribeAll();
      o.setCurrentThumbnail( o.iIndex );
      o.startNav( true );
    }, o, true );
    o.showThumbnails( o.iPage );
  }
  $D.setStyle( o.oGallery.oMain, 'height', o.oConf.mainH+'px' );
  $D.setStyle( o.oGallery.oThumbsMain, 'display', dsp );
  o.centerImg();
};
// PW.gallery.play /////////////////////////////////////////////////////////////
PW.gallery.prototype.play = function( e ) {
  var o = this;
  if ( e ) { 
    $E.stopEvent( e );
    $E.getTarget( e ).blur();
  }
  if ( !o.bNavActive ) { return; }
  //alert( 'play' )
  o.bPlaying = true;
  var obj = $D.get( o.sID+'_nav_play' );
  $D.replaceClass ( obj, 'play', 'pause' );
  $E.removeListener( o.sID+'_nav_play', 'click', o.play );
  $E.on( o.sID+'_nav_play', 'click', o.pause, o, true );
  o.oTOPlay = setInterval ( function() { o.next(); }, o.oConf.playinterval );
};
// PW.gallery.pause ////////////////////////////////////////////////////////////
PW.gallery.prototype.pause = function( e ) {
  var o = this;
  if ( e ) { 
    $E.stopEvent( e );
    $E.getTarget( e ).blur();
  }
  if ( !o.bNavActive ) { return; }
  //alert( 'pause' )
  o.bPlaying = false;
  clearInterval( o.oTOPlay );
  var obj = $D.get( o.sID+'_nav_play' );
  $D.replaceClass ( obj, 'pause', 'play' );
  $E.removeListener( o.sID+'_nav_play', 'click', o.pause );
  $E.on( o.sID+'_nav_play', 'click', o.play, o, true );
};
// PW.gallery.startNav /////////////////////////////////////////////////////////
PW.gallery.prototype.startNav = function( soft ) {
  var o = this;
  o.bNavActive = true;
  o.oKLnext.enable();
  o.oKLprev.enable();
  o.oKLup.enable();
  o.oKLdown.enable();
  o.oKLplay.enable();
  o.oKLhide.enable();
  if ( soft ) { return; }
  $E.on( o.sID+'_nav_prev', 'click', o.prev, o, true );
  $E.on( o.sID+'_nav_next', 'click', o.next, o, true );
  $E.on( o.sID+'_page_prev', 'click', o.prevPage, o, true );
  $E.on( o.sID+'_page_next', 'click', o.nextPage, o, true );
  $E.on( o.sID+'_thumbs_close', 'click', o.toggleThumbs, o, true );
  $E.on( o.sID+'_nav_play', 'click', o.play, o, true );
};
// PW.gallery.stopNav //////////////////////////////////////////////////////////
PW.gallery.prototype.stopNav = function( soft ) {
  var o = this;
  o.bNavActive = false;
  o.oKLnext.disable();
  o.oKLprev.disable();
  o.oKLup.disable();
  o.oKLdown.disable();
  o.oKLplay.disable();
  o.oKLhide.disable();
  if ( soft ) { return; }
  $E.removeListener( o.sID+'_nav_prev', 'click', o.prev );
  $E.removeListener( o.sID+'_nav_next', 'click', o.next );
  $E.removeListener( o.sID+'_page_prev', 'click', o.prevPage );
  $E.removeListener( o.sID+'_page_next', 'click', o.nextPage );
  $E.removeListener( o.sID+'_thumbs_close', 'click', o.toggleThumbs );
  $E.removeListener( o.sID+'_nav_play', 'click' );
  clearInterval( o.oTOPlay );
};













