<? $this->load->view( $this->aSystemVar['template_viewer_root'] . '_header') ?>

<script type="text/javascript">

	upload_form_num = <?=('-' == Catalog_Controller::_grGetValue('post_pic_num') ? 999 : Catalog_Controller::_grGetValue('post_pic_num'))?>;

	function addNewUploadForm( targetId )
	{

		var el = document.getElementById( targetId );
		var input_file = createNamedElement( 'input', 'Picture[]' );
			input_file.type = "file";
			input_file.size="20";


		var div_desc = document.createElement('div');
			div_desc.innerHTML = '<?=$this->lang->line('description')?>';
			div_desc.style.padding = '5px;';
			div_desc.style.margin = '0px';

		var input_desc = createNamedElement( 'input', 'Desc[]' );
			input_desc.type = "text";
			input_desc.style.display = 'block';
			input_desc.size="20";
			/*input_desc.value="<?= $this->lang->line('description_here') ?> ..."*/
			input_desc.value=""
			/*input_desc.style.fontStyle = 'italic';*/
			input_desc.onclick = function() { this.value=''; };

			div_desc.appendChild(input_desc);

		var div_copyright = document.createElement('div');
			div_copyright.innerHTML = '<?=$this->lang->line('copyright')?>';
			div_copyright.style.borderBottom = '1px solid silver';
			div_copyright.style.padding = '5px;';

		var input_copyright = createNamedElement( 'input', 'Copyright[]' );
			input_copyright.type = "text";
			input_copyright.style.display = 'block';
			input_copyright.size="20";
			/*input_copyright.value="<?= $this->lang->line('copyright_here') ?> ..."*/
			input_copyright.value=""
			input_copyright.onclick = function() { this.value=''; };

			div_copyright.appendChild(input_copyright);


		var br = document.createElement( "br" );
			el.appendChild( input_file );
			el.appendChild( br.cloneNode(true) );
			el.appendChild( div_desc );
			/*el.appendChild( br.cloneNode(true) );*/
			el.appendChild( div_copyright );
			/*el.appendChild( br.cloneNode(true) );*/
			el.appendChild( br.cloneNode(true) );

		upload_form_num--;

		return true;

	}

	function clearEmptyDesc()
	{

		var nodes = document.getElementsByName('Desc[]');
		for ( var i=0; i<nodes.length; i++ )
		{
		/*
			if ( nodes[i].value == '<?= $this->lang->line('description_here') ?> ...' )
			{
				nodes[i].value = '';
			}
		*/
		}

		var nodes = document.getElementsByName('Copyright[]');

		for ( var i=0; i<nodes.length; i++ )
		{
		/*
			if ( nodes[i].value == '' )
			{
				nodes[i].value = '';
			}
		*/
		}

	}

	function addDateSelect( container, year, month, day )
	{
		var cont = document.getElementById( 'container_' + container );
		var oDate = new Date();
		var aMonthes = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		var oMonth = createNamedElement( 'select', container + '_month' );
			oMonth.onchange = function() { makeDayList( container + '_day', this.value, day ) };
			oMonth.options[0] = new Option( 'MONTH', '', false, false);
		for ( var i=1; i<=aMonthes.length; i++ )
		{
			bSelected = month == i ? true : false;
			oMonth.options[i] = new Option( aMonthes[i-1], i, false, bSelected);
		}
			cont.appendChild( oMonth );
		var oDay = createNamedElement( 'select', container + '_day' );
			oDay.id = container + '_day';
			oDay.options[0] = new Option( 'DAY', '', false, false);

			cont.appendChild( oDay );
		var cYear = oDate.getYear();
		if( cYear < 2000 ) { cYear = cYear + 1900; }
		{
			var oYear = createNamedElement( 'select', container + '_year' );
		}
		oYear.options[0] = new Option( 'YEAR', '', false, false);
		for ( var i=0; i <= 6; i++ )
		{
			bSelected = (year == (cYear+i)) ? true : false;
			oYear.options[i+1] = new Option( cYear+i, cYear+i,false,bSelected);
		}

		cont.appendChild( oYear );

		if( month ) { oMonth.onchange(); }
	}
	function makeDayList( node, monthIndex, day )
	{
		var oDays = document.getElementById( node );
			oDays.options.length = 0;
		var aDays = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
			oDays.options[0] = new Option( 'DAY', 'null', false, false);

		for ( i=1; i<=aDays[monthIndex]; i++ )
		{
			bSelected = day == i ? true : false;
			oDays.options[i] = new Option( i, i, false, bSelected);
		}
	}

	function charCounter(field,countTarget)
	{
		maxLength = <?= $this->config->item('post_title_characters') ?>;
		field = document.getElementById(field);
		countTarget = document.getElementById(countTarget);
		var inputLength=field.value.length;
		if(inputLength >= maxLength)
		{
			field.value=field.value.substring(0,maxLength);
		}
		countTarget.innerHTML=maxLength-field.value.length;
	}

	function movePhoto(id, dir)
	{

		var cNode = document.getElementById('PicId_'+id).value;

		if ( 'up' == dir && cNode > 1 )
			var rNode = cNode-1;
		else if ( 'down' == dir && document.getElementById('PicOrder['+(+cNode+1)+']'))
			var rNode = (+cNode+1);
		else
			return false;

		var cNodeId = document.getElementById('PicOrder['+cNode+']').value;
		var rNodeId = document.getElementById('PicOrder['+rNode+']').value;
		document.getElementById('PicOrder['+cNode+']').value = rNodeId;
		document.getElementById('PicOrder['+rNode+']').value = cNodeId;

		document.getElementById('PicId_'+cNodeId).value=rNode;
		document.getElementById('PicId_'+rNodeId).value=cNode;

		temp = document.getElementById('imagebox_' + cNode).innerHTML;
		document.getElementById('imagebox_' + cNode).innerHTML = document.getElementById('imagebox_' + rNode).innerHTML;
		document.getElementById('imagebox_' + rNode).innerHTML = temp;

		document.getElementById('imagebox_' + cNode).style.backgroundColor = '#E9F7C7';
		document.getElementById('imagebox_' + rNode).style.backgroundColor = '#FCFCDD';

		return true;
	}

