;(function($){

	$.fn.ajaxLoad = function(options){

		var settings = {
			'closeTxt' : 'Close',
			'placeholderHeight' : '700px',
			'callback' : function(){}
		}

		if (options) {
			$.extend(settings, options);
		}

		this.each(function(){

			var jThis = $(this);

			jThis.click(function(e){

				e.preventDefault();
				var box,
					jCloseBtn = $('<a href="#" class="close" >'+settings.closeTxt+'</a>'),
					jParent = jThis.closest('.box'),
					jPreviousBox = jParent.next('.new-box');

				if (!jPreviousBox.length) {
					
					// If no previous box, create one
					box = $('<div class="box loading"></div>')
						.insertAfter(jParent)
						.hide()
						.css({'height' : settings.placeholderHeight})
						.slideDown();

				} else {
					
					// If a box is already present, hide its content and add loading class
					box = jPreviousBox
						.find('>*')
							.fadeOut(200, function(){
								$(this)
									.css({'visibility': 'hidden'})
									.show();
							})
						.end()
						.addClass('loading');

				}

				$.ajax({
					'url' : jThis.attr('href'),
					'data' : {'ajax' : 1},
					'success' : function(data){

						var newBox = $(data).addClass('new-box'),
							prevHeight = box.height(),
							boxHeight;
						
						// Insert the new box and hide it
						newBox
							.insertAfter(jParent)
							.find('>*')
							.css({'visibility': 'hidden'});
						
						// Get the new box height
						boxHeight = newBox.height();

						// Remove the previous box
						box.remove();

						// Set the height of the new box to the height of the previous box in order to animate it to its actual height, then show its content
						newBox
							.css({
								'height' : prevHeight,
								'overflow' : 'hidden'
							})
							.animate({
								height:boxHeight
							}, 500)
							.find('>*')
								.hide()
								.css({'visibility': 'visible'})
								.fadeIn(100);
						
						// Append the close button and bind the close event
						jCloseBtn
							.prependTo(newBox)
							.click(function(e){
								e.preventDefault();
								newBox.slideUp(200, function(){
									$(this).remove();
								});
							})
						
						settings.callback(newBox);

					}
				})
			})
			return(this);
		})
	};

	$.fn.clickableRow = function() {
		this.each(function(){
			var jTable = $(this),
				jLinks = jTable.find('tr a:first-child');
			jLinks.each(function(){
				var jThis = $(this),
					jRow = $(this).closest('tr');
				jRow.click(function(e){
					window.location=jThis.attr('href');
				});
			});
		});
		return this;
	}

	function videosCallback(jBox){

        var jPlayer = jBox.find('.vimeoplayer'),
    		fPlayer = $f(jPlayer[0]);

    	function pauseOtherVideos() {
    		$('.vimeoplayer').not(jPlayer).each(function(){
	        	$f(this).api('pause');
	        });
    	}

   		fPlayer.addEvent('ready', function(){
   			fPlayer.addEvent('play', pauseOtherVideos);
	   		fPlayer.api('play');
	   	});

	}

	$(function(){
		$('.songs-list a ').ajaxLoad();
		$('.box-video .play-video a').ajaxLoad({
			'placeholderHeight' : '243px',
			'callback' : videosCallback
		});
		$('#pf_sortableTable1').clickableRow();
	});

}(jQuery));
