$(document)
		.ready(
				function() {
					jQuery.fn.exists = function() {
						return jQuery(this).length > 0;
					}

					$(".selectable-event")
							.click(
									function(e) {
										window.location.href = '/agenda/sortie/' + this.id
												.substr(1) + '.html';
									});

					$("#button-post-comment-event").click(function(e) {
						$("#event-comments-loader").show();
						$.getJSON("/ajax/add-comment", {
							'comment' : $("#user-comment").val(),
							'eventId' : $("#event-id").val()
						}, displayEventComments);
						$("#user-comment").val("");
					});

					displayEventComments();

					function displayEventComments() {
						if ($("#event-comments").exists()) {
							$.getJSON("/ajax/get-comments?eventId="
									+ $("#event-id").val(), function(data) {
								$("#event-comments").html("");

								$.each(data, function(i, item) {
									var commentDiv = $(document
											.createElement('div'));
									commentDiv.html(
											'<strong>'
													+ item.user_nickname
													+ '</strong> '
													+ item.creation_timestamp
													+ '<br /><p>'
													+ item.comment.replace(
															/\n/g, "<br />")
													+ '</p>').appendTo(
											$("#event-comments"));
								});
							});

							$("#event-comments-loader").hide();
						}
					}

					$("#form_button_reset").click(
							function(e) {
								$(':input', '#agenda_search_form').not(
										':button, :submit, :reset, :hidden')
										.val('').removeAttr('checked')
										.removeAttr('selected');
								$('#event_musical_style_id').val(0);
								$('#event_region').val('none');
								$('#event_category').val('none');
								/* $("#agenda_search_form").submit(); */
								return false;
							});
					
					$("#form_button_clubbing_reset").click(
							function(e) {
								$(':input', '#agenda_clubbing_search_form').not(
										':button, :submit, :reset, :hidden')
										.val('').removeAttr('checked')
										.removeAttr('selected');
								$('#event_location_id').val(0);
								return false;
							});

					$(function() {
						$('.agenda-date-picker').datepicker( {
							dateFormat : 'dd/mm/yy',
							minDate : new Date(),
							showAnim : 'drop'
						}, $.datepicker.regional['fr'])
					});

					$(function() {
						$('.date-picker').datepicker( {
							dateFormat : 'dd/mm/yy'
						}, $.datepicker.regional['fr'])
					});

					$("#post_event_form").keypress(function(e) {
						if (e.which == 13) {
							var tagName = e.target.tagName.toLowerCase();
							if (tagName !== "textarea") {
								return false;
							}
						}
					});

					$("#use_contact_phone").click(function(e) {
						if ($('#use_contact_phone').is(':checked')) {
							$('#contact_phone').removeAttr('disabled');
						} else {
							$('#contact_phone').attr('disabled', true);
						}
					});

					function togglePriceBox() {
						if (($('#free_entrance').is(':checked') && $(
								'#free_entrance_gender').val() != 'everyone')
								|| ($('#free_entrance').is(':checked')
										&& $('#free_entrance_gender').val() == 'everyone' && $(
										'#free_entrance_period').val() == "delimited")
								|| !$('#free_entrance').is(':checked')) {
							$('#price').removeAttr('disabled');
							$('#price_in_advance').removeAttr('disabled');
							$('#price-box').show("fast");

						} else {
							$('#price').attr('disabled', true);
							$('#price_in_advance').attr('disabled', true);
							$('#price-box').hide("fast");

						}
					}

					$("#free_entrance")
							.click(
									function(e) {
										if ($('#free_entrance').is(':checked')) {
											$('#free_entrance_gender')
													.removeAttr('disabled');
											$('#free_entrance_period')
													.removeAttr('disabled');

											if ($('#free_entrance_period')
													.val() == "delimited") {
												$(
														'#free_entrance_closing_timeslot')
														.removeAttr('disabled');
												$('#timeslot-box').show();
											} else {
												$(
														'#free_entrance_closing_timeslot')
														.attr('disabled', true);
												$('#timeslot-box').hide();
											}
											togglePriceBox();
										} else {
											$('#free_entrance_gender').attr(
													'disabled', true);
											$('#free_entrance_period').attr(
													'disabled', true);
											$('#free_entrance_closing_timeslot')
													.attr('disabled', true);

											togglePriceBox();
										}
									});

					$("#free_entrance_period").change(
							function() {
								togglePriceBox();
								if ($(this).val() == "delimited") {
									$('#free_entrance_closing_timeslot')
											.removeAttr('disabled');
									$('#timeslot-box').show();
								} else {
									$('#free_entrance_closing_timeslot').attr(
											'disabled', true);
									$('#timeslot-box').hide();
								}
							});

					$("#free_entrance_gender").change(function() {
						togglePriceBox();
					});

					if ($('#free_entrance_period').val() == "delimited") {
						$('#free_entrance_closing_timeslot').removeAttr(
								'disabled');
						$('#timeslot-box').show();
					}

					$("#happy_hour").click(function(e) {
						if ($('#happy_hour').is(':checked')) {
							$('#happy_hour_type').removeAttr('disabled');
							$('#happy_hour_gender').removeAttr('disabled');
							$('#happy_hour_start').removeAttr('disabled');
							$('#happy_hour_stop').removeAttr('disabled');

						} else {
							$('#happy_hour_type').attr('disabled', true);
							$('#happy_hour_gender').attr('disabled', true);
							$('#happy_hour_start').attr('disabled', true);
							$('#happy_hour_stop').attr('disabled', true);
						}
					});

					if ($("#button_upload_flyer_recto").exists()) {
						var button_recto = $('#button_upload_flyer_recto'), interval;

						new AjaxUpload(
								button_recto,
								{
									action : '/ajax/upload-flyer/side/recto',
									name : 'recto',
									onSubmit : function(file, ext) {
										this.disable();
										$('#loading_flyer_recto').show();
									},
									onComplete : function(file, response) {
										window.clearInterval(interval);
										this.enable();
										if (response.substring(0, 6) == 'ERROR:') {
											alert(response.substring(6,
													response.length));
										} else {
											$('#flyer_recto_filename').val(
													response);
											$('#image_flyer_recto')
													.attr(
															'src',
															'/images/flyers/medium/' + response);
										}
										$('#loading_flyer_recto').hide();
									}
								});
					}

					if ($("#button_upload_flyer_verso").exists()) {
						var button_verso = $('#button_upload_flyer_verso'), interval;
						new AjaxUpload(
								button_verso,
								{
									action : '/ajax/upload-flyer/side/verso',
									name : 'verso',
									onSubmit : function(file, ext) {
										this.disable();
										$('#loading_flyer_verso').show();
									},
									onComplete : function(file, response) {
										window.clearInterval(interval);
										this.enable();
										if (response.substring(0, 6) == 'ERROR:') {
											alert(response.substring(6,
													response.length));
										} else {
											$('#flyer_verso_filename').val(
													response);
											$('#image_flyer_verso')
													.attr(
															'src',
															'/images/flyers/medium/' + response);
										}
										$('#loading_flyer_verso').hide();
									}
								});
					}
					$("#button_remove_flyer_recto").click(
							function(e) {
								$('#flyer_recto_filename').val('');
								$("#image_flyer_recto").attr('src',
										'/images/flyer-a-side.png');
								return false;
							});

					$("#button_remove_flyer_verso").click(
							function(e) {
								$('#flyer_verso_filename').val('');
								$("#image_flyer_verso").attr('src',
										'/images/flyer-b-side.png');
								return false;
							});

					$(".selectable-article").click(
							function(e) {
								window.location.href = '/nightlife/' + this.id
										.substr(1) + '.html';
							});

					$("#button-post-comment-article").click(function(e) {
						$.getJSON("/ajax/add-article-comment", {
							'comment' : $("#user-comment").val(),
							'articleId' : $("#article-id").val()
						}, displayArticleComments);
						$("#user-comment").val("");
					});

					displayArticleComments();

					function displayArticleComments() {
						if ($("#article-comments").exists()) {
							$.getJSON("/ajax/get-article-comments?articleId="
									+ $("#article-id").val(), function(data) {
								$("#article-comments").html("");

								$.each(data, function(i, item) {
									var commentDiv = $(document
											.createElement('div'));
									commentDiv.html(
											'<strong>'
													+ item.user_nickname
													+ '</strong> '
													+ item.creation_timestamp
													+ '<br /><p>'
													+ item.comment.replace(
															/\n/g, "<br />")
													+ '</p>').appendTo(
											$("#article-comments"));
								});
							});
						}
					}

					$("#post_article_form").keypress(function(e) {
						if (e.which == 13) {
							var tagName = e.target.tagName.toLowerCase();
							if (tagName !== "textarea") {
								return false;
							}
						}
					});

					if ($("#button_upload_article_cover").exists()) {
						var button_cover = $('#button_upload_article_cover'), interval;

						new AjaxUpload(
								button_cover,
								{
									action : '/ajax/upload-article-cover',
									name : 'file',
									onSubmit : function(file, ext) {
										this.disable();
										$('#loading-article-cover').show();
									},
									onComplete : function(file, response) {
										window.clearInterval(interval);
										this.enable();
										if (response.substring(0, 6) == 'ERROR:') {
											alert(response.substring(6,
													response.length));
										} else {
											$('#article_cover_filename').val(
													response);
											$('#image_article_cover')
													.attr(
															'src',
															'../images/covers/medium/' + response);
										}
										$('#loading-article-cover').hide();
									}
								});
					}

					$("#button_remove_article_cover").click(
							function(e) {
								$('#article_cover_filename').val('');
								$("#image_article_cover").attr('src',
										'../images/flyer-a-side.jpg');
								return false;
							});

					$(".selectable-contest").click(
							function(e) {
								window.location.href = '/concours/' + this.id
										.substr(1) + '.html';
							});

					$(".selectable-gallery").click(
							function(e) {
								window.location.href = 'photos/' + this.id
										.substr(1) + '.html';
							});

					if ($("#slideshow").exists()) {
						$('#slideshow').cycle( {
							fx : 'scrollLeft',
							speed : 'slow',
							timeout : 3000,
							cleartype : true,
							cleartypeNoBg : true,
							pager : '#slideshow-navigation',
							pagerAnchorBuilder : function(idx, slide) {
								return '<a href="#">&nbsp;</a>';
							}
						});

						$('#slideshow').mouseover(function() {
							$('#slideshow').cycle('pause');
						});

						$('#slideshow').mouseout(function() {
							$('#slideshow').cycle('resume');
						});
					}

					var newsletterEmailValue = 'Votre adresse e-mail';
					$('#newsletter-email')
							.click(
									function() {
										if ($('#newsletter-email').val() == newsletterEmailValue)
											$('#newsletter-email').val('');
									});
					$('#newsletter-email').focusout(function() {
						if ($('#newsletter-email').val() == '')
							$('#newsletter-email').val(newsletterEmailValue);
					});

					$('.birthdate-picker').datepicker( {
						dateFormat : 'dd/mm/yy',
						yearRange : '1900:2010',
						showAnim : 'drop',
						changeYear : true
					}, $.datepicker.regional['fr']);

					$(".selectable-video").click(
							function(e) {
								window.location.href = '/video-' + this.id
										.substr(1) + '.html';
							});

					$("#video_form").keypress(function(e) {
						if (e.which == 13) {
							var tagName = e.target.tagName.toLowerCase();
							if (tagName !== "textarea") {
								return false;
							}
						}
					});

					$("#button-post-comment-video").click(function(e) {
						$.getJSON("/ajax/add-video-comment", {
							'comment' : $("#user-comment").val(),
							'videoId' : $("#video-id").val()
						}, displayVideoComments);
						$("#user-comment").val("");
					});

					displayVideoComments();

					function displayVideoComments() {
						if ($("#video-comments").exists()) {
							$.getJSON("/ajax/get-video-comments?videoId="
									+ $("#video-id").val(), function(data) {
								$("#video-comments").html("");

								$.each(data, function(i, item) {
									var commentDiv = $(document
											.createElement('div'));
									commentDiv.html(
											'<strong>'
													+ item.user_nickname
													+ '</strong> '
													+ item.creation_timestamp
													+ '<br /><p>'
													+ item.comment.replace(
															/\n/g, "<br />")
													+ '</p>').appendTo(
											$("#video-comments"));
								});
							});
						}
					}

					$(".selectable-artist").click(
							function(e) {
								window.location.href = '/artiste/' + this.id.substr(1) + '.html';
							});

					if ($("#audioplayer").exists()) {
						jwplayer('audioplayer').setup( {
							'flashplayer' : '/js/player.swf',
							'id' : 'playerID',
							'width' : '0',
							'height' : '0',
							'controlbar' : 'bottom'
						}).onComplete(function() {
							$(".audio-play").show();
							$(".audio-stop").hide();
						  });

					}

					$(".audio-play").click(function(e) {
						var url = $(this).attr('href');

						jwplayer('audioplayer').load(url);
						jwplayer('audioplayer').play();

						$(".audio-play").show();
						$(".audio-stop").hide();
						$(this).parent().children('a').toggle();

						return false;
					});

					$(".audio-stop").click(function(e) {

						jwplayer('audioplayer').stop();

						$(this).parent().children('a').toggle();

						return false;
					});
					
					$("#expand-full-itunes-track-list").click(function(e) {

						$('tbody.full-itunes-track-list').show('slow');
						$('#expan-link-row').hide();
						
						return false;
					});
					
					if ($("#button_upload_artist_picture").exists()) {
						var button_picture = $('#button_upload_artist_picture'), interval;

						new AjaxUpload(
								button_picture,
								{
									action : '/ajax/upload-artist-picture',
									name : 'file',
									onSubmit : function(file, ext) {
										this.disable();
										$('#loading-artist-picture').show();
									},
									onComplete : function(file, response) {
										window.clearInterval(interval);
										this.enable();
										if (response.substring(0, 6) == 'ERROR:') {
											alert(response.substring(6,
													response.length));
										} else {
											$('#artist_picture_filename').val(
													response);
											$('#image_artist_picture')
													.attr(
															'src',
															'/images/artists/small/' + response);
										}
										$('#loading-artist-picture').hide();
									}
								});
					}

					$("#button_remove_artist_picture").click(
							function(e) {
								$('#artist_picture_filename').val('');
								$("#image_artist_picture").attr('src',
										'../images/flyer-a-side.jpg');
								return false;
							});
					
				});
