

//$(".jcarousel-skin-mode").addClass('hide');

/* ====================================================================================== */
/* jQuery document-is-ready function. Place all calls here */
jQuery(document).ready(function() {

	//$("a").tipTip();
	if($("#items").length) {
		/*$("#items").jcarousel({
			visible:3
		});*/
		$('#items').tinycarousel({ duration: 100 });
	}
/*	if($("#items").length) {
		$('#items').jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev"
		});		
	}*/
	
	
	// Get homepage releases box to be the same height as
	// the search box next to it
	if($("#home_releases").length) {
		var sh = parseInt($("#searchbox").css("height"),10);
		var nrh = ((sh - 36) + "px");
		$("#home_releases .frame_content").css("height",nrh);
	}

	//var safari5counter;	
	
	$("#jq_jplayer").jPlayer({
		swfPath: "/Filestore/_javascripts/jQuery.jPlayer.1.2.0/",
		ready: function() {
		},
		//volume:			30,
		warningAlerts:	true
		//nativeSupport:	false,
		//position:		"relative",
		//width: 			"200px",
		//height: 		"100px"
	}).jPlayer("onProgressChange", function(lp,ppr,ppa,pt,tt) {
		// loadPercent (0-100),playedPercentRelative (0-100),
		// playedPercentAbsolute (0-100), playedTime (ms),
		// totalTime (ms)
 		var lpInt = parseInt(lp);
 		var ppaInt = parseInt(ppa);
  		var playedTime = secondsToHms(parseInt(pt) / 1000);
  		var totalTime = secondsToHms(parseInt(tt) / 1000);

		//$("#safari_fix").html(ppaInt);

		global_lp = lpInt;
		
                
                


//$("#jplayer_play_bar").css("width","200px");
		//$("#op").text(ppaInt);
		/*$("#jplayer_load_bar").css("width",lpInt+"px");
		$("#jplayer_play_bar").css("width",ppaInt+"px");*/
		$("#jplayer_duration").text(playedTime + " / " + totalTime);
		
	});
	
	// Remove dotted outline on links when clicked
	$('a').css("outline","none");

	// ---------------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------------
	// We may leave a page with the player open and want it open again when we return.
	//
/*	if($("#track_info").length) {
		var loc = $("#track_info_display").attr("class");
		if(loc.length > 5) {
			$.post("download.php?f="+loc, function(data) {
				var track = data.split("|");
				loadTrack(track);
				$("#info_track_summary").slideDown("fast");
			});
		}
	}*/
	// ---------------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------------
	// TRACK LIST
	if($("#table_tracks").length) {
		$("#table_tracks tr:has(td)").live('mouseover mouseout', function(event) {
			if (event.type == 'mouseover') {
				// do something on mouseover
				$(this).addClass("selected");
			} else {
				// do something on mouseout
				$(this).removeClass("selected");
			}
		
		});

/*		$("#table_tracks tr").live("click",function(e) {
			e.preventDefault();

			var el = $(this).find(".link_play");

			el.find("img").attr("src","/images/timer.png");

			$.post(el.attr('href'), function(data) {
	
				var track = data.split("|");
				loadTrack(track);
				
				//$("#jq_jplayer").jPlayer("play");
				//$("#jp-play").css("background-image","url(/images/b_player_play_on.png)");
				
				// ..change play button to stop button on row with playing track
				//el.find("img").attr("src","/images/b_list_play_on.png");
		
				// highlight the table row of the playing track
				$("#table_tracks").find("tr.playing").removeClass("playing");
				el.parents("tr").addClass("playing");
				$("#info_track_summary").slideDown("slow");
			});
			
		});*/
	}
	// ---------------------------------------------------------------------------------------------------	
	// ---------------------------------------------------------------------------------------------------
	
	$("#pb_login").live("click",function(e) {
		e.stopPropagation();
		e.preventDefault();
		$(".popup_frame_login").show();
	});
	
	//$(".popup_frame_login").mouseleave(function() {
	//$('body').live("click",function(e) {
		//e.preventDefault();
		// IE can't handle fade out gracefully..
		//($.browser.msie ? $(".popup_frame_login").hide() : $(".popup_frame_login").fadeOut());
	//});
	// ---------------------------------------------------------------------------------------------------
	// Search / Playbox tabs
	// Show Search tab
	$("#img_tab_search").live("click",function(e) {
		e.preventDefault();
		showTab(1);
	});
	// Show Playbox tab
	$("#img_tab_my_playbox").live("click",function(e) {
		e.preventDefault();
		showTab(2);
	});
	$(".b_show_playbox").live("click",function(e) {
		e.preventDefault();
		var id = $(this).attr("rel");
		$.post("/process.php", {view_playbox:id}, function(result) {
			$("#active_playbox").html(result);
			//img.attr("src","/images/loading.png");
		});
	});
	

	// ---------------------------------------------------------------------------------------------------
	// List all Playboxes in main content area
	$("#b_playbox_list").live("click",function(e) {
		e.preventDefault();
		$.post("/process.php", {get_playbox_list:-1}, function(result) {
			$("#active_playbox").html(result);
		});
	});
	
	// ---------------------------------------------------------------------------------------------------
	// Create Playbox
	// display form
	$("#b_playbox_create").live("click",function(e) {
		e.preventDefault();
		$(".frame_playbox_name").toggle("fast");
	});
	// hide form
	$("#playbox_create .cancel").live("click",function(e) {
		e.preventDefault();
		$(".frame_playbox_name").toggle("fast");
	});
	// call to create the playbox
	$("#playbox_create").submit(function(e){
		e.preventDefault();
		$.post("/process.php", {add_playbox_name:$("#playbox_name").val()}, function(result) {
			$("#container_playbox h1").html(result);
			$("#active_playbox").html(result);
			$.post("/process.php", {get_playbox_list:-1}, function(result) {
				$("#active_playbox").html(result);
			});
			$.post("/process.php", {get_playbox_list_simple:-1}, function(result) {
				$("#tab_playbox").html(result);
			});
			$(".frame_playbox_name").toggle("fast");
		});
	});
	// Remove a playbox - from main content area
	$(".remove_playbox_main").live("click",function(e) {
		e.preventDefault();
		var id = $(this).attr("rel");
		deletePlaybox(id,false);
	});
	// Remove a playbox
	$(".remove_playbox").live("click",function(e) {
		e.preventDefault();
		var id = $(this).attr("rel");
		deletePlaybox(id,true);
	});
	
	// --------------------------------------------------------------------------------------
	// Passed an id and a flag (simple = from sidebar search box), delete the playbox
	function deletePlaybox(id,is_simple) {

		var c = (confirm("Are you sure you want to delete this Project?"));
		
		if(c) {
			$.post("/process.php", {delete_playbox:id,simple:is_simple}, function(result) {
				if(is_simple) {
					$("#tab_playbox").html(result);
					if($("#active_playbox").length) {
						// are we showing the active playbox in the main view? If so, update
						$.post("/process.php", {get_playbox_list:-1}, function(result) {
							$("#active_playbox").html(result);
						});
					}
				} else {
					$("#active_playbox").html(result);
					$.post("/process.php", {get_playbox_list_simple:-1}, function(result) {
						$("#tab_playbox").html(result);
					});
				}
			});
		}
	}
	
	// --------------------------------------------------------------------------------------
	// [+] Add to playlist button
	$(".add_to_playbox").live("click",function(e) {
		e.preventDefault();
		var id = $(this).attr("rel");
		var img = $(this).find("img");

		//var add = (img.attr("src").indexOf("add") > 1);
		//if(add) {
		var prev = img.attr("src");
		img.attr("src","/images/timer.png");
		$.post("/process.php", {add_track_id:id}, function(result) {
			$("#results").html(result);
			if(result.indexOf("success") > 0) {
				/*$.post("/process.php", {get_playbox:-1}, function(result) {
					$("#active_playbox").html(result);
				});*/
				$.post("/process.php", {get_playbox_list_simple:-1}, function(result) {
					$("#tab_playbox").html(result);
				});
				img.attr("src","/images/b_tick.png");
			} else
				img.attr("src",prev);
		});
	});

	// --------------------------------------------------------------------------------------
	// Remove item from playbox
	$(".remove_from_playbox").live("click",function(e) {
		e.preventDefault();
		var tmp = $(this).attr("rel").split("/");
		var id = tmp[0];
		var pid = tmp[1];
		var all = tmp[2];

		var c = (confirm("Are you sure you want to delete this track from your Project?"));
		
		if(c) {
			var img = $(this).find("img");
			img.attr("src","/images/timer.png");
			$.post("/process.php", {remove_track_id:id,playbox_id:pid}, function(action) {
				//$("#results").html(result);
				if(action.indexOf("success") > 0) {
					if(all == 1) {
						// we're displaying all projects, update the list
						$.post("/process.php", {get_playbox_list:1}, function(result) {
							$("#active_playbox").html(action + result);
						});
					} else {
						$.post("/process.php", {get_playbox:pid}, function(result) {
							$("#active_playbox").html(action + result);
						});
					}
					$.post("/process.php", {get_playbox_list_simple:-1}, function(result) {
						$("#tab_playbox").html(result);
					});
					img.attr("src","/images/b_tick.png");
				} else
					alert('An error occurred. Please try again');
			});
		}

	});
	
	// --------------------------------------------------------------------------------------
	// Make Playbox active
	// ... from the sidebar
	$(".pbactive_simple").live("click",function(e) {
		e.preventDefault();
		var id = $(this).attr("rel");
		$.post("/process.php", {make_active_playbox_simple:id}, function(result) {
			$("#tab_playbox").html(result);
			if($("#container_playbox").length)
				// are we showing the active playbox in the main view? If so, update
				$.post("/process.php", {get_playbox:-1}, function(result) {
					$("#active_playbox").html(result);
				});
		});
	});
	// ... from the main area
	$(".pbactive").live("click",function(e) {
		e.preventDefault();
		var id = $(this).attr("rel");
		$.post("/process.php", {make_active_playbox:id}, function(result) {
			$("#active_playbox").html(result);
			if($("#tab_playbox").length)
				// are we showing the playbox list in the search view? If so, update
				$.post("/process.php", {make_active_playbox_simple:id}, function(result) {
					$("#tab_playbox").html(result);
				});
		});
	});
	// --------------------------------------------------------------------------------------
	// 'Browse Albums' link
	$("#album_browse").live("click",function(e) {
		
		if($("#latest_releases").length) {
			e.preventDefault();
		
			$("#latest_releases").slideDown("slow");
			$("#info_track_summary").slideUp("slow");
		}
		
	});
	
	
	
	// --------------------------------------------------------------------------------------
	// Request true file name and pass to our audio code
	$(".link_play").live("click",function(e) {
		e.preventDefault();
		var el = $(this);

		el.find("img").attr("src","/images/timer.png");

		var url = $(this).attr('href');
		$.post(url, function(data) {

			var track = data.split("|");
			loadTrack(track);
			$("#track_id").attr("class",track[10]);

			// The Project page uses ajax to load the track.
			// We need to know if we're on that page because we
			// have to start the track we've loaded
			if(url.indexOf("playbox"))
				$("#jq_jplayer").jPlayer("play");
				
			// If the link is clicked from the Available Versions table
			// in the player, highlight the link
			if(el.attr("class").indexOf("v_") !== -1 ) {
				// remove previous playing class if it's there already
				el.removeClass("playing");
				var c = el.attr("class");
				var tmp = c.split(" ");
				var v = "."+tmp[(tmp.length)-1];//.substr(2);
				$("#jq_jplayer").jPlayer("play");

				$("#track_version").attr("class",v);
			}

			updateUI(el,track[10],false);
			
			// If we're on a Project list (either a single project or all projects)
			if(el.attr("class").indexOf("list") > -1) {
				$("#table_tracks .list_play img").attr("src","/images/b_list_play_off.png");
				$("#table_tracks tr.playing").removeClass("playing");
				$("#table_tracks .list_play").filter('[rel=' + track[10] + ']').find("img.mplay").attr("src","/images/b_list_play_on.png");
				$(".v_full").addClass("playing");
				$("#info_track_summary").slideDown("slow");
			}
		});
	});

	
	// ----------------------------------------------------------------------------------
	// Player controls
	$("#jp-play").live("click",function(e) {
		e.preventDefault();
		var el = $(this);
		$("#jq_jplayer").jPlayer("play");
		$("#jp-play").css("background-image","url(/images/b_player_play_on.png)");
		$("#jp-pause").css("background-image","url(/images/b_player_pause.png)");
		$("#jp-stop").css("background-image","url(/images/b_player_stop.png)");
		var id = $("#track_id").attr("class");
		$("#table_tracks .list_play").filter('[rel=' + id + ']').find("img.mplay").attr("src","/images/b_list_play_on.png");
		var cl = $("#track_version").attr("class");
		$("."+cl).addClass("playing");
	});
	$("#jp-pause").live("click",function(e) {
		e.preventDefault();
		$("#jq_jplayer").jPlayer("pause");
		$("#jp-play").css("background-image","url(/images/b_player_play.png)");
		$("#jp-pause").css("background-image","url(/images/b_player_pause_on.png)");
		$("#jp-stop").css("background-image","url(/images/b_player_stop.png)");
		
		// Change stop button to play button in the list on previous playing row..
		var id = $("#track_id").attr("class");
		$("#table_tracks .list_play").filter('[rel=' + id + ']').find("img.mplay").attr("src","/images/b_list_play_off.png");
		$(".version").removeClass("playing");
		$("#track_version").removeClass("playing");

	});
	$("#jp-stop").live("click",function(e) {
		e.preventDefault();
		$("#jq_jplayer").jPlayer("stop");
		$("#jp-play").css("background-image","url(/images/b_player_play.png)");
		$("#jp-pause").css("background-image","url(/images/b_player_pause_off.png)");
		$("#jp-stop").css("background-image","url(/images/b_player_stop_off.png)");
		$(".version").removeClass("playing");
		$("#track_version").removeClass("playing");
		
		// Change stop button to play button in the list on previous playing row..
		$("#table_tracks img.mplay").attr("src","/images/b_list_play_off.png");
		
		// unhighlight the row
		//$("#table_tracks").find("tr.playing").removeClass("playing");

	});
	// ----------------------------------------------------------------------------------

});
// --------------------------------------------------------------------------------------
// Either show the Search Tab or the Playbox Tab.
// The function sits here because it's called from the site as well a from jQuery
function showTab(id) {
/*	if(id == 1) {
		$("#tab_playbox").slideUp("fast");
		$("#tab_search").slideDown("fast");
		$("#img_tab_my_playbox").find("img").attr("src","/images/tab_my_playbox.png");
		$("#img_tab_search").find("img").attr("src","/images/tab_search_here.png");
	} else {
		$("#tab_search").slideUp("fast");
		$("#tab_playbox").slideDown("fast");
		$("#img_tab_search").find("img").attr("src","/images/tab_search.png");
		$("#img_tab_my_playbox").find("img").attr("src","/images/tab_my_playbox_here.png");
	}*/
	if(id == 1) {
		$("#tab_playbox").addClass("hide");
		$("#tab_search").removeClass("hide");
		$("#img_tab_my_playbox").find("img").attr("src","/images/tab_my_playbox.png");
		$("#img_tab_search").find("img").attr("src","/images/tab_search_here.png");
	} else {
		$("#tab_search").addClass("hide");
		$("#tab_playbox").removeClass("hide");
		$("#img_tab_search").find("img").attr("src","/images/tab_search.png");
		$("#img_tab_my_playbox").find("img").attr("src","/images/tab_my_playbox_here.png");
	}
	
	$.post("/process.php", {store_current_tab:id}, function(result) {
		//$("#tab_playbox").html(result);
	});
}
// --------------------------------------------------------------------------------------
function getTrack(trk) {

	$.post(trk, function(data) {
		var track = data.split("|");
		loadTrack(track);
		$("#jq_jplayer").jPlayer("play");
		
	});

}
// Called from php
function play(t,id) {
	$("#jq_jplayer").jPlayer({
		swfPath: "/Filestore/_javascripts/jQuery.jPlayer.1.2.0/",
		ready: function() {
			if(t) {
				this.element.jPlayer("setFile",t).jPlayer("play");

				// update UI
				var link = $(".list_play[rel="+id+"]");
				$("#track_id").attr("class",id);
				$("#track_version").attr("class","v_full");

				$("#jp-pause").css("background-image","url(/images/b_player_pause.png)");
				$("#jp-stop").css("background-image","url(/images/b_player_stop.png)");

				updateUI(link,id,true);
				
			}
		},
		warningAlerts:	true
	}).jPlayer("onProgressChange", function(lp,ppr,ppa,pt,tt) {
 		var lpInt = parseInt(lp);
 		var ppaInt = parseInt(ppa);
  		var playedTime = secondsToHms(parseInt(pt) / 1000);
  		var totalTime = secondsToHms(parseInt(tt) / 1000);
		global_lp = lpInt;
		$("#jplayer_duration").text(playedTime + " / " + totalTime);
	});
}
// --------------------------------------------------------------------------------------
// Update the various items when playing
function updateUI(el,id,fromList) {

	//$("#jq_jplayer").jPlayer("play");
	$("#jp-play").css("background-image","url(/images/b_player_play_on.png)");
	
	// NOW we need to update some elements on the page
	
	// ..change play button to stop button on row with playing track
	el.find("img").attr("src","/images/b_list_play_off.png");
	$("#table_tracks .list_play").filter('[rel=' + id + ']').find("img.mplay").attr("src","/images/b_list_play_on.png");

	// highlight the table row of the playing track
	$("#table_tracks").find("tr.playing").removeClass("playing");
	el.parents("tr").addClass("playing");

	// highlight Play button in the row
	//$("#table_tracks .list_play[rel="+id+"] img.mplay").attr("src","/images/b_list_play_on.png");

	// remove highlight on previous version playing
	$(".version").removeClass("playing");

	// in the play frame, highlight the Full version if clicked from the row
	if(el.attr("class").indexOf("list") > 0 || fromList)
		$(".v_full").addClass("playing");
	
	// If the link is clicked from the Available Versions table
	// in the player, highlight the link
	if(el.attr("class").indexOf("v_") !== -1 ) {
		// remove previous playing class if it's there already
		el.removeClass("playing");
		var c = el.attr("class");
		var tmp = c.split(" ");
		var v = "."+tmp[(tmp.length)-1];//.substr(2);
		//alert(".link_play ."+v);
		$(v).addClass("playing");
		//alert(v);
	}
	
	// If we are showing latest releases frame, slide it out of the way..
	// This way, as we slide down the player, this fella has gone
/*	if($('#latest_releases')) {
		$("#latest_releases").slideUp("slow");
	}
	
	$("#info_track_summary").slideDown("slow");*/
}
// --------------------------------------------------------------------------------------
function loadTrack(track) {

	$("#track_info #track_number").html(track[4]);
	$("#track_info #track_catalog_number").html(track[5]);
	$("#track_info #track_composer").html(track[6]);
//alert(track[0]);
	//var title_info = track[2] + " &nbsp; <span class=\"light\">" + track[3] + "</span>";
	var title_info = track[2] + " &nbsp; <span class=\"light\">" + track[1] + "</span>";

	$("#track_info #album_track_title").html(title_info);
	//$("#track_info #album_title").html(track[1]);
	$("#track_info #track_tags #tags").html(track[7]);
	
	// Make the album image clickable to show all tracks from this album
	$("#track_info #album_image a").attr("href",track[12]);
	// Place the album artwork
	$("#track_info #track_coverart").attr("src",track[9]);
	$("#track_info #track_coverart").attr("alt","Album cover - " + track[1]);

	// get available versions
	$("#track_info #track_details_right").html(track[11]);

	$("#jp-play").css("background-image","url(/images/b_player_play.png)");
	$("#jp-pause").css("background-image","url(/images/b_player_pause.png)");
	$("#jp-stop").css("background-image","url(/images/b_player_stop.png)");

	// Change stop button to play button in the list on previous playing row..
	$("#table_tracks").find("img.mplay").attr("src","/images/b_list_play_off.png");
			
	$("#jq_jplayer").jPlayer("setFile", track[0]);

	$("#track_id").attr("class",track[10]);

//	$("#info_track_summary").slideDown("slow");
	
}
// ---------------------------------------------------------------------------------------
function secondsToHms(d) {
	d = Number(d);
	var h = Math.floor(d / 3600);
	var m = Math.floor(d % 3600 / 60);
	var s = Math.floor(d % 3600 % 60);
	return ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s);
}

(function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var b=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var c=$(this),ul=$("ul",c),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v}var f=$("li",ul),itemLength=f.size(),curr=o.start;c.css("visibility","visible");f.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});c.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var g=o.vertical?height(f):width(f);var h=g*itemLength;var j=g*v;f.css({width:f.width(),height:f.height()});ul.css(sizeCss,h+"px").css(animCss,-(curr*g));c.css(sizeCss,j+"px");if(o.btnPrev)$(o.btnPrev).click(function(){return go(curr-o.scroll)});if(o.btnNext)$(o.btnNext).click(function(){return go(curr+o.scroll)});if(o.btnGo)$.each(o.btnGo,function(i,a){$(a).click(function(){return go(o.circular?o.visible+i:i)})});if(o.mouseWheel&&c.mousewheel)c.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll)});if(o.auto)setInterval(function(){go(curr+o.scroll)},o.auto+o.speed);function vis(){return f.slice(curr).slice(0,v)};function go(a){if(!b){if(o.beforeStart)o.beforeStart.call(this,vis());if(o.circular){if(a<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*g)+"px");curr=a==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll}else if(a>=itemLength-v+1){ul.css(animCss,-((v)*g)+"px");curr=a==itemLength-v+1?v+1:v+o.scroll}else curr=a}else{if(a<0||a>itemLength-v)return;else curr=a}b=true;ul.animate(animCss=="left"?{left:-(curr*g)}:{top:-(curr*g)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());b=false});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled")}}return false}})};function css(a,b){return parseInt($.css(a[0],b))||0};function width(a){return a[0].offsetWidth+css(a,'marginLeft')+css(a,'marginRight')};function height(a){return a[0].offsetHeight+css(a,'marginTop')+css(a,'marginBottom')}})(jQuery);
/* ====================================================================================== */
