var userAgent = navigator.userAgent;
var is_touch = userAgent.match(/iPhone/i) || userAgent.match(/iPod/i) || userAgent.match(/iPad/i);

$(document).ready ( function() {

	//Si és un iPad/iPhone/iPod no creem instàncies de FlowPlayer, substituïm per <video>
	var videos = $("a.vwtv");
	if(is_touch)
	{
		install_html5video(videos);
	}
	else
	{
		install_player(videos);
	}
});


var controls =
{
	url: 'flowplayer.controls.swf',
	autoHide: 'always',
	hideDelay: '2000',
	backgroundColor: '#777777',
	backgroundGradient: 'none',
	timeColor: '#CCCCCC',
	durationColor: '#FF6600',
	progressColor: '#FF6600',
	progressGradient: 'none',
	bufferColor: '#555555',
	buttonColor: '#555555',
	buttonOverColor: '#FF6600',
	volumeSliderColor: '#222222',
	sliderColor: '#222222',
	tooltipColor: '#555555',
	tooltips: {
		buttons: true,
		play: 'Reproduïr',
		pause: 'Pausa',
		mute: 'Silenciar',
		unmute: 'Activar so',
		stop: 'Aturar',
		fullscreen: 'Pantalla completa',
		fullscreenExit: 'Sortir del mode Pantalla completa',
		next: 'Següent',
		previous: 'Anterior'
	}
};


function install_html5video(obj)
{
	$(obj).each(function(i) {
		$local = $(this);
		video = this.href.replace('flv', 'mp4');
		height = $local.height();
		width = $local.width();
		poster = $local.children('img').attr('src');
		html = '<video width="'+ width +'" height="'+ height +'" poster="'+ poster +'" controls="controls" src="'+ video +'"></video>';
		$local.replaceWith(html);
	});
}


function install_player(obj)
{
	$(obj).each(function(i) {
		var $local = $(this);
		width = $local.width();
		//Si el tamany és menor de x píxels, treiem certs controls
        controls.time = true;
        controls.volume = true;
        controls.mute = true;
		var config = 
		{
			key: '#$f78c82556a36828fccf',
			play: {
				url: '/images/boto_play.png',
				width: 60,
				height: 60
			},
			clip: {
				scaling: 'fit',
                loop: true
			},
			canvas: {
				backgroundGradient: 'none',
				backgroundColor: '#252525'
			},
			plugins: {
				controls: controls,
				gatracker: {
					url: 'flowplayer.analytics.swf',
					googleId : 'UA-2810592-2'
				}
				
			},
			onLoad: function() {
				this.setVolume(80);
			},

            onBeforeFinish: function() { 
                // loop to the first clip
                this.play(0); 
                // return false so that the player will not do anything it
                // normally does with this event
                return false; 
            } 

		};
		$local.flowplayer("/flash/flowplayer.swf", config);
	});
}

