(function($){
    $.fn.tabs = function(index){
		var container = $(this);
		var contents = container.find('div');
		var indexes = container.find(index);

		contents.addClass('tab-content');
		contents.filter(':first').addClass('active');
		contents.not(':first').hide();
		container.prepend('<ul class="tabs-links"></ul>');
		$(indexes).each(function(){
			var target = $(this).parent('div').attr('id');
			var text = $(this).text();
			container
				.children('ul:first')
				.append('<li><a href="#'+target+'" title="'+text+'">'+text+'</a></li>');

			$(this).hide();
		});
		container.find('ul.tabs-links li').filter(':first').addClass('first active-tab');
		container.find('ul.tabs-links li').filter(':last').addClass('last');
		container.find('ul.tabs-links a').click(function(e){
			e.preventDefault();
			var target = $(this).attr('href');
				container.find('ul.tabs-links li').removeClass('active-tab');
				$(this).parent().addClass('active-tab');
				contents.not(target).removeClass('active').hide();
				$(target).addClass('active').fadeIn('slow');
		});
    }
})(jQuery);

// code for making inline labels which then move above form inputs when the inputs have content
eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('j 0={5:e,n:3(2){$(2).o(3(){0.d(8);$(8).q(3(){$(\'#\'+$(8).1(\'7\')).5()})});$(\'.f\').5(3(){0.5=$(8);0.c($(8))}).p(3(){0.5=e;0.d($(\'2[7=\'+$(8).1(\'9\')+\']\'))})},d:3(2){j 6=$(\'#\'+$(2).1(\'7\'));b(!6){r}b(0.5!==e&&0.5.1(\'9\')==6.1(\'9\')){0.c(6)}h b(6.u()===\'\'){0.l(6)}h{0.c(6)}},c:3(4){$(4).a(\'f\');$(\'2[7=\'+$(4).1(\'9\')+\']\').g(\'k\').g(\'m\').a(\'i\')},l:3(4){$(4).a(\'f\');b($.v.s&&$(4).1(\'t\')){$(\'2[7=\'+$(4).1(\'9\')+\']\').a(\'m\')}h{$(\'2[7=\'+$(4).1(\'9\')+\']\').a(\'k\').g(\'i\')}}};',32,32,'labeler|attr|label|function|el|focus|target|for|this|id|addClass|if|aboveLabel|check|null|islabeled|removeClass|else|abovecontent|var|overcontent|overLabel|hidden|init|each|blur|click|return|safari|placeholder|val|browser'.split('|'),0,{}))

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover(
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};

