var newwindow = '';

$(document).ready(function(){
	$('#newwin').addClass('clickable')
     .click(function(){  popitup('playeronly.php')})
//     .click(function(){  popitup('ptest.php')})

	$('#stuur').addClass('clickable')
     .click(function(){
		var sewat=$('input[@name=stuurwat]').val()
       newwindow.sedit(sewat)
       newwindow.self.focus()
     })

	$('.playlistanchor').click(function() {
		var sewat=$(this).attr('title')
       newwindow.sedit(sewat)
       newwindow.self.focus()
		return false
	})

	$('.addtoplaylist').click(function() {
		$(this).after('<img class="progress" src="progress.gif" alt="loading ...">')
		addfiletoplaylist($(this).attr('record_id'))
		})
	.addClass('clickable')


	$('.openplaylist').click(function() {
		$(this).after('<img class="progress" src="progress.gif" alt="loading ...">')
			window.setTimeout('fokusnuut()',1000)
		})
	.addClass('clickable')


	$('.plremove').click(function() {
		removefilefromplaylist($(this).attr('record_id'))

	})
	.addClass('clickable')

	window.setTimeout("periodicfocus()",5000)
})

function periodicfocus () {

//	window.self.focus()
	if (top.location != self.location) {
		top.location = self.location
	}

//	window.setTimeout("periodicfocus()",2000)
	//$('#playlist').after('x')
}

function popitup(url) {
	if (!newwindow.closed && newwindow.location) {
		newwindow.location.href = url;
//		alert('existing')
	}
	else {
		newwindow=window.open(url,'name','height=280,width=440,left=1000,scrollbars=1,resizeable=1');
		if (!newwindow.opener) newwindow.opener = self;
	}

	newwindow.self.focus()

}

function setplaylist_origin(xml,variant,static){

	newwindow.setPlaylist(xml,'xspf',0)

}



function addfiletoplaylist(record_id) {
  var poststr="record_id="+record_id
		$.ajax({
   			type: "POST",
   			url: "ajx/ajx_addtoplaylist.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){

				if (jr.respz.code==0) {
					alert(jr.respz.feedback)
					$('.progress').remove()
				}
				else {
				// actually ajax should rebuild playlist on server
				// then instruct player to reload playlistanchor
				//  playlist is NOT built by sending filenames to player
					window.setTimeout('fokusnuut()',1000)
//					newwindow.sedit(jr.respz.xml)
				}

			}
 		});
}

function removefilefromplaylist(record_id) {
 // playlist record_id, not mp3 or comp or entry file
  var poststr="record_id="+record_id
		$.ajax({
   			type: "POST",
   			url: "ajx/ajx_removefromplaylist.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){

				if (jr.respz.code==0) {
					alert(jr.respz.feedback)
					$('.progress').remove()
				}
				else {
					window.location.reload()
//					setPlaylistbySession()
				}

			}
 		});
}
function fokusnuut() {
	$('.progress').remove()
	popitup('playeronly.php')
//	newwindow.focus()
//	   var sewat=$('.addtoplaylist').attr('title')
//       newwindow.sedit(sewat)
	   newwindow.self.focus()


}