</script>

	<div class="colCapt">
		<?= $this->lang->line('add_post'); ?>
	</div>

	<div id="breadcrumb">
		<span><?= anchor( '', $this->lang->line('home') ) ?></span>
		<span class="divider"><?= $this->config->item('breadcrumb_divider') ?></span>
		<span><?= $this->lang->line('add_post'); ?></span>
	</div>

	<div class="formBlock">
		<div class="fSet">
			<div class="legend"><?=$this->lang->line('post')?></div>

			<div>
				&nbsp;<?= $this->validation->error_string; ?>
			</div>

			<?=form_open_multipart('post/save')?>
				<input type="hidden" name="CategoryId" value="<?=$data['Category']['Id']?>" />
				<input type="hidden" name="PostId" value="<?=$data['Post']['Id']?>" />

				<? if ( $this->bAdmin ) : ?>

					<div style="float:right;margin-bottom:10px;padding:5px;width:240px;border:1px solid #60809C;background-color:#E0E8EF;">
						<div>
							Post type:
						</div>
						<div>
							<select name="SubType">
							<? foreach ( $data['SubType']['SubTypeList'] as $aSubType ) : ?>
								<?= '<option value="' . $aSubType . '" ' . ( $aSubType != $data['Post']['SubType'] ? '' : 'selected="selected"') . ' >' . $aSubType . '</option>' ?>
							<? endforeach ?>
							</select>
						</div>

						<div><?= $this->lang->line('start_on') ?>:</div>
						<div id="container_date_start"></div>

						<div><?= $this->lang->line('end_on') ?>:</div>
						<div id="container_date_end"></div>

						<script language="javascript" type="text/javascript">
							addDateSelect( 'date_start', <?=$data['Post']['SYear']?>, <?=$data['Post']['SMonth']?>, <?=$data['Post']['SDay']?> );
							addDateSelect( 'date_end',  <?=$data['Post']['EYear']?>, <?=$data['Post']['EMonth']?>, <?=$data['Post']['EDay']?> );
						</script>

					</div>

				<? endif; ?>


				<div><?= $this->lang->line('category'); ?></div>

				<div style="margin-bottom:10px;width:230px;">
					<select name="NewCategoryId">
						<? //'<option value="0" ' . ( '0' != $data['Category']['Id'] ? '' : 'selected="selected"') . ' />root</option>' ?>
						<? foreach ( $data['Category']['CategoryList'] as $aCategory ) : ?>
							<?= '<option value="' . $aCategory['Id'] . '" ' . ( $aCategory['Id'] != $data['Category']['Id'] ? '' : 'selected="selected"') . ' />' . $aCategory['Title'] . '</option>' ?>
						<? endforeach ?>
					</select>
				</div>
				<? if ( 1 < count($this->aSystemVar['lang_list']) ) : ?>
				<div style="width:40%;">
					<div><?= $this->lang->line('langs') ?>:</div>
					<select name="PostLang">
						<?
							$sCurLang = empty($data['Post']['CatLang'])?$this->aSystemVar['cur_lang']:$data['Category']['PostLang'];
							foreach ($this->aSystemVar['lang_list'] as $aItem) : ?>
							<?='<option value="'.$aItem['BaseName'].'" '.($aItem['BaseName']!=$sCurLang?'':'selected="selected"').' />'.$aItem['Title'].'</option>'?>
						<? endforeach; ?>
					</select>
				</div>
				<? endif; ?>

				<div class="clearBoth"></div>
				<div style="margin-bottom:10px;width:400px;">
					<div class="text">
						<?= $this->lang->line('post_title') ?>&nbsp;(&nbsp;<span id="charCount" class="charCount"><?= $this->config->item('post_title_characters') ?></span><span class="charCount">&nbsp;<?= $this->lang->line('characters_left') ?></span>&nbsp;)
					</div>
					<div class="input">
						<input type="text" name="Title" id="Title" value="<?=$data['Post']['Title']?>" class="text" style="width:100%" onkeyup="return charCounter('Title', 'charCount');" />
					</div>
				</div>

				<div style="clear:both;">
					<div class="text"><?=$this->lang->line('post')?></div>
					<div class="input">
						<textarea name="Body" style="font-size:13px; width:98%; height:150px;"><?=$data['Post']['Body']?></textarea>
					</div>
				</div>
				
				<!-- custom fields -->
				<? foreach( $data['CustomFields'] as $aField )
					{
						$this->load->view($this->aSystemVar['template_viewer_root'].'design_items/custom_e_'.$aField['FieldType'],$aField);
					}
				?>
				
				<!-- -->
				<div>

					<div style="margin-bottom:10px;">
						<?= $this->lang->line('assigned_pictures') ?><a href="javascript:void(0);" class="asynchelp" onClick="javascript: initAsyncHelp( event, '<?= site_url('async/topic/_post_add_pic');?>' ); return false;" onMouseOut="javascript: hideAsyncHelp();">[?]</a>:
					</div>

					<div id="container_pic_arrange" style="margin-bottom:10px;">
						<select name="PicArrange" id="PicArrange">
						<?
						foreach( $data['Post']['PicArrangeOption'] as $sOption )
						{
							echo '<option value ="' . $sOption . '" ' . ( $data['Post']['PicArrange'] == $sOption ? 'selected="selected"' : '' ) . ' >' . $this->lang->line($sOption) . '</option>';
						}
						?>
						</select>
					</div>
					<?
						$iNum = 0;
						foreach( $data['Post']['ImageList'] as $aImage ) :
						$iNum++;
					?>

						<input type="hidden" name="PicOrder[<?=$iNum?>]" id="PicOrder[<?=$iNum?>]" value="<?=$aImage['Id']?>" />
						<div id="imagebox_<?=$iNum?>" style="margin:2px 0px;padding:5px;border:1px solid #94AA5B;background-color:#E9F7C7;">
							<div style="clear:both;"></div>
							<div style="float:left;">
								<img src="<?=$aImage['UrlIcon']?>" title="<?=$aImage['Description']?>" border="0" alt="thumb"
									onClick="window.open('<?=$aImage['UrlPic']?>', null, 'height=600,width=600,status=yes,toolbar=no,menubar=no,location=no');" />
							</div>
							<div style="margin-left:5px;float:left;">
								<?=$this->lang->line('description')?>:
								<br />
								<input type="text" name="PicDescription[<?=$aImage['Id']?>]" value="<?=$aImage['Description']?>" />
							</div>
							<div style="margin-left:5px;float:left;line-height:15px;">
								<input type="hidden" id="PicId_<?=$aImage['Id']?>" value="<?=$iNum?>" />
								<a href="javascript: void(0);" onclick="javascript: movePhoto('<?=$aImage['Id']?>','up'); return false;" >
									<img src="<?=$this->config->item('design_pic_url')?>arrow_up.gif" alt="up" />
								</a>
								<a href="javascript: void(0);" onclick="javascript: movePhoto('<?=$aImage['Id']?>','down'); return false;" >
									<img src="<?=$this->config->item('design_pic_url')?>arrow_down.gif" alt="down" />
								</a>
								<br />
								<input type="radio" name="PrimaryPicId" value="<?=$aImage['Id']?>" <?=( $data['Post']['PrimaryPicId'] == $aImage['Id'] ? 'checked="checked"' : '' )?> style="vertical-align:middle;" /><?=$this->lang->line('avatar')?>
								<br />
								&nbsp;
								<br />
								<input type="checkbox" name="DeletePicId[]" value="<?=$aImage['Id']?>" style="vertical-align:middle;" /><?=$this->lang->line('delete')?>
							</div>
							<div class="clearBoth"></div>
						</div>
						<script type="text/javascript">upload_form_num--;</script>

					<? endforeach; ?>
					<div>
						<div id="container_upload">
						</div>
						<div id="container_upload_more">
							<script language="javascript" type="text/javascript">
								if ( 0 < upload_form_num )
								{
									addNewUploadForm('container_upload');
								}

								if ( 0 >= upload_form_num )
								{
									document.getElementById('container_upload_more').style.display = 'none';
								}
							</script>

							<a href="javascript:void(0);" onClick="javascript: addNewUploadForm('container_upload');
								if ( 0 >= upload_form_num ) { document.getElementById('container_upload_more').style.display='none'; }
								return false;" ><?= $this->lang->line('add_more_pic') ?></a>
							<br />
							<br />
						</div>
					</div>
				</div>


				<? if ( empty($data['Post']['Id']) && !empty($data['Post']['SecurityCode']) ): ?>
				<div class="warning"><?= $this->lang->line('human_only') ?></div>
				<div><img src="<?=site_url('catalog/get_security_image')?>" alt="security image" title="security image" /></div>
				<div><input type="text" name="SecurityCode" class="text" /></div>
				<?	endif; ?>

				<div class="submit">
					<input type="image" src="<?= $this->config->item('design_pic_url') ?>submit_button.gif" class="submit" />
				</div>
			<?= form_close() ?>
		</div>
	</div>


<? $this->load->view( $this->aSystemVar['template_viewer_root'] . '_footer') ?>