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: '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', 'lbBottomContainer2').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><div><img src='/images/reportspam.png' alt='Report this video as spam' style='cursor:pointer;' onclick='Videobox.reportSpam()'></div>";
	
		this.reportDiv=new Element('div').setProperty('id','reportDiv').setStyles({display:'none'}).injectInside(this.bottomContainer);
	this.reportDiv.innerHTML="<div style='padding-top:10px;border-top:1px solid #394963;'><form name='shareForm'><div style='color:#666666;font-weight:bold;font-size:11px;'>Report item as:</div><div><select name='reportitem' style='color:#666666;font-size:11px;'><option value='Obscenity/vulgarity'>Obscenity/vulgarity</option><option value='Hate speech'>Hate speech</option><option value='Personal attack'>Personal attack</option><option value='Advertising/Spam'>Advertising/Spam</option><option value='Copyright/Plagiarism'>Copyright/Plagiarism</option><option  value='Other'>Other</option></select></div><div style='color:#666666;font-weight:bold;font-size:11px;padding-top:5px;'>Comment</div><div><textarea name='ucomment' cols=44 rows=4 style='color:#666666;font-size:11px;'></textarea> &nbsp;<img src='/images/go.png' style='cursor:pointer;' onclick='Videobox.reportSpam(document.shareForm);'>&nbsp;<img src='/images/x.png' style='cursor:pointer;' onclick='Videobox.cancelReport();'></div></form></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})
			
		};

	},
	
	cancelReport:function(){
		
		this.reportDiv.setStyle('display','none');
	},
	reportSpam:function(sForm){
		
		this.reportDiv.setStyle('display','block');
		
		if(typeof(sForm)!="undefined"){
		
		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/sendReport.cfm?ritem="+sForm.reportitem.value+"&ucomment="+sForm.ucomment.value+"&vid="+this.vID+"&nc="+Math.random();
  		xmlhttp.open("GET",url,false);
  		xmlhttp.send(null);
		alert("An email was sent to the webmaster regarding this video.");
		this.reportDiv.setStyle('display','none');
		}
				
	}
	,
	incrementCount:function(vID){

		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/increaseCount.cfm?vID="+vID;
  		xmlhttp.open("GET",url,false);
  		xmlhttp.send(null);
	
	
	},

	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="+this.vID+"&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.");
		document.getElementById("vbfemail").value="Enter email address";
		
	},
	
	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(vID,sLinkHref, sLinkTitle, sLinkRel) {
		//INCREASE COUNT
		this.incrementCount(vID);
		this.vID=vID;
		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.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 = (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){
		if (sLinkHref.match(/youtube/i)) {
      this.flash = true;
	  var vURL=sLinkHref+"&fs=1&hl=en&rel=0&color1=0x3a3a3a&color2=0x999999&autoplay=1&ap=%2526fmt%3D18";
			var hRef = sLinkHref;
			var videoId = hRef.split('=');
			this.videoID = videoId[1];
			this.so = new SWFObject(vURL, "flvvideo", this.options.contentsWidth, this.options.contentsHeight, "0");
			this.so.addParam("wmode", "transparent");
			this.so.addParam("allowfullscreen","true")
		}
		/*else if (sLinkHref.match(/html/i) || sLinkHref.match(/cfm/i) || sLinkHref.match(/php/i) || sLinkHref.match(/asp/i) || sLinkHref.match(/aspx/i) || sLinkHref.match(/htm/i) || sLinkHref.match(/jsp/i)) {
			this.flash=false;
			this.other='<img src="/images/externalvideo.gif" style="border:1px solid #cccccc;">';//'<div style="font-weight:bold;font-size:14px;text-align:center;">Loading External Video</div>';
			//this.center.setStyle('background','#fff url(/images/externalvideo.gif) no-repeat center');
			setTimeout("window.open('"+sLinkHref+"')",2000);
			
		}*/
		
		else if (sLinkHref.match(/metacafe\.com\/watch/i)) {
      this.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('/');
			this.videoID = videoId[4];
			this.so = new SWFObject("http://www.metacafe.com/fplayer/"+this.videoID+"/.swf", "flvvideo", this.options.contentsWidth, this.options.contentsHeight, "0");
			this.so.addParam("wmode", "transparent");
		}
		else if (sLinkHref.match(/google\.com\/videoplay/i)) {
      this.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('=');
			this.videoID = videoId[1];
			this.so = new SWFObject("http://video.google.com/googleplayer.swf?docId="+this.videoID+"&hl=en", "flvvideo", this.options.contentsWidth, this.options.contentsHeight, "0");
			this.so.addParam("wmode", "transparent");
		}
		else if (sLinkHref.match(/ifilm\.com\/video/i)) {
		  this.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('video/');
			this.videoID = videoId[1];
			this.so = new SWFObject("http://www.ifilm.com/efp", "flvvideo", this.options.contentsWidth, this.options.contentsHeight, "0", "#000");
			this.so.addVariable("flvbaseclip", this.videoID+"&");
			this.so.addParam("wmode", "transparent");
		}
		else if (sLinkHref.match(/\.mov/i)) {
		  this.flash = false;
			if (navigator.plugins && navigator.plugins.length) {
          this.other ='<object id="qtboxMovie" type="video/quicktime" codebase="http://www.apple.com/qtactivex/qtplugin.cab" data="'+sLinkHref+'" width="'+this.options.contentsWidth+'" height="'+this.options.contentsHeight+'"><param name="src" value="'+sLinkHref+'" /><param name="scale" value="aspect" /><param name="controller" value="true" /><param name="autoplay" value="true" /><param name="bgcolor" value="#000000" /><param name="enablejavascript" value="true" /></object>';
      } else {
        this.other = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="'+this.options.contentsWidth+'" height="'+this.options.contentsHeight+'" id="qtboxMovie"><param name="src" value="'+sLinkHref+'" /><param name="scale" value="aspect" /><param name="controller" value="true" /><param name="autoplay" value="true" /><param name="bgcolor" value="#000000" /><param name="enablejavascript" value="true" /></object>';
      }
		}
		else if (sLinkHref.match(/\.wmv/i) || sLinkHref.match(/\.asx/i)) {
		this.flash = false;
		 this.other = '<object NAME="Player" WIDTH="'+this.options.contentsWidth+'" HEIGHT="'+this.options.contentsHeight+'" align="left" hspace="0" type="application/x-oleobject" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"><param NAME="URL" VALUE="'+sLinkHref+'"><param><param NAME="AUTOSTART" VALUE="false"></param><param name="showControls" value="true"></param><embed WIDTH="'+this.options.contentsWidth+'" HEIGHT="'+this.options.contentsHeight+'" align="left" hspace="0" SRC="'+sLinkHref+'" TYPE="application/x-oleobject" AUTOSTART="false"></embed></object>'
		}
		else if (sLinkHref.match(/\.flv/i)) {
		 this.flash = true;
		 this.so = new SWFObject(this.options.flvplayer+"?file="+sLinkHref, "flvvideo", this.options.contentsWidth, this.options.contentsHeight, "0", "#000");
		}
		else {
			
				this.flash=false;
			this.other='<img src="/images/externalvideo.gif" style="border:1px solid #cccccc;">';//'<div style="font-weight:bold;font-size:14px;text-align:center;">Loading External Video</div>';
			//this.center.setStyle('background','#fff url(/images/externalvideo.gif) no-repeat center');
			setTimeout("window.open('"+sLinkHref+"')",2000);
		  //this.flash = true;
			//this.videoID = sLinkHref;
			//this.so = new SWFObject(this.videoID, "flvvideo", this.options.contentsWidth, this.options.contentsHeight, "0");
		}
	},

	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 = '';
				this.reportDiv.setStyle('display','none');
		return false;
	}

};


window.addEvent('domready', Videobox.init.bind(Videobox));
