/* Prints an article */
function PrintIt()
{
	window.print();
}

function PrintArticle(id, cat)
{
	var left, top, n4, n6, ie;
	left = 0;
	top  = 0;
	n4 = false;
	ie = false;
	n6 = false;

	if (document.layers) 
		n4 = true;
	else if (document.all)
		ie = true;
	else if (document.getElementById)
		n6 = true;

	//Get dimensions of the window
	if(n4 || n6)
	{
		win_width = window.screen.width;
		win_height = window.screen.height;
	} 
	else if(ie) 
	{
		win_width = screen.width;
		win_height= screen.height;
	}

	left 	= (win_width-640)  / 2; // left position of popup
	top 	= (win_height-480) / 2; // top  position of popup


	printWindow = window.open ("/print.php?id="+id+"&cat="+cat ,"preview","scrollbars=yes,width=640,height=480,top="+top+", left="+left+",toolbar=no,menubar=no,location=no");
	return false;
}

/*
 * toggleEmailer()
 *
 * @ node 		: The link node acting as a referejnce
*/
function toggleEmailer( node ) {
	if( !node )
		node = $("emailer-handler");

	var block 		= $("emailer-container");
	
	// Sanity check
	if( !block )
		return;
	

	// Expanding
	if( !block.fx ) {
		//alert( 0);
		//block.style.height = "0";
		//alert( block.scrollHeight);
		//alert( 0);
		Node.show(block);

		onComplete = function() { $("to").focus(); }
		block.fx	= new Fx.size( block, {
				duration: 200,
				fromH 		: 0,
				toH 		: "end",
				"onComplete"	: onComplete
			}
		 );
		node.innerHTML 		= "απόκρυψη φόρμας";
	} else { 
		// Collapsing
		block.fx.toggle(); // Lovely, lovely method
		block.fx= null;
		node.innerHTML 		= "αποστολή";
	}
	
	
}

function getMoonPhase()
{
	var black = "http://javascript.internet.com/img/moon-phases/black.gif";
	var white = "http://javascript.internet.com/img/moon-phases/white.gif";

	var height=1;
	var size = 15;
	var i;
	var currentDate  = new Date();
	var x = currentDate;
	currentDate.setTime(currentDate.getTime() + (currentDate.getTimezoneOffset()*60000));
	var blueMoonDate = new Date(96, 1, 3, 16, 15, 0);
	var lunarPeriod  = 29*(24*3600*1000) + 12*(3600*1000) + 44.05*(60*1000);
	var moonPhaseTime = (currentDate.getTime() - blueMoonDate.getTime()) % lunarPeriod;
	var percentRaw = (moonPhaseTime / lunarPeriod);
	var percent    = Math.round(100*percentRaw) / 100;
	var percentBy2 = Math.round(200*percentRaw);
	var left  = (percentRaw >= 0.5) ? black : white;
	var right = (percentRaw >= 0.5) ? white : black;
	var daysleft = Math.round((lunarPeriod - moonPhaseTime)/(24*3600*1000));
	if (percentBy2 > 100)
		percentBy2 = percentBy2 - 100;

	return daysleft+1;
}

function showTooltip(ID) {
	if(document.getElementById) {
		document.getElementById("help_"+ID).className = "tooltip_on";
		document.getElementById("help_"+ID).style.Top =  document.getElementById("link_"+ID).offsetTop;
	}
}
function hideTooltip(ID) {
	if(document.getElementById) {
		document.getElementById("help_"+ID).className = "tooltip";
	}
}

function popup(place, type, id, cat)
{
	var left, top, n4, n6, ie;
	left = 0;
	top  = 0;
	n4 = false;
	ie = false;
	n6 = false;

	if (document.layers) 
		n4 = true;
	else if (document.all)
		ie = true;
	else if (document.getElementById)
		n6 = true;

	//Get dimensions of the window
	if(n4 || n6)
	{
		win_width = window.screen.width;
		win_height = window.screen.height;
	} 
	else if(ie) 
	{
		win_width = screen.width;
		win_height= screen.height;
	}

	left 	= (win_width-460)  / 2; // left position of popup
	top 	= (win_height-400) / 2; // top  position of popup


	if (place)
		pwindow = window.open ("/place.php?id="+id ,"preview","scrollbars=no,width=460,height=400,top="+top+", left="+left+",toolbar=no,menubar=no,location=no");
	else
		pwindow = window.open ("/organiser.php?cat="+cat+"&type="+type+"&id="+id ,"preview","scrollbars=yes,width=500,height=500,top="+top+", left="+left+",toolbar=no,menubar=no,location=no");
	return false;
}

