$(document).ready(function(){	
    
    var date = function() {
        var d_names = new Array(
            "Κυριακή",
            "Δευτέρα",
            "Τρίτη", 
            "Τετάρτη",
            "Πέμπτη",
            "Παρασκευή",
            "Σάββατο"
        );        
        
        var m_names = new Array(
            "Ιανουαρίου",
            "Φεβρουαρίου",
            "Μαρτίου",
            "Απριλίου",
            "Μαΐου",
            "Ιουνίου",
            "Ιουλίου",
            "Αυγούστου",
            "Σεπτεμβρίου", 
            "Οκτωβρίου",
            "Νοεμβρίου",
            "Δεκεμβρίου"
        );

        var d = new Date();
        var curr_day = d.getDay();
        var curr_date = d.getDate();
        var curr_month = d.getMonth();
        var curr_year = d.getFullYear();
        var curr_hour = d.getHours();
        var curr_minutes = d.getMinutes();
        
        if (curr_minutes < 10) {
            curr_minutes = "0" + curr_minutes;
        }
        
        if (curr_hour < 10) {
            curr_hour = "0" + curr_hour;
        }
        
        return d_names[curr_day] + ", " + curr_date + " " + m_names[curr_month] + " " + curr_year + " | " + curr_hour + ":" + curr_minutes;

    }
    
    
    $("div#date-frame").html(date());
    
	$(function() {
		$("a.tooltip").tooltip({ 
			bodyHandler: function() { 
			return $(this).attr("value"); 
		}, 
		showURL: false 
		});
	});
	$("a.printlink").click(function() {
		//$('#content-frame').jqprint();
		//if (this.rel){
		//	window.open(this.rel,'printwindow','width=535,height=600,scrollbars=yes,resizable=yes');
		//}
	});	
	
	$(".jqmPrint").click(function() {
		$('#content-frame').jqprint();
		//frames["jqmContent"].focus();
		//frames["jqmContent"].print();
	});
	$("#backlink").click(function() {
		history.back(-1);
	});
	$("#closelink").click(function() {
		window.close();
	});
	$("#finalprintlink").click(function() {
		window.print();
	});
	$('#tab2').hide();
	$('#tab3').hide();
	$('#tab4').hide();
	$("a[@rel*=tab]").mouseover(function(){
		$('#tab1').hide();
		$('#tab2').hide();
		$('#tab3').hide();
		$('#tab4').hide();
		$($(this).attr('rel')).show();
		$('#tab-labels a').each(function() {
			$(this).removeClass('on').addClass('off');
		});
		$(this).removeClass('off').addClass('on');
	});

	
//********** News Scroller on TOP ************
	$('#newsscroller').cycle({ 
	    fx: 'scrollUp', 
		pause: 1,
		random: 1,
	    speed: 300,
	    timeout: 7000,
		cleartype:1,
		nowrap:1
	 });
	 $('#newsscroller').show();
//********** Popup Window ************
	var closeModal = function(hash) {
		var $modalWindow = $(hash.w);
		$modalWindow.fadeOut('2000', function() {
			hash.o.remove();
			//refresh parent
			if (hash.refreshAfterClose === 'true') {
				window.location.href = document.location.href;
			}
		});
	};
	var openInFrame = function(hash) {
		var $trigger = $(hash.t);
		var $modalWindow = $(hash.w);
		var $modalContainer = $('iframe', $modalWindow);
		var myUrl = $trigger.attr('rel');
		var myTitle = $trigger.attr('title');
		var newWidth = 0, newHeight = 0, newLeft = 0, newTop = 0;
		myUrl = (myUrl.lastIndexOf("#") > -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;
		var queryString = (myUrl.indexOf("?") > -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;
		myUrl = (myUrl.indexOf("?") > -1) ? myUrl.substr(0, myUrl.indexOf("?")) : null;
		$modalContainer.html('').attr('src', myUrl);
		$('#jqmTitleText').text(myTitle);
		if (queryString != null && typeof queryString != 'undefined') {
			var queryVarsArray = queryString.split("&");
			for (var i = 0; i < queryVarsArray.length; i++) {
				if (unescape(queryVarsArray[i].split("=")[0]) == 'width') {
					var newWidth = queryVarsArray[i].split("=")[1];
				}
				if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'height') {
					var newHeight = queryVarsArray[i].split("=")[1];
				}
				if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'refresh') {
					// if true, launches a "refresh parent window" order after the modal is closed.
					hash.refreshAfterClose = queryVarsArray[i].split("=")[1]
				} else {
					hash.refreshAfterClose = false;
				}
			}
			// let's run through all possible values: 90%, nothing or a value in pixel
			if (newHeight != 0) {
				if (newHeight.indexOf('%') > -1) {
					newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));
				}
				var newTop = Math.floor(parseInt($(window).height() - newHeight) / 2) - 50;
				if (newTop < 10) {
					nweTop = 10;
				}
			} else {
				newHeight = $modalWindow.height();
			}
			if (newWidth != 0) {
				if (newWidth.indexOf('%') > -1) {
					newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));
				}
				var newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);
			} else {
				newWidth = $modalWindow.width();
			}
			// do the animation so that the windows stays on center of screen despite resizing
			$modalWindow.css({
				top: newTop+'px',
				left: newLeft+'px',
				width: newWidth+'px',
				height: newHeight+'px'				
			});
			newWidth = newWidth - 20;
			newHeight = newHeight - 40;
			$modalContainer.css({
				width: newWidth+'px',
				height: newHeight+'px'
			});
			$modalWindow.show();
		} else {
			// don't do animations
			$modalWindow.jqmShow();
		}
	}	
	$('#modalWindow').jqm({
		overlay: 70,
		modal: true,
		trigger: 'a.thickbox',
		target: '#jqmContent',
		onHide: closeModal,
		onShow: openInFrame
	});
	$('#printWindow').jqm({
		overlay: 70,
		modal: true,
		trigger: 'a.printbox',
		target: '#jqmContent',
		onHide: closeModal,
		onShow: openInFrame
	});
});
