String.prototype.parseURL = function(query) {
	var url=this,rx=/^((?:ht|f|nn)tps?)\:\/\/(?:([^\:\@]*)(?:\:([^\@]*))?\@)?([^\/]*)([^\?\#]*)(?:\?([^\#]*))?(?:\#(.*))?$/,rg=[null,'scheme','user','pass','host','path','query','fragment'],r=url.match(rx),i,q,ret={};
	if (r==null) return ret;
	for (i=1; i<rg.length; i++)
		if (r[i]!=undefined)
			ret[rg[i]]=r[i];       
	if (ret.path=='') ret.path='/';
	if (query!=undefined && r[6]!=undefined) {
			var q=r[6];
			ret.query={};
			q=q.split('&');
				for (var i=0; i<q.length; i++) {
				q[i]=q[i].split('=',2);
				ret.query[unescape(q[i][0])]=unescape(q[i][1]);
			}
		}
	
	return ret;
}

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

var flv = {
	allowedHosts: Array('www.youtube.com', 'youtube.com', 'www.blip.tv', 'blip.tv', 'www.metacafe.com', 'metacafe.com', 'www.vimeo.com', 'vimeo.com'),

	sanityCheck: function(){
		var videoUrl = $('input#video-url').val();
		var parsedURL = videoUrl.parseURL();
		var hostURL = parsedURL.host;
		if(flv.allowedHosts.inArray(hostURL)){
			flv.getDetails(videoUrl);
		}
		else{
			jQuery.facebox('<span style="font-weight:bold;">Sorry, not a valid URL.</span> <br/><br/>Example of valid URL for YouTube is http://www.youtube.com/watch?v=y56quTJ88O0');
		}
		
		$('input#video-url').attr("value","");
	},
	
	getDetails: function(videoUrl){
		jQuery.facebox(function() {
		  jQuery.get('getVideo.php?videoURL=' + videoUrl, function(data) {
			  jQuery.facebox(data);
		  })
		});
	}
};

jQuery(document).ready(function() {
	jQuery('#youtube-carousel').jcarousel({
		//auto: 5,
		scroll: 1,
		wrap: 'last',
		buttonNextHTML: null,
        buttonPrevHTML: null,
        initCallback: function(carousel){
			jQuery('#youtube-carousel-next').bind('click', function() {
		        carousel.next();
		        carousel.startAuto(0);
		        return false;
		    });
	
		    jQuery('#youtube-carousel-previous').bind('click', function() {
		        carousel.prev();
		        carousel.startAuto(0);
		        return false;
		    });
		    
		    // Pause autoscrolling if the user moves with the cursor over the clip.
		    carousel.clip.hover(
	    		function() {
	    			carousel.stopAuto();
	    		}, 
	    		function() {
	    			carousel.startAuto();
	    		}
		    );
		}
	});

	jQuery('#blip-carousel').jcarousel({
		//auto: 7,
		scroll: 1,
		wrap: 'last',
		buttonNextHTML: null,
        buttonPrevHTML: null,
        initCallback: function(carousel){
			jQuery('#blip-carousel-next').bind('click', function() {
		        carousel.next();
		        carousel.startAuto(0);
		        return false;
		    });
	
		    jQuery('#blip-carousel-previous').bind('click', function() {
		        carousel.prev();
		        carousel.startAuto(0);
		        return false;
		    });
		    
		    // Pause autoscrolling if the user moves with the cursor over the clip.
		    carousel.clip.hover(
	    		function() {
	    			carousel.stopAuto();
	    		}, 
	    		function() {
	    			carousel.startAuto();
	    		}
		    );
		}
	});

	jQuery('#metacafe-carousel').jcarousel({
		//auto: 6,
		scroll: 1,
		wrap: 'last',
		buttonNextHTML: null,
        buttonPrevHTML: null,
        initCallback: function(carousel){
			jQuery('#metacafe-carousel-next').bind('click', function() {
		        carousel.next();
		        carousel.startAuto(0);
		        return false;
		    });
	
		    jQuery('#metacafe-carousel-previous').bind('click', function() {
		        carousel.prev();
		        carousel.startAuto(0);
		        return false;
		    });
		    
		    // Pause autoscrolling if the user moves with the cursor over the clip.
		    carousel.clip.hover(
	    		function() {
	    			carousel.stopAuto();
	    		}, 
	    		function() {
	    			carousel.startAuto();
	    		}
		    );
		}
	});

	jQuery('#vimeo-carousel').jcarousel({
		//auto: 8,
		scroll: 1,
		wrap: 'last',
		buttonNextHTML: null,
        buttonPrevHTML: null,
        initCallback: function(carousel){
			jQuery('#vimeo-carousel-next').bind('click', function() {
		        carousel.next();
		        carousel.startAuto(0);
		        return false;
		    });
	
		    jQuery('#vimeo-carousel-previous').bind('click', function() {
		        carousel.prev();
		        carousel.startAuto(0);
		        return false;
		    });
		    
		    // Pause autoscrolling if the user moves with the cursor over the clip.
		    carousel.clip.hover(
	    		function() {
	    			carousel.stopAuto();
	    		}, 
	    		function() {
	    			carousel.startAuto();
	    		}
		    );
		}
	});
	
	$('a[rel*=facebox]').facebox() 
});