$w1(document).ready(function(){
	
	
	/* Toggle functions */
	$w1(".search_expand_collapse").bind("click", function(){

		var rel = $w1(this).attr("rel");

		$w1(".album_tracklist_version_container[rel="+rel+"], .album_tracklist_full[rel="+rel+"],.album_tracklist_version_button[rel="+rel+"]").toggle();
		
		if(!$w1(".album_tracklist_track_version[rel="+$w1("#current_playing").val()+"]").text().substring("Full Version")){
			stopSound();
			resetPlayer();
		}
		
		return false;
		
	})
	
	$w1("#albumcontent .album_tracklist_version_button, #albumcontent .album_tracklist_description_buttons_img").bind("click", function(){

		var rel = $w1(this).attr("rel");
		$w1(".album_tracklist_version_container[rel="+rel+"], .album_tracklist_full[rel="+rel+"],.album_tracklist_version_button[rel="+rel+"]").toggle();
		$w1(".album_tracklist_container[rel="+rel+"]").toggleClass("black_border");
		$w1(".album_tracklist_full[rel="+rel+"]").removeClass("expanded");
	
		
		if(!$w1(".album_tracklist_track_version[rel="+$w1("#current_playing").val()+"]").text().substring("Full Version")){
			stopSound();
			resetPlayer();
		}
		
		return false;
		
	})
	
	var volume_value = ($w1.cookie("volume_value")) ? $w1.cookie("volume_value") : 75

	$w1("#sidebar_volume_control").slider({min: 0, max: 100, value: volume_value});	
	
	$w1("#album_tracklist_full_all_button").bind("click", function(){
	
		switch($w1("#ev").val()){
			
			case "0":
				$w1(".album_tracklist_version_container, .album_tracklist_full").show();
				$w1(".album_tracklist_version_button").hide();
				$w1(".album_tracklist_container").addClass("black_border");
				$w1("#ev").val(1);
			break;
			case "1":
				
				if ($w1(".album_tracklist_full").hasClass("expanded")) {
		
					$w1(".album_tracklist_version_container, .album_tracklist_full").hide();
					$w1(".album_tracklist_version_button").show();
					$w1(".album_tracklist_container").removeClass("black_border");
					$w1(".album_tracklist_full").removeClass("expanded");
					$w1("#ev").val(0);
					
				}
				else {
		
					$w1(".album_tracklist_version_container, .album_tracklist_full").hide();
					$w1(".album_tracklist_version_button").show();
					$w1(".album_tracklist_container").removeClass("black_border");
					$w1("#ev").val(0);
				}
			break;
			
			
		}
		
		
	})
	
	$w1("#project_full_all_button").bind("click", function(){
		
		switch($w1("#pev").val()){
		
			case "0":
				
				$w1(".project_tracklist_container").show();
				$w1(".account_projectlist").addClass("project_highlighted");
				$w1("#pev").val(1);
				
				
			break;
			case "1":
				
				$w1(".project_tracklist_container").hide();
				$w1(".account_projectlist").removeClass("project_highlighted");
				$w1("#pev").val(0);
				
			break;
		}
		
	})
	
	$w1(".account_tracklist_full_button").bind("click", function(){
	
		var rel = $w1(this).attr("rel");

		$w1(".account_projectlist[rel="+rel+"]").toggleClass("project_highlighted");
		$w1(".project_tracklist_container[rel="+rel+"]").toggle();
		
	})
	
	
	/* Code to handle the add and downloading of project/track interaction */
	$w1(".add_track_to_project").bind("click", function(){
		
		var rel = $w1(this).attr("rel");
		addTrack(rel);
	});
	
	
	$w1(".download_track").bind("click", function(){
		
		var rel = $w1(this).attr("rel");
		downloadTrack(rel);
		
	});
	
	
	/* Player oriented interaction */
	catno = $w1("#current_playing").val();
	complete = false;
	
	if (catno != "") {
		
		// check pause
		var currentPlaystate = $w1("#current_playstate").val();
		if (currentPlaystate == "play") {
			// pause showing so show approp list button
			$w1("#album_tracklist_track_buttons_img_play[rel=" + catno + "]").hide();
			$w1("#album_tracklist_track_buttons_img_pause[rel=" + catno + "]").show();
			$w1("#album_tracklist_track_buttons_img_resume[rel=" + catno + "]").hide();
		}
		
		// check resume
		if (currentPlaystate == "pause") {
			// pause showing so show approp list button
			$w1("#album_tracklist_track_buttons_img_play[rel=" + catno + "]").hide();
			$w1("#album_tracklist_track_buttons_img_pause[rel=" + catno + "]").hide();
			$w1("#album_tracklist_track_buttons_img_resume[rel=" + catno + "]").show();
		}
	}
	
	
	
	$w1(".album_tracklist_track_buttons_play_img").bind("click", function(){
		
		pauseSound();
		resetPlayer();
		
		
		var rel = $w1(this).attr("rel");
		
		$w1("div.album_tracklist_track_buttons_img_play").show();
		$w1("div.album_tracklist_track_buttons_img_resume").hide();
		$w1("div.album_tracklist_track_buttons_img_pause").hide();
		$w1("div.album_tracklist_track_buttons_img_play[rel="+rel+"]").hide();
		$w1("div.album_tracklist_track_buttons_img_pause[rel="+rel+"]").show();
		
		$w1("#sidebar_track_play").hide();
		$w1("#sidebar_track_pause").show();
		$w1("#sidebar_track_resume").hide();
		

	
		// update current playing hidden var
		$w1("#current_playing").val(rel);
		
		 // set mousedown function for scrubber
		$w1(document).mouseup(function(){
		
			$w1("#current_mousestate").val("up");
			
		});
		
		// set mouseover tool tip for scrubber
		$w1("div.album_tracklist_track_title").mouseover(function(){
		
			var currentPlaying = $w1("#current_playing").val()
	
			if ($w1(this).attr("rel") == currentPlaying) {
				Tip('Click and drag to scrub');
			}
			
		});
		
		// set mouseout remove tool tip for scrubber
		$w1("div.album_tracklist_track_title").mouseout(function(){	
			UnTip('Click and drag to scrub');
			
		});
		
		// set mousedown function for scrubber
		$w1("div.album_tracklist_track_title").mousedown(function(e){
		
			// check its the right catno
			var thisId = $w1(this).attr('id');
			var catno = thisId.replace("album_tracklist_track_title","");
			var currentPlaying = $w1("#current_playing").val()

			if (catno == currentPlaying) {
			
				// set hidden values
				$w1("#current_mousestate").val("down");
				
				// disable text selection for this track title
				disableSelection(document.getElementById("album_tracklist_track_title" + catno));
				
				// set play buttons to resume buttons and resume
				$w1("#album_tracklist_track_buttons_img_play" + catno).hide();
				$w1("#album_tracklist_track_buttons_img_pause" + catno).show();
				$w1("#album_tracklist_track_buttons_img_resume" + catno).hide();
				// sidebar buttons
				$w1("#sidebar_track_play").hide();
				$w1("#sidebar_track_pause").show();
				$w1("#sidebar_track_resume").hide();
				
				resumeSound();
			
				// initial update play position
				var divOffset = $w1(this).offset().left;
				var divPosX = e.pageX - divOffset;
				var loadPerc = 	$w1("#current_percent").val();
				var loadPosX = 325 / 100 * loadPerc; // 360 is width of title area
				if (divPosX > loadPosX) {
					divPosX = loadPosX;
				}
				var playPosPerc = 100 / 325 * divPosX;
				// $w1("div.album_tracklist_track_title_text").html(e.pageX + " " + divPosX + " " + divOffset + " " + playPosPerc);
				setSongPosition(playPosPerc);
				
				// then bind same func to mousemove for drag
				$w1(this).mousemove(function(ee){
						var currentMouseState = $w1("#current_mousestate").val();
						// $w1("#album_tracklist_track_play" + catno).css("width" , '50%');
						if (currentMouseState == "down") {
							var divOffset = $w1(this).offset().left;
							var divPosX = ee.pageX - divOffset;
							var loadPerc = 	$w1("#current_percent").val();
							var loadPosX = 325 / 100 * loadPerc; // 360 is width of title area
							if (divPosX > loadPosX) {
								divPosX = loadPosX;
							}
							var playPosPerc = 100 / 325 * divPosX;
							// $w1("div.album_tracklist_track_title_text").html(ee.pageX + " " + divPosX + " " + divOffset + " " + playPosPerc);
							setSongPosition(playPosPerc);
						}
				});
				
			} // if right catno
				
		});
		
		playTrack(rel);
	
	});
	
	$w1(".album_tracklist_track_buttons_pause_img").bind("click", function(){
		
		var rel = $w1(this).attr("rel");
		
		$w1(".album_tracklist_track_buttons_img_play[rel=" + rel + "]").hide();
		$w1(".album_tracklist_track_buttons_img_pause[rel=" + rel + "]").hide();
		$w1(".album_tracklist_track_buttons_img_resume[rel=" + rel + "]").show();
		
		// sidebar buttons
		$w1("#sidebar_track_play").hide();
		$w1("#sidebar_track_pause").hide();
		$w1("#sidebar_track_resume").show();

		pauseSound();
	
	});
	
	$w1(".album_tracklist_track_buttons_resume_img").bind("click", function(){
		
		var rel = $w1(this).attr("rel");

		$w1(".album_tracklist_track_buttons_img_play[rel=" + rel + "]").hide();
		$w1(".album_tracklist_track_buttons_img_pause[rel=" + rel + "]").show();
		$w1(".album_tracklist_track_buttons_img_resume[rel=" + rel + "]").hide();
		// sidebar buttons
		$w1("#sidebar_track_play").hide();
		$w1("#sidebar_track_pause").show();
		$w1("#sidebar_track_resume").hide();
	
		resumeSound();	
	});
	
	
	$w1("#sidebar_volume_control").bind("slide", function(){
		setVolume($w1(this).slider("value"));		
	});
	
	$w1("#sidebar_volume_control").bind("slidestop", function(){
		$w1.cookie("volume_value", $w1("#sidebar_volume_control").slider("value"));
	});
	
	/* Search field functions */
	$w1("#search_text").bind("focus", function(){
		
		if($w1(this).val() == "Enter your music search..."){
			
			$w1(this).val("");
		}
		
	});
	
	$w1("#search_text").bind("blur", function(){
		
		if($w1(this).val() == ""){
			
			$w1(this).val("Enter your music search...");
		}
		
	});
	
	$w1(".show_hide_control").bind("click", function(){
		$w1("#" + $w1(this).attr("rel")).toggle();
	});
	
	$w1("#sidebar_filters_toggle_target select").bind("change", function(){
		
		$w1("#search_form").submit();
		
	});
	
	$w1("#disc_jump_to").bind("change", function(){
		location.href = "/album?catno=" + $w1(this).attr("rel") + "&discno=" + $w1(this).val();
	});
	
	
});