<?php 
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED ^ E_STRICT); 
$rootPath = is_file("../../../../library/document.php") ? "../../../../" : "../../";
define("CMS", "korCMS");
include($rootPath."config/odyssey.ini.php");
include($rootPath."library/db.php");
include($rootPath."library/system.php");
include($rootPath."library/messages.php");
include($rootPath."library/variables.php");
include($rootPath."library/language.php");
include($rootPath."library/sef.php");
$sef = new sef();
$db = new db();
$system = new system();
$messages = new messages();
$variables = new variables();
$language = new language();
$langID = ($_GET['langID']) ? (int)$_GET['langID'] : $language->getDefaultLanguage();
$variables->getSafeVars(array_keys($_REQUEST));
if(empty($section)) $messages->addError("section not defined");
if(empty($itemID)) $messages->addError("itemID not defined");

function t($key, $langID=NULL, $context=NULL){ // shortcut of $language->getText($key, $langID=NULL, $context=NULL)
	global $language;
	global $langID;
	return $language->getText($key, $langID, $context);
}
?>

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="<?php echo $rootPath;?>admin/css/admin.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="<?php echo $rootPath;?>css/jPicker-1.1.2.min.css">
<link type="text/css" href="<?php echo $rootPath;?>admin/css/ui/jquery-ui-odyssey.css" rel="stylesheet">
<script type="text/javascript" src="<?php echo $rootPath;?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo $rootPath;?>js/jpicker-1.1.2.min.js"></script>
<script type="text/javascript" src="<?php echo $rootPath;?>admin/js/jquery-ui-All.min.js"></script>
<?php
$regional = ($language->getLanguageExtension($langID) == "_gr") ? "el" : "en";
?>
<script type="text/javascript">
jQuery(function($){
	<?php if($regional == "el"){ ?>
	$.datepicker.regional['lang'] = {
		closeText: 'Κλείσιμο',
		prevText: 'Προηγούμενος',
		nextText: 'Επόμενος',
		currentText: 'Τρέχων Μήνας',
		monthNames: ['Ιανουάριος','Φεβρουάριος','Μάρτιος','Απρίλιος','Μάιος','Ιούνιος',
		'Ιούλιος','Αύγουστος','Σεπτέμβριος','Οκτώβριος','Νοέμβριος','Δεκέμβριος'],
		monthNamesShort: ['Ιαν','Φεβ','Μαρ','Απρ','Μαι','Ιουν',
		'Ιουλ','Αυγ','Σεπ','Οκτ','Νοε','Δεκ'],
		dayNames: ['Κυριακή','Δευτέρα','Τρίτη','Τετάρτη','Πέμπτη','Παρασκευή','Σάββατο'],
		dayNamesShort: ['Κυρ','Δευ','Τρι','Τετ','Πεμ','Παρ','Σαβ'],
		dayNamesMin: ['Κυ','Δε','Τρ','Τε','Πε','Πα','Σα'],
		weekHeader: 'Εβδ',
		dateFormat: 'dd/mm/yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	<?php
	 }else{
	?>
	$.datepicker.regional['lang'] = {
		dateFormat: 'dd/mm/yy'
	}
	<?php	 
	 }
	 ?>
 	$.datepicker.setDefaults($.datepicker.regional['lang']);
	$('#ui-datepicker-div').css("display", "none");
	$('#dialog_link, ul#icons li').hover(
	function() { $(this).addClass('ui-state-hover'); }, 
	function() { $(this).removeClass('ui-state-hover'); }
	);
	
});
</script>
<style  type="text/css">
<!--
body {
	background: #ededed;	
	font-family: Verdana, sans-serif, Arial; 
	font-size:11px;	
}
.section_title{
	font-family: 'Roboto Condensed', sans-serif;
	font-size:1.4em;	
	margin: 0;
}
-->
</style>
</head>

<body>
<div id="wrapper">
<?php
$messages->printSystemMessages();
if(!$messages->hasErrors){
	$url = "index.php?section=".$section."&view=node&id=".$itemID;
	//get existing events
	$q="select events.id, events.day, events.month, events.year 
	from events, content_events where 
	content_events.mainID = events.id 
	and content_events.url like '$url%' 
	group by events.alias";
	$eventDates = $db->getRecords($q);
	$dpClass = rand();
?>
<div class="section_title"><?php echo t("ΔΙΑΧΕΙΡΙΣΗ ΤΟΥ EVENT");?></div>

<br><br>
<div class="normal">
    <div class="events_elements"><?php echo t("Αρχική ημερομηνία του event");?>: </div>
    <div class="events_elements"><input type="text" name="startingDate" id="eventDate" class="listbox <?php echo $dpClass;?>" size="15">&nbsp;&nbsp;</div>
    <div class="events_elements"><?php echo t("και να ισχύει για");?>&nbsp;&nbsp;</div>
    <div class="events_elements"><input type="text" name="daysOfEvent" id="daysOfEvent" class="listbox" size="3" value="1" onkeyup="this.value=(!this.value || parseInt(this.value) <1 || isNaN(this.value)) ? '1' : this.value;">&nbsp;&nbsp;</div>
	<div class="events_elements"><?php echo t("μέρες");?>&nbsp;&nbsp;</div>
    <div class="events_elements" style="line-height:25px;"><input type="button" value="<?php echo t("Προσθήκη");?>" class="btn btn-success" onclick="addTag($('#eventDate').val(), $('#daysOfEvent').val());">
    <script>$('.<?php echo $dpClass;?>').datepicker();</script>
    </div>
</div>
<div style="clear:both;"></div>
<div style="text-align:center;margin-top:30px;"><h4 style="font-weight:bold !important; color:white; background:grey; padding:3px;"><?php echo t("Καταχωρημένες ημερομηνίες του event");?></h4></div>
<div id="eventDates">
<?php
if(!count($eventDates)){
?>
<div style='color:silver; font-size:11px; font-style:italic'><?php echo t("Δεν υπάρχουν δεδομένα");?>...</div>
<?php 
}
foreach($eventDates as $eventDate){
	$d = $eventDate->day."/".$d = $eventDate->month."/".$d = $eventDate->year;
	?>
	<div class='date_tag'><a href='javascript:void(0)' onclick='removeTag(this, "<?php echo $d;?>");'></a><tag><?php echo $d;?></tag></div>
	<?php 
}
?>
</div>


<?php 
}
?>
</div>
<script type="text/javascript">

function addTag(tag, daysOfEvent){
	var tagContainer = $('#eventDates');
	$('#eventDate').val('');
	if(tag != ""){
		if(isTagExists(tag) === false) {
			$.ajax({
				url: '<?php echo $rootPath;?>admin/add_event_dates.php',
				data: { itemID: "<?php echo $itemID;?>", d: tag, daysOfEvent: daysOfEvent, section: '<?php echo $section;?>' },
				success: function(data) {
					if(data.indexOf("<div class='date_tag'>") != -1) {
						if(tagContainer.text().indexOf("<?php echo t("Δεν υπάρχουν δεδομένα");?>...") != -1) {
							tagContainer.html("");
							}
						tagContainer.append(data);
					}else{
						if(daysOfEvent == 1) alert("<?php echo t("Παρουσιάστηκε σφάλμα");?>\n<?php echo t("δεν ήταν δυνατή η προσθήκη της ημερομηνίας");?>");
						if(daysOfEvent > 1) alert("<?php echo t("Παρουσιάστηκε σφάλμα");?>\n<?php echo t("δεν ήταν δυνατή η προσθήκη των ημερομηνιών");?>");
					}
				}
			});			
		}else{
			alert("<?php echo t("Η ημερομηνία αυτή είναι ήδη καταχωρημένη");?>");
		}
	}
}

function removeTag(elm, d){
	var tagContainer = $('#eventDates');
	$.ajax({
		url: '<?php echo $rootPath;?>admin/remove_event_dates.php',
		data: { itemID: "<?php echo $itemID;?>", d: d },
		success: function(data) {
			data = data.trim();
			if(data == "ok") {
				$(elm).parent().remove();
				if(tagContainer.html() == ""){
					tagContainer.append("<div style='color:silver; font-size:11px; font-style:italic'><?php echo t("Δεν υπάρχουν δεδομένα");?>...</div>");
				}
			}else{
				alert("<?php echo t("Παρουσιάστηκε σφάλμα");?>\n<?php echo t("δεν ήταν δυνατή η αφαίρεση της ημερομηνίας");?>");
			}
		}
	});	
}

function isTagExists(tag){
	var exists = false;
	var content;
	var needle = "<tag>" + tag + "</tag>";
	var content = document.getElementById("eventDates").innerHTML;
		if(content){
			if(content.indexOf(needle) != -1){
				 exists = true;
			}
		}
	return exists;
}

</script>
</body>
</html>