// Photos
var photoProvider = new Flickr('photoProvider');
photoProvider.containerId = 'artist_photos';
photoProvider.thumbWidth = 75;
photoProvider.thumbHeight = 75;
photoProvider.maxPages = 1;
photoProvider.pagingHtml = '<a href="' + $('#flickrChannelURL').val() + '" target="_blank" class="channelLink"><img src="../img/flickrChannel.png" /><span>flickr</span></a><div id="flickrtext">Artist Gallery</div>';

// Videos
var videoProvider = new YouTube('videoProvider');	
videoProvider.containerId = 'artist_videos';
videoProvider.thumbWidth = 114;
videoProvider.thumbHeight = 75;
videoProvider.overlayImage = '/img/play.png';
videoProvider.pagingHtml = '<a href="' + $('#youTubeChannelURL').val() + '" target="_blank" class="channelLink"><img src="../img/ytChannel.png" /><span>youtube</span></a><div id="youtubetext">Artist Channel</div>';

// Artist Products
var ajaxPage = new AjaxPage('ajaxPage');
ajaxPage.pageURL = BASE + '/ajax/artistproducts';
ajaxPage.containerId = 'artist_products_box';
ajaxPage.pagingContainerId = 'artist_products_paging';
ajaxPage.numItemsPerPage = 8;

$(document).ready(function()
{
	// Artist products
	ajaxPage.totalElements = $('#totalProducts').html();
	var artistId = $('#artistId').html();
	
	ajaxPage.init(artistId);
	
	// Photos
	// This always returns up to 20 results because this is all the flick allows
	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=" + $('#flickr_tag').val() + "&lang=en-us&format=json&jsoncallback=?",
		function (data)
		{
			photoProvider.showFeed(data);
		});
});


function showMoreInfo(showId)
{
	var divId = '#ShowMoreInfo' + showId;
	var isVisible = !$(divId).hasClass('hidden');

	if (!isVisible)
	{
		$(divId).show('normal');
	}
	else
	{
		$(divId).hide('normal');
	}
	
	$(divId).toggleClass('hidden');
}

function showWaterfrontShare()
{
	$('#waterfront_share').slideToggle("fast");
}

function selectWaterfrontLink()
{	
	document.getElementById('waterfrontArtistShareLink').focus();
    document.getElementById('waterfrontArtistShareLink').select();
}

function cssUpload(uploadCssFieldName)
{
	var artistId = $("#artisthiddenId").val();
	var cssfile = $("#cssfile").val();
	
	if (cssfile != '')
	{
		$.ajaxFileUpload
		({
			url: BASE + '/ajax/customcssupload/?fileNameId=' + uploadCssFieldName
			  	+ '&artistId=' + artistId
				+ '&cssfile=' + cssfile,
			secureuri: false,
			fileElementId: uploadCssFieldName,
			dataType: 'json',
			success: function (msg)
			{
				window.location = BASE + "/artist/" + artistId;
			}
		});
	}
	else
	{
		alert("Please upload css file.");
	}
}

function sampleDownload()
{
	window.open(BASE + '/ajax/downloadcss', 'Download');
}

function artistSignup(prevPage)
{
	var name = $("#name").val();
	var phone = $("#phone").val();
	var email = $("#email").val();
	var password = $("#newPassword").val();
	var confirmpassword = $("#confirmPassword").val();
	var reason = $("#reason").val();
	
	$.ajax({
		type: "POST",
		url: BASE + "/ajax/artistusersignupprocess",
		data: "name=" + name
			+ "&phone=" + phone
			+ "&email=" + email
			+ "&newPassword=" + password
			+ "&confirmPassword=" + confirmpassword
			+ "&reason=" + reason
			+ "&prevpage=" + prevPage,
		success: function(msg)
		{
			if (msg == "OK")
			{
				close_form();
				alert ("Your detail has been submitted. We'll inform you as soon as your regestration will be confirmed.");
				window.location = BASE + '/' + prevPage;
			}
			else
			{
				$("#signup_form_box").html(msg);
			}
		}
	});
}

