/**************************************************
 * eZpopup - class.eZpopup.js
 * 
 * by Inchul Koo (http://www.kriostech.com)
 * Oct. 10, 2009
 * 
 * need jQuery files:
 * jquery.js, ui.core.js, ui.draggable.js
 * 
 * borrowed background css from thickbox
 * 
 **************************************************
*/
eZpopup.basePath = '/js/eZpopup/';

function eZpopup(width, height, modal)
{
  if(typeof(modal)=='number' || typeof(modal)=='boolean') this.modal = modal ? true : false;
  this.init(width, height);
}

eZpopup.btnXClick = function(obj)
{ return function(e)
  { obj.hide();
  };
};

eZpopup.btnXMouseOver = function(obj)
{ return function(e)
  { obj.btnOn(this);
  };
};

eZpopup.btnXMouseOut = function(obj)
{ return function(e)
  { obj.btnOff(this);
  };
};

eZpopup.prototype =
{
  id:0
 ,skin:'default'
 ,modal:false
 ,created:false
 ,width:400
 ,height:400
 ,minWidth:100
 ,minHeight:100
 ,borderX:8*2
 ,borderY:28+8
 ,handleY:28
 ,overflowX:'auto'
 ,overflowY:'auto'

 ,contentX:0
 ,contentY:0

 ,paddingTop:15
 ,paddingRight:15
 ,paddingBottom:15
 ,paddingLeft:15

 ,color:'#000000'
 ,background:'#FFFFFF'
 ,fonts:'Arial,Helvetica'

 ,baseZIndex:100
 ,basePath:''
 ,skinPath:''
 ,icon:''
 ,dragInit:false

 ,title:document.domain
 ,content:'&nbsp;'
 ,contentKeep:false
 ,contentLoaded:false

 ,handleOpacity:null
 ,frameOpacity:null

 ,ezpopWrapper:null
 ,ezpopWindow:null
 ,ezpopHandle:null
 ,ezpopTitle:null
 ,ezpopContent:null

 ,showHandle:true
 ,showButton:true
 ,oBtn:null
 ,btnClose:new Image()
 ,btnCloseOn:new Image()
 ,onClose:null

  // init
 ,init:function(width, height)
  { this.id = Math.floor(Math.random()*9999999999);
    
    this.setWindowSize(width, height);

    this.basePath = eZpopup.basePath || '/js/eZpopup/';
    this.skinPath = this.basePath+'skins/'+this.skin+'/';
    this.icon     = this.skinPath+'ezlogo_sml.gif';

    this.btnClose.src   = this.skinPath+'btn_close.gif'
    this.btnCloseOn.src = this.skinPath+'btn_close_on.gif'
  }
  // setTitle
 ,setTitle:function(title)
  { this.title = title;
  	if(this.ezpopTitle!=null)
      this.ezpopTitle.innerHTML = this.title;
  }
  // setContent
 ,setContent:function(content)
  { this.content = content;
  	if(this.ezpopContent!=null)
      this.ezpopContent.innerHTML = this.content;
  }
  // setBorder
 ,setBorder:function()
  { if(this.showHandle)
    { this.handleY = 28;
      this.borderY = 28 + 8;
    }
    else
    { this.handleY = 10;
      this.borderY = 10 + 8;
    }
  }
 ,setWindowSize:function(width, height)
  { this.setBorder();

    if(typeof(width)=='number') this.width = width >= this.minWidth ? width : this.minWidth;
    else if(width=='auto') this.width = width;
    if(typeof(height)=='number') this.height = height >= this.minHeight ? height : this.minHeight;
    else if(height=='auto') this.height = height;
    if(typeof(modal)=='number' || typeof(modal)=='boolean') this.modal = modal ? true : false;

    if(this.width=='auto')
      this.contentX = 'auto';
    else
      this.contentX = this.width  - this.borderX - this.paddingLeft - this.paddingRight;

    if(this.height=='auto')
      this.contentY = 'auto';
    else
      this.contentY = this.height - this.borderY - this.paddingTop - this.paddingBottom;

    this.resizePopup();
  }
  // setContentSize
 ,setContentSize:function(sizeX, sizeY)
  { var width = this.width;
  	var height = this.height;
  	this.setBorder();

  	if(typeof(sizeX)=='number')
 	    width  = sizeX + this.borderX + this.paddingLeft + this.paddingRight;
    else if(sizeX=='auto')
      width  = sizeX;

  	if(typeof(sizeY)=='number')
  	  height = sizeY + this.borderY + this.paddingTop + this.paddingBottom;
    else if(sizeY=='auto')
      width  = sizeY;

  	this.setWindowSize(width, height);
  }
  // resizePopup
 ,resizePopup:function()
  { if(this.ezpopWindow!=null)
    { this.ezpopWindow.style.width  = this.width  + (typeof(this.width) =='number' ? 'px' : '');
      this.ezpopWindow.style.height = this.height + (typeof(this.height)=='number' ? 'px' : '');
    }
    //if(this.ezpopHandle!=null)
    //{ this.ezpopHandle.style.width  = this.width  + (typeof(this.width) =='number' ? 'px' : '');
    //}
    if(this.ezpopContent!=null)
    { this.ezpopContent.style.width  = this.contentX + (typeof(this.contentX) =='number' ? 'px' : '');
      this.ezpopContent.style.height = this.contentY + (typeof(this.contentY) =='number' ? 'px' : '');
    }
  }
  // setContentBackground
 ,setContentColor:function(background, color)
  { this.background = background;
    if(color)
      this.color = color;

    if(this.ezpopConWrap!=null)
      this.ezpopConWrap.style.background = this.background;
    if(this.ezpopContent!=null && color)
      this.ezpopContent.style.color = this.color;
  }
  // setContentOverflow
 ,setContentOverflow:function(ovfX, ovfY)
  { this.overflowX = ovfX;
    this.overflowY = ovfY;
    
    if(this.ezpopContent!=null)
    { this.ezpopContent.style.overflowX = this.overflowX;
      this.ezpopContent.style.overflowY = this.overflowY;
    }
  }
  // setContentPadding
 ,setContentPadding:function(pTop, pRgt, bBtm, pLft)
  { this.paddingTop    = parseInt(pTop);
    this.paddingRight  = parseInt(pRgt);
    this.paddingBottom = parseInt(bBtm);
    this.paddingLeft   = parseInt(pLft);

    if(this.ezpopConWrap!=null)
    { this.ezpopConWrap.style.paddingTop    = this.paddingTop + 'px';
      this.ezpopConWrap.style.paddingRight  = this.paddingRight + 'px';
      this.ezpopConWrap.style.paddingBottom = this.paddingBottom + 'px';
      this.ezpopConWrap.style.paddingLeft   = this.paddingLeft + 'px';
    }
    this.setWindowSize();
  }
  // setDivObjects
 ,setDivObjects:function()
  { if(this.modal)
  	this.ezpopWrapper = this.getE('ezpopWrapper__'+this.id);
    this.ezpopWindow  = this.getE('ezpopWindow__'+this.id);
    this.ezpopHandle  = this.getE('ezpopHandle__'+this.id);
    if(this.showHandle)
    this.ezpopTitle   = this.getE('ezpopTitle__'+this.id);
    this.ezpopContent = this.getE('ezpopContent__'+this.id);
    this.ezpopConWrap = this.getE('ezpopConWrap__'+this.id);
  }
  // setDrag
 ,setDrag:function()
  { if(!this.dragInit && this.showHandle)
  	{ try
      { $(this.ezpopWindow).draggable({handle:$(this.ezpopHandle)});
        this.dragInit = true;
      } catch(e){};
    }
  }
  // show functions
 ,show:function(speed, posX, posY)
  { this.showWindow('show', speed, posX, posY);
  }
 ,fadeIn:function(speed, posX, posY)
  { this.showWindow('fade', speed, posX, posY);
  }
  ,slideDown:function(speed, posX, posY)
  { this.showWindow('slide', speed, posX, posY);
  }
  ,slideRight:function(speed, posX, posY)
  { this.showWindow('right', speed, posX, posY);
  }
  // show
 ,showWindow:function(effect, speed, posX, posY)
  { if(!this.created)
  	  this.create();

    if(this.showHandle)
    $(this.ezpopTitle).html(this.title);
    
    if(!this.contentLoaded)
    { $(this.ezpopContent).html(this.content);
      if(this.contentKeep)
        this.contentLoaded = true;
    }

  	var winW = $(window).width();
  	var winH = $(window).height();
    var scrX = $(window).scrollLeft();
    var scrY = $(window).scrollTop();
  	var popW = $(this.ezpopWindow).width();
    var popH = $(this.ezpopWindow).height();

    var leftpos = 0;
    var toppos  = 0;

    var pX = null;
    var pY = null;
    var pSpc = 0;

    if(typeof(posX)=='object')
    { try
    	{ pX = posX.pageX;
        pY = posX.pageY;
        pSpc = (typeof(posY)=='number') ? posY : 2;
      } catch(e) {}
    }
    else if(typeof(posX)=='number' && typeof(posY)=='number')
    { pX = posX;
      pY = posY;
    }

    if(typeof(pX)=='number' && typeof(pY)=='number')
    { if(pX - scrX + popW + pSpc < winW || winW - pX - scrX > pX - scrX)
    	  leftpos = pX + pSpc;
      else
    	  leftpos = pX - popW - pSpc;

      if(pY - scrY + popH + pSpc < winH || winH - pY - scrY > pY - scrY)
      	toppos  = pY + pSpc;
    	else
      	toppos  = pY - popH - pSpc;
    }
    else
  	{ leftpos = scrX + winW/2 - popW/2;
      toppos  = scrY + winH/2 - popH/2;
    }

    this.ezpopWindow.style.left = leftpos+'px';
    this.ezpopWindow.style.top  = toppos +'px';

    this.setDrag();

    if(this.modal)
    { $(this.ezpopWrapper).css('z-index', this.baseZIndex);

		  if(typeof document.body.style.maxHeight === "undefined") //if IE 6
		  { $("body","html").css({height: "100%", width: "100%"});
		  	$("html").css("overflow","hidden");
		  }

			if(this.detectMacXFF())
			 	$(this.ezpopWrapper).addClass("ezpopWrapperMacFFBGHack");//use png overlay so hide flash
			else
		   	$(this.ezpopWrapper).addClass("ezpopWrapperBG");//use background and opacity

      $(this.ezpopWrapper).show();
    }
  	if(effect=='fade')
      $(this.ezpopWindow).fadeIn(speed);
    else if(effect=='slide')
      $(this.ezpopWindow).slideDown(speed);
    else if(effect=='right')
      $(this.ezpopWindow).show('slide',{direction:"right"},speed);
    else
      $(this.ezpopWindow).show(speed);
  }
  // hide functions
 ,hide:function(speed)
  { this.hideWindow('hide', speed);
  }
 ,fadeOut:function(speed)
  { this.hideWindow('fade', speed);
  }
  ,slideUp:function(speed)
  { this.hideWindow('slide', speed);
  }
  // hideWindow:function(speed)
 ,hideWindow:function(effect, speed)
  { if(!this.contentKeep)
  	{ this.ezpopContent.innerHTML = '';
      this.contentLoaded = false;
    }
  	if(effect=='fade')
  	  $(this.ezpopWindow).fadeOut(speed);
    else if(effect=='slide')
  	  $(this.ezpopWindow).slideUp(speed);
    else
  	  $(this.ezpopWindow).hide(speed);
  	
  	if(this.modal)
    { $(this.ezpopWrapper).hide();

	    if(typeof document.body.style.maxHeight == "undefined") //if IE 6
	    { $("body","html").css({height: "auto", width: "auto"});
	    	$("html").css("overflow","");
	    }
    }

    if(this.onClose!=null)
      eval(this.onClose);
  }
  // getE
 ,getE:function(id)
  { return document.getElementById(id);
  }
  // detectMacXFF <-- from ThickBox
 ,detectMacXFF:function()
  { var userAgent = navigator.userAgent.toLowerCase();
    if(userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1)
      return true;
    else
      return false;
  }
  // btnOn, btnOff
 ,btnOn:function(img)
  { img.src = this.btnCloseOn.src;
  }
 ,btnOff:function(img)
  { img.src = this.btnClose.src;
  }
  // create
 ,create:function()
  { if(this.modal && this.ezpopWrapper==null)
    { $("body").append('<div id="ezpopWrapper__'+this.id+'" class="ezpopWrapper" style="display:none;"></div>');
    }

    if(this.handleOpacity==null)
      this.handleOpacity = this.showHandle ? 0.9 : 0.75;
    if(this.frameOpacity==null)
      this.frameOpacity  = 0.75

    var txtHandleOpacity = 'filter:alpha(opacity='+(this.handleOpacity*100)+');-moz-opacity:'+this.handleOpacity+';opacity:'+this.handleOpacity+';';
    var txtFrameOpacity  = 'filter:alpha(opacity='+(this.frameOpacity*100)+');-moz-opacity:'+this.frameOpacity+';opacity:'+this.frameOpacity+';';
    this.setBorder();
    this.setWindowSize(this.width, this.height);

    var width    = this.width  + (typeof(this.width)  =='number' ? 'px' : '');
    var height   = this.height + (typeof(this.height) =='number' ? 'px' : '');
    var contentX = this.contentX + (typeof(this.contentX)  =='number' ? 'px' : '');
    var contentY = this.contentY + (typeof(this.contentY)  =='number' ? 'px' : '');

    if(this.ezpopWindow==null)
    { $("body").append('<div id="ezpopWindow__'+this.id+'" style="width:'+width+';height:'+height+';position:absolute;z-index:'+(this.baseZIndex+1)+';display:none;"></div>');
    	
      $("#ezpopWindow__"+this.id).html(
        '<div id="ezpopHandle__'+this.id+'" style="'+txtHandleOpacity+'width:100%;height:'+this.handleY+'px;cursor:default;">'
      + '<table cellspacing="0" cellpadding="0" border="0" width="100%">'
      + '<tr>'
      + '  <td valign="top" width="8" height="'+this.handleY+'" style="background-image:url('+this.skinPath+'frame_tl.gif);background-repeat:no-repeat;">'
      + '    <div class="ezpopSpacer">&nbsp;</div></td>'
      + '  <td style="background-image:url('+this.skinPath+'frame_tc.gif);">'
      + '    <div id="ezpopHead__'+this.id+'"><div class="ezpopSpacer">&nbsp;</div></div>'
      + '  </td>'
      + '  <td valign="top" width="8" style="background-image:url('+this.skinPath+'frame_tr.gif);background-repeat:no-repeat;">'
      + '    <div class="ezpopSpacer">&nbsp;</div></td>'
      + '</tr>'
      + '</table>'
      + '</div>'
      + '<table cellspacing="0" cellpadding="0" width="100%">'
      + '<tr>'
      + '  <td width="8" valign="top" style="'+txtFrameOpacity+'background-image:url('+this.skinPath+'frame_ml.gif);">'
      + '    <img src="'+this.skinPath+'frame_mlo.gif" width="8" height="90"></td>'
      + '  <td>'
      + '    <div id="ezpopConWrap__'+this.id+'" style="background-color:'+this.background+';padding:'+this.paddingTop+'px '+this.paddingRight+'px '+this.paddingBottom+'px '+this.paddingLeft+'px;">'
      + '    <div id="ezpopContent__'+this.id+'" style="width:'+contentX+';height:'+contentY+';color:'+this.color+';font-family:'+this.fonts+';">'+this.content+'</div></div></td>'
      + '  <td width="8" valign="top" style="'+txtFrameOpacity+'background-image:url('+this.skinPath+'frame_mr.gif);">'
      + '    <img src="'+this.skinPath+'frame_mro.gif" width="8" height="90"></td>'
      + '</tr>'
      + '<tr>'
      + '  <td style="'+txtFrameOpacity+'" height="8">'
      + '    <img src="'+this.skinPath+'frame_bl.gif" width="8" height="8"></td>'
      + '  <td style="'+txtFrameOpacity+'background-image:url('+this.skinPath+'frame_bc.gif);">'
      + '    <img src="'+this.skinPath+'frame_bc.gif" width="8" height="8"></td>'
      + '  <td style="'+txtFrameOpacity+'">'
      + '    <img src="'+this.skinPath+'frame_br.gif" width="8" height="8"></td>'
      + '</tr>'
      + '</table>'
      );
    }

    if(this.showHandle)
    { $("#ezpopHead__"+this.id).html(
        '    <table cellspacing="0" cellpadding="0" width="100%">'
      + '    <tr>'
      + '      <td valign="top" width="22" height="'+this.handleY+'" style="padding:9px 0px 0px 2px;"><img src="'+this.icon+'" width="16" height="16"></td>'
      + '      <td valign="top" style="padding:9px 0px 0px 0px;font-family:Arial;font-size:12px;font-weight:bold;color:#000000;"><div id="ezpopTitle__'+this.id+'">'+this.title+'</div></td>'
      + '      <td valign="top" style="padding:10px 2px 0px 0px;" align="right">'
      + '        <div id="ezpopClose__'+this.id+'"></div></td>'
      + '    </tr>'
      + '    </table>'
      );
    }
    else
      this.showButton = false;

    if(this.showButton)
    { $("#ezpopClose__"+this.id).html('<img id="ezpopCloseBtn__'+this.id+'" src="'+this.skinPath+'btn_close.gif'+'" width="'+this.handleY+'" height="15" />');
      this.oBtn = this.getE("ezpopCloseBtn__"+this.id);
      $(this.oBtn).mouseover(eZpopup.btnXMouseOver(this)).mouseout(eZpopup.btnXMouseOut(this)).click(eZpopup.btnXClick(this));
    }

    this.setDivObjects();
    if(this.overflowX)
      this.ezpopContent.style.overflowX = this.overflowX;
    if(this.overflowY)
      this.ezpopContent.style.overflowY = this.overflowY;
  
    this.created = true;
    if(this.contentKeep && this.content != '&nbsp;')
      this.contentLoaded = true; 
  }
}

