var Videobox = {

	init: function (options) {
		// init default options
		this.options = Object.extend({
			resizeDuration: 400,	// Duration of height and width resizing (ms)
			initialWidth: 250,		// Initial width of the box (px)
			initialHeight: 250,		// Initial height of the box (px)
			defaultWidth: 425,		// Default width of the box (px)
			defaultHeight: 350,	// Default height of the box (px)
			animateCaption: true,	// Enable/Disable caption animation
			flvplayer: '/video/swf/player.swf'
		}, options || {});

		this.anchors = [];
		$A($$('a')).each(function(el){
			if(el.rel && el.href && el.rel.test('^vidbox', 'i')) {
				el.addEvent('click', function (e) {
          e = new Event(e);
          e.stop();
          this.click(el);
				}.bind(this));
				this.anchors.push(el);
			}
    	}, this);

		this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);
		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);

		this.bottomContainer = new Element('div').setProperty('id', 'lbBottomContainer').setStyle('display', 'none').injectInside(document.body);
		this.bottom = new Element('div').setProperty('id', 'lbBottom').injectInside(this.bottomContainer);
		new Element('a').setProperties({id: 'lbCloseLink', href: '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div').setProperty('id', 'lbCaption').injectInside(this.bottom);
		this.number = new Element('div').setProperty('id', 'lbNumber').injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		
		this.overlay.fade('hide');
		this.center.set('morph', {duration: 500, transition: Fx.Transitions.sineInOut, onComplete: nextEffect});
		this.bottom.set('tween', {'duration':400});
			
	},

	click: function(link) {
     	return this.open (link.href, link.title, link.rel);
	},
	
	open: function(sLinkHref, sLinkTitle, sLinkRel) {
		this.href = sLinkHref;
		this.title = sLinkTitle;
		this.rel = sLinkRel;
		this.position();
		this.setup();
		this.video(this.href);
		this.top = Window.getScrollTop() + (Window.getHeight() / 15);
		this.center.setStyles({top: this.top+'px', display: ''});
		this.overlay.fade(0.8);
		this.step = 1;
		this.center.setStyle('background','#fff url(loading.gif) no-repeat center');
		this.caption.innerHTML = this.title;
		this.center.morph({'height': this.options.contentsHeight});
	},

	setup: function(){
		var aDim = this.rel.match(/[0-9]+/g);
		this.options.contentsWidth = (aDim && (aDim[0] > 0)) ? aDim[0] : this.options.defaultWidth;
		this.options.contentsHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : this.options.defaultHeight;

	},

	position: function(){
    this.overlay.setStyles({'top': 0 +'px', 'height': document.body.clientHeight +'px'});
	},

