/************************************************
 * Zestaw funkcji do obsługi klipów poprzez AJAX
 ************************************************/

function searchClips(uniqueId, offset) {
	if ($('#szukaj_klipu'+uniqueId).val()) {
		eval('clipConfig'+uniqueId+'.clipSearch="'+$('#szukaj_klipu'+uniqueId).val()+'"');
	}
	var sth = '';
	eval("sth = clipConfig"+uniqueId+".getClipListConfigurationAsString('&')");
	if (offset != 'undefined') {
		loadPage(offset, uniqueId);
	} else {
		loadPage(0, uniqueId);
	}
} 

function setClipsGenre(uniqueId) {
	eval('clipConfig'+uniqueId+'.clipGenre="'+$('#genresList'+uniqueId).val()+'"');
	searchClips(uniqueId);
	//pagerGoToPage(0, uniqueId);
}

function setClipsOrder(order, anchorElement, uniqueId) {
	recoverAllOrderLinks();
	$(anchorElement.parentNode.firstChild).hide();
	$(anchorElement.parentNode.lastChild).show();
	eval('clipConfig'+uniqueId+'.clipOrder="'+order+'"');
	searchClips(uniqueId);
	//pagerGoToPage(0, uniqueId);
}

function recoverAllOrderLinks() {
	$('.order_link span').hide();
	$('.order_link a').show();
}

function pagerGoToPage(offset, uniqueId) {
	if (typeof uniqueIdPass != 'undefined')
	{
		if (uniqueIdPass == false)
		{
			uniqueId = '';
		}
	}
	searchClips(uniqueId, offset);
}

function loadPage(offset, uniqueId) {
	var template = '';
	
	if ($("#recommended-"+uniqueId).val() == 'true') {
		eval('clipConfig'+uniqueId+'.clipRecommended="true"');
	}
	if (listClipTemplateName)
	{
		template = '&listClipTemplateName=' + listClipTemplateName;
	}
	
	var ajaxResponseHtml 	= null;
	
	request = BASE_URL + 'indexajax.php?action=ALSong&start=listClip' + template;
	request += '&uniqueId=' + uniqueId;
	request += '&offset=' + offset;

	eval("request += clipConfig"+uniqueId+".getClipListConfigurationAsString('&')");
	$.ajax({
		type: "get",
		url: request,
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = $(this).text();
			});
			$('#clip-list-').html(ajaxResponseHtml);
		}
	});	
}

function reloadMainClip(songId, yt_id, order)
{
	var template = '';
	
	if (typeof showClipTemplateName != 'undefined')
	{
		template = '&showClipTemplateName=' + showClipTemplateName;
	}
	
	var ajaxResponseHtml 	= null;
	$('#clip-content').html('');
  $('#clip-content').addClass('fm-ajax-loading');
	$.ajax({
		type: "get",
		url: BASE_URL + 'indexajax.php?action=ALSong&start=showClip' + template + '&also_id=' + songId + '&yt_id=' + yt_id + '&order=' + order,
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = $(this).text();
			});
			$('#clip-content').html(ajaxResponseHtml);
			$('#clip-content').removeClass('fm-ajax-loading');
			for(instance in playerControllerInstances)
			{
				clearInterval(playerControllerInstances[instance]);
			}
			playerControllerInstances = new Array();
		}
	});
	
	return false;
}
function reloadMainClipWithUnique(songId, uniqueId)
{
	var template = '';
	
	if (typeof showClipTemplateName != 'undefined')
	{
		template = '&showClipTemplateName=' + showClipTemplateName;
	}
	
	var ajaxResponseHtml 	= null;
	
	$.ajax({
		type: "get",
		url: BASE_URL + 'indexajax.php?action=ALSong&start=showClip' + template + '&also_id=' + songId + '&uniqueId=' + uniqueId,
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = $(this).text();
			});
			$('#clip-content-'+uniqueId).html(ajaxResponseHtml);

		}
	});
	
	return false;
}

