/*
function getVStats(ytid, xid)
{
	jQuery.ajax({
		url: ajax_url,
		data: {_q:'vstats', ytid:ytid, xid:xid},
		dataType: "json",
		success: function(response, textStatus){
			jQuery("#commentCount").text(response.comment_count);
			jQuery("#viewCount").text(response.view_count);
		}
	});
}


function getYoutubeViewCount(ytid)
{
	var data_url = "http://gdata.youtube.com/feeds/api/videos/" + ytid + "?v=2&alt=jsonc";
	//var data_url = "http://gdata.youtube.com/feeds/api/users/<?php echo YOUTUBE_USERID ?>?v=2";

	jQuery.ajax({
		url: data_url,
		dataType: "json",
		success: function(response, textStatus){
			//alert("ok: " + response.data.viewCount);
			jQuery("#viewCount").text(response.data.viewCount);
		}
	});
}
*/

function updateVideo(VIDEO)
{
	//jQuery('div.tip-video-cont div.youtube-player').html('<iframe class="youtube-player" type="text/html" src="'+VIDEO.src+'?hl=es_ES&amp;autoplay=1" frameborder="0"></iframe>');
	if(VIDEO.has_frame == 1)
		jQuery('div.tip-video-cont div.youtube-player').html('<a href="#" onclick="playVideo(\''+VIDEO.src+'\');return false;" rel="'+VIDEO.src+'" title="Click para reproducir"><img src="'+VIDEO.frame+'" alt="" style="margin-top:0;" /></a>');
	else
		jQuery('div.tip-video-cont div.youtube-player').html('<a href="#" onclick="playVideo(\''+VIDEO.src+'\');return false;" rel="'+VIDEO.src+'" title="Click para reproducir"><img src="'+VIDEO.frame+'" alt="" /></a>');
	//jQuery('div.tip-header div.like').html('<iframe src="http://www.facebook.com/plugins/like.php?href=' + escape(VIDEO.url) + '&amp;layout=button_count&amp;show_faces=false&amp;action=like&amp;font=tahoma&amp;colorscheme=light&amp;width=100&amp;height=30" scrolling="no" frameborder="0" style="border:none; width:100px; height:30px;" allowTransparency="true"></iframe>');
	//getYoutubeViewCount(VIDEO.youtube);

	var xid = 'TIPS-' + VIDEO.youtube;
	jQuery.ajax({
		url: ajax_url,
		data: {_q:'vstats', ytid:VIDEO.youtube, xid:xid},
		dataType: "json",
		success: function(response, textStatus){
			jQuery("#commentCount").text(response.comment_count);
			jQuery("#viewCount").text(response.view_count);
		}
	});

	if ( jQuery('div.fb-comments').is('div') ) {
		jQuery('div#fb-comments').html('<fb:comments xid="'+xid+'" numposts="10" width="550" simple="1" css="'+stylesheet_directory+'/css/fb_comments.css?2"></fb:comments>');
		if(is_FB_inited) FB.XFBML.parse(document.getElementById('fb-comments'));
	}
}

function playVideo(vid)
{
	jQuery('div.tip-video-cont div.youtube-player').html('<iframe class="youtube-player" type="text/html" src="'+vid+'?hl=es_ES&amp;autoplay=1" frameborder="0"></iframe>');
}

var vPlayer = {
	i: 0
	,
	total: 0
	,
	list: []
	,
	init: function(list)
	{
		this.list  = list;
		this.total = list.length;
		//document.getElementById('vid_total').setTextValue(this.total);
	}
	,
	loadIndex: function(i)
	{
		updateVideo(this.list[i]);
	}
	,
	next: function(inc)
	{
		this.i = this.i + inc;

		if(this.i < 0)
			this.i = this.total - 1;
		else if(this.i >= this.total)
			this.i = 0;

		this.loadIndex(this.i);
	}
}


jQuery(document).ready(function(){


	if ( jQuery('div.tip-video-cont').is('div') && videos_list.length > 0 ) {
		vPlayer.init(videos_list);
		var video_index = 0;
		if(video_id != '')
		{
			for(var i=0; i<videos_list.length; i++)
			{
				if(videos_list[i]['id'] == parseInt(video_id))
				{
					video_index = i;
					break;
				}
			}
		}
		vPlayer.loadIndex(video_index);
	}

	jQuery("div.tip-video-cont a.arrow-left").click(function(){
		vPlayer.next(-1);return false;
	});

	jQuery("div.tip-video-cont a.arrow-right").click(function(){
		vPlayer.next(+1);return false;
	});

});


