<!-- 
	Calendar Edit Popup
-->
<?php if($this->invalidEvent) {
	echo ("This event has been removed.");
	}
	else
	{
?>
<div id="calpop">
	<div class="scroll-content">
	<p>
		<label class="TitleLabel" style="height: 28px;">
 			<input type="text" maxlength="1000" value="<?php echo ical_decode($this->params['Title']); ?>" class="EventName" title="<?php echo $this->translate("Title"); ?>">
		</label>
	</p>
	
	<p>
		<label class="LocationLabel" style="height: 22px;">
 			<input type="text" maxlength="1000" value="<?php echo ical_decode($this->params['Location']); ?>" class="EventLocation" title="<?php echo $this->translate("Location"); ?>">
		</label>
	</p>
	
	
	<p>
		<label class="AllDay">
			<span class="labelName"><?php echo $this->translate('All Day') ?></span>
			<input type="checkbox" id="caledit_allday" <?php if(isset($this->params['allDay']) && $this->params['allDay'] == 1) echo "checked" ?>>
		</label>
	</p>
	<p>
		<label class="TimeZone">
			<span class="labelName"><?php echo $this->translate('Timezone') ?></span>
		</label>
		<select name="fields[TimeZone]" id='TimeZone' style="float: left; margin-top:3px;">
			<option value=""><?php echo $this->translate('None') ?></option>
			<?php echo $this->timezoneList; ?>
		</select>
		
	</p>
	<p>
		<label class="StartDate">
			<span class="labelName"><?php echo $this->translate('Start') ?></span>
			<input type="text" class="datepicker" value="<?php print $this->params["DateStart"]; ?>" id="caledit_startdate">
		</label>
		<select id="caledit_starthour" style="float: left;  margin: 5px;"><?php echo $this->params['startHourList']?></select>
		<select id="caledit_startminute" style="float: left;  margin: 5px;"><?php echo $this->params['startMinuteList']?></select>
	</p>
	<p>
		<label>
			<span class="labelName"><?php echo $this->translate('End') ?></span>
			<input type="text" class="datepicker" value="<?php print $this->params["DateEnd"]; ?>" id="caledit_enddate">
		</label>
		<select id="caledit_endhour" style="float: left; margin: 5px;"><?php echo $this->params['endHourList']?></select>
		<select id="caledit_endminute" style="float: left;  margin: 5px;"><?php echo $this->params['endMinuteList']?></select>
	
	</p>
	<p>
		<label>
			<span class="labelName"><?php echo $this->translate('Attendees') ?></span>
		</label>
			<div id="attendeesDiv"><input type="text" name="attendees" id="attendeesInput" class="calPopupInput" value="<?php print $this->params["attendees"]; ?>" style="width:197px;" /></div>
	</p>
		<p>
			<label>
				<span class="labelName"><?php echo $this->translate('Category') ?></span>
				<select id="caledit_category"><?php echo $this->params['categoryList']; ?></select>
			</label>
		</p>
		
		<p>
			<label>
				<span class="labelName"><?php echo $this->translate('Availability') ?></span>
				<select id="caledit_availablity"><?php echo $this->params['availabilityList']; ?></select>
			</label>
		</p>
		
		<p>
			<label>
				<span class="labelName">Note</span>
				<textarea rows="1" style="height: 100%; width: 197px;" id="caledit_message"><?php echo ical_decode($this->params["message"]) ?></textarea>
			</label>
		</p>
		
		<p>	
			<label>
				<span class="labelName"><?php echo $this->translate('Remind') ?></span>
				<input type="text" id='caledit_alert' value="<?php echo $this->params['alertTrigger'];?>"  style="width:64px;" class="caledit_alert_num"></input>
			</label>
			<select id="caledit_alertType" style="float: left; margin: 2px;"><?php echo $this->params['alertTypeList']; ?></select>
			<select id='caledit_alertposition' style="float: left; margin: 2px;"><?php echo $this->params['alertPositionList']; ?></select>
		</p>
	
		<p>
			<label>
				<span class="labelName"><?php echo $this->translate('Repeat') ?></span>
				<select id="repeat_freqency">
					<?php echo $this->params['repeatList']; ?>
				</select>
			</label>
		</p>

		<p>
			<label>
				<span class="labelName"><?php echo $this->translate('End') ?></span>
			</label>
			<span id="PatternSelect">
				<select id="end_type" name="end_type">
						<option value="0" <?php if($this->params['RecurrenceArray']['Occurences'] == '' && gettype($this->params['RecurrenceArray']['PatternEndDate']) != 'object') echo "selected"; ?>><?php echo $this->translate('None') ?></option>
						<option value="1" <?php if($this->params['RecurrenceArray']['Occurences'] != '') echo "selected"; ?>><?php echo $this->translate('After') ?></option>
						<option value="2" <?php if(gettype($this->params['RecurrenceArray']['PatternEndDate']) == 'object') echo "selected"; ?>><?php echo $this->translate('By') ?></option>
				</select>
				<span id="end_after" style="display: none;">
					<input type="input" name="end_after_occurrences" maxlength="3" value="<?php if($this->params['RecurrenceArray']['Occurences'] != '') echo $this->params['RecurrenceArray']['Occurences']; else echo "1";?>" style="width: 50px;"> <?php echo $this->translate('time(s)') ?>
				</span>
				<span id="end_by" style="display: none;">
					<input type="text" class="datepicker" value="<?php print (isset($this->params["RecurrenceArray"]["PatternEndDate"]) ? date('Y-m-d', ( gettype($this->params["RecurrenceArray"]["PatternEndDate"]) == "object" ) ? $this->params["RecurrenceArray"]["PatternEndDate"]->getTime() : time() ) : $this->params["DateStart"]); ?>" id="caledit_endbydate">
				</span>
			</span>
		</p>
		
		</div>
		<ul id="EditEvent-Btns">
			<li onclick="deleteEvent('<?php echo $this->params['full_id']; ?>');"><a href="#"><span><strong><?php echo $this->translate('Delete') ?></strong></span></a></li>
			<li class="EventSave" onclick="saveAndExit('<?php echo $this->contextId; ?>');"><a href="#"><span><strong><?php echo $this->translate('Save') ?></strong></span></a></li>
			<li class="EventCancel" onclick="saveAndExit('<?php echo $this->contextId; ?>', true);"><a href="#"><span><strong><?php echo $this->translate('Cancel') ?></strong></span></a></li>
		</ul>
</div>

<script type="text/javascript">

	// lazy code right here **** FIX ME
	var siteBaseUrl = "<?php echo $this->siteBaseUrl ?>";
	var ownerUsername = "<?php echo $this->owner_username; ?>";
	var relativeHref = "<?php echo $this->params['relative_href']; ?>";

	// **************************************
	function keypressHandler(ev)
	{
		if(ev.keyCode == 13)
		{
			$('.EventSave', '#calpop').trigger('click');
		}
	}
	
	$( function() {
		
		var titleElement = $('.TitleLabel', '#calpop');
		titleElement.bind('keypress', keypressHandler ).focus();
		var params = new Array();
		params['fullId'] = "<?php echo $this->params['full_id']; ?>";
		params['alertType'] = "<?php echo $this->params['alertType']; ?>";
		params['alertTrigger'] = "<?php echo $this->params['alertTrigger']; ?>";
		params['alertTriggerStartHour'] = "<?php echo (isset($this->params['alertTriggerStartHour']) ? $this->params['alertTriggerStartHour'] : ''); ?>";
		params['alertPosition'] = "<?php echo $this->params['alertPosition']; ?>";
		params['alertTriggerStartMinute'] = "<?php (isset($this->params['alertTriggerStartMinute']) ? $this->params['alertTriggerStartMinute'] : ''); ?>";
		params['nowdate'] = "<?php $now = new Date(); $now->addHours(1); list($nowdate, $junk) = explode(' ', $now->getDate()); echo $nowdate; ?>";
		params['starthour1'] = "<?php $now = new Date(); $now->addHours(1); echo $now->getHour(); ?>";
		params['startmin1'] = "<?php $now = new Date(); $now->addHours(1); echo $now->getMinute(); ?>";
		startEditCalendar("<?php echo $this->contextId; ?>", params);
		titleElement.find('input').select();

		$('span', 'li[name="<?php print $this->contextId; ?>"]', '#navMessages').text("<?php print htmlentities(addslashes($this->params['Title'])); ?>");
		var tz = '<?php echo $this->params['timezone']; ?>';
		$("#TimeZone", "<?php echo $this->contextId; ?>").val(tz);
		
		
		// TextboxList and Autocomplete initialization prepopulated with any saved-draft addresses
		attendeesArray = activeSplit( $('#attendeesInput', '#calpop').attr('value') );
		$('#attendeesInput', '#calpop').attr('value', '');
		
		var attendeesTextBoxListHandle = new TextboxList(  $('#attendeesInput', '#calpop'), {bitsOptions:{editable:{addOnBlur:true }}, unique: true, plugins: {autocomplete: {placeholder:false,cacheSize: <?php echo $this->autocompleteCacheSize; ?>}}});	
        
		//for loaded appointments, load in saved attendees
		for( var rcpt in attendeesArray ) 
		{
			value = attendeesArray[rcpt];
			//value = $('<div/>').text( value ).html()
			if( value.length > 3 )
				attendeesTextBoxListHandle.add(value);
		}
		
		// Support clicking anywhere in the div box to get to the last "bitbox selection"
		$("#attendeesDiv", '#calpop').bind('click', function() { $("input.textboxlist-bit-editable-input:last", $(this) ).focus() })
		
		if( $.jCacher.get('autocomplete') )
		{

			autocompleteValues = $.jCacher.get('autocomplete').value;
			attendeesTextBoxListHandle.plugins['autocomplete'].setValues( autocompleteValues )
			if(autocompleteValues.length >= <?php echo $this->autocompleteFetchThreshold ?>)
			{

				attendeesTextBoxListHandle.plugins['autocomplete'].setOptions({

					placeholder: false,
					cacheSize: <?php echo $this->autocompleteCacheSize; ?>,
					minRemoteLength: <?php echo $this->autocompleteMinFetchLength; ?>,
					queryRemote: true,
					remote: {url: '<?php echo $this->moduleBaseUrl ?>/contacts/autocomplete/limit/<?php echo $this->autocompleteFetchSize; ?>'}

				});

			}

		}
		else
		{

		    $.ajax(
			{
				cache:false, 
				url: '<?php echo $this->moduleBaseUrl ?>/contacts/autocomplete/limit/<?php echo $this->autocompleteFetchThreshold; ?>', 
				dataType: 'json', 
				success: function(r) 
				{
					// above static lookup, enable dynamic lookup
					if( r.length >= <?php echo $this->autocompleteFetchThreshold ?> )
					{
						attendeesTextBoxListHandle.plugins['autocomplete'].setOptions({
							placeholder: false,
							cacheSize: <?php echo $this->autocompleteCacheSize; ?>,
							minRemoteLength: <?php echo $this->autocompleteMinFetchLength; ?>,
							queryRemote: true,
							remote: {url: '<?php echo $this->moduleBaseUrl ?>/contacts/autocomplete/limit/<?php echo $this->autocompleteFetchSize; ?>'}
						});

					}
					attendeesTextBoxListHandle.plugins['autocomplete'].setValues(r)
					$.jCacher.add('autocomplete', r, 3600);
					
				}
				
			})
			
		}
		
		//Fetch Attendees
		$.ajax(
		{
			cache:false, 
			url: '<?php echo $this->moduleBaseUrl ?>/calendar/getattendees',
			data: {"id" : '<?php echo $this->params['full_id']; ?>', "href" : relativeHref}, 
			dataType: 'json', 
			success: function(r) 
			{
				
				if( r.status != undefined && r.status == true && r.result != undefined  )
				{

					$.each(r.result, function(k, value) 
					{
						attendeesTextBoxListHandle.add(value);
					})

				}
				
			}
			
		})
		
	});
	
</script>
<?php
	}
