$(function() {
		
	var youtube_url = "http://gdata.youtube.com/feeds/api/users/thepinkertones?callback=?";
	var youtube_url_video_destacado = "";
	var link_usuario = "http://www.youtube.com/user/thepinkertones?feature=mhum";
	
	var $videoHomeContainer 	= $('#widgetYoutubeImagen');
	
	var video;
		
	$(document).ready(function() {
		$videoHomeContainer.css("display", "none");
		$videoHomeContainer.fadeOut("0", "linear");
		
		
		$.getJSON(youtube_url, function (xml) { 
			$(xml).find("link").each(function(){
				if ($(this).attr("rel") == "http://gdata.youtube.com/schemas/2007#featured-video") {
					youtube_url_video_destacado = $(this).attr("href");
				}
			});
			
			if (youtube_url_video_destacado != "") {
				//cogemos el id del video
				var partesLink = youtube_url_video_destacado.split("/videos/");				
				if (partesLink.length > 1) {
					var idVideo = partesLink[1];
					
					$('#youtubeVideoDestacado').attr('src', 'http://www.youtube.com/embed/' + idVideo + '?rel=0');
					
					$videoHomeContainer.fadeIn("100", "linear");
				}
			}
		});
		
	});
	
	function resizeYoutube($image){
		var widthMargin	 = 0; 
		var windowW      = 400 - widthMargin;
		var heightMargin = 0;
		var windowH      = 320 - heightMargin;		
		var theImage     = new Image();
		theImage.src     = $image.attr("src");
		var imgwidth     = theImage.width;
		var imgheight    = theImage.height;
		
		if((imgwidth > windowW)||(imgheight > windowH)){
			if(imgwidth > imgheight){
				var newwidth 	= windowW;
				var ratio 		= imgwidth / windowW;
				var newheight	= imgheight / ratio;
				theImage.height = newheight;
				theImage.width	= newwidth;
				if(newheight>windowH){
					var newnewheight= windowH;
					var newratio 	= newheight/windowH;
					var newnewwidth = newwidth/newratio;
					theImage.width 	= newnewwidth;
					theImage.height	= newnewheight;
				}
			}
			else{
				var newheight = windowH;
				var ratio = imgheight / windowH;
				var newwidth = imgwidth / ratio;
				theImage.height = newheight;
				theImage.width= newwidth;
				if(newwidth>windowW){
					var newnewwidth = windowW;
					var newratio = newwidth/windowW;
					var newnewheight =newheight/newratio;
					theImage.height = newnewheight;
					theImage.width= newnewwidth;
				}
			}
		}
		
		//para calcular el margin que debemos dejar para centrar la imagen
		var sobranteW = windowW - theImage.width;
		var sobranteH = windowH - theImage.height;
		var marginW = (sobranteW / 2);
		var marginH = (sobranteH / 2);
		
		$image.css({'width':theImage.width+'px','height':theImage.height+'px'});
		$image.css({'margin-left':marginW+'px','margin-top':'12px'});
	}	
	
});