function artistUpdate(prevPage)
{
	var artistId = $("#currentArtistId").val();
	var userId = $("#userId").val();
	var reason = $("#reason").val();
	
	$.ajax({
		type: "POST",
		url: BASE + "/ajax/artistuserupdateprocess",
		data: "artistId=" + artistId
			+ "&userId=" + userId
			+ "&reason=" + reason
			+ "&prevpage=" + prevPage,
		success: function(msg)
		{
			if (msg == "OK")
			{
				close_form();
				
				alert ("Your detail has been submitted. We'll inform you as soon as your regestration will be confirmed.");
				window.location = BASE + '/' + prevPage;
			}
			else
			{
				$("#signup_form_box").html(msg);
			}
		}
	});
}

var editor;

function displayRichTextBox(div, strLen)
{
	/*if (editor)
	{
		editor.destroy();
	}*/
	
	var description = $("#artist_description").html();
	
	$("#left_box .jScrollPaneContainer").remove();
	$("#description_message").hide();
	$("#edit_button").hide();
	
	$("#edit_artist_description").show();
	$("#edit_artist_description").html(description);
	
	editor = CKEDITOR.replace(div);
	$("#save_button").show();
}

function saveDescription(artistId)
{
	var content_text = editor.getData();

	content_text = escape(content_text);
	content_text = content_text.replace('+', '%2B');
	content_text = content_text.replace('/', '%2F');

	$.ajax({
		type: "POST",
		url: BASE + '/ajax/artistdescription/',
		data: "content_text=" + content_text
			+ "&artistId=" + artistId,
		success: function(msg)
		{
			if (msg == 'OK')
			{
				window.location = BASE + "/artist/" + artistId;
			}
		}
	});
}

function uploadImage(uploadFieldName)
{
	var artistId = $("#artistHiddenId").val();
	var artistImage = $("#artistDefaultImage").val();
	
	$.ajaxFileUpload({
		url: BASE + '/ajax/artistuploadimage/?fileInputId=' + uploadFieldName
			+ '&artistId=' + artistId
			+ '&artistImage=' + artistImage,
		secureuri: false,
		fileElementId: uploadFieldName,
		dataType: 'json',
		success: function (msg)
		{
			if (msg != '')
			{
				alert(msg);
			}
			else
			{
				window.location = BASE + "/artist/" + artistId;
			}
		}
	});
}

function deleteImage(artistId)
{
	var isConfirmed = confirm('Are you sure you want to delete the image?');
	
	if (isConfirmed)
	{
		$.ajax({
			type: "POST",
			url: BASE + '/ajax/artistdeleteimage',
			data: "artistId=" + artistId,
			success: function()
			{
				$("#imagePreviewWrapper").html('');
			}
		});
	}
}

function removeShow(showId, artistId)
{
	var isConfirmed = confirm('Are you sure you want to delete the show?');
	
	if (isConfirmed)
	{
		$.ajax({
			type: "POST",
			url: BASE + '/ajax/artistremoveshow',
			data: "showId=" + showId,
			success: function()
			{
				window.location = BASE + "/artist/" + artistId;
			}
		});
	}
}

function addLink()
{
	var artistId = $("#artistHiddenId").val();
	var linkTitle = $("#linkTitle").val();
	var linkUrl = $("#linkUrl").val();
	
	$.ajax({
		type: "POST",
		url: BASE + '/ajax/artistaddlink',
		data: "artistId=" + artistId
			+ "&linkTitle=" + linkTitle
			+ "&linkUrl=" + linkUrl,
		success: function(msg)
		{
			if (msg == "OK")
			{
				window.location = BASE + "/artist/" + artistId;
			}
			else
			{
				var title = $("#divLinkTitle").html();
				var url = $("#divLinkUrl").html();
				var icon = $("#iconDiv").html();
				
				$("#link_form_box").html(msg);
				
				$("#iconDiv").html(icon);
				$("#divLinkTitle").html(title);
				$("#divLinkUrl").html(url);
			}
		}
	});
}

function updateDiv(divId, val)
{
	$("#" + divId).html("<b>" + val + "</b>");
	
	if (divId == 'divLinkUrl')
	{
		$.ajax({
			type: "POST",
			url: BASE + '/ajax/artistaddlinkdisplayimage',
			data: "url=" + val,
			success: function(msg)
			{
				$("#iconDiv").html(msg);
			}
		});
	}
}

function removeLink(contentId, artistId)
{
	var isConfirmed = confirm('Are you sure you want to delete the link?');
	
	if (isConfirmed)
	{
		$.ajax({
			type: "POST",
			url: BASE + '/ajax/artistremovelink',
			data: "contentId=" + contentId,
			success: function()
			{
				window.location = BASE + "/artist/" + artistId;
			}
		});
	}
}

