$(document).ready(function(){	
	$("input[name^='ratingEVENT']").rating({ 
		callback: function(value, link){ 
			var datas = value.split(',');
			var objId = datas[2];
			var objType = datas[1];
			var rating = datas[0];
			loginCheck(function (rating, objId, objType) {
				$.post(baseUrl+'my-account/index/rate-object', 
				{'objType': objType, 'objId': objId, 'rating': rating}, 
				function(data) {
					
				}, 'json');
			}, rating, objId, objType);
		}
	});

	$('.otherDates').hide();
	$('.showOtherDates').live("click", function(event){
		event.preventDefault();
		event.stopPropagation();
		var id = $(this).attr('href');
		$('#otherDates_' + id).toggle('slow');
	});
	
	$('.people').hide();
	$('.showPeople').live("click", function(event){
		event.preventDefault();
		event.stopPropagation();
		var id = $(this).attr('href');
		$('#people_' + id).toggle('slow');
	});
	
	$(".deleteEvent").live("click", function(event){
		event.preventDefault();
		event.stopPropagation();
		var url = $(this).attr('href');
		var splitUrl = url.split('/');
		for(var i=0; i < splitUrl.length; i++) {
			if (splitUrl[i] == 'event') {
				var id = splitUrl[++i];
				break;
			}
		}
		var deleteEvent = function(target){
			if($(target).attr('data-recurring') == '1') {
				if (confirm('This is a recurring event, would you like to delete all instances of it?')) {
					$('#fixed').css('cursor', 'wait');
					$.post(baseUrl + 'events/edit/delete-all/',{event:id}, 
						function(data, textStatus) {
							$('#fixed').css('cursor', 'auto');
							if(textStatus == 'success') {
								alert('All these events have been deleted');
								window.location = baseUrl;
							} else if(textStatus == 'error') {
								alert(msg);
						} 
					});
				} else {
					if (confirm('Are you sure you want to delete this event?')) {
						$('#fixed').css('cursor', 'wait');
						$.post(baseUrl + 'events/edit/delete/',{event:id}, 
								function(data, textStatus) {
								$('#fixed').css('cursor', 'auto');
								if(textStatus == 'success') {
									$("#event_info_block_"+id).hide("slow");
										$("#event_info_block_"+id).remove();
										alert('This event has been deleted');
								} else if(textStatus == 'error') {
									alert(msg);
								} 
						});
					}
				}
			} else {							
				if (confirm('Are you sure you want to delete this event?')) {
					$('#fixed').css('cursor', 'wait');
					$.post(baseUrl + 'events/edit/delete/',{event:id}, 
							function(data, textStatus) {
							$('#fixed').css('cursor', 'auto');
							if(textStatus == 'success') {
								$("#event_info_block_"+id).hide("slow");
									$("#event_info_block_"+id).remove();
									alert('This event has been deleted');
							} else if(textStatus == 'error') {
								alert(msg);
							} 
					});
				}
			}
		}
				
		loginCheck(deleteEvent, $(this));
	});		
});
