// JavaScript Document
function getFlashMovie(movieName) {   
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];  
}  
/*function sendNextSong() { 
	var myFlashMovie = getFlashMovie("playlist_player");
	myFlashMovie.flashLoadSong();  
} */   
function loadNextList() {
	var myFlashMovie = getFlashMovie("playlist_player");
	myFlashMovie.flashLoadPlaylist('507-playlist.xml');
	//setRequest2(
}

// test for what the current track's id is
function tellId(id) {
	alert(id);
}

// captures the amount of plays from flash
var view_id = 0;
function viewMedia(media_id, media_type) {
	view_id=media_id;
	//alert(view_id+" "+media_type);
	
	var url=HTTP_SERVER+"profile/handlers/handle_media_view.php";
	url=url+"?media_id="+media_id;
	url=url+"&media_type="+media_type;
	url=url+"&sid="+Math.random();
	$.get(url, function(){
			/*
			var views = document.getElementById('views-'+view_id);
			views.innerHTML = parseInt(views.innerHTML)+1;
			document.getElementById('view_button_'+view_id).removeAttribute('onclick', 0);
			*/
		}				
	);
}


function getPlayerClasses(){
	
	// song_unit_wrapper
	$('.song_unit_wrapper').click(function() {
		$('.song_unit_wrapper_active').removeClass('song_unit_wrapper_active');
		$(this).addClass('song_unit_wrapper_active');
		
		var myFlashMovie = getFlashMovie("playlist_player");
		myFlashMovie.flashLoadActivatedSong(this.id); 
		getTrackDetails(this.id);
	});
	
	
	$('.song_unit_wrapper').hover(function() {
		$(this).addClass('song_unit_wrapper_over');
			$(this).contents().find(".track_options").attr("src", HTTP_SERVER+'images/GCircle_Icon-Active.png');
	}, function() {
		$(this).removeClass('song_unit_wrapper_over');
			$(this).contents().find(".track_options").attr("src", HTTP_SERVER+'images/WCircle_Icon-Inactive.png');
	});
	
	
}

function searchPlayerClasses() {
	// song_unit_wrapper_search
	$('.song_unit_wrapper_search').click(function() {
		$('.song_unit_wrapper_search_active').removeClass('song_unit_wrapper_search_active');
		$(this).addClass('song_unit_wrapper_search_active');
		
		var get_playlist = this.getAttribute("rel").split('_');
			
		submitXML('create_search_playlist.php?playlist_name='+get_playlist[2]+','+get_playlist[0], handleXML, 'playlist');
		setRequest('playlist_hp_load.php?playlist_name='+get_playlist[1]+'-'+get_playlist[2]+'-'+get_playlist[3]+'-playlist.xml');
	});
	
	
	$('.song_unit_wrapper_search').hover(function() {
		$(this).addClass('song_unit_wrapper_search_over');
			$(this).contents().find(".track_options").attr("src", HTTP_SERVER+'images/GCircle_Icon-Active.png');
	}, function() {
		$(this).removeClass('song_unit_wrapper_search_over');
			$(this).contents().find(".track_options").attr("src", HTTP_SERVER+'images/WCircle_Icon-Inactive.png');
	});	
}

// flash also calls on this function
function getClassActive(id) {
	
	$('.song_unit_wrapper_active').removeClass('song_unit_wrapper_active');
	$('#'+id).addClass('song_unit_wrapper_active');
	
	getTrackDetails(id);
}

function getTrackDetails(id) {
	username = document.getElementById('artist_username_'+id).innerHTML;
	document.getElementById('artist_current').innerHTML = '<a href="'+HTTP_SERVER+"#profile/"+username+'" rel="history">'+document.getElementById('artist_'+id).innerHTML+'</a>';
	document.getElementById('album_current').innerHTML = document.getElementById('album_'+id).innerHTML;
	document.getElementById('track_current').innerHTML = document.getElementById('track_'+id).innerHTML;
	document.getElementById('loves_current').innerHTML = document.getElementById('loves_'+id).innerHTML;
	//document.getElementById('comments_current').innerHTML = document.getElementById('comments_'+id).innerHTML;
	
	$('#comments_current').html($('#comments_'+id).html());
	
	
	var get_cover = document.getElementById('cover_'+id).src;
	get_cover = get_cover.split("&");
	//alert(get_cover);
	document.getElementById('cover_current').src = get_cover[0]+"&width=82&height=82&crop=.7";
	
	// history.functions.js  - bind links to hash navigation
	globalClasses();
}

function handleXML(req, id) { 
		document.getElementById(id).innerHTML=req.responseText;
		//alert(req.responseText)
		getPlayerClasses();
}

// submit form to update-profile.php and call doFunc to handle response
function submitXML(url, doFunc, id) {

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	
	xmlHttp.onreadystatechange=function() { 
		if (xmlHttp.readyState==4) {
			if (xmlHttp.status==200) {
					doFunc(xmlHttp, id);
			}
		}
   }
	
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}