var Videobox = {

	init: function (options) {
		// init default options
		this.options = Object.extend({
			resizeDuration: 400,	// Duration of height and width resizing (ms)
			initialWidth: 300,		// Initial width of the box (px)
			initialHeight: 150,		// Initial height of the box (px)
			defaultWidth: 480,		// Default width of the box (px)
			defaultHeight: 400,	// Default height of the box (px)
			animateCaption: true,	// Enable/Disable caption animation
			flvplayer: 'swf/flvplayer.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',padding: '5px'}).injectInside(document.body);
		
		this.caption=new Element('div').setProperty('id', 'lbCaption').setStyles({float: 'left'}).injectInside(this.center);
		this.closeBut=new Element('div').setProperty('id', 'lbCaptionx').setStyles({float: 'right'}).injectInside(this.center);
		this.closeButton = new Element('a').setProperties({id: 'lbCloseLink', href: '#'}).injectInside(this.closeBut);
		this.closeButton.onclick =  this.close.bind(this);
		
		this.videoDiv=new Element('div').setProperty('id', 'lbVideoId').injectInside(this.center);
		this.bottomContainer = new Element('div').setProperty('id', 'lbBottomContainer').setStyles({display: 'none',padding: '5px'}).injectInside(document.body);
		this.bottom = new Element('div').setProperty('id', 'lbBottom').injectInside(this.bottomContainer);
		
		//this.share = new Element('div').setProperty('id', 'lbShare').injectInside(this.bottomContainer);
		//this.share.innerHTML="<br><div><img src='/images/share.png'></div><div><input value='Enter email address' onfocus='Videobox.blurOutfield(this)' onblur='Videobox.validEmail(this)' type='text' id='vbfemail' class='' style='width:370px;height:15px;marginL0px;border: 1px solid #888888;color:#999999;font-size:11px;'> <img src='/images/go.png' style='cursor:pointer;' onclick='Videobox.sendShare()' align='absmiddle'></div><div style='padding-top:5px;'><img src='/images/embed.png'</div><div style='padding-bottom:10px;'><input type='text' id='embedID' style='width:370px;height:15px;border: 1px solid #888888;color:#999999;font-size:11px;' onClick='this.select();'></div>";
	
		this.desp = new Element('div').setProperty('id', 'lbCaptionsm').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.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			center: this.center.effects({duration: 500, transition: Fx.Transitions.sineInOut, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 400})
		};

	},
	sendShare:function(){
		var xval=document.getElementById("vbfemail").value;
		if(xval=="" || xval=="Enter email address"){
			alert("Please enter an email address to share.");	
			return;
		}
		
		var xmlhttp=null;
		if (window.XMLHttpRequest)
		  {// code for Firefox, Mozilla, IE7, etc.
		  xmlhttp=new XMLHttpRequest();
		  }
		else if (window.ActiveXObject)
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }

		var url="/Ajaxcall/sendShare.cfm?femail="+xval+"&vid=1&nc="+Math.random();
  		xmlhttp.open("GET",url,false);
  		xmlhttp.send(null);
		if(xmlhttp.responseText.indexOf("error")>-1){
			alert("There was an error trying to send the email.\nPlease try again.");	
			return;
		}
		alert("An Email was sent to your friend's email regarding this video.");
		
	},
	
	validEmail:function(sobj){
		if(sobj.value==""){
			sobj.value="Enter email address";	
		}else{
			
		}
		
	},
	blurOutfield:function(sobj){
		if(sobj.value!="Enter email address"){
		}else{
		sobj.value="";
		}
	},
	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()+150;// + (Window.getHeight() / 15);
		this.center.setStyles({top: this.top+'px', display: ''});
		this.fx.overlay.start(0.8);
		this.step = 1;
		this.center.setStyle('background','#fff url(loading.gif) no-repeat center');

		if(this.title.indexOf("|")>-1){
			var tempV=this.title.split("|");
			var xtitle=tempV[0];
			var xdesp=tempV[1];
		}else{
			var xtitle=this.title;
			var xdesp="";
		}
		this.caption.innerHTML = xtitle;
		//this.desp.innerHTML=xdesp;
		this.fx.center.start({'height': [385]});
	},

	setup: function(){
		var aDim = this.rel.match(/[0-9]+/g);
		this.options.contentsWidth = 452;//(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': window.getScrollTop()+'px', 'height': window.getHeight()+'px'});
			var h = window.getScrollHeight()+'px'; 
			var w = window.getScrollWidth()+'px';
			this.overlay.setStyles({top: '0px', height: h, width: w}); 
	
	},

	video: function(sLinkHref){
		
		this.flash=false;
			var embstr="<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=451 height=380   id=\"hartv_video\" align=\"middle\"><param name=\"movie\" value=\""+sLinkHref+"\" /><param name=\"allowFullScreen\"  value=\"True\"><param name=\"quality\" value=\"high\" /><embed src=\""+sLinkHref+"\" quality=\"high\" bgcolor=\"#ffffff\" width=451 height=380 name=video align=middle  type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /></object>";
this.other=embstr;
//<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=451 height=380   id=\"hartv_video\" align=\"middle\"><param name=\"movie\" value=\""+sLinkHref+"\" /><param name=\"quality\" value=\"high\" /><embed src=\""+sLinkHref+"\" quality=\"high\" bgcolor=\"#ffffff\" width=451 height=380 name=video align=middle  type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /></object>";
	
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.fx.center.start({'width': [this.options.contentsWidth], 'marginLeft': [this.options.contentsWidth/-2]});
			break;
			this.step++;
		case 2:
			this.center.setStyle('background','#fff');
			this.flash ? this.so.write(this.videoDiv) : this.videoDiv.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.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				//alert(this.videoDiv.innerHTML);
			//	document.getElementById("embedID").value=this.videoDiv.innerHTML;
			break;
		
		}
			this.bottomContainer.style.height = '';
			this.step++;
		
		}
	},


	close: function(){
	
		this.fx.overlay.start(0);
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.videoDiv.innerHTML = '';
		return false;
	}

};


window.addEvent('domready', Videobox.init.bind(Videobox));