
$(document).ready(
	function () {
		$("#licensetitle").change(function () {
			$('#licensetitle-loading').show("fast");
			$.getJSON("/musichub/ext.php",{action: 'gettitles', ltid: $('#licensetitle').val()}, function(data){
				var htmltext = '<option value="-1">TITLE</option>';
				$.each(data, function(i,item){
					htmltext+='<option value="' + item.titleid + '">' + item.title + '</option>';
				});
				$("select#title").html(htmltext);
				$('#licensetitle-loading').hide("fast");
			});
//			$('#title').load("ext.php?action=gettitles&ltid=" + $('#licensetitle').val(),function(){ $('.loading-subgenre').hide("fast"); });
			if($('#licensetitle').val()!=-1)
				$('#licensetitle_input').text($('#licensetitle').children("[@selected]").text());
			else
				$('#licensetitle_input').text('');

			$("select#subtitle").html('<option value="-1">SUB TITLE</option>');
			$('#title_input').text('');
			$("select#time").html('<option value="-1">TIME</option>');
			$('#subtitle_input').text('');
			$("select#territory").html('<option value="-1">TERRITORY</option>');
			$('#time_input').text('');
			$('#territory_input').text('');
			$('#price_input').text('');
		});

		$("#title").change(function () {
			$('#title-loading').show("fast");
			$.getJSON("/musichub/ext.php",{action: 'getsubtitles', titleid: $('#title').val()}, function(data){
				var htmltext = '<option value="-1">SUB TITLE</option>';
				$.each(data, function(i,item){
					htmltext+='<option value="' + item.subtitleid + '">' + item.subtitle + '</option>';
				});
				$("select#subtitle").html(htmltext);
				$('#title-loading').hide("fast");
				
			});
//			$('#title').load("ext.php?action=gettitles&ltid=" + $('#licensetitle').val(),function(){ $('.loading-subgenre').hide("fast"); });
			if($('#title').val()!=-1)
				$('#title_input').text($('#title').children("[@selected]").text());
			else
				$('#title_input').text('');	
				
			$("select#time").html('<option value="-1">TIME</option>');
			$('#subtitle_input').text('');
			$("select#territory").html('<option value="-1">TERRITORY</option>');
			$('#time_input').text('');
			$('#territory_input').text('');
			$('#price_input').text('');
		});
		
		$("#subtitle").change(function () {
			$('#subtitle-loading').show("fast");
			$.getJSON("/musichub/ext.php",{action: 'gettime', subtitleid: $('#subtitle').val()}, function(data){
				var htmltext = '<option value="-1">TIME</option>';
				$.each(data, function(i,item){
					htmltext+='<option value="' + $('#subtitle').val()+'_'+item.subitemid+'_'+item.timeunit+'">' + item.timeunit + '</option>';
				});
				$("select#time").html(htmltext);
				$('#subtitle-loading').hide("fast");
			});
//			$('#title').load("ext.php?action=gettitles&ltid=" + $('#licensetitle').val(),function(){ $('.loading-subgenre').hide("fast"); });
			if($('#subtitle').val()!=-1)
				$('#subtitle_input').text($('#subtitle').children("[@selected]").text());
			else
				$('#subtitle_input').text('');	
				
			$("select#territory").html('<option value="-1">TERRITORY</option>');
			$('#time_input').text('');
			$('#territory_input').text('');
			$('#price_input').text('');
		});
		
		$("#time").change(function () {
			$('#time-loading').show("fast");
			timeunit = $('#time').val();
			timearray = timeunit.split("_");
			$.getJSON("/musichub/ext.php",{action: 'getterritory', timeunit: timearray[2], subitemid: timearray[1], subtitleid: timearray[0]}, function(data){
				var htmltext = '<option value="-1">TERRITORY</option>';
				$.each(data, function(i,item){
					htmltext+='<option value="' + item.subitemid + '">' + item.territory + '</option>';
				});
				$("select#territory").html(htmltext);
				$('#time-loading').hide("fast");
			});

			if($('#time').val()!=-1)
				$('#time_input').text($('#time').children("[@selected]").text());
			else
				$('#time_input').text('');
			
			$('#territory_input').text('');
			$('#price_input').text('');
		});
		
		$("#territory").change(function () {
			$.get("/musichub/ext.php",{ action: "getprice", territory: $('#territory').val() }, function(data){
			  if(data == 0)
				  $('#price_input').text("n/a");
			  else
				  $('#price_input').text(data+"\u20AC");
			});

//			$('#price_input').load('ext.php?action=getprice&amp;subtitleid=' + $('#subtitle').val() + '&amp;timeunit=' + $('#time').val() + '&amp;territory=' + $('#territory').val());
			if($('#territory').val()!=-1)
				$('#territory_input').text($('#territory').children("[@selected]").text());
			else
				$('#territory_input').text('');
		});
			
				
	}
);
