var init = munky.init;

munkyExt = {

	sortingThumbs : false,

	init : function(){
		init();
		
		munky.calendar($('form#addEvent p').eq(1));
		
		$('#currentEvents a').click(function(){
														if(confirm('Are you sure?')){
														
														}
														else {
															return false;
														}
													});
		
		/*----- Expand and close galleries when DJs update photos -----*/		
													
		var galleryOrder = $('div.grayBox.djGallery');
		galleryOrder.each(function(){
			$(this).children('p.bold').append(' <a class="expand" href="#">expand</a><span style="display:none"> | <a class="save" href="#">Save Order</a><span class="saving" style="display: none">Saving</span></a><span class="saved" style="display: none"> Saved</span>');
			$(this).children('div.djThumbnails').hide();
		});
		
		galleryOrder.children('p').children('a.expand').toggle(function(){
			$(this).blur();
			$(this).parent().parent().children('div.djThumbnails').show();
			$(this).parent().children('span').show();
			$(this).html('close');
			return false;
		}, function(){
			$(this).blur();
			$(this).parent().parent().children('div.djThumbnails').hide();
			$(this).parent().children('span').hide();
			$(this).html('expand');
			return false;
		});
		
		/*----- Save the updated order of photos -----*/
		
		/*
		 * When the sortable was released the link was receiving
		 * a click and continuing on to edit the photo that was moved.
		 * A flag - munky.sortingThumbs - is set to true when sorting
		 * starts and all links first check this value.
		 * By having the stop function set a timeout it places the function
		 * removing the flag at the end of queue for processing thus the link
		 * doesn't propagate but there is no delay in making the links active again.
		 */
		galleryOrder.children('div.djThumbnails').sortable({	'stop' : function(){ setTimeout(function(){ munky.sortingThumbs = false }, 0); }, //setTimeout with 0 time causes it to wait until after the event has propagated to the link
																				'start' : function(){ munky.sortingThumbs = true }});
																				
		galleryOrder.children('div.djThumbnails').children('a').click(function(){
			return !munky.sortingThumbs;
		});
		var saveOrder = galleryOrder.children('p').children('span').children('a.save');
		
		saveOrder.click(function(){
			$(this).blur();
			$(this).hide();
			$(this).parent().children('span.saving').show();
			
			var saveLink = $(this); //so getJSON can act on it
			
			var thumbs = $(this).parent().parent().parent().children('div.djThumbnails');
			var order = thumbs.sortable('serialize');
			
			$.getJSON(	"ajax?"+order,
							{},
							function(json){
								saveLink.show();
								saveLink.parent().children('span.saving').hide();
								if(json=="fail"){
									alert("An error occurred. The new order was not saved.");
								}
								else {
									var saved = saveLink.parent().children('span.saved');
									saved.show();
									setTimeout(function(){ saved.fadeOut(); }, 2000);
								}
							});
			
			return false;
		});
		
		//filter dj names
		$('#filter').show();
	}

}

$.extend(munky, munkyExt);