function artistShow()
{
	var artistId = $("#showArtistId").val();
	var showId = $("#showId").val();
	var tourHiddenId = $("#tourHiddenId").val();
	var tourId = $("#tourId").val();
	var showVenueName = $("#showVenueName").val();
	var showLocation = $("#showLocation").val();
	var showShowDate = $("#showShowDate").val();
	var showOnSaleDate = $("#showOnSaleDate").val();
	var showPreSaleDate = $("#showPreSaleDate").val();
	var showWebsite = $("#showWebsite").val();
	var showSupportArtists = $("#showSupportArtists").val();
	var showPlayingTimes = $("#showPlayingTimes").val();
	var showTicketCategoryId = $("#showTicketCategoryId").val();
	var showShowTypeId = $("#showShowTypeId").val();
	
	$.ajax({
		type: "POST",
		url: BASE + '/ajax/artistsaveshow',
		data: "showArtistId=" + artistId		
			+ "&showId=" + showId
			+ "&tourHiddenId=" + tourHiddenId			
			+ "&tourId=" + tourId
			+ "&showVenueName=" + showVenueName
			+ "&showLocation=" + showLocation
			+ "&showShowDate=" + showShowDate
			+ "&showOnSaleDate=" + showOnSaleDate
			+ "&showPreSaleDate=" + showPreSaleDate
			+ "&showWebsite=" + showWebsite
			+ "&showSupportArtists=" + showSupportArtists
			+ "&showPlayingTimes=" + showPlayingTimes
			+ "&showTicketCategoryId=" + showTicketCategoryId
			+ "&showShowTypeId=" + showShowTypeId,
		success: function(msg)
		{
			if (msg == "OK")
			{
				window.location = BASE + "/artist/" + artistId;
			}
			else
			{
				$("#show_form_box").html(msg);
			}
		}
	});
}

function initAutoComplete(id, ajaxHandler, maxLimit)
{
	var html = '';
	
	$('#' + id).autocomplete
	(
		BASE + '/ajax/' + ajaxHandler + '/',
		{
			minChars: 1,
			delay: 300,
			max: maxLimit,
			matchSubset: false,
			scrollHeight: 600,
			formatItem:function(item)
			{
				html = item.length > 1 ? "<img class=\"right\" src=\"" + item[1] + "\" alt=\"\"/>\n" : "";
				return html + item[0];
			}
		}
	);
}

function tourFormDisplay(action)
{
	var tourArtistId = $("#tourArtistId").val();
	var tourId = (action == 'edit') ? $("#tourId").val() : "";
	
	$.ajax({
		type: "POST",
		url: BASE + '/ajax/artisttourform',
		data: "tourId=" + tourId
			+ "&tourArtistId=" + tourArtistId,
		success: function(msg)
		{
			$("#tourInfo").html(msg);
		}
	});
}

function artistTour(action)
{
	var artistId = $("#tourArtistId").val();
	var tourId = (action == 'edit') ? $("#tourId").val() : "";
	var tourEventStatusId = $("#tourEventStatusId").val();
	var tourName = $("#tourName").val();	
	var tourStatusComment = $("#tourStatusComment").val();
	var tourArtistName = $("#tourArtistName").val();
	var tourOnSaleDate = $("#tourOnSaleDate").val();
	var tourPreSaleDate = $("#tourPreSaleDate").val();
	var tourArchiveDate = $("#tourArchiveDate").val();
	
	$.ajax({
		type: "POST",
		url: BASE + '/ajax/artistsavetour',
		data: "tourArtistId=" + artistId
			+ "&tourId=" + tourId
			+ "&tourEventStatusId=" + tourEventStatusId
			+ "&tourName=" + tourName
			+ "&tourStatusComment=" + tourStatusComment
			+ "&tourArtistName=" + tourArtistName
			+ "&tourOnSaleDate=" + tourOnSaleDate
			+ "&tourPreSaleDate=" + tourPreSaleDate
			+ "&tourArchiveDate=" + tourArchiveDate,
		success: function(msg)
		{
			if (msg == "OK")
			{
				window.location = BASE + "/artist/" + artistId;
			}
			else
			{
				$("#tourInfo").html(msg);
			}
		}
	});
}