$(document).ready(

				

	function() {

		

		init_autocomplite();

		

		$.geo({

			country: {

				object_id: 'user_country_id'

			},

			region: {

				object_id: 'user_region_id'

			}

		});

		

		init_maxlength();

		

	}

				

);



function init_autocomplite() {

	

	$('.city-autocomplete').each(

			

		function() {

			

			$(this).autocomplete({serviceUrl:'libraries/geo/autocomplete_cities'});

				

		}

		

	);

	

	$('.region-autocomplete').each(

			

		function() {

			

			$(this).autocomplete({serviceUrl:'libraries/geo/autocomplete_regions'});

				

		}

		

	);

		

	$('.artist-autocomplete').each(

		

		function() {

				

			$(this).autocomplete({serviceUrl:'libraries/artist/autocomplete_artists'});

				

		}

		

	);

	

	$('.venue-autocomplete').each(

			

		function() {

					

			$(this).autocomplete({serviceUrl:'libraries/venue/autocomplete_venues'});

					

		}

			

	);

		

	$('.festival-autocomplete').each(

		

		function() {

				

			$(this).autocomplete({serviceUrl:'libraries/festival/autocomplete_festivals'});

				

		}

		

	);

	

	$('.venue-festival-autocomplete').each(

			

		function() {

					

			$(this).autocomplete({serviceUrl:'libraries/venue/autocomplete_venues_festivals'});

					

		}

			

	);

	

}



function init_maxlength() {

	

	$('textarea[maxlength]').each(

	

		function() {

			

			var maxlength = parseInt($(this).attr('maxlength'));

			

			if(maxlength) {

				

				var maxlength_counter = null; 

				

				if($('#' + $(this).attr('id') + '_counter').size()) {

					

					maxlength_counter = $('#' + $(this).attr('id') + '_counter');

					

					var l = $(this).val()=='Type your comment in here...'?0:$(this).val().length;

					

					$(maxlength_counter).html(((maxlength - l)>0?(maxlength - l):0) + ' characters remaining');

					

				}	

			

				$(this).keypress(

				

					function(event) {

						

						var key = event.which;

						

						if(key >= 33 || key == 13) {

						

							if($(this).val().length >= maxlength) {

							

								return false;

								

							}

							

						}

						

						

						

					}

				

				);

				

				if(maxlength_counter) {

				

					$(this).keyup(

					

						function() {

							

							var l = $(this).val()=='Type your comment in here...'?0:$(this).val().length;

							

							$(maxlength_counter).html(((maxlength - l)>0?(maxlength - l):0) + ' characters remaining');

							

						}	

							

					);

					

					$(this).focus(

							

						function() {

							

							var l = $(this).val()=='Type your comment in here...'?0:$(this).val().length;

							

							$(maxlength_counter).html(((maxlength - l)>0?(maxlength - l):0) + ' characters remaining');

								

						}	

								

					);

					

					$(this).blur(

							

						function() {

							

							var l = $(this).val()=='Type your comment in here...'?0:$(this).val().length;

							

							$(maxlength_counter).html(((maxlength - l)>0?(maxlength - l):0) + ' characters remaining');

								

						}	

									

					);

				

				}

				

				$(this).mouseover(

						

					function() {

							

						if($(this).val().length > maxlength) {

							

							$(this).attr('value', $(this).val().substr(0, maxlength));

								

						}

						

						if(maxlength_counter) {

							

							var l = $(this).val()=='Type your comment in here...'?0:$(this).val().length;

							

							$(maxlength_counter).html(((maxlength - l)>0?(maxlength - l):0) + ' characters remaining');

							

						}

							

					}	

					

				);

				

				$(this).change(

						

					function() {

								

						if($(this).val().length > maxlength) {

								

							$(this).attr('value', $(this).val().substr(0, maxlength));

									

						}

						

						if(maxlength_counter) {

							

							var l = $(this).val()=='Type your comment in here...'?0:$(this).val().length;

							

							$(maxlength_counter).html(((maxlength - l)>0?(maxlength - l):0) + ' characters remaining');

							

						}	

						

					}	

						

				);

				

			}	

			

		}	

	

	);

	

}



