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

////////////////////////////////////////////////////////////////////////////////
// Shortcuts ///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var $U = YAHOO.util;
var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;

////////////////////////////////////////////////////////////////////////////////
// PW //////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var PW = {};
// PW.sAssets //////////////////////////////////////////////////////////////////
PW.sAssets = '/PW/media/';
// PW.bIE //////////////////////////////////////////////////////////////////////
if ( navigator.userAgent.indexOf('MSIE')!=-1 ) { PW.bIE = true; }


////////////////////////////////////////////////////////////////////////////////
// PW.util /////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
PW.util = {};
// PW.util.setStyles ///////////////////////////////////////////////////////////
PW.util.setStyles = function( obj, styles ) {
  for ( i in styles ) { 
    if ( styles[i]===0 ) { 
      $D.setStyle( obj, i, styles[i] ); 
    } else if ( styles[i]!==false ) { 
      $D.setStyle( obj, i, styles[i] );
    }
  }
};
// PW.util.createDiv ///////////////////////////////////////////////////////////
PW.util.createDiv = function( sID, psEl ) {
  var sEl = psEl ? psEl : 'div';
  var oObj = document.createElement( sEl );
  oObj.id = sID;
  return oObj;
};
// PW.util.appendLink ///////////////////////////////////////////////////////////
PW.util.appendLink = function( oObj, psHref, psID, psClass, sContent ) {
  var sHref = psHref ? psHref : 'javascript:void(0);';
  var sID = psID ? ' id="'+psID+'"' : '';
  var sClass = psClass ? ' class="'+psClass+'"' : '';
  oObj.innerHTML += '<a href="'+sHref+'"'+sID+sClass+'>'+sContent+'</a>';
};
// PW.util.addkey //////////////////////////////////////////////////////////////
PW.util.addkey = function( key, f, sc ) {
  return new $U.KeyListener( document, 
                             { keys:key }, 
                             { fn:f, scope:sc, correctScope:true } );
};
// PW.util.listeners ///////////////////////////////////////////////////////////
PW.util.listeners = function( obj, prop ) {
  var aL = $E.getListeners ( obj );
  var sL = '';
  for ( i in aL ) {
    sL += aL[i].obj[prop]+': '+aL[i].type+' - '+aL[i].adjust+' - '+aL[i].scope[prop]+'\n';
  }
  alert( sL );
};
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// PW.image ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
PW.image = function( sUrl, iTimeout ) {
  this.sURL = sUrl ? sUrl : '';
  if ( iTimeout===0 ) { 
    this.iTimeout = iTimeout; 
  } else {
    this.iTimeout = iTimeout ? iTimeout : 5000;	
  }
  this.bLoaded    = false;
  this.sDimension = false;
  this.iWidth     = false;
  this.iHeight    = false;
  this.load       = new $U.CustomEvent( 'load', this );
  this.timeout    = new $U.CustomEvent( 'timeout', this );
};
// PW.image.preload ////////////////////////////////////////////////////////////
PW.image.prototype.preload = function() {
  var o = this;
  var t = new Date();
  o.sTmpID = 'tmp_' + t.getMilliseconds() + Math.random().toString().slice(5,-5);
  o.oImg = document.createElement('img');
  o.oImg.id = o.sTmpID;
  
  $E.addListener( o.oImg, 'load', function() {
    PW.util.setStyles( o.oImg, { 'position':'absolute','top':'-999px','left':'-999px' } );
    document.body.appendChild( o.oImg );
    var oImg = $D.get( o.sTmpID );
    o.iWidth = oImg.width;
    o.iHeight = oImg.height;
    if ( o.iWidth>o.iHeight ) { o.sDimension = 'landscape'; } 
    else if ( o.iWidth<o.iHeight ) { o.sDimension = 'portrait'; }
    else { o.sDimension = 'quadratic'; }
    document.body.removeChild( oImg );
    o.bLoaded = true;
    var args = { 'width':o.iWidth, 'height':o.iHeight, 'dimension':o.sDimension };
    o.load.fire( args );
  }, o, true );
  o.oImg.src = o.sURL;
  
  if ( !o.iTimeout ) { return; }
  setTimeout( function() { 
    if ( !o.bLoaded ) {
      $E.removeListener( o.oImg, 'load' );
      o.timeout.fire();
    }
  }, o.iTimeout );
};
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// PW.images ///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
PW.images = function( aURLS, iTimeout, bUseGlobalTimeout ) {
  this.aURLS = aURLS ? aURLS : [];
  this.iTimeout = iTimeout ? iTimeout : 5000; 
  this.bUseGlobalTimeout = bUseGlobalTimeout ? bUseGlobalTimeout : false;
  this.oImages        = [];
  this.iImages        = 0;
  this.iImagesDone    = 0;
  this.iImagesLoaded  = 0;
  this.iImagesTimeout = 0;
  this.load           = new $U.CustomEvent( 'load', this );
  this.timeout        = new $U.CustomEvent( 'timeout', this );
};
// PW.images.preload ///////////////////////////////////////////////////////////
PW.images.prototype.preload = function() {
  var o = this;
  o.iImages = o.aURLS.length;
  for ( i in o.aURLS ) {
    o.oImages[ i ] = new PW.image( o.aURLS[ i ], 0 );
    o.oImages[ i ].sURL = o.aURLS[ i ];
    o.oImages[ i ].iTimeout = o.iTimeout;
    o.oImages[ i ].load.subscribe( function() {
      var o = this[0];
      var i = this[1];
      o.iImagesDone += 1;
      o.iImagesLoaded += 1;
      if ( o.iImagesDone==o.iImages ) {
        var args = { 'loaded':o.iImagesLoaded,
                     'timeout':o.iImagesTimeout };
        if ( o.oGTO ) { clearTimeout( o.oGTO ); }
        o.load.fire( args );
      }
    }, [o,i], true );
    if ( !o.bUseGlobalTimeout ) {
      o.oImages[ i ].timeout.subscribe( function() { 
        var o = this[0];
        var i = this[1];
        o.iImagesDone += 1;
        o.iImagesTimeout += 1;
        if ( o.iImagesDone==o.iImages ) {
          var args = { 'loaded':o.iImagesLoaded,
                       'timeout':o.iImagesTimeout };
          o.load.fire( args );
        }
      }, [o,i], true );
    }
    o.oImages[ i ].preload();
  }
  if ( o.bUseGlobalTimeout ) {
    o.oGTO = setTimeout( function() {
      for ( i in o.oImages ) {
        $E.removeListener( o.oImages[i], 'load' );
      }
      if ( o.iImagesDone!=o.iImages ) {
        var args = { 'loaded':o.iImagesLoaded,
                     'timeout':o.iImages-o.iImagesLoaded };
        o.load.fire( args );
      }
    }, o.iTimeout );
  }
};
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// PW.panel ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
PW.panel = function( sID, sCID, oConf, oStyles ) {
  this.sID = sID ? sID : 'pw_panel';
  this.sCID = sCID ? sCID : false;
  this.oConf = { 'relative':false,
                 'top':'auto', 
                 'left':'auto',
                 'bottom':'auto',
                 'right':'auto', 
                 'width':'auto', 
                 'height':'auto',
                 'fixX':false, 
                 'fixY':false,
                 'content':'&nbsp;',
                 'anim':true,
                 'animopacity':1,
                 'animspeed':0.2 };
  this.oStyles = { 'display':'none',
                   'position':'absolute', 
                   'z-index':'100' };
  if ( oConf ) { for ( i in oConf ) { this.oConf[ i ] = oConf[ i ]; } }
  if ( oStyles ) { for ( i in oStyles ) { this.oStyles[ i ] = oStyles[ i ]; } }
  if ( this.oConf.anim ) { this.oStyles.opacity = 0; }
  this.oObj = false;
  this.oCon = false;
  this.bConIsBody = false;
  this.bDisplayed = false;
  this.bHc = false;
  this.bVc = false;
  this.bWa = false;
  this.bHa = false;
  this.oPos = { 'T':0, 'L':0, 'B':0, 'R':0, 'W':0, 'H':0 };
  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.wresize = new $U.CustomEvent( 'wresize', this );
  $E.addListener( window, 'resize', function() { this.wresize.fire(); }, this, true );
};
// PW.panel.init ///////////////////////////////////////////////////////////////
PW.panel.prototype.init = function() {
  var o = this;
  if ( o.oObj ) { return; }
  if ( o.sCID ) {
    o.oCon = $D.get( o.sCID );
    if ( !o.oCon ) { alert( 'Container '+o.oCon+' doesn\'t exist.' ); }
  } else {
    o.oCon = $D.get( document.body );
    o.bConIsBody = true;
  }
  o.oObj = document.createElement( 'div' );
  o.oObj.id = o.sID;
  o.oObj.innerHTML = o.oConf.content;
  PW.util.setStyles( o.oObj, o.oStyles );
  o.oCon.appendChild( o.oObj );  
  if ( o.oConf.left=='auto' && o.oConf.right=='auto' ) { o.bHc = true; }
  if ( o.oConf.top=='auto' && o.oConf.bottom=='auto' ) { o.bVc = true; }
  if ( !o.bHc && o.oConf.width=='auto' ) { o.bWa = true; }
  if ( !o.bVc && o.oConf.height=='auto' ) { o.bHa = true; }
  var aPos = ['width','height'];
  for ( i in aPos ) {
    if ( o.oConf[aPos[i]]!='auto' ) { $D.setStyle( o.oObj, aPos[i], o.oConf[aPos[i]] ); }
  }
  if ( o.bHc || o.bVc ) {
    PW.util.setStyles( o.oObj, { 'top':'-999px', 'left':'-999px', 'display':'block' } );
    var oReg = $D.getRegion( o.oObj );
    if ( oReg ) {
      if ( o.bHc ) { o.oPos.W = oReg.right-oReg.left; }
      if ( o.bVc ) { o.oPos.H = oReg.bottom-oReg.top; }
    } else {
      if ( o.bHc ) { o.oPos.W = parseInt( o.oConf.width, 10 ); }
      if ( o.bVc ) { o.oPos.H = parseInt( o.oConf.height, 10 ); }
    }
    $D.setStyle( o.oObj, 'display', 'none' );
  }
  aPos = ['top','left','bottom','right'];
  for ( i in aPos ) {
    if ( o.oConf[aPos[i]]!='auto' ) { $D.setStyle( o.oObj, aPos[i], o.oConf[aPos[i]] ); }
  }
  if ( o.oConf.anim ) {
    var fadein = { opacity:{ from:0, to:o.oConf.animopacity } };
    o.oFadeIn = new $U.Anim( o.sID, fadein, o.oConf.animspeed );
    var fadeout = { opacity:{ from:o.oConf.animopacity, to:0 } };
    o.oFadeOut = new $U.Anim( o.sID, fadeout, o.oConf.animspeed );
  }
};
// PW.panel.show ///////////////////////////////////////////////////////////////
PW.panel.prototype.show = function() {
  var o = this;
  o.init();
  if ( o.bDisplayed ) { return; }
  o.position();
  setTimeout( function() {
    $D.setStyle( o.oObj, 'display', 'block' );
    if ( o.oConf.anim ) {
      o.oFadeIn.onComplete.subscribe( function() {
        o.oFadeIn.onComplete.unsubscribeAll();
        o.wresize.subscribe( function() { o.position(); }, o, true );
        o.shown.fire();
        o.bDisplayed = true;
      }, o, true );
      o.bshown.fire();
      o.oFadeIn.animate();
    } else {
      o.bshown.fire();
      o.shown.fire();
      o.bDisplayed = true;
    }
  }, 30 );
};
// PW.panel.hide////////////////////////////////////////////////////////////////
PW.panel.prototype.hide = function() {
  var o = this;
  if ( !o.bDisplayed ) { return; }
  var i = o.wresize.unsubscribeAll();
  if ( o.oConf.anim ) {
    o.oFadeOut.onComplete.subscribe( function() {
      o.oFadeOut.onComplete.unsubscribeAll();
      $D.setStyle( o.oObj, 'display', 'none' );
      o.hidden.fire();
      o.bDisplayed = false;
    }, o, true );
    o.bhidden.fire();
    o.oFadeOut.animate();
  } else {
    $D.setStyle( o.oObj, 'display', 'none' );
    o.bhidden.fire();
    o.hidden.fire();
    o.bDisplayed = false;
  }
};
//  PW.panel.position //////////////////////////////////////////////////////////
PW.panel.prototype.position = function() {
  var o = this;
  if ( o.bResize ) { return; }
  if ( o.bConIsBody && o.bWa ) { $D.setStyle( o.oObj, 'width', $D.getViewportWidth()+'px' ); }
  if ( o.bConIsBody && o.bHa ) { $D.setStyle( o.oObj, 'height', $D.getViewportHeight()+'px' ); }
  setTimeout( function() {
    if ( o.bConIsBody ) {
      if ( o.bHc ) { o.oPos.L = $D.getViewportWidth()/2; }
      if ( o.bVc ) { o.oPos.T = $D.getViewportHeight()/2; }
      if ( o.bWa ) { o.oPos.W = $D.getDocumentWidth(); }
      if ( o.bHa ) { o.oPos.H = $D.getDocumentHeight(); }
    } else {
      var oReg = $D.getRegion( o.oCon );
      if ( o.bHc ) { o.oPos.L = (oReg.right-oReg.left)/2; }
      if ( o.bVc ) { o.oPos.T = (oReg.bottom-oReg.top)/2; }
      if ( o.bWa ) { o.oPos.W = oReg.right-oReg.left; }
      if ( o.bHa ) { o.oPos.H = oReg.bottom-oReg.top; }
      if ( !o.oConf.relative && o.bHc ) { o.oPos.L += oReg.left; }
      if ( !o.oConf.relative && o.bVc ) { o.oPos.T += oReg.top; }
      if ( !o.oConf.relative && o.bWa ) { o.oPos.W += oReg.left; }
      if ( !o.oConf.relative && o.bHa ) { o.oPos.H += oReg.top; }
    }
    if ( o.oConf.fixX ) { o.oPos.L += o.oConf.fixX; }
    if ( o.oConf.fixY ) { o.oPos.T += o.oConf.fixY; }
    if ( o.bHc ) { $D.setStyle( o.oObj, 'left', Math.round(o.oPos.L-(o.oPos.W/2))+'px' ); }
    if ( o.bVc ) { $D.setStyle( o.oObj, 'top', Math.round(o.oPos.T-(o.oPos.H/2))+'px' ); }
    if ( o.bWa ) { $D.setStyle( o.oObj, 'width', o.oPos.W+'px' ); }
    if ( o.bHa ) { $D.setStyle( o.oObj, 'height', o.oPos.H+'px' ); }
  }, 20 );
  setTimeout( function() { o.bResize = false; }, 50 );
};
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// PW.mask /////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
PW.mask = function( sID, sCID, oConf, oStyles ) {
  this.sID = sID ? sID : 'pw_mask';
  this.sCID = sCID ? sCID : false;
  this.oConf = { 'animopacity':0.6 };
  if ( oConf ) { for ( i in oConf ) { this.oConf[ i ] = oConf[ i ]; } }
  this.oStyles = { 'background-color':'#000', 'opacity':0.6 };
  if ( oStyles ) { for ( i in oStyles ) { this.oStyles[ i ] = oStyles[ i ]; } }
  this.oMask = 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 );
};
// PW.mask.show ////////////////////////////////////////////////////////////////
PW.mask.prototype.show = function() {
  var o = this;
  if ( !o.oMask ) {
    o.oConf.top = 0;
    o.oConf.left = 0;
    o.oConf.width = 'auto';
    if ( o.oConf.relative ) { o.oConf.width = '100%'; }
    o.oConf.height = 'auto';
    o.oMask = new PW.panel( o.sID, o.sCID, o.oConf, o.oStyles );
  }
  o.oMask.shown.subscribe( function() {
    o.oMask.shown.unsubscribe();
    o.shown.fire();
  }, o, true );
  o.bshown.fire();
  o.oMask.show();
};
// PW.mask.hide ////////////////////////////////////////////////////////////////
PW.mask.prototype.hide = function() {
  var o = this;
  o.oMask.hidden.subscribe( function() {
    o.oMask.hidden.unsubscribe();
    o.hidden.fire();
  }, o.oMask, true );
  o.bhidden.fire();
  o.oMask.hide();
};
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// PW.load /////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
PW.load = function( sID, sCID, oConf, oStyles ) {
  this.sID = sID ? sID : 'pw_load';
  this.sCID = sCID ? sCID : false;
  this.oConf = { 'width':'150px', 'height':'50px', 'content':'loading ...' };
  if ( oConf ) { for ( i in oConf ) { this.oConf[ i ] = oConf[ i ]; } }
  this.oStyles = { 'background-color':'#fff', 'border':'1px solid #000' };
  if ( oStyles ) { for ( i in oStyles ) { this.oStyles[ i ] = oStyles[ i ]; } }
  this.oLoad = 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 );
};
// PW.load.init ////////////////////////////////////////////////////////////////
PW.load.prototype.init = function() {
  var o = this;
  o.oLoad = new PW.panel( o.sID, o.sCID, o.oConf, o.oStyles );
  o.oLoad.init();
};
// PW.load.show ////////////////////////////////////////////////////////////////
PW.load.prototype.show = function() {
  var o = this;
  if ( !o.oLoad ) {
    o.oLoad = new PW.panel( o.sID, o.sCID, o.oConf, o.oStyles );
  }
  o.oLoad.shown.subscribe( function() {
    o.oLoad.shown.unsubscribe();
    o.shown.fire();
  }, o.oLoad, true );
  o.bshown.fire();
  o.oLoad.show();
};
// PW.load.hide ////////////////////////////////////////////////////////////////
PW.load.prototype.hide = function() {
  var o = this;
  o.oLoad.hidden.subscribe( function() {
    o.oLoad.hidden.unsubscribe();
    o.hidden.fire();
  }, o.oLoad, true );
  o.bhidden.fire();
  o.oLoad.hide();
};
////////////////////////////////////////////////////////////////////////////////




























//
