$(document).ready(function() {
	
	$('a.songLinkShowDetails').live('click', function(){
		var id = $(this).attr('song_id');
		createSongDialog(id);
	});
	$('a#openloginDialog').click(function(){
		$('body').append(getLoginDialogHtml());
		$('.flashplayer_top').css('display', 'none');
		$('a#details_close').click(function() {
			$('div#additional_info').dialog('destroy').remove();
			$('.flashplayer_top').css('display', 'block');
		})
		$('div#additional_info').dialog({
			resizable:false,
			draggable:false,
			modal:true,
			minWidth:300,
			width:300,
			minHeight:200,
			height:200,
			title:"",
			closeText: ""
		});
	});
	$('a#opendeleteDialog').click(function(){
		var id = $(this).attr('song_id');
		var get = $(this).attr('get');
		var title = $(this).attr('title');
		var interpret = $(this).attr('interpret');
		$('body').append(getDeleteDialogHtml(id, get, title, interpret));
		$('.flashplayer_top').css('display', 'none');
		$('.details_close').click(function() {
			$('div#additional_info').dialog('destroy').remove();
			$('.flashplayer_top').css('display', 'block');
		})
		$('div#additional_info').dialog({
			resizable:false,
			draggable:false,
			modal:true,
			minWidth:300,
			width:300,
			minHeight:200,
			height:200,
			title:"",
			closeText: ""
		});
	});
})

function createSongDialog(id) {
    
	$.ajax({
		url: 'ajax.php',
		data: 'id='+id,
		dataType: 'json',
		success: function(msg) {
			//alert(msg);
			$('body').append(getSongDialogHtml(msg));
			$('.flashplayer_top').css('display', 'none');
			$('a#details_close').click(function() {
				$('div#additional_info').dialog('destroy').remove();
				$('.flashplayer_top').css('display', 'block');
			})
			$('div#additional_info').dialog({
				resizable:false,
				draggable:false,
				modal:true,
				minWidth:800,
				width:800,
				minHeight:580,
				height:580,
				title:"Song Details",
				closeText: ""
			});
		}
	});
}



function load(orderby) {
    
	$.ajax({
		url: 'allonlinesongs.php',
                data: 'allRatedSongs&orderby='+orderby,
		success: function(msg) {
			//alert(msg);
			$('#allRatedSongsContent').html((msg));
		}
	});
//        $.ajax({
//		url: 'allonlinesongs.php',
//                data: 'allUnratedSongs',
//		success: function(msg) {
//			//alert(msg);
//			$('#allUnratedSongsContent').html((msg));
//		}
//	});
}

function getSongDialogHtml(answer){
	var dialogHtml = '<div id="additional_info">';
	
	dialogHtml += '<div id="cross"><a id="details_close"><img src="images/cross.png" /></a></div>';
	
	dialogHtml += '<table id="dialogTable">';
	dialogHtml += '<tr><td style="text-align: center;">';
		dialogHtml += 'Song '+answer.interpret+' - '+answer.title+' hier Probeh&ouml;ren und bei bei Amazon downloaden';
	dialogHtml += '</td></tr>';
	
	dialogHtml += '<tr>';
	dialogHtml += '<td class="details_data" style="text-align: center;">'+answer.amazon_widget+'</td>';
	dialogHtml += '</tr>';
	
	dialogHtml += '<tr><td style="text-align: center;">';
	dialogHtml += 'oder '+answer.interpret+' - '+answer.title+' hier kostenlos mit einem Testzugang downloaden';
	dialogHtml += '</td></tr>';
	
	dialogHtml += '<tr class="advertise"><td style="text-align: center;">';
		dialogHtml += '<a href="'+answer.ad_link_top+'"><img src="banner/'+answer.ad_path_to_banner_top+'" title="'+answer.ad_title_top+'" border="0" /></a>';
	dialogHtml += '</td></tr>';
	
	dialogHtml += '</table></div>';
	
	return dialogHtml;
}

function getLoginDialogHtml() {
	
	var html = '<div id="additional_info">';
	
	html += '<div id="cross"><a id="details_close"><img src="images/cross.png" /></a></div>';

	html += '<form method="post" action="auth.php">';
	html += '<div id="logon">';
	html += '<table border="0" cellspacing="0" cellpadding="0" class="outside_table">';
	html += '<colgroup>';
	html += '<col width="120" />';
	html += '<col width="*" />';
	html += '</colgroup>';
	html += '<thead>';
	html += '<tr>';
	html += '<td class="grouptitle" colspan="2">Zugangsdaten</td>';
	html += '</tr>';
	html += '</thead>';
	html += '<tbody>';
	html += '<tr>';
	html += '<td class="labelspace" colspan="2">&nbsp;</td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td class="label">Login*&nbsp;</td>';
	html += '<td class="label"><input class="input" type="text" id="login" name="login" size="10" /></td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td class="labelspace" colspan="2">&nbsp;</td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td class="label">Passwort*&nbsp;</td>';
	html += '<td class="label"><input class="input" type="password" id="password" name="password" size="10" /></td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td class="labelspace" colspan="2">&nbsp;</td>';
	html += '</tr>';
	html += '</tbody>';
	html += '</table>';
	html += '<input name="login_button" type="submit" id="login_button" value="Anmelden" />';
	html += '</div>';
	
	html += '</form>';
	return html;
}

function getDeleteDialogHtml(id, get, title, interpret){
	
	var html = '<div id="additional_info">';
	
	html += '<div id="cross"><a class="details_close"><img src="images/cross.png" /></a></div>';

	html += '<form method="post" action="'+get+'">';
	html += '<div id="logon">';
	html += '<table border="0" cellspacing="0" cellpadding="0" class="outside_table">';
	html += '<colgroup>';
	html += '<col width="120" />';
	html += '<col width="*" />';
	html += '</colgroup>';
	html += '<thead>';
	html += '<tr>';
	html += '<td class="grouptitle" colspan="2">Wollen Sie diesen Eintrag wirklich l&ouml;schen?</td>';
	html += '</tr>';
	html += '</thead>';
	html += '<tbody>';
	html += '<tr>';
	html += '<td class="labelspace" colspan="2">&nbsp;</td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td class="label">Titel</td>';
	html += '<td class="label">'+title+'</td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td class="labelspace" colspan="2">&nbsp;</td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td class="label">Interpret</td>';
	html += '<td class="label">'+interpret+'</td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td class="labelspace" colspan="2">&nbsp;</td>';
	html += '</tr>';
	html += '</tbody>';
	html += '</table>';
	html += '<input type="hidden" name="del" value="'+id+'" />';
	html += '<input type="submit" name="delete_yes_button" onclick="location.href=\''+get+'\'" value="Ja" />';
	html += '<input type="button" class="details_close" value="Nein" />';
	html += '</div>';
	
	html += '</form>';
	return html;
}
