﻿/* hb.js v:2.0, launched:20-1-11 by:Vidros Socrates */

/* Qtip */
function initQTip() {
	$('.tooltip').each(function() {
		var tooltipText = $(this).contents('span:last-child').text();
		if (tooltipText)
			$(this).qtip( {
				content : tooltipText,
				show : 'mouseover',
				hide : 'mouseout',
				style : {
					'font-size' : 12,
					name : 'blue'
				},
				position : {
					target : 'mouse',
					corner : {
						tooltip : 'leftTop'
					},
					adjust : {
						mouse : true,
						screen : true
					}
				}
			});
	})
}

/* Jquery UI - 4 - Datepickers */

function setOneYearRange() {
	var dateMin = new Date();
	var dateMax = new Date();
	var dayRange = 365;
	dateMax.setDate(new Date().getDate() + dayRange);
	return {
		minDate : dateMin,
		maxDate : dateMax
	};
}

function validateDates() {
	var d1 = new Date($('.dp-ci').datepicker("getDate"));
	var d2 = new Date($('.dp-co').datepicker("getDate"));
	if ((d1.setDate(d1.getDate() + 30)) <= d2) {
		alert("Η περίοδος αναζήτησης δεν πρέπει να ξεπερνά τις 30 ημέρες!");
		return false;
	} else
		return true;
}

$(function() {
	var dpImgPath = "/images/icons/dp.png";
	$('.dp-ci')
			.datepicker(
					{
						changeMonth : true,
						changeYear : true,
						showButtonPanel : false,
						showOn : "both",
						buttonImage : dpImgPath,
						buttonImageOnly : true,
						beforeShow : setOneYearRange,
						firstDay : 1,
						changeFirstDay : false,
						showAnim : 'blind',
						onSelect : function(theDate) {
							if ($('.dp-co').datepicker("getDate") <= $('.dp-ci')
									.datepicker("getDate")) {
								var plus1 = $.datepicker.parseDate('dd-mm-yy',
										theDate);
								plus1.setDate(plus1.getDate() + 1);
								$('.dp-co').datepicker('setDate', plus1);
							}
						}
					});

	$('.dp-co').datepicker(
			{
				changeMonth : true,
				changeYear : true,
				showButtonPanel : false,
				showOn : "both",
				buttonImage : dpImgPath,
				buttonImageOnly : true,
				beforeShow : setOneYearRange,
				firstDay : 1,
				changeFirstDay : false,
				showAnim : 'blind',
				onSelect : function(theDate) {
					if ($('.dp-co').datepicker("getDate") <= $('.dp-ci')
							.datepicker("getDate")) {
						var minus1 = $.datepicker
								.parseDate('dd-mm-yy', theDate);
						minus1.setDate(minus1.getDate() - 1);
						var today = new Date();
						if (minus1 < today) {
							var tomorrow = new Date();
							tomorrow.setDate(today.getDate() + 1);
							$('.dp-ci').datepicker('setDate', today);
							$('.dp-co').datepicker('setDate', tomorrow);
						} else
							$('.dp-ci').datepicker('setDate', minus1);
					}
				}
			});

	$('.dp-ci-2')
			.datepicker(
					{
						changeMonth : true,
						changeYear : true,
						showButtonPanel : false,
						showOn : "both",
						buttonImage : dpImgPath,
						buttonImageOnly : true,
						beforeShow : setOneYearRange,
						firstDay : 1,
						changeFirstDay : false,
						showAnim : 'blind',
						onSelect : function(theDate) {
							if ($('.dp-co-2').datepicker("getDate") <= $(
									'.dp-ci-2').datepicker("getDate")) {
								var plus1 = $.datepicker.parseDate('dd-mm-yy',
										theDate);
								plus1.setDate(plus1.getDate() + 1);
								$('.dp-co-2').datepicker('setDate', plus1);
							}
						}
					});

	$('.dp-co-2').datepicker(
			{
				changeMonth : true,
				changeYear : true,
				showButtonPanel : false,
				showOn : "both",
				buttonImage : dpImgPath,
				buttonImageOnly : true,
				beforeShow : setOneYearRange,
				firstDay : 1,
				changeFirstDay : false,
				showAnim : 'blind',
				onSelect : function(theDate) {
					if ($('.dp-co-2').datepicker("getDate") <= $('.dp-ci-2')
							.datepicker("getDate")) {
						var minus1 = $.datepicker
								.parseDate('dd-mm-yy', theDate);
						minus1.setDate(minus1.getDate() - 1);
						var today = new Date();
						if (minus1 < today) {
							var tomorrow = new Date();
							tomorrow.setDate(today.getDate() + 1);
							$('.dp-ci-2').datepicker('setDate', today);
							$('.dp-co-2').datepicker('setDate', tomorrow);
						} else
							$('.dp-ci-2').datepicker('setDate', minus1);
					}
				}
			});

	$('.dp-co,.dp-ci,.dp-co-2,dp-ci-2').datepicker($.datepicker.regional['el']);
});

/* Waiting Message */
function wait() {
	//$("body").css("overflow", "hidden");
	$('.background-overlay').css( {
	    'width': $(document).width(),
	    'height': $(document).height()
	}).show();
	$('#wait-msg').css( {
		'position': 'fixed',
        'left': ($(document).width() - $('#wait-msg').width()) / 2,
        'top': ($(window).height() - $('#wait-msg').height()) / 2
	}).show();
}

/* Top menu Initialization */
function initTopMenu() {
	$.each($("#header li"), function() {
		if ($(this).find(".submenu").length > 0) {
			$(this).mouseover(function() {
				$(this).stop().animate( {
					height : '100px'
				}, {
					queue : false,
					duration : 400,
					complete : function() {
					}
				})
			});

			$(this).mouseout(function() {
				$(this).stop().animate( {
					height : '30px'
				}, {
					queue : false,
					duration : 400,
					easing : 'easeOutBounce'
				})
			});
		}
	})
}

function initHelpdesk() {
	/* Helpdesk */
	$("#open-helpdesk").click(function() {
		$("div#helpdesk-content").slideDown("slow");
	});

	$("#close-helpdesk").click(function() {
		$("div#helpdesk-content").slideUp("slow");
	});

	$("#helpdesk-button a").click(function() {
		$("#helpdesk-button a").toggle();
	});
}

$(document).ready(
		function() {
			/* Enhance datepickers */
			$('img.ui-datepicker-trigger').css( {
				'cursor' : 'pointer',
				'vertical-align' : 'middle'
			});
			$('.ui-datepicker-trigger').attr('alt', 'Ημερολόγιο').attr('title',
					'Επιλέξτε ημερομηνία');

			initTopMenu();
			initQTip();
			initHelpdesk();
		})
