<?php
//define this module's name and path
$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)
*/

$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 . " ";
}

/*********************** Check Version ***************************/
global $thisVersion;
$version = (float)$thisVersion;
if($version < 3.3){
    ?>
<script type="text/javascript">alert("Currency Converter module requires Odyshop v3.3 or newer");</script>
<?php
    return;
}
/*********************** Check Version ***************************/

$MenuTheme = $moduleParameters["MenuTheme"];

$currency = $eshop->getCurrency();
$currency_sign = $currency->sign;
$currency_code = $currency->code;
$currency_start_position = $currency->start_position;
$currency_decimal = $currency->decimal;
$currency_decimal_seperator = $currency->decimal_seperator;
$currency_thousands_seperator = $currency->thousands_seperator;
$currency_title = $currency->title;
$currencies = $content->getContent('currencies', NULL, '*', array('title'), $langID, ' sort ');
$start_sign = ($currency_sign == $currency_code) ? $currency_sign : $currency_sign . " " . $currency_code;
$rates = $eshop->getExchangeRates();

if ($useModuleContainer){
?>
<div id="<?php echo $mn . $sufixID; ?>" class="responsive_float" data-editable="editable">
    <?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['moduleAlign'] && $moduleParameters['moduleHR']){ // there are system parameters...

if($moduleParameters['moduleHR'] == "top" || $moduleParameters['moduleHR'] == "bottom"){
?>
	border-<?php echo $moduleParameters['moduleHR'];?> : 1px solid black;
<?php 
}
}
?>
}<?php
        }
        if($MenuTheme == "light"){
?> .open_currency {
            background-position: left bottom;
            color: #e9e9e9;
        }

        .open_currency:hover {
            color: #e9e9e9; /* overidde general link behavior*/
        }

        .open_currency_down_arrow {
            color: #e9e9e9;
        }

        <?php
        }
        ?>
        #currencyMenu<?php echo $sufixID;?> {
            display: none;
            position: absolute;
            top: 25px;
            right: 0;
            border: 1px solid black;
            background-color: white;
            z-index: 9999;
            padding: 10px 0;
            min-width: 280px;
            box-shadow: 2px 2px 16px 0px rgba(0, 0, 0, 0.52) !important;
        }

        .sweet-alert p {
            text-align: left !important;
            color: #4a4a4a;
            font-size: .85em;
        }

        .sweet-alert h2 {
            font-size: 1.3em;
        }
    </style>

    <script type=text/javascript>
        odyshop.currency = {
            sign: "<?php echo $currency_sign;?>",
            code: "<?php echo $currency_code;?>",
            start_position: "<?php echo $currency_start_position;?>",
            decimal: "<?php echo $currency_decimal;?>",
            decimal_seperator: "<?php echo $currency_decimal_seperator;?>",
            thousands_seperator: "<?php echo $currency_thousands_seperator;?>",
            title: "<?php echo $currency_title;?>"
        };
        odyshop.currencies = {};
        <?php
        foreach($currencies as $item){
        ?>
        odyshop.currencies['<?php echo $item->code;?>'] = {
            sign: "<?php echo $item->sign;?>",
            code: "<?php echo $item->code;?>",
            start_position: "<?php echo $item->start_position;?>",
            decimal: "<?php echo $item->decimal;?>",
            decimal_seperator: "<?php echo $item->decimal_seperator;?>",
            thousands_seperator: "<?php echo $item->thousands_seperator;?>",
            title: "<?php echo $item->title;?>"
        };
        <?php
        }
        ?>
        odyshop.rates = [];
        odyshop.rates['EUR'] = 1;
        <?php
        foreach($rates as $key=>$value){
        ?>
        odyshop.rates['<?php echo $key;?>'] = parseFloat('<?php echo $value;?>');
        <?php
        }
        ?>
        //console.log(odyshop.rates);
        odyshop.target_currency = (getCookie('target_currency')) ? getCookie('target_currency') : "<?php echo $currency->code; ?>";
        odyshop.convert_price = function (html, e) {
            var x = e.clientX;
            var y = e.clientY;
            var window_width = $(window).width();
            var window_height = $(window).height();
            var content = $(html);
            $(e.target).append(content);
            var width = content.width();
            var height = content.height();
            if ((x + width) > window_width) content.css('right', 10);
            if ((x - width) < 0) content.css('left', 10);
            if ((y + height) > window_height) content.css('bottom', 30);
            if ((y - height) < 0) content.css('top', 50);
            $('body, .converted_price_container').on('mousedown', function () {
                $('body, .converted_price_container').off('mousedown');
                $('.converted_price_container').remove();
            });
        }
        jQuery().ready(function () {
            odyshop.sign = (odyshop.currencies[odyshop.target_currency].sign == odyshop.currencies[odyshop.target_currency].code) ? odyshop.currencies[odyshop.target_currency].sign : odyshop.currencies[odyshop.target_currency].sign + " " + odyshop.currencies[odyshop.target_currency].code;
            $('#current_currency').html(odyshop.target_currency);
            $('#current_currency_sign').html(odyshop.sign);
            setTimeout(function() {
                $('span[itemprop="price"]').each(function () {
                    if (odyshop.target_currency != odyshop.currency.code) $(this).css('cursor', 'pointer').attr('title', 'click to convert');
                    $(this).on('click', function (e) {
                        if (odyshop.target_currency == odyshop.currency.code) return; // no conversion for primary currency
                        var price = $(this).html().replace(odyshop.currency.thousands_seperator, "").replace(odyshop.currency.decimal_seperator, ".");
                        var source_rate = odyshop.rates[odyshop.currency.code];
                        var target_rate = odyshop.rates[odyshop.target_currency];
                        var new_price = formatPrice(price * (target_rate / source_rate), odyshop.currencies[odyshop.target_currency]);
                        var html = '';
                        html += '<div class="converted_price_container"><img src="<?php echo $mp . "flags/";?>' + odyshop.currencies[odyshop.target_currency].code + '<?php echo ".png";?>">';
                        html += new_price;
                        html += '&nbsp;&nbsp;<div class="approximately"><?php echo t("Τιμή κατά προσέγγιση σε") . "<br>";?>' + odyshop.currencies[odyshop.target_currency].title + '</div></div>';
                        odyshop.convert_price(html, e);
                    });
                });
            },1500);

            $('.open_currency').on('click', function(){
                $('#currency_records').scrollTop(0);
            });

            $('.currency_record').on('click', function () {
                var selected_currency = $('#current_currency').html();
                var target_currency = $(this).data('currency');
                $('#current_currency').html(target_currency);
                odyshop.target_currency = target_currency;
                var sign = (odyshop.currencies[target_currency].sign == odyshop.currencies[target_currency].code) ? odyshop.currencies[target_currency].sign : odyshop.currencies[target_currency].sign + " " + odyshop.currencies[target_currency].code;
                $('#current_currency_sign').html(sign);
                var isSeen = getCookie('currency_converter') == 'seen';
                setCookie('currency_converter', 'seen', '', '/');
                setCookie('target_currency', target_currency, '', '/');
                if (target_currency == odyshop.currency.code) {
                    $('span[itemprop="price"]').each(function () {
                        $(this).css('cursor', 'text').attr('title', null);
                    });
                    return;
                } else {
                    $('span[itemprop="price"]').each(function () {
                        $(this).css('cursor', 'pointer').attr('title', 'click to convert');
                    });
                }
                if ((odyshop.currency.code != target_currency) && (selected_currency != target_currency) && !isSeen) {
                    swal({
                        title: "<?php echo t("Προσοχή");?>",
                        text: "<span style='color:#be0000'><?php echo t("Λαμβάνεται υπ' όψη η τελευταία γνωστή ισοτιμία, αλλά η τιμή χρέωσης που θα ισχύσει παραμένει σε") . " " . $currency_code;?></span><br><br><?php echo t("Μπορείτε να κάνετε κλικ στις τιμές και να βλέπετε την κατά προσέγγιση αξία στο νόμισμα που επιλέξατε");?>. ",
                        type: "warning",
                        html: true,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "<?php echo t("Το κατάλαβα!");?>"
                    });
                }
            });
        });
    </script>
    <div class="dropDownMenuContainer">
        <a href="javascript:void(0)" onclick="dropDownMenu($(this).attr('id'), 'currencyMenu<?php echo $sufixID; ?>')" id="currencyMenuButton<?php echo $sufixID; ?>" class="open_currency">
            <?php echo t("Νόμισμα"); ?> : <span id="current_currency"></span> <span class="open_currency_down_arrow menu_content">▼</span>
        </a>
        <div id="currencyMenu<?php echo $sufixID; ?>" data-menu="dropdown" class="currencyMenu">
            <div class="currency_menu menu_content"><?php echo t("Τρέχον νόμισμα", $langID); ?> : <span id="current_currency_sign"><?php echo $start_sign; ?></span></div>
            <div id="currency_records">
            <?php
            foreach ($currencies as $currency) {
                if(!$rates[$currency->code]) continue; // skip currencies with empty exchange rate value
                $flag = $mp . "flags/" . $currency->code . ".png";
                $sign = ($currency->sign == $currency->code) ? str_replace('&nbsp;', '', $currency->sign) : str_replace('&nbsp;', '', $currency->sign) . " " . $currency->code;
                ?>
                <a href="javascript:void(0)" class="currency_record" data-currency="<?php echo $currency->code; ?>"><img src="<?php echo $flag; ?>" style="width: 35px;"><?php echo $sign; ?> ( <?php echo $currency->title; ?> )</a>
                <?php
            }
            ?>
        </div>
        </div>
    </div>
    <div id="notis"></div>
    <?php
    if ($useModuleContainer){
    ?>
</div>
<?php
}
?>