video: function(sLinkHref){
	this.isVideo = false;
    this.isZoom = false;

		// PMA -------------------
		 if (sLinkHref.match(/philamuseum\.org\/watch/i)) {
            this.isVideo = true;
      		this.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('=');
			this.videoID = videoId[1];
			
	this.modalflashvars =
	{
            'abouttext': 'Philadelphia Museum of Art',
            'aboutlink': 'http://www.philamuseum.org',
			'logo.file': 'http://www.philamuseum.org/video/swf/logo.png',
			'logo.hide': 'false',
            'logo.link': 'http://www.philamuseum.org',
			'plugins': 'viral-2, gapro-1',
			'viral.email_footer': '',
			'viral.oncomplete': 'none',
			'viral.onpause':  'false',
	
			'gapro.accountid':  'UA-1914086-1',
			'gapro.trackstarts':  'true',
			'gapro.trackpercentage':  'true',
			'gapro.tracktime':  'true',
        
			'config':  '/video/'+this.videoID+'.xml'
		};

  this.modalparams = {
  
			'allowfullscreen': 'true',
			'allowscriptaccess':  'always',
			'allownetworking':  'all',
			'bgcolor':  '#FFFFFF',
			'wmode':  'transparent'
   };

   this.modalattributes = {
      'id':  'meflash',
      'name': 'meflash'
   };



}else if (sLinkHref.match(/philamuseum\.org\/zoom/i)) {
	this.title = '<i><strong>' + this.title +  '</strong></i>' + '<div class="requestRR"><a href="/rights.html" class="requestRR">License this image >></a></div>';
            this.isZoom = true;
      		this.flash = true;
            var hRef = sLinkHref;
			var zoomID = hRef.split('=');
            this.fz_assetName = zoomID[1];
			
	this.modalflashvars =
	{
            'assetName': this.fz_assetName,
			'closeModal': 'yes'
		};

  this.modalparams = {
  
			'menu': 'false',
			'allowscriptaccess':  'always',
			'BASE':  '.',
			'bgcolor':  '#FFFFFF',
			'wmode':  'transparent'
   };

   this.modalattributes = {
      'id':  'meflash',
      'name': 'meflash'
   };

}else if (sLinkHref.match(/philamuseum\.org\/flipBook/i)) {
		this.title = '<i><strong>' + this.title +  '</strong></i>' + '<div class="requestRR"><a href="/rights.html" class="requestRR">License this image >></a></div>';

            this.isFlipBook = true;
      		this.flash = true;
            var hRef = sLinkHref;
			var flipBookID = hRef.split('=');
            this.fz_assetName = flipBookID[1];
			
	this.modalflashvars =
	{
            'assetName': this.fz_assetName,
			'showInfo': '0',
			'closeModal': 'yes'
		};

  this.modalparams = {
  
			'menu': 'false',
			'allowscriptaccess':  'sameDomain',
			'BASE':  '.',
			'bgcolor':  '#FFFFFF',
			'wmode':  'transparent'
   };

   this.modalattributes = {
      'id':  'meflash',
      'name': 'meflash'
   };

}

	
},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.morph({'width':this.options.contentsWidth, 'marginLeft': this.options.contentsWidth/-2});
			break;
			this.step++;
		case 2:
			this.center.setStyle('background','#fff');
            // need a no flash get option
		this.center.innerHTML = "<div id=\"noFlashDiv\"><a href='http://get.adobe.com/flashplayer/' target='_blank' border='0'><img src='/images/getFlash.gif' border='0' style='padding-top:" +((this.center.clientHeight/2)-60)+ "px;'></a></div>";
			//this.flash ? this.so.write(this.center) : this.center.setHTML(this.other) ;
           if(this.isVideo == true){
            this.flash ? swfobject.embedSWF('/video/swf/player.swf', 'noFlashDiv', this.options.contentsWidth , this.options.contentsHeight, '10', 'false', this.modalflashvars, this.modalparams, this.modalattributes) : this.center.setHTML(this.other) ;
            }else if(this.isZoom == true){
        this.flash ? swfobject.embedSWF('/f_widgets/zoomBigins/zoomBigins.swf', 'noFlashDiv', this.options.contentsWidth , this.options.contentsHeight, '7', 'false', this.modalflashvars, this.modalparams, this.modalattributes) : this.center.setHTML(this.other) ;
        
        }else if(this.isFlipBook == true){
        this.flash ? swfobject.embedSWF('/f_widgets/zoomFlipBooks/zoomFlipBooks.swf', 'noFlashDiv', this.options.contentsWidth , this.options.contentsHeight, '7', 'false', this.modalflashvars, this.modalparams, this.modalattributes) : this.center.setHTML(this.other) ;
        
        }
			this.bottomContainer.setStyles({top: (this.top + this.center.clientHeight)+'px', height: '0px', marginLeft: this.center.style.marginLeft, width: this.options.contentsWidth+'px',display: ''});
			if (this.options.animateCaption){
				this.bottomContainer.style.height = '';
				this.bottom.setStyle('margin-top', -this.bottom.offsetHeight);
				this.bottom.tween("margin-top", 0);
				break;
			}
			this.bottomContainer.style.height = '';
			this.step++;
		}
	},

	close: function(){
  swfobject.removeSWF('meflash');
		this.overlay.fade('out');
		this.center.style.display = this.bottomContainer.style.display = 'none';
        
		this.center.innerHTML = '';
		return false;
        
        
	}

};



window.addEvent('load', Videobox.init.bind(Videobox));