function login() {

	

	$('#submit_login').addClass('loading');

	

	$('.validation').html('');

	
	if (parent.location.protocol == "http:") {
	
		base_url = $('#BASE_URL').attr('value')
		
	}
	else {
		
		base_url = $('#BASE_URL_HTTPS').attr('value')
		
	}
	
	
	$.post(

		base_url + 'libraries/user/login',

		$("#login_form").serialize(),

		function(report) {

			

			$('#submit_login').removeClass('loading');

			

			if(!report.error) {

				

				if(report.valid) {

					

					window.location = report.message;

					

				} else {

					

					$('.validation').each(

					

						function() {

							

							if(report.validation[$(this).attr('id').replace('validation_','')]) {

							

								$(this).html(report.validation[$(this).attr('id').replace('validation_','')]);

								

							}

							

						}	

					

					);

					

				}	

				

			}	

			

		},

		'json'

	);

	

}



function logout() {
	

	if (parent.location.protocol == "http:") {
	
		base_url = $('#BASE_URL').attr('value')
		
	}
	else {
		
		base_url = $('#BASE_URL_HTTPS').attr('value')
		
	}
	

	$.post(

		base_url + 'libraries/user/logout',

		{},

		function(report) {

				

			if(!report.error) {

				

				/*var l = window.location.toString();

					

				if(l.indexOf('#') != -1) {

									

					l = l.substr(0,l.indexOf('#'));

									

					window.location = l;

									

				} else {

									

					window.location = window.location;

					

				}*/

				

				window.location = $('#BASE_URL').attr('value');

				

			}	

				

		},

		'json'

	);

	

}



function add_user() {

	

	$('#submit_add_user').addClass('loading');

	

	$('.validation').html('');


	if (parent.location.protocol == "http:") {
	
		base_url = $('#BASE_URL').attr('value');
		
	}
	else {
		
		base_url = $('#BASE_URL_HTTPS').attr('value');
		
	}
	

	$.post(

		base_url + 'libraries/user/add',

		$("#add_user_form").serialize(),

		function(report) {

			

			$('#submit_add_user').removeClass('loading');

			

			if(!report.error) {

				

				if(report.valid) {

					

					$('#add_user_form').replaceWith(report.message);

					

				} else {

					

					$('.validation').each(

					

						function() {

							

							if(report.validation[$(this).attr('id').replace('validation_','')]) {

							

								$(this).html(report.validation[$(this).attr('id').replace('validation_','')]);

								

							}

							

						}	

					

					);

					

				}	

				

			}	

			

		},

		'json'

	);



}









































function send_email() {

	

	$('#submit_send_email').addClass('loading');

	

	$('.validation').html('');

	

	$.post(

		$('#BASE_URL').attr('value') + 'libraries/user/send_email/',

		$("#send_email_form").serialize(),

		function(report) {

			

			$('#submit_send_email').removeClass('loading');

			

			if(!report.error) {

				

				if(report.valid) {

					

					alert('Email sent');

					

					tb_remove();

					

				} else {

					

					$('.validation').each(

					

						function() {

							

							if(report.validation[$(this).attr('id').replace('validation_','')]) {

							

								$(this).html(report.validation[$(this).attr('id').replace('validation_','')]);

								

							}

							

						}

					

					);

					

				}	

				

			}	

			

		},

		'json'

	);



}

//VS
function send_email_report_inappropriate_material() {

	
	
	$('#submit_send_email').addClass('loading');

	

	$('.validation').html('');

	

	$.post(

		$('#BASE_URL').attr('value') + 'libraries/user/send_email_report_inappropriate_material/',

		$("#send_email_form").serialize(),

		function(report) {

			

			$('#submit_send_email').removeClass('loading');

			

			if(!report.error) {

				

				if(report.valid) {

					

					alert('Email sent');

					

					tb_remove();

					

				} else {

					

					$('.validation').each(

					

						function() {

							

							if(report.validation[$(this).attr('id').replace('validation_','')]) {

							

								$(this).html(report.validation[$(this).attr('id').replace('validation_','')]);

								

							}

							

						}

					

					);

					

				}	

				

			}	

			

		},

		'json'

	);



}

