$(document).ready(function(){

	// update URL for Edit Author if selection changes
	$('#author_id').change(function() {
	        var author_id = $("option:selected", this).val();
	        $('#author_edit').attr("href", "/author/" + author_id + "/edit");
	});

	//LICENSE SELECTOR
	$('#license').change(function() {
		$('.licensebox').hide();
		$('.' + $(this).val()).show();
	});
	//SHORTLINK GENERATOR
	$('#UploadForm .generateShortlink').change(function() {
		var shortlink = this.value.replace(/[^a-zA-Z0-9]+/g,'');
		//shortlink = shortlink.substring(0,16);
		$('#UploadForm #shortlink').val(shortlink.toLowerCase().substring(0,16));
	});

});
 
// refresh thumbnail image after uploading it
function refresh_image(thumbnail) {
	var random = (new Date()).getTime();
	$("#thumbnail_file").attr("src", thumbnail + "?"+random);
}

// show Play Trailer link if trailer exists for given film
function show_play_trailer(itemID) {
        $.getJSON('/item/json_has_trailer/' + itemID, {
        }, function(json) {
		if (json.trailer == true) {
			//alert("item has trailer");
			$("#play_trailer").css("display","block");
		} else {
			//alert("item has NO trailer");
			$("#play_trailer").css("display","none");
		}
	});
}

function validateShortlink() {
	var iChars = "!@#$%^&*()+=-[]\\\';, ./{}|\":<>?~_"; 
	for (var i = 0; i < document.publishForm.shortlink.value.length; i++) {
		if (iChars.indexOf(document.publishForm.shortlink.value.charAt(i)) != -1) {
			alert ("Your shortlink has special characters. \nThese are not allowed.");
			return false;
		}
	}
}
