REAL.Artists = (function () {

	// Private variables

	// Public variables
	var obj					= {};


	// Private methods
	function init () {

		var bio = $("#artists_bio p"),
			bioText = bio.html(),
			bioMore = $("#artists_bio a.read_more"),
			bioVisible = false;

		function bioHide() {
			var trunc = bioText,
				len = 400;
			if (trunc.length > len) {
			    trunc = trunc.substring(0, len);
			    trunc = trunc.replace(/\w+$/, '');
			    trunc += '...';
			    bio.html(trunc);
			  }
			bioMore.html("Read more");
		}

		function bioShow() {
			bio.html(bioText);
			bioMore.html("Read less");
		}

		function bioToggle() {
			bioVisible = !bioVisible;
			if (bioVisible) bioShow();
			else bioHide();

			// resize the aside
			setTimeout(function(){
				REAL.Navigation.setAsideHeight(60);
			}, 20);

			return false;
		}

		bioMore.bind("click", bioToggle, false);
		bioHide();


		// DISCOGRAPHY

		var disco = $("#artists_disco div.disco"),
			discoMore = $("#artists_disco a.read_more"),
			discoVisible = false;

		function discoHide() {
			disco.hide();
			disco.eq(0).show();
			discoMore.html("More discography");
		}

		function discoShow() {
			disco.show();
			discoMore.html("Less discography");
		}

		function discoToggle() {
			discoVisible = !discoVisible;
			if (discoVisible) discoShow();
			else discoHide();

			// resize the aside
			setTimeout(function(){
				REAL.Navigation.setAsideHeight(60);
			}, 20);

			return false;
		}

		discoMore.bind("click", discoToggle, false);
		discoHide();


		// VIDEOS

		var video = $("#artists_video #videos-to-load li"),
			videoMore = $("#artists_video a.read_more"),
			videosInc = 3;

		function showMoreVideos (e) {
			var videosLong = (video.size()<videosInc)? video.size() : videosInc;

			for (var i=0; i<videosLong; i++){
				var titulo = video.eq(i).attr("id"),
					url = video.eq(i).html(),
					html = '<iframe class="youtube-player" type="text/html" width="558" height="343" src="' + url + '" frameborder="0"></iframe>';

				videoMore.before(html);
			}
			// resize the aside
			setTimeout(function(){

				for (var i=0; i<videosLong; i++){
					video.eq(i).remove();
				}
				video = $("#videos-to-load li");

				if (videosLong < videosInc) {
					videoMore.fadeOut("slow");
					REAL.Navigation.setAsideHeight();
				} else {
					REAL.Navigation.setAsideHeight(60);
				}

			}, 20);
			// cancel bubble
			return false;
		}

		videoMore.bind("click", showMoreVideos, false);

		// TOUR DATES
		var readMore = $("article#artists_tourdata a.read_more");

		function readHide($table, $link) {
			$table.each(function (index){
				if (index > 10) $(this).hide();
			});
			$link.html("More tour dates");
		}

		function readShow($table, $link) {
			$table.each(function (index){
				if (index > 10) $(this).css({opacity:0}).show().stop().delay(50*(index-10)).animate({opacity:1}, "normal");
			});
			$link.html("Less tour dates");
		}

		function readToggle(e) {
			// check visibility
			var isVisible = ($(this).html() == "More tour dates");
			if (isVisible) readShow($(this).parent().find("table tr"), $(this));
			else readHide($(this).parent().find("table tr"), $(this));

			// resize the aside
			setTimeout(function(){
				REAL.Navigation.setAsideHeight(60);
			}, 20);

			return false;
		}

		readMore.bind("click", readToggle, false);


		// PLAYER

		// add play songs
		$("a.disco-button").bind("click", function(e){
			if ($(this).hasClass("selected")){
				// desmarcamos
				$(this).removeClass("selected");
				REAL.oPlayer.removeSong();
			} else {
				// desmarcamos el anterior
				$("a.disco-button").removeClass("selected");
				// marcamos el nuevo
				$(this).addClass("selected");
				var name = $(this).parent().attr("class") + " - " + $(this).parent().text().split("(")[0];
					name = name.replace(/&amp;/g,'&');

				var mp3 = $(this).attr("href");

				REAL.oPlayer.addSongAndPlay(name, mp3);
			}
			return false;
		});
	}


	// Public methods
	obj.init	= init;

	return obj;

})();