function send_email_enhanced()
{
	$('#submit_send_email_enhanced').addClass('loading');
	
	$('.validation').html('');
	
	$.post
	(
		$('#BASE_URL').attr('value') + 'libraries/email/send_email_enhanced/',

		$('#send_email_form_enhanced').serialize(),

		function( report )
		{
			$('#submit_send_email_enhanced').removeClass('loading');
			
			if( !report.error )
			{
				if( report.valid )
				{
					alert('Email sent');
					
					tb_remove();
				}
				else
				{
					$('.validation').each
					(
						function()
						{
							if( report.validation[$(this).attr('id').replace('validation_','')])
							{
								$(this).html(report.validation[$(this).attr('id').replace('validation_','')]);
							}
						}
					)
					;
				}
			}
		}
		,
		'json'
	)
	;
}



function add_user_to_favorites(user_id) {

	

	$.post(

		$('#BASE_URL').attr('value') + 'libraries/favorite/add_user/',

		{user_id:user_id},

		function(report) {

			

			alert(report.message);

			

		},

		'json'

	);

	

}



function add_photo_to_favorites(photo_id) {

	

	if($('.add-photo-to-favorites-' + photo_id).size()) {

		

		$.post(

			$('#BASE_URL').attr('value') + 'libraries/favorite/add_photo/',

			{photo_id:photo_id},

			function(report) {

				

				$('.add-photo-to-favorites-' + photo_id).each(

				

					function() {

						

						$(this).removeClass('add-photo-to-favorites');

						

						$(this).removeClass('add-photo-to-favorites-' + photo_id);

						

						$(this).addClass('remove-photo-from-favorites');

						

						$(this).addClass('remove-photo-from-favorites-' + photo_id);

						

					}

				

				);

				

			},

			'json'

		);

		

	} else if($('.remove-photo-from-favorites-' + photo_id).size()) {

	

		$.post(

			$('#BASE_URL').attr('value') + 'libraries/favorite/delete_photo/',

			{photo_id:photo_id},

			function(report) {

				

				$('.remove-photo-from-favorites-' + photo_id).each(

				

					function() {

						

						$(this).removeClass('remove-photo-from-favorites');

						

						$(this).removeClass('remove-photo-from-favorites-' + photo_id);

						

						$(this).addClass('add-photo-to-favorites');

						

						$(this).addClass('add-photo-to-favorites-' + photo_id);

						

					}

				

				);

				

			},

			'json'

		);

		

	}

	

}



function add_video_to_favorites(video_id) {

	

	if($('.add-video-to-favorites-' + video_id).size()) {

		

		$.post(

			$('#BASE_URL').attr('value') + 'libraries/favorite/add_video/',

			{video_id:video_id},

			function(report) {

				

				$('.add-video-to-favorites-' + video_id).each(

				

					function() {

						

						$(this).removeClass('add-video-to-favorites');

						

						$(this).removeClass('add-video-to-favorites-' + video_id);

						

						$(this).addClass('remove-video-from-favorites');

						

						$(this).addClass('remove-video-from-favorites-' + video_id);

						

					}

				

				);

				

			},

			'json'

		);

		

	} else if($('.remove-video-from-favorites-' + video_id).size()) {

	

		$.post(

			$('#BASE_URL').attr('value') + 'libraries/favorite/delete_video/',

			{video_id:video_id},

			function(report) {

				

				$('.remove-video-from-favorites-' + video_id).each(

				

					function() {

						

						$(this).removeClass('remove-video-from-favorites');

						

						$(this).removeClass('remove-video-from-favorites-' + video_id);

						

						$(this).addClass('add-video-to-favorites');

						

						$(this).addClass('add-video-to-favorites-' + video_id);

						

					}

				

				);

				

			},

			'json'

		);

		

	}

	

}