function VForm(form)
{
        var re =  new RegExp("^[a-z._0-9-]+\@[a-z._0-9-]+$", "i");
        if (!re.test(form.recipientEmail.value))
        {
                alert("Παρακαλούμε δώστε ένα έγκυρο email");
                form.recipientEmail.focus();
                return false;
        }

        if (!form.senderName.value)
        {
                alert("Παρακαλούμε δώστε το όνομά σας");
                form.senderName.focus();
                return false;
        }

        if (!form.senderEmail.value)
        {
                alert("Παρακαλούμε δώστε το email σας");
                form.senderEmail.focus();
                return false;
        }

        form.submit();
}

function EmailPage(cat, id, type)
{
	var left, top, n4, n6, ie;
	left = 0;
	top  = 0;
	n4 = false;
	ie = false;
	n6 = false;

	if (document.layers) 
		n4 = true;
	else if (document.all)
		ie = true;
	else if (document.getElementById)
		n6 = true;

	//Get dimensions of the window
	if(n4 || n6)
	{
		win_width = window.screen.width;
		win_height = window.screen.height;
	} 
	else if(ie) 
	{
		win_width = screen.width;
		win_height= screen.height;
	}

	left 	= (win_width-550)  / 2; // left position of popup
	top 	= (win_height-440) / 2; // top  position of popup



	lala = window.open("/mail.php?cat="+cat+"&page="+type+"&id="+id,"agenda","width=550,height=440,top="+top+", left="+left);
	if (lala) 
		lala.focus();
	return false;
}

function LocationPage(id)
{
	lala = window.open("/location.php?id="+id,"agenda","width=550,height=440");
	if (lala) 
		lala.focus();
	return false;
}

function checkSearchData(obj) {
	if (document.getElementById(obj).value) 
		return true;

	alert('Παρακαλώ, συμπληρώστε το κριτήριο αναζήτησης.');
	document.getElementById(obj).focus();
	return false;
}

Event.add( window, "DOMContentLoaded", function() {
	Menu.initialize();
	if ($('rater-my'))
	{
		var myRater = new Rater( $("rater-my"), {
			loginUrl	: loginUrl,
			section		: "agenda",
			username	: login,
			'ip' 		: ip,
			id		: id,
			'save' 	: function () {
				this.loader = $C('img').
				setProperty('src', 'http://c.pathfinder.gr/img/icons/loading-circle.gif').
				setStyles({'position': 'absolute', 'text-align' : 'center', 'top' : 0, 'left' : 0}).
				injectIn(this.container);
				this.loader.show();
			}
		}).build(rate);
	}

	if ($('theatre')) {
		$('theatre').addEvent('change', function() {
			location = '/theatres/'+ this.value +'.html';
		});
	}

});

var Menu = {
	initialize : function()	{
		/* toggler for ul more */
		this.container = document.getElements('.nav')[0];
		this.container.getElements('a.icon-more').each(function()	{
			var w = this.id.replace(/-toggle/, '');
			var ul = $(w+'-more');
				console.log(ul);
			this.fx = new Fx.Slide( ul, { duration: 200 });
			this.addEvent('click', Menu.ulToggle.bind(this));	
		});
	},
	ulToggle : function(e)	{
		console.log(e);
		if (!e.target.fx.open) {
			e.target.setClass('icon icon-less');
			e.target.setHTML('Λιγότερα...');
		} else {
			e.target.setClass('icon icon-more');
			e.target.setHTML('Περισσότερα...');
		}
		
		var containerDiv = e.target.fx.wrapper.getParent(function(element) { return element.nodeName === 'DIV'; });
		if(containerDiv)
			containerDiv.setStyle('height', 'auto');
		
		e.target.fx.toggle();
		
	}
}
