$(document).ready(function() {
	Cufon.replace(
		'#content h1:not(.ignore), #content h2:not(.ignore), #content h3:not(.ignore), .banner p:not(.ignore), .cufon',
		{
			hover: true
		}
	); // Works without a selector engine
	
	$(".video-banner").each(function(){
		var videoSrc = parseVideoURL($(this).attr("id"));
		var videoID = videoSrc["id"];
		var videoProvider = videoSrc["provider"];
		$obj = $(this);
		
		if(videoProvider=="youtube"){
			//var htmlSrc = '<object style="height: 390px; width: 640px"><param name="movie" value="http://www.youtube.com/v/'+videoID+'"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/'+videoID+'" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390"></object>';
			
			var htmlSrc = '<iframe width="640" height="390" src="http://www.youtube.com/embed/'+videoID+'?rel=0" frameborder="0" allowfullscreen></iframe>';
			
			
		}else{
			var htmlSrc = '<iframe src="http://player.vimeo.com/video/'+videoID+'" width="640" height="390" frameborder="0"></iframe>';
			}

		$(this).css("cursor","pointer");

		$(this).click(function(e){
			e.preventDefault();
			
			$.colorbox({
				html:htmlSrc,
				opacity: 0.75,
				width:640,
				height:425
			});
		});
	});
	
	
	$(".video-box").each(function(){
		var videoSrc = parseVideoURL($(this).attr("id"));
		var videoID = videoSrc["id"];
		var videoProvider = videoSrc["provider"];
		$obj = $(this);
		
		if(videoProvider=="youtube"){
			var htmlSrc = '<object style="height: 260px; width: 460px"><param name="movie" value="http://www.youtube.com/v/'+videoID+'"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/'+videoID+'" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="460" height="260"></object>';
		}else{
			var htmlSrc = '<iframe src="http://player.vimeo.com/video/'+videoID+'" width="460" height="260" frameborder="0"></iframe>';
			}

		$(this).html(htmlSrc);

	});
	
	$(".video-box-col-3").each(function(){
		var videoSrc = parseVideoURL($(this).attr("id"));
		var videoID = videoSrc["id"];
		var videoProvider = videoSrc["provider"];
		$obj = $(this);
		
		if(videoProvider=="youtube"){
			var htmlSrc = '<object style="height: 220px; width: 390px"><param name="movie" value="http://www.youtube.com/v/'+videoID+'"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/'+videoID+'" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="390" height="220"></object>';
		}else{
			var htmlSrc = '<iframe src="http://player.vimeo.com/video/'+videoID+'" width="390" height="220" frameborder="0"></iframe>';
			}

		$(this).html(htmlSrc);

	});
	
	
	
	
	
	
	$(".youtube-video").each(function(){
		var videoSrc = parseVideoURL($(this).attr("id"));
		var videoID = videoSrc["id"];
		var videoProvider = videoSrc["provider"];
		var imgUrl   ="";
		$obj = $(this);
		
		if(videoProvider=="youtube"){
			imgUrl   = getScreen(videoID);
			var htmlSrc = '<object style="height: 295px; width: 480px"><param name="movie" value="http://www.youtube.com/v/'+videoID+'"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/'+videoID+'" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="480" height="295"></object>';
			$obj.css("background","url("+imgUrl+") no-repeat right");
		}else{
			
			
			$.getJSON('http://www.vimeo.com/api/v2/video/' + videoID + '.json?callback=?', {format: "json"}, function(data) {
				imgUrl   =  data[0].thumbnail_large;
				$obj.css("background","url("+imgUrl+") no-repeat right");
			});
			

			var htmlSrc = '<iframe src="http://player.vimeo.com/video/'+videoID+'" width="400" height="150" frameborder="0"></iframe>';
			}

		$(this).css("cursor","pointer");
		
		
		
		
		$(this).click(function(e){
			e.preventDefault();
			
			$.colorbox({
				html:htmlSrc,
				opacity: 0.75,
				width:480,
				height:336
			});
		});
	});
});


/************************************
	 *	RETURNS A YOUTUBE SCREENSHOOT
	 ************************************/
function getScreen( url, size ){
	if(url === null){ return ""; }
	
	size = (size === null) ? "big" : size;
	var vid;
	var results;
	
	results = url.match("[\\?&]v=([^&#]*)");
	
	vid = ( results === null ) ? url : results[1];
	
	if(size == "small"){
		return "http://img.youtube.com/vi/"+vid+"/2.jpg";
	}else {
		return "http://img.youtube.com/vi/"+vid+"/0.jpg";
	}
}

/************************************
	 *	RETURNS A VIDEO ID FROM EITHER YOUTUBE OR VIMEO URL
	 ************************************/
function parseVideoURL(url) {

    var provider = url.match(/http:\/\/(:?www.)?(\w*)/)[2],
        id;

    if(provider == "youtube") {

        id = url.match(/http:\/\/(?:www.)?(\w*).com\/.*v=(\w*)/)[2];
    } else if (provider == "vimeo") {

        id = url.match(/http:\/\/(?:www.)?(\w*).com\/(\d*)/)[2];
    } else {
        id = null  
    }
    return {
        provider : provider,
        id : id
    }
}

