<?php
$items = $this->contents;
if (count($items)) : ?>
<?php $i = 0; ?>
<?php foreach($items as $item) :
$staticBlock = null;
$nextFileUrl = null;
if ($item['type'] == 2) {
    $bl= new Application_Model_Block($item['item_id']);
    $staticBlock = $bl->isStatic();
    $fileUrl = null;
}
else if ($item['type'] == 1) {
    $fileUrl = null;
}
else if ($item['type'] == 0) {
	$audiofile = Application_Model_StoredFile::RecallById($item['item_id']);
	$fileUrl = $audiofile->getFileUrl();
}

if (($i < count($items) -1) && ($items[$i+1]['type'] == 0)) {
	$nextAudiofile = Application_Model_StoredFile::RecallById($items[$i+1]['item_id']);
	$nextFileUrl = $nextAudiofile->getFileUrl();
}
?>
    <li class="ui-state-default" id="spl_<?php echo $item["id"] ?>" unqid="<?php echo $item["id"]; ?>">
        <div class="list-item-container">

            <?php if ($item['type'] == 0 && $item['exists']):?>
            <div class="big_play" audioFile="<?php echo $item["id"]; ?>" data-mime-type="<?php echo $item["mime"]; ?>">
                <span class="ui-icon ui-icon-play"></span>
            </div>
            <?php elseif ($item['type'] == 1 && $item['exists']): ?>
            <div class="big_play" data-mime-type="<?php echo $item["mime"]; ?>">
                <span class="ui-icon ui-icon-play"></span>
            </div>
            <?php elseif ($item['type'] == 2 && $item['exists']): ?>
            <div class="big_play" blockId="<?php echo $item["item_id"]; ?>" blocktype="<?php echo $staticBlock?"static":"dynamic"?>">
                <span class="ui-icon ui-icon-play"></span>
            </div>
            <?php else:?>
            <div class="big_play ui-state-hover">
                <span class="ui-icon ui-icon-alert"></span>
            </div>
            <?php endif; ?>
            <div class="text-row top">
                <span class="spl_playlength"><?php echo $item["length"] ?></span>
                <?php if ($item['type'] == 0) {?>
                <span class="spl_cue ui-state-default"></span>
                <?php } else if ($item['type'] == 2) {
                        if ($staticBlock) {?>
                <span class="spl_block_expand close" blockId="<?php echo $item["item_id"]; ?>" id="expand_block_<?php echo $item["id"]?>">
                    <span class="ui-icon ui-icon-triangle-2-n-s"></span>
                    <?php echo _("Expand Static Block")?>
                </span>
                <?php   } else { ?>
                <span class="spl_block_expand close" blockId="<?php echo $item["item_id"]; ?>" id="expand_block_<?php echo $item["id"]?>">
                    <span class="ui-icon ui-icon-triangle-2-n-s"></span>
                    <?php echo _("Expand Dynamic Block")?>
                </span>
                <?php   }
                      } ?>
                <span class="spl_title"><?php echo $item['track_title'] ?></span>
            </div>
            <div class="text-row">
                <span class="spl_artist"><?php echo $item['creator'] ?></span>
                <span class="spl_offset"><?php echo $item["offset"]?></span>
            </div>
            <?php //create the crossfade icon.
                if (($i < count($items) -1) && !($items[$i]['type'] == 2 && $items[$i+1]['type'])):
            ?>
            <div id="fade_<?php echo $i ?>" class="spl_fade_control ui-state-default"></div>
            <?php endif; ?>
            <span id="remove_<?php echo $item["id"] ?>" class="ui-icon ui-icon-closethick"></span>
        </div>
        <?php if ($item['type'] == 0) {?>
        <div id="cues_<?php echo $i ?>" class="cue-edit clearfix" style="display: none">
            <?php echo $this->partial('playlist/set-cue.phtml', array(
                'id' => $item["id"],
                'cueIn' => $item['cuein'],
                'cueOut' => $item['cueout'],
            	'cueInSec' => $item['cueInSec'],
            	'cueOutSec' => $item['cueOutSec'],
            	'uri' => $fileUrl,
                'origLength' => $item['orig_length'])); ?>
        </div>
        <?php }?>


        <?php //create a fade editor box
              //(fadeout of current position + fade in of next position)
            if(($i < count($items) -1) && !($items[$i]['type'] == 2 && $items[$i+1]['type'])):
        ?>
        <div id="crossfade_<?php echo $i ?>-<?php echo $i+1 ?>" class="crossfade clearfix" style="display: none">
            <?php

            $vars = array(
                'item1' => $items[$i]['id'],
            	'fadeOut' => $items[$i]['fadeout'],
            	'fadeIn' => $items[$i+1]['fadein'],
                'item1Type' => $items[$i]['type'],
            	'cueIn1' => $items[$i]['cueInSec'],
            	'cueOut1' => $items[$i]['cueOutSec'],
                'item1Url' => $fileUrl
            );

            $item2 = array(
            	'item2Url' => $nextFileUrl,
            	'item2' => $items[$i+1]['id'],
            	'item2Type' => $items[$i+1]['type'],
            	'offset' => $items[$i]['trackSec'] - $items[$i+1]['trackoffset'],
            	'cueIn2' => $items[$i+1]['cueInSec'],
            	'cueOut2' => $items[$i+1]['cueOutSec']
            );

            if (isset($nextFileUrl)) {
            	$vars = $vars + $item2;
            }

            echo $this->partial('playlist/set-fade.phtml', $vars);
            ?>
        </div>
        <?php endif; ?>
        <?php if ($item['type'] == 2) {?>
        <ul style='display:none' id="block_<?php echo $item["id"]?>_info" class="smart-block-info expand-block-separate"></ul>
        <?php } ?>
    </li>
    <?php $i = $i+1; ?>
<?php endforeach; ?>

<?php else : ?>
<li class="spl_empty">
<?php
    if ($this->obj instanceof Application_Model_Block) {
        echo _("Empty smart block");
    } else {
        echo _("Empty playlist");
    }
?>
</li>
<?php endif; ?>
