document.addEvent('domready',function(){
	//create the youtubeLink popup class, no further action required
	new dataPopupOverlay();

	// add setBusy to input ui elements
	$$("input").each(function(element) {

		element.setBusy = function(isBusy) {
			this.isBusy = isBusy;
			if(isBusy) {
				// set item loading
				this.setProperty("disabled","disabled");
				// create and inject element (if needed)
				if(!$chk(element.busyDiv)) {
					element.busyDiv = new Element('div',{'class':'input_loading'});
					element.busyDiv.inject(document.body);
				}

				// compute pos
				thisCoords = this.getCoordinates();
				busyDivCoords = element.busyDiv.getCoordinates();
				element.busyDiv.position({
					x: (thisCoords.left + thisCoords.width) - busyDivCoords.width,
					y: (thisCoords.top + (thisCoords.height/2)) - (busyDivCoords.height/2)
				});
				element.busyDiv.setStyle('display','block');
			} else {
				// unset loading state
				this.removeProperty("disabled");
				if($chk(this.busyDiv)) {
					this.busyDiv = this.busyDiv.destroy();
				}
			}
		}
	});
});




// video link class
// based on overlay class (http://davidwalsh.name)
var dataPopupOverlay = new Class({
	Implements: [Options,Events],
	options: {
		id: 'overlay',
		color: '#000',
		duration: 500,
		opacity: 0.5,
		zIndex: 5000,
		canvasDefaultWidth: 600,
		canvasDefaultHeight: 385/*,
		onClick: $empty,
		onClose: $empty,
		onHide: $empty,
		onOpen: $empty,
		onShow: $empty
		*/
	},
	
	initialize: function(options) {
		this.setOptions(options);
		if (Browser.Engine.trident && Browser.Engine.version <= 6) this.ie6 = true;
		$$("*[class*=youtubeVideoPopup]").each(function(elm){
			if (elm.get('href').match("^.*youtube.com/watch\\?v=(.*)$")) {
				elm.addEvent('click', function(e) {
					e.stop();
					this.handleClick(elm);
				}.bind(this));
			}
		}.bind(this));
	},

	test: function(clickedLink){
		//e.stop();
		console.debug('fire');
	},
	
	handleClick: function(clickedElement) {
		clickedElement.get('class').split(' ').each(function(classes){
			if(classes.match("^youtubeVideoPopup(\[[0-9]*,[0-9]*\])$")) {
				var params = eval(classes.match("^youtubeVideoPopup\\[([0-9]*),([0-9]*)\]$"));
				this.canvasWidth = params[1].toInt();
				this.canvasHeight = params[2].toInt();
			} else {
				this.canvasWidth = this.options.canvasDefaultWidth,
				this.canvasHeight = this.options.canvasDefaultHeight;
			}
		}.bind(this));
		
		var youtubeID = eval(clickedElement.get('href').match("^.*youtube.com/watch\\?v=(.*)$"))[1];
		this.showOverlay().showYoutubeContent(youtubeID);
	},
	
	showOverlay: function() {
		this.overlay = new Element('div', {
			id: this.options.id,
			opacity: 0,
			styles: {
				position: (this.ie6) ? 'absolute' : 'fixed',
				background: this.options.color,
				left: 0,
				top: 0,
				'z-index': this.options.zIndex
			}
		}).inject(document.body);
		this.overlay.addEvent('click',this.handleClose.bind(this));
		this.overlay.setStyles({
			width: '100%',
			height: $(document.body).getScrollSize().y
		});

		this.tween = new Fx.Tween(this.overlay, { 
			duration: this.options.duration,
			link: 'cancel',
			property: 'opacity'
		}).start(this.options.opacity);
		return this;
	},
	
	showYoutubeContent: function(youtubeID) {
		this.canvasDiv = new Element('div', {
			'class': 'youtubeVideoPopupCanvas',
			'styles':{
				'width':this.canvasWidth,
				'height':this.canvasHeight,
				'position':'fixed',
				'top': '50%',
				'left': '50%', 
				'z-index': this.options.zIndex+1,
				'margin': '-'+(this.canvasHeight/2)+'px 0px 0 -'+(this.canvasWidth/2)+'px'
			},
			'html': '<object width="'+this.canvasWidth+'" height="'+this.canvasHeight+'"><param name="movie" value="http://www.youtube.com/v/' + youtubeID + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + youtubeID + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+this.canvasWidth+'" height="'+this.canvasHeight+'"></embed></object>'
		});
		closeBtn = new Element('div',{
			id: 'closeBtn'
		})
		closeBtn.inject(this.canvasDiv);
		closeBtn.addEvent('click',this.handleClose.bind(this));
		this.canvasDiv.inject(document.body);
		return this;
	},
	
	handleClose: function() {
		this.canvasDiv.destroy();
		this.tween.start(0).chain(this.destroyOverlay.bind(this));
	},
	
	destroyOverlay: function() {
		this.overlay.destroy();
	}
	
});