var player = {
	usePlayList: false,
	playItem: 0,
	playlist: null,
	loaded: false,

	init: function() {
		$("#player").jPlayer({
			ready: function(){
				player.getAudios('', 10);
				player.playListEnable(true);
			},
			swfPath: swfpath
		});

		$("#player").jPlayerId("play", "player-play");
		$("#player").jPlayerId("pause", "player-pause");
		$("#player").jPlayerId("loadBar", "player-progress-load-bar");
		$("#player").jPlayerId("playBar", "player-progress-play-bar");
		$("#player").jPlayerId("volumeBar", "player_volume_bar");
		$("#player").jPlayerId("volumeBarValue", "player_volume_bar_value");
		$("#player").onSoundComplete(player.endOfSong);

		$('#player-play a').click(function(e) {
			e.preventDefault();
			$('#player').play();
			player.playing();
		});
		$('#player-pause a').click(function(e) {
			e.preventDefault();
			$('#player').pause();
		});

		$("#player").onProgressChange(function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
			var myPlayedTime = new Date(playedTime);
			var ptMin = (myPlayedTime.getMinutes()<10) ? "0"+myPlayedTime.getMinutes() : myPlayedTime.getMinutes();
			var ptSec = (myPlayedTime.getSeconds()<10) ? "0"+myPlayedTime.getSeconds() : myPlayedTime.getSeconds();
			$("#play-time").text(ptMin+":"+ptSec);

			var myTotalTime = new Date(totalTime);
			var ttMin = (myTotalTime.getMinutes()<10) ? "0"+myTotalTime.getMinutes() : myTotalTime.getMinutes();
			var ttSec=(myTotalTime.getSeconds()<10) ? "0"+myTotalTime.getSeconds() : myTotalTime.getSeconds();
			$("#total-time").text(ttMin+":"+ttSec);
		});

		$('#audio-categories li li a').click(function(e){
			var category = $(this).attr('href');
			$(this).parent().addClass('current-cat');
			$('#cat-selector').text($(this).text());
			player.getAudios(category, -1);
			e.preventDefault();
		});
	},

	getAudios: function(category, limit) {
		$("#playlist ul").empty().append("<li id='playlist-loading'><span class='loading'>Cargando...</span></li>");
		var data = {
			audio_cat: category,
			limit: limit,
			action: 'get_audios'
		}
		$.post(document.location.href, data, player.handleResponse, 'json');
	},

	handleResponse: function(playlist) {
		player.playlist = playlist;
		player.playItem = 0;
		$('#playlist ul').empty();

		for (i=0; i<playlist.length; i++){
			$("#playlist ul").append("<li id='playlist-item-"+i+"'><a href='#' title='"+playlist[i].title+"'><span class='song-number'>"+(i+1)+".</span> <span class='song-name'>"+playlist[i].name+"</span></a></li>");
			$("#playlist-item-"+i).data("index", i).hover(function(){
				if (player.playItem != $(this).data("index")){
					$(this).addClass("playlist-hover");
				}
			},
			function(){
				$(this).removeClass("playlist-hover");
			}).click(function(e){
				e.preventDefault();
				var index = $(this).data("index");
				player.playListChange(index, true);
			});
		}
		$("#playlist ul li:first-child").addClass('first-song');
		$("#playlist ul li:last-child").addClass('last-song');

		if (!player.loaded) {
			$("#player").setFile(player.playlist[player.playItem].filename);
			//$("#playlist ul li:first-child").addClass('playlist-current');
		}
		player.loaded = true;
	},
	
	endOfSong: function (){
		if (player.usePlayList){
			player.playListNext(true);
		}
	},

	playListEnable: function(e) {
		player.usePlayList = e;
		if (player.usePlayList) {
			playListChange(player.playItem);
		} else {
			$("#player").stop();
			player.playing();
		}
	},

	playListChange: function(index, play) {
		$("#playlist-item-"+player.playItem).removeClass("playlist-current");
		$("#playlist-item-"+index).addClass("playlist-current");
		player.playItem = index;
		$("#player").setFile(player.playlist[player.playItem].filename);
		
		if(play) {
			$("#player").setFile(player.playlist[player.playItem].filename).play();
			player.playing();
		}
	},

	playListNext: function(play) {
		if (player.usePlayList) {
			var index = (player.playItem+1 < player.playlist.length) ? player.playItem+1 : 0;
			if (index == 0)
				play = false;

			player.playListChange(index, play);
		}
	},

	playing: function() {
		$('#player-listen p').html(player.playlist[player.playItem].title);
	}
}

$(document).ready(function (){

	/*- Primer elemento de lista en cada Ul del sidebar -*/
	$('li:first-child').each(function(){
		$(this).addClass('first');
	});

	/*- Primer elemento de lista en cada Ul del sidebar -*/
	$('li:last-child').each(function(){
		$(this).addClass('last');
	});

	/*- Tabs -*/
	$('.tabs').tabs('h3');

	/*- Paper Clip -*/
	$('img.aligncenter,img.alignleft,img.alignright,img.alignnone,.page img').each(function(){
		var imgSize = $(this).width();
		var wrap = $('<span />')
				.addClass('image-wrapper')
				.css({
					'display': 'block',
					'width': imgSize
				});
		$(this).wrap(wrap);
		$('<span class="tape"></span>').insertAfter(this);
	});

	/*- Opacidad para campo de busqueda, subscribirse y comentario -*/
	$('input[type="text"], textarea, .respond button').each(function() {
		$(this).stop().animate({'opacity': .3});
	});
	$('input[type="text"], textarea, .respond button').focus(function() {
		$(this).stop().animate({'opacity': 1});
	});
	$('input[type="text"], textarea, .respond button').blur(function() {
		$(this).stop().animate({'opacity': .3});
	});

	/*- Boton de enviar comentario en hover -*/
	$('#commentform .button').hover(function() {
		$(this).addClass('hover');
		$(this).stop().animate({'opacity': 1});
		}, function() {
		$(this).stop().animate({'opacity': .3});
	});
	
	/*- Inline labels -*/
	labeler.init('label');

	$("ul.dropdown li").each(function(){
		var li = $(this);
		li.children('ul').hide();
		li.bind('mouseover', function(){
			li.children('ul').show().css({'opacity': .9});
		});
		li.bind('mouseout click', function(){
			li.children('ul').hide();
		});
	});

	/* Media Player */
	player.init();
});