function add_album_to_favorites(album_id) {

	

	if($('.add-album-to-favorites-' + album_id).size()) {

		

		$.post(

			$('#BASE_URL').attr('value') + 'libraries/favorite/add_album/',

			{album_id:album_id},

			function(report) {

				

				$('.add-album-to-favorites-' + album_id).each(

				

					function() {

						

						$(this).removeClass('add-album-to-favorites');

						

						$(this).removeClass('add-album-to-favorites-' + album_id);

						

						$(this).addClass('remove-album-from-favorites');

						

						$(this).addClass('remove-album-from-favorites-' + album_id);

						

					}

				

				);

				

			},

			'json'

		);

		

	} else if($('.remove-album-from-favorites-' + album_id).size()) {

	

		$.post(

			$('#BASE_URL').attr('value') + 'libraries/favorite/delete_album/',

			{album_id:album_id},

			function(report) {

				

				$('.remove-album-from-favorites-' + album_id).each(

				

					function() {

						

						$(this).removeClass('remove-album-from-favorites');

						

						$(this).removeClass('remove-album-from-favorites-' + album_id);

						

						$(this).addClass('add-album-to-favorites');

						

						$(this).addClass('add-album-to-favorites-' + album_id);

						

					}

				

				);

				

			},

			'json'

		);

		

	}

	

}



function add_artist_to_favorites(artist_id) {

	

	if($('.add-artist-to-favorites-' + artist_id).size()) {

		

		$.post(

			$('#BASE_URL').attr('value') + 'libraries/favorite/add_artist/',

			{artist_id:artist_id},

			function(report) {

				if (report.message == 'You must be a registered user') {
					
					window.location = $('#BASE_URL').attr('value') + 'login.html';
					
				}

				$('.add-artist-to-favorites-' + artist_id).each(

				

					function() {

						

						$(this).removeClass('add-artist-to-favorites');

						

						$(this).removeClass('add-artist-to-favorites-' + artist_id);

						

						$(this).addClass('remove-artist-from-favorites');

						

						$(this).addClass('remove-artist-from-favorites-' + artist_id);

						

					}

				

				);

				

			},

			'json'

		);

		

	} else if($('.remove-artist-from-favorites-' + artist_id).size()) {

	

		$.post(

			$('#BASE_URL').attr('value') + 'libraries/favorite/delete_artist/',

			{artist_id:artist_id},

			function(report) {

				

				$('.remove-artist-from-favorites-' + artist_id).each(

				

					function() {

						

						$(this).removeClass('remove-artist-from-favorites');

						

						$(this).removeClass('remove-artist-from-favorites-' + artist_id);

						

						$(this).addClass('add-artist-to-favorites');

						

						$(this).addClass('add-artist-to-favorites-' + artist_id);

						

					}

				

				);

				

			},

			'json'

		);

		

	}

	

}

function add_show_to_favorites( show_id )
{
	if( $('.add-show-to-favorites-' + show_id).size() )
	{
		$.post
		(
			$('#BASE_URL').attr('value') + 'libraries/favorite/add_show/'
			,
			{
				'show_id': show_id
			}
			,
			function( report )
			{
				$('.add-show-to-favorites-' + show_id)
				.
				each
				(
					function()
					{
						$(this).removeClass('add-show-to-favorites');
						
						$(this).removeClass('add-show-to-favorites-' + show_id);
						
						$(this).addClass('remove-show-from-favorites');
						
						$(this).addClass('remove-show-from-favorites-' + show_id);
					}
				)
				;
			}
			,
			'json'
		)
		;
	}
	else if( $('.remove-show-from-favorites-' + show_id).size() )
	{
		$.post
		(
			$('#BASE_URL').attr('value') + 'libraries/favorite/delete_show/'
			,
			{
				'show_id': show_id
			}
			,
			function( report )
			{
				$('.remove-show-from-favorites-' + show_id)
				.
				each
				(
					function()
					{
						$(this).removeClass('remove-show-from-favorites');
						
						$(this).removeClass('remove-show-from-favorites-' + show_id);
						
						$(this).addClass('add-show-to-favorites');
						
						$(this).addClass('add-show-to-favorites-' + show_id);
					}
				)
				;
			}
			,
			'json'
		)
		;
	}
}

function rock_on_photo(photo_id) {

	

	$.post(

		$('#BASE_URL').attr('value') + 'libraries/photo/rock_on',

		{photo_id:photo_id},

		function(report) {

			

			if(!report.error) {

				

				$('.photo-rock-ons-counter-' + photo_id).each(

				

					function() {

						

						var count = parseInt($(this).html());

						

						count++;

						

						$(this).html(count);

						

					}

					

				);

				

				$('.rock-on-this-' + photo_id).hide();

				

			}

			

		},

		'json'

	);

	

}



function rock_on_video(video_id) {

	

	$.post(

		$('#BASE_URL').attr('value') + 'libraries/video/rock_on',

		{video_id:video_id},

		function(report) {

			

			if(!report.error) {

				

				$('.video-rock-ons-counter-' + video_id).each(

				

					function() {

						

						var count = parseInt($(this).html());

						

						count++;

						

						$(this).html(count);

						

					}

					

				);

				

				$('.rock-on-this-' + video_id).hide();

				

			}

			

		},

		'json'

	);

	

}



function start_search(method) {

	

	var data;

	

	var filters = '';

	

	var filters_count = 0;

	

	if($('#search_artist_name_field').size()) {

		

		data = $('#search_artist_name_field').attr('value');

		

		if(data != 'artist') {

		

			filters += '&artist=' + data;

			

			filters_count++;

			

		}

		

	}

	

	if($('#search_date_year_field').size()) {

		

		data = $('#search_date_year_field').attr('value');

		

		if(data != 'yyyy') {

			

			filters += '&year=' + data;

			

			filters_count++;

			

		}

		

	}

	

	if($('#search_date_month_field').size()) {

		

		data = $('#search_date_month_field').attr('value');

		

		if(data != 'mm') {

		

			filters += '&month=' + data;

			

			filters_count++;

			

		}

		

	}

	

	if($('#search_date_day_field').size()) {

		

		data = $('#search_date_day_field').attr('value');

		

		if(data != 'dd') {

			

			filters += '&day=' + data;

			

			filters_count++;

			

		}

		

	}

	

	if($('#search_city_name_field').size()) {

		

		data = $('#search_city_name_field').attr('value');

		

		if(data != 'city') {

		

			filters += '&city=' + data;

			

			filters_count++;

			

		}

		

	}

	

	if($('#search_place_name_field').size()) {

		

		data = $('#search_place_name_field').attr('value');

		

		if(data != 'venue/festival') {

			

			filters += '&place=' + data;

			

			filters_count++;

			

		}

		

	}

	

	if($('#search_keywords_field').size()) {

		

		data = $('#search_keywords_field').attr('value');

		

		if(data != 'keywords') {

			

			filters += '&keywords=' + data;

			

			filters_count++;

			

		}

		

	}

	

	if(method == 'photos' || method == 'photo') {

		

		window.location = $('#BASE_URL').attr('value') + 'search/photos.html?' + filters;

		

	} else if(method == 'videos' || method == 'video') {

		

		window.location = $('#BASE_URL').attr('value') + 'search/videos.html?' + filters;

		

	} else {

	

		window.location = $('#BASE_URL').attr('value') + 'search/shows.html?' + filters;

		

	}

	

}

function update_layout_of_table_with_items()
{
	jQuery('td.cell-with-items').each
	(
		function()
		{
			var maxHeight			=	0;
			
			jQuery(this).find('div.item').each
			(
				function()
				{
					if( jQuery(this).height() > maxHeight )
					{
						maxHeight	=	jQuery(this).height();
					}
				}
			)
			;
			
		    jQuery(this).find('div.item').height(maxHeight);
		}
	)
	;
}
