/*

 * jGFeed 1.0 - Google Feed API abstraction plugin for jQuery

 *

 * Copyright (c) 2009 jQuery HowTo

 *

 * Licensed under the GPL license:

 *   http://www.gnu.org/licenses/gpl.html

 *

 * URL:

 *   http://jquery-howto.blogspot.com

 *

 * Author URL:

 *   http://me.boo.uz

 *

 */

(function($){$.extend({jGFeed:function(url,fnk,num,key){if(url==null){return false;}var gurl="http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+url;if(num!=null){gurl+="&num="+num;}if(key!=null){gurl+="&key="+key;}$.getJSON(gurl,function(data){if(typeof fnk=="function"){fnk.call(this,data.responseData.feed);}else{return false;}});}});})(jQuery);



/*

 * jYoutube 1.0 - YouTube video image getter plugin for jQuery

 *

 * Copyright (c) 2009 jQuery Howto

 *

 * Licensed under the GPL license:

 *   http://www.gnu.org/licenses/gpl.html

 *

 * Plugin home & Author URL:

 *   http://jquery-howto.blogspot.com

 *

 */

(function($){$.extend({jYoutube:function(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"}}})})(jQuery);



$.fn.center = function (relation) {

	var center_in = (relation) ? $(relation) : $(window);

        

	this.css("position","absolute");

	this.css("top", ( center_in.height() - this.height() ) / 2+center_in.scrollTop() + "px");

	this.css("left", ( center_in.width() - this.width() ) / 2+center_in.scrollLeft() + "px");

	this.css("z-index", "200");

	return this;

};







function play(id){

   var html  = '';

   html += '<object height="390" width="640">';

   html += '<param name="movie" value="http://www.youtube.com/v/'+id+'&autoplay=1"></param>';

   html += '<param name="wmode" value="transparent"></param>';

   html += '<param name="allowfullscreen" value="true"></param>'; 

   html += '<embed src="http://www.youtube.com/v/'+id+'&autoplay=1"';

   html += 'allowfullscreen="true"';

   html += 'type="application/x-shockwave-flash" wmode="transparent" height="390" width="640"></embed>';

   html += '</object>';

   return html;

};



function youtubeid(url) {

	var ytid = url.match("[\\?&]v=([^&#]*)");

	ytid = ytid[1];

	return ytid;

};



function createModal(){

	var overlay = $('<div id="overlay"></div>');

		modalVideo = $('<div id="modal-video"></div>');

	$("body").append(overlay);

	$("body").append(modalVideo);

	$(window).bind('resize', function(){

		modalVideo.center();							  

	});

}



function closeModal(){

	$('#overlay').click(function(){

		$("#overlay").fadeOut(200);

    	$('#modal-video').css({display:'none'});

		$('#video').remove();						 

	});

}



function openVideo(){

	$("#youtube a").click(function() {

		$.fancybox({

				'padding'		: 0,

				'autoScale'		: false,

				'overlayColor'	: '#000',

           		'overlayOpacity'	: 0.7,

				'transitionIn'	: 'none',

				'transitionOut'	: 'none',

				'title'			: this.title,

				'width'		: 680,

				'height'		: 495,

				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),

				'type'			: 'swf',

				'swf'			: {

					 'wmode'		: 'transparent',

					'allowfullscreen'	: 'true'

				}

			});

	

		return false;

});

}



function truncate(text,limit){

	if(text.length>limit){

		limit--;

		last = text.substr(limit-1,1);

		while(last!=' ' && limit > 0){

			limit--;

			last = text.substr(limit-1,1);

		}

		last = text.substr(limit-2,1);

		if(last == ',' || last == ';'  || last == ':'){

			 text = text.substr(0,limit-2) + '...';

		} else if(last == '.' || last == '?' || last == '!'){

			 text = text.substr(0,limit-1);

		} else {

			 text = text.substr(0,limit-1) + '...';

		}

	}

	return text;

}



function gallery(user, size){

	$.jGFeed('http://gdata.youtube.com/feeds/base/users/'+user+'/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile',function(feeds){

		if(!feeds){

			return false;

		}



		var html = '';

		

		for(var i=0; i<feeds.entries.length; i++){

			var entry = feeds.entries[i];

				video = entry.link;

				imgUrl = $.jYoutube(entry.link);

				title = entry.title;

			

			html +='<li class="grid_3"><div class="thumbs"><a href="'+video+'" title="'+title+'"><img src="'+imgUrl+'" alt="'+title+'" title="'+title+'" /><span class="subtitles-video">'+truncate(title,30)+'</span></a></div></li>';



		}

		$('#youtube').html(html);

		createModal();

		openVideo();

		closeModal();

	}, size);

}



(function(){

	gallery('visaomedica', 20);

})();
