<?php
/**
 * Pro Perfect Karousel
 *This program is free software: you can redistribute it and/or modify it under the terms
 *of the GNU General Public License as published by the Free Software Foundation,
 *either version 3 of the License, or (at your option) any later version.
 *
 *This program is distributed in the hope that it will be useful,
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *GNU General Public License for more details.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *@author ProJoom
 *@copyright (C) 2008 - 2012 ProJoom
 *@link http://www.projoom.com/extensions/pro-perfect-karousel.html.html Official website
 **/
defined( '_JEXEC' ) or die( 'Restricted access' );
class PlaylistsViewPlaylist
{
	function setPlaylistsToolbar()
	{
		JToolBarHelper::title( JText::_( 'Playlist Manager' ), 'generic.png' );
		JToolBarHelper::publishList();
		JToolBarHelper::unpublishList();
		JToolBarHelper::deleteList();
		JToolBarHelper::media_manager();
		JToolBarHelper::editListX();
		JToolBarHelper::addNewX();
		JToolBarHelper::custom("npchelp", "help.png", "help.png", "Documentation", false, false);
	}
	function isValid($pic){
		if(is_file(JPATH_ROOT ."/". $pic)){
			$ret = "<a class=\"modal\" rel=\"{handler: 'image'}\" href=\"".JURI::root().$pic."\"><img src=\"".JURI::root() . $pic."\" width=\"100\"/></a>";
		} else {
			$ret = "<a class=\"modal\" href=\"http://www.projoom.com/component/kb/article/images_are_not_loading.html?template=component\" rel=\"{handler: 'iframe', size: {x: 570, y: 550}}\">Invalid Image</a>";
		}
		return $ret;
	}
	function playlists( &$rows, &$pageNav, &$lists )
	{
		PlaylistsViewPlaylist::setPlaylistsToolbar();
		$user =& JFactory::getUser();
		JHTML::_('behavior.tooltip');
		JHTML::_('behavior.mootools');
		JHTML::_('behavior.modal');
		$document = JFactory::getDocument();
		$document->addScriptDeclaration("window.addEvent('domready', function() {document.preview = SqueezeBox;});");
		?>
		<form action="index.php?option=com_novapc" method="post" name="adminForm">
		<table>
		<tr>
			<td align="left" width="100%">
				<?php echo JText::_( 'Filter' ); ?>:
				<input type="text" name="search" id="search" value="<?php echo $lists['search'];?>" class="text_area" onchange="document.adminForm.submit();" />
				<button onclick="this.form.submit();"><?php echo JText::_( 'Go' ); ?></button>
				<button onclick="document.getElementById('search').value='';this.form.getElementById('filter_catid').value='0';this.form.getElementById('filter_state').value='';this.form.submit();"><?php echo JText::_( 'Filter Reset' ); ?></button>
			</td>
			<td nowrap="nowrap">
				<?php
				echo $lists['cid'];
				echo $lists['state'];
				?>
			</td>
		</tr>
		</table>
			<table class="adminlist">
			<thead>
				<tr>
					<th width="20">
						<?php echo JText::_( 'Num' ); ?>
					</th>
					<th width="5%">
						<input type="checkbox" name="toggle" value=""  onclick="checkAll(<?php echo count( $rows ); ?>);" />
					</th>
					<th width="50%" nowrap="nowrap" class="title">
						<?php echo JHTML::_('grid.sort',  'Name', 'b.name', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th width="5%" nowrap="nowrap">
						Thumbnail
					</th>
					<th width="20%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Category', 'c.name', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th width="8%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Published', 'b.showPlaylist', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th width="10%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Order', 'b.ordering', @$lists['order_Dir'], @$lists['order'] ); ?>
						<?php echo JHTML::_('grid.order',  $rows ); ?>
					</th>
					<th width="5%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'ID', 'b.bid', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
				</tr>
			</thead>
			<tfoot>
			<tr>
				<td colspan="8">
				<?php echo $pageNav->getListFooter(); ?>
				</td>
			</tr>
				<tr>
					<td colspan="13">
						<center><a href="http://www.projoom.com/" target="_blank" title="Pro Perfect Karousel">Pro Perfect Karousel</a> © ProJoom</center>
					</td>
				</tr>
			</tfoot>
			<tbody>
			<?php
			$k = 0;
			for ($i=0, $n=count( $rows ); $i < $n; $i++) {
				$row = &$rows[$i];
				$row->id	= $row->bid;
				$link		= JRoute::_( 'index.php?option=com_novapc&task=edit&cid[]='. $row->id );
				$row->published = $row->showPlaylist;
				$published		= JHTML::_('grid.published', $row, $i );
				$checked		= JHTML::_('grid.checkedout',   $row, $i );
				?>
				<tr class="<?php echo "row$k"; ?>">
					<td align="center">
						<?php echo $pageNav->getRowOffset($i); ?>
					</td>
					<td align="center">
						<?php echo $checked; ?>
					</td>
					<td align="center">
					<span class="editlinktip hasTip" title="<?php echo JText::_( 'Edit' );?>::<?php echo $row->name; ?>">
						<?php
						if ( JTable::isCheckedOut($user->get ('id'), $row->checked_out ) ) {
							echo $row->name;
						} else {
							?>
							<a href="<?php echo $link; ?>">
								<?php echo $row->name; ?></a>
							<?php
						}
						?>
						</span>
					</td>
					<td align="center">
						<?php echo PlaylistsViewPlaylist::isValid($row->npcimg); ?>
					</td>
					<td align="center">
						<?php echo $row->catg_name;?>
					</td>
					<td align="center">
						<?php echo $published;?>
					</td>
					<td class="order">
						<span><?php echo $pageNav->orderUpIcon($i, ($row->catid == @$rows[$i-1]->catid), 'orderup', 'Move Up', true ); ?></span>
						<span><?php echo $pageNav->orderDownIcon($i, $n, ($row->catid == @$rows[$i+1]->catid), 'orderdown', 'Move Down', true ); ?></span>
						<input type="text" name="order[]" size="5" value="<?php echo $row->ordering;?>" class="text_area" style="text-align: center" />
					</td>
					<td align="center">
						<?php echo $row->id; ?>
					</td>
				</tr>
				<?php
				$k = 1 - $k;
			}
			?>
			</tbody>
			</table>
		<input type="hidden" name="c" value="playlist" />
		<input type="hidden" name="option" value="com_novapc" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="filter_order" value="<?php echo $lists['order']; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $lists['order_Dir']; ?>" />
		<?php echo JHTML::_( 'form.token' ); ?>
		</form>
		<?php
	}
	function setPlaylistToolbar()
	{
		$task = JRequest::getVar( 'task', '', 'method', 'string');
		JToolBarHelper::title( $task == 'add' ? JText::_( 'Playlist' ) . ': <small><small>[ '. JText::_( 'New' ) .' ]</small></small>' : JText::_( 'Playlist' ) . ': <small><small>[ '. JText::_( 'Edit' ) .' ]</small></small>', 'generic.png' );
		JToolBarHelper::save( 'save' );
		JToolBarHelper::apply('apply');
		JToolBarHelper::cancel( 'cancel' );
	}
	function playlist( &$row, &$lists )
	{
		PlaylistsViewPlaylist::setPlaylistToolbar();
		JRequest::setVar( 'hidemainmenu', 1 );
		JFilterOutput::objectHTMLSafe( $row, ENT_QUOTES, 'npcbigdesc' );
		JRequest::setVar( 'hidemainmenu', 1 );
		JHTML::_('behavior.tooltip');
		JHTML::_('behavior.mootools');
 		JHTML::_('behavior.modal', 'a.modal');
		$document = JFactory::getDocument();
		$document->addScriptDeclaration("window.addEvent('domready', function() {document.preview = SqueezeBox;});");
		?>
		<script language="javascript" type="text/javascript">
		<!--
		function submitbutton(pressbutton) {
			var form = document.adminForm;
			if (pressbutton == 'cancel') {
				submitform( pressbutton );
				return;
			}
			if (form.name.value == "") {
				alert( "<?php echo JText::_( 'You must provide a slide name.', true ); ?>" );
			} else if (getSelectedValue('adminForm','cid') < 1) {
				alert( "<?php echo JText::_( 'Please select a category.', true ); ?>" );
			} else if (form.npctt.value == "") {
				alert( "<?php echo JText::_( 'You must provide a slide title.', true ); ?>" );
			} else if (form.npcimg.value == "") {
				alert( "<?php echo JText::_( 'You must provide a slide image path.', true ); ?>" );
			} else {
				submitform( pressbutton );
			}
		}
		function addPictureEntry(selection){
			var form = document.adminForm;
			if (selection != "None"){
				form.npcimg.value = "images/ppk/" + selection;
				address = "<?php echo JURI::root(); ?>";
				thediv = document.getElementById("picbox");
				thediv.innerHTML = '<img src="' + address + 'images/ppk/' + selection + '" width="450px"/>';
			} else {
				thediv.innerHTML = "";
			}
		}
		function jSelectArticle(id, title, object) {
			document.getElementById(object).value = id;
			document.getElementById('sbox-window').close();
		}
        function jInsertEditorText( text, editor ) {
		  text = text.match(/src=(.+?[\.jpg|\.gif]")/i)[1];
          text = text.replace(/\"/g,"");
          document.getElementById(editor).value = text;
		}
		//-->
		</script>
		<form action="index.php" method="post" name="adminForm">
		<div class="col100">
			<fieldset class="adminform">
				<legend><?php echo JText::_( 'Details' ); ?></legend>
				<table class="admintable">
				<tbody>
					<tr>
						<td width="20%" class="key">
							<label for="name">
								<?php echo JText::_( 'Name' ); ?>:
							</label>
						</td>
						<td width="40%">
							<input class="inputbox" type="text" name="name" id="name" size="50" value="<?php echo $row->name;?>" />
						</td>
						<td rowspan="9" align="center">
							<?php echo JText::_( 'Preview' ); ?>
							<div id="picbox"> </div>
						</td>
					</tr>
					<tr>
						<td class="key">
							<label for="cid">
								<?php echo JText::_( 'Category' ); ?>:
							</label>
						</td>
						<td >
							<?php echo $lists['cid']; ?>
						</td>
					</tr>
					<tr>
						<td valign="top" class="key">
							<label for="npctt">
								<?php echo JText::_( 'Title' ); ?>:
							</label>
						</td>
						<td>
					<input class="inputbox" type="text" name="npctt" id="npctt" size="50" value="<?php echo $row->npctt;?>" />
						</td>
					</tr>
					<tr>
						<td width="20%" class="key">
							<label for="picture">
								<?php echo JText::_( 'Stored Picture' ); ?>:
							</label>
						</td>
						<td width="35%">
							<select style="float:left;" onclick="addPictureEntry(this.value);">
								<option>None</option>
							<?php
								$dir = JPATH_ROOT . "/images/ppk";
    							$farray = JFolder::files($dir, $filter = '.');
    							foreach ($farray as $file) {
    								echo '<option value="'.$file.'">'.$file.'</option>';
    							}
							?>
							</select>
                            <span style="float:left;" class="button2-left"><div class="blank"><a class="modal" title="Select an image" href="index.php?option=com_media&amp;view=images&amp;tmpl=component&amp;e_name=npcimg" rel="{handler: 'iframe', size: {x: 700, y: 400}}">Select image</a></div></span>
						</td>
					</tr>
					<tr>
						<td valign="top" class="key">
							<label for="npcimg">
								<?php echo JText::_( 'Image' ); ?>:
							</label>
						</td>
					<td>
						<textarea class="inputbox" cols="70" rows="2" name="npcimg" id="npcimg"><?php echo $row->npcimg;?></textarea>
						</td>
					</tr>
					<tr>
						<td valign="top" class="key">
							<label for="npcbigdesc">
								<?php echo JText::_( 'Description' ); ?>:
							</label>
						</td>
					<td>
						<textarea class="inputbox" cols="70" rows="10" name="npcbigdesc" id="npcbigdesc"><?php echo $row->npcbigdesc;?></textarea>
						</td>
					</tr>
					<tr>
						<td valign="top" class="key">
							<label for="npclink">
								<?php echo JText::_( 'Link' ); ?>:
							</label>
						</td>
						<td>
						<?php
							$article =& JTable::getInstance('content');
							$value = $row->npclink;
							if (is_numeric($value)){
								$article->load($value);
								$tip = "Article::".htmlspecialchars($article->title, ENT_QUOTES, 'UTF-8');
							} else {
								$tip = "Link::".$value;
							}
						?>
						<textarea class="hasTip" title="<?php echo $tip; ?>" cols="62" rows="2" name="npclink" id="npclink"><?php echo $row->npclink;?></textarea>
						<span style="float:right;" class="button2-left"><div class="blank"><a class="modal" title="Select an Article"  href="index.php?option=com_content&amp;task=element&amp;tmpl=component&amp;object=npclink" rel="{handler: 'iframe', size: {x: 650, y: 375}}">Select</a></div></span>
						</td>
					</tr>
					<tr>
						<td class="key">
							<?php echo JText::_( 'Open in new window?' ); ?>:
						</td>
						<td>
							<?php echo $lists['npctarget']; ?>
						</td>
					</tr>
					</tbody>
				</table>
			</fieldset>
		</div>
		<div class="clr"></div>
		<input type="hidden" name="c" value="playlist" />
		<input type="hidden" name="option" value="com_novapc" />
		<input type="hidden" name="bid" value="<?php echo $row->bid; ?>" />
		<input type="hidden" name="task" value="" />
		<?php echo JHTML::_( 'form.token' ); ?>
		</form>
		<?php
	}
}
