<?php
//define this module's name
$mn = basename(__FILE__, ".php");
include("modules/modules.php");

/*
Available variables:
All system classes
All request variables
$p (the template path)
$mp (the module path)
$sufixID (id sufix for this module's divs)
*/

global $currentVersion;
$sectionID = $this->sectionID;
$id = (int)$_REQUEST['id'];
if (!empty($id)) {

    $marginTop = ($moduleParameters['marginTop'] == "__EMPTY__" || empty($moduleParameters['marginTop'])) ? 0 : $moduleParameters['marginTop'];
    $marginRight = ($moduleParameters['marginRight'] == "__EMPTY__" || empty($moduleParameters['marginRight'])) ? 0 : $moduleParameters['marginRight'];
    $marginBottom = ($moduleParameters['marginBottom'] == "__EMPTY__" || empty($moduleParameters['marginBottom'])) ? 0 : $moduleParameters['marginBottom'];
    $marginLeft = ($moduleParameters['marginLeft'] == "__EMPTY__" || empty($moduleParameters['marginLeft'])) ? 0 : $moduleParameters['marginLeft'];
    $paddingTop = ($moduleParameters['paddingTop'] == "__EMPTY__" || empty($moduleParameters['paddingTop'])) ? 0 : $moduleParameters['paddingTop'];
    $paddingRight = ($moduleParameters['paddingRight'] == "__EMPTY__" || empty($moduleParameters['paddingRight'])) ? 0 : $moduleParameters['paddingRight'];
    $paddingBottom = ($moduleParameters['paddingBottom'] == "__EMPTY__" || empty($moduleParameters['paddingBottom'])) ? 0 : $moduleParameters['paddingBottom'];
    $paddingLeft = ($moduleParameters['paddingLeft'] == "__EMPTY__" || empty($moduleParameters['paddingLeft'])) ? 0 : $moduleParameters['paddingLeft'];
    $useModuleContainer = ($moduleParameters['useModuleContainer'] == "__EMPTY__" || empty($moduleParameters['useModuleContainer'])) ? 0 : (int)$moduleParameters['useModuleContainer'];
    $widthInPixels = ($moduleParameters['widthInPixels'] == "__EMPTY__" || empty($moduleParameters['widthInPixels'])) ? null : (int)$moduleParameters['widthInPixels'];


    if (empty($marginTop) && empty($marginRight) && empty($marginBottom) && empty($marginLeft)) {
        $Mmargin = "0";
    } else {
        $Mmargin = $marginTop . " " . $marginRight . " " . $marginBottom . " " . $marginLeft . " ";
    }
    if (empty($paddingTop) && empty($paddingRight) && empty($paddingBottom) && empty($paddingLeft)) {
        $Mpadding = "0";
    } else {
        $Mpadding = $paddingTop . " " . $paddingRight . " " . $paddingBottom . " " . $paddingLeft . " ";
    }

    global $protocol;

    $previewWidth = $moduleParameters['previewWidth'];
    $previewHeight = $moduleParameters['previewHeight'];
    $showTitle = ($moduleParameters['showTitle'] == "__EMPTY__" || empty($moduleParameters['showTitle'])) ? false : true;
    $skin = $moduleParameters['skin'];
    $backgroundColor = ($skin == "dark") ? '#1a1a1a' : 'transparent';
    $closeBackgroundColor = ($skin == "dark") ? '#000' : '#717171';

    $q = "SELECT distinct videos.*, content_videos.title 
		FROM videos, content_videos 
		WHERE videos.linkID='$id' 
		and videos.sectionID='$sectionID' 
		and content_videos.mainID=videos.id 
		and content_videos.langID='$langID' 
		order by videos.sort";
    $records = $db->getRecords($q);
    if (count($records)) {
        if ($useModuleContainer) {
            ?>
            <div id="<?php echo $mn . $sufixID; ?>" data-editable="editable" class="responsive_float" <?php if ($moduleParameters['moduleAlign'] == "center") { ?>style="max-width:<?php echo $previewWidth; ?>px; margin-left:auto; margin-right: auto"<?php } ?><?php if ($moduleParameters['moduleAlign'] == "right") { ?>style="max-width:<?php echo $previewWidth; ?>px; margin-left:auto; margin-right: 0"<?php } ?>>

            <?php
        }
        ?>
        <style type="text/css">
            <?php
            if($useModuleContainer ){
            ?>
            #<?php echo $mn.$sufixID;?>{
	<?php 
	$alignRule = ($moduleParameters['moduleAlign'] == "center") ? "text-align" : "float";
	echo $alignRule.": ".$moduleParameters['moduleAlign'].";".PHP_EOL;
	?>
	margin : <?php echo $Mmargin;?>;
	padding : <?php echo $Mpadding;?>;
<?php
if($widthInPixels){
	?>
	width: <?php echo $widthInPixels;?>px;
	<?php 
}	
if($moduleParameters['moduleHR'] == "top" || $moduleParameters['moduleHR'] == "bottom"){
?>
	border-<?php echo $moduleParameters['moduleHR'];?> : 1px solid black;
<?php 
}
?>
}<?php
}
?>

                /**/
            #svBox {
                background-color: <?php echo $backgroundColor;?>;
            }

            .closeUiBtn {
                background-color: <?php echo $closeBackgroundColor;?>;
            }
        </style>

        <ul id="svList">
            <?php
            foreach ($records as $record) {
                switch ($record->provider) {
                    case 'youtube':
                        $videoClass = 'ytVideo';
                        break;
                    case 'vimeo':
                        $videoClass = 'vimeoVideo';
                        break;
                    case 'dailymotion':
                        $videoClass = 'dailyMVideo';
                        break;
                }
                ?>
                <li class="svThumb <?php echo $videoClass; ?>" data-videoID="<?php echo $record->videoCode; ?>"><?php echo $variables->sanitize($record->title); ?></li>
                <?php
            }
            ?>
        </ul>
        <div style="clear:both;"></div>
        <script type="text/javascript">
            $(document).ready(function () {
                <?php
                if (!$showTitle) {
                ?>
                $('.svThumb').on('click', function () {
                    $('.infoBox').html('');
                });
                <?php
                }
                if (count($records) == 1) {
                ?>
                $('.svThumb').on('click', function() {
                    var ifrm = $('.meuVideo').find('iframe');
                    var src = ifrm.attr('src');
                    var new_src = src.replace('autoplay=1', 'autoplay=0');
                    ifrm.attr('src', new_src);
                    $('.closeUiBtn').remove();
                    $('#svList').remove();
                }).click();
                <?php
                }
                ?>
            });
        </script>
        <?php
        if ($useModuleContainer) {
            ?>
            </div>
            <?php
        }
    }
}
?>