/*
* Create droppable grid.
*/

(function($) { 
$.ti.bindDroppableGrid = $.prototype = {	
	
	draggableDuration: 1,

	bindDroppables: function(){
		
		$(".dayGrid",'#calendarDays').droppable({
			
			drop: function(event, ui) {
				var allowDrop, dayStart, rowStart, count, ieDropDelay;

				allowDrop = true;
				
				dayStart = $.ti.utils.getDay($(this).attr('id'));
				rowStart = $.ti.utils.getRow($(this).attr('id'));
				
				// Check if trip has start date - N.B. this can return true or false so do this check first
				allowDrop = $.ti.bindDroppableGrid.checkDates(ui.draggable, this);
				if(allowDrop == false) { 
//		  			 $.ti.messages.nonDroppableActivity;
//    				 $.ti.popup.init($.ti.messages.nonDroppableActivity());
				}
				
				// Check that we are not attempting to drop a non accommodation activity onto an accommodation grid
				if(!$(ui.draggable).hasClass('activityAccommodation') && $(this).hasClass('dayGridAccommodation')) {
					allowDrop = false;					
				}
					
				// Check that we are not attempting to drop an accommodation activity into a non accommodation slot
				if($(ui.draggable).hasClass('activityAccommodation') && !$(this).hasClass('dayGridAccommodation')) {
					allowDrop = false;
				}
				// Check that non of the possible drop grids are already occupied
				for(count = dayStart; count < ($.ti.bindDroppableGrid.draggableDuration + dayStart); count += 1) {					
					if($('#d' + count + 'r' + rowStart).hasClass('dayGridSelected')) {
						allowDrop = false;
					}
				}

				if(allowDrop === true) {
					// remove paragraph and reset margin on draggables old container paragraph
					if($(this).hasClass('dayGridAccommodation')) {
						$('p', this).remove();
						$('p', $(ui.draggable).parent()).css({'margin-top': 0});	
					}	
					
					// Set selected state					
					$.ti.bindDroppableGrid.toggleSelected($(this), true);
					
					// If this is a new activity from the bag
					if($(ui.draggable).hasClass('activity')) {
						// Create new activity at that location
						$.ti.createCalendarActivity.createActivity($(this).attr('id'), $.ti.bindDroppableGrid.draggableDuration, $(ui.draggable).attr('id'), true);
						// Remove helper
						$(ui.draggable.helper).remove();
						// Add reference to tripDate object
						$.ti.recalculateCalendarActivities.addActivity($(this).attr('id'), $.ti.bindDroppableGrid.draggableDuration, ui.draggable);

						var rel	= parseInt($(ui.draggable).attr('rel'), 10);
						var	node= $.ti.utils.getArrayItem($.ti.tripData.trip.tripActivities.activity, 'id', rel);
						if(!$.ti.tripData.trip.tripStarted) {
							dcsMultiTrack (
								'DCS.dcsuri',					document.location.pathname + 'trip_planner/trip_started',
								'WT.ti',						'Trip Planner - Plan Started',
								'WT.cg_n',						section,
								'WT.cg_s',						subSection,
								'DCSext.trip_planner_action',	'plan_started'
								);
							$.ti.tripData.trip.tripStarted = true;
						}						
						dcsMultiTrack (
							'DCS.dcsuri',					document.location.pathname + 'trip_planner/add_to_plan',
							'WT.ti',						
								'Trip Planner - Add to Plan - '+node.actName+' ['+rel+']',
							'WT.cg_n',						section,
							'WT.cg_s',						subSection,
							'DCSext.trip_planner_action',	'item_in_plan',
							'DCSext.item_name',				node.actName,
							'DCSext.item_category',			node.actCategory
							);
					} else {
						// Otherwise we are moving an existing activity around the grid
						$(this).append(ui.draggable);
						$(ui.draggable).css({
							"left": 0,
							"top": 0
						});
						// Update reference in the tripData object
						$.ti.recalculateCalendarActivities.moveActivity($(this).attr('id'), ui.draggable);
						// Update draggable id
						$(ui.draggable).attr('id', $(this).attr('id') + 'Activity');						
					}					
				}
				else {					
					// clear hover state
					$(this).removeClass('dropHover');
					$.ti.bindDroppableGrid.bigHover($(ui.draggable.parent()).attr('id'));	
					// Revert droppable to home
					$(ui.draggable).css({						
						'left': 0,
						'top': 0
					});
				}				
			},
			
			activate: function(event, ui) {								
				if($.ti.bindDroppableGrid.active === false) {
					$.ti.bindDroppableGrid.active = true;
					
					$('#calendar').addClass('dragActive');
					$.ti.bindDroppableGrid.draggableDuration = Math.floor($(ui.helper).width() / $.ti.createTripCalendar.activityWidth);
					// Clear selected state from origin					
					$.ti.bindDroppableGrid.toggleSelected($(ui.draggable).parent(), false);
				}
			},
			
			deactivate: function(event, ui) {
				if($.ti.bindDroppableGrid.active === true) {					
					$('#calendar').removeClass('dragActive');					
					$.ti.bindDroppableGrid.active = false;
				}
			},
			
			over: function(event, ui) {
								
				if($.ti.bindDroppableGrid.draggableDuration > 1) {				
					$.ti.bindDroppableGrid.bigHover(this.id);
				} else {
					$(this).addClass('dropHover');
				}
			},
			
			out: function(event, ui) {
				// Remove single droppable hover states - multiples are handled in bigHover function
				if($.ti.bindDroppableGrid.draggableDuration === 1) {
					$(this).removeClass('dropHover');
				}
			},
			
			tolerance: 'pointer'		
		});

	
		
	},
	
	active: false,
	lastDay: null,
	lastRow: null,
	
	checkDates: function(dragable, dropable) {
		var data, dropDate;
		
		if($.ti.tripData.trip.tripStartDate) {
			// Get data object from associated draggable list container
			data = $('#' + $(dragable).attr('rel')).closest('li').data('data');
			// Check if item has start date
			if(data.actStartDate) {
				// Get date of day being dropped onto
				dropDate = $(dropable).closest('.day').data('date');
				// set allowDrop as true or false based on check to see if possible drop dates fit within the activity dates
				return $.ti.calendarErrors.checkDates(data.actStartDate, data.actEndDate, dropDate, this.draggableDuration);
			}
		}
		return true;
	},
	
	bigHover: function(id) {
		var day, row, count;
		
		if(this.lastDay) {
			for (count = 0; count < $.ti.bindDroppableGrid.draggableDuration; count += 1) {
				$('#d' + this.lastDay + 'r' + this.lastRow).removeClass('dropHover');
				this.lastDay += 1;
			}
		}
		
		day = $.ti.utils.getDay(id);
		row = $.ti.utils.getRow(id);
		
		this.lastDay = day;
		this.lastRow = row;
		
		for (count = 0; count < $.ti.bindDroppableGrid.draggableDuration; count += 1) {
			$('#d' + day + 'r' + row).addClass('dropHover');
			day += 1;
		}	
	},
	
	// Clear old selected states
	toggleSelected: function(target, addOrRemove) {
		var count, day, row;
		
		day = $.ti.utils.getDay($(target).attr('id'));
		row = $.ti.utils.getRow($(target).attr('id'));
		
		for(count = day; count < ($.ti.bindDroppableGrid.draggableDuration + day); count += 1) {
			$('#d' + count + 'r' + row).toggleClass('dayGridSelected', addOrRemove)
				.removeClass('dropHover');
		}
	}
	
};
})(jQuery);

