/*

## Copyright (C) 2009 Piero Randazzo - piero@cervellone.com ##

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/


$(document).ready(function() {
	
	
	function filename(url) {
		
		var m = url.match(/([^\/\\]+)\.(\w+)$/);
        if (m)
            return {name: m[1], ext: m[2]};
        else
	    return m;
	}
	
	function loadInfo(file){
		

		$('#tabs').load(file, function(){
			
			var tabs = this;
			var fragment = new Array();

			try {
				$(tabs).tabs("destroy");
			} catch(err) {}
			
			$(tabs)
				// set tabs options 
				.find('ul:first').tabs({
						selected:0,
						fx: {opacity: "toggle"}
				})
				// fill array
				.find('a').each(function(){
					fragment.push($(this).attr('href'))
				});
		
			/* set scroll for tabs */
			$.each(fragment, function(i,t){
				
				/* local anchors */
				$(t).localScroll({
						target: t,
						filter: 'a[target!=_blank]',
						onBefore: function(){	
							$(tabs).tabs().tabs('select', t);
						}
				});	
		
				/* anchors in different tabs */
				$.each(fragment, function(i,f){
					$(t).localScroll({
						target: f,
						filter: 'a[target='+f+']',
						onBefore: function(){	
							$(tabs).tabs().tabs('select', f);
						}
					});
				})	
			})


			$('.interlink a')
			 .attr('title', "Link al glossario")
			
		});
	
	}
	
	
	
	$("#playlist a").each( function(){
		
		$(this).click(function(e){
			
			e.preventDefault();
			
			$(this)
				.parents()
					.children()
						.removeClass('playing')
						.removeClass('paused')
						.removeClass('progress')
					.end()
				.end()
				.addClass('progress');
			
			loadInfo("lessons/text/" + filename(this.href).name + ".html");
			
			$f("player", {src: 'player/flowplayer-3.0.3.swf', wmode: 'transparent'},  { 
					clip:{ 
						autoPlay: false,
						autoBuffering: false,
						baseUrl: 'http://www.cervellone.com/ajaxcontest/' + 'lessons/video/',
						scaling: 'orig'
				    }, 
					play: {opacity: 0},
					playlist: [
						{
							url: 'splash_ac.png',		
							autoplay:true,
							autoBuffering: true,
							duration:0
						},
						{
							url: $(this).attr('href')
						}
					]	
				});
		});
		
	});
	
	
});




	
