/* Prints an article */
function PrintIt()
{
	window.print();
}

function PrintArticle(code, 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-585)  / 2; // left position of popup
	top 	= (win_height-600) / 2; // top  position of popup

	pwindow = window.open ("/print.php?id="+code+"&cat="+cat ,"print","scrollbars=yes,width=585,height=600,top=" + top + ",left=" + left + "toolbar=no,menubar=no,location=no");
}

function PhotosPage(cat, id)
{
	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-585)  / 2; // left position of popup
	top 	= (win_height-600) / 2; // top  position of popup

	lala = window.open("/photos.php?id="+id+"&cat="+cat, "gallery", "width=550,height=480,top=" + top + ",left=" + left + "toolbar=no,menubar=no,location=no");
	if (lala) 
		lala.focus();

	return false;
}

function VFeedbackForm(form) {
        var re =  new RegExp("^[a-z._0-9-]+\@[a-z._0-9-]+$", "i");
        if ( form.email.value && !re.test(form.email.value))
        {
                alert("Παρακαλούμε δώστε ένα έγκυρο email");
                form.email.focus();
                return false;
        }

        if (!form.feedbackLikes.value && !form.feedbackDisLikes.value && !form.feedbackWish.value)
        {
                alert("Παρακαλούμε δώστε τι σας αρέσει, τι δε σας αρέσει ή τι θα θέλατε να δείτε στο Pathfinder Sports");
                return false;
        }

	alert("Σας ευχαριστούμε για τα σχόλιά σας. Πατήστε [ΟΚ] προκειμένου να μεταφερθείτε στην κεντρική σελίδα.");
        form.submit();
	return true;
}

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();
	return true;
}



function videopopup(comp, filename) 
{
	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-585)  / 2; // left position of popup
	top 	= (win_height-600) / 2; // top  position of popup

	pwindow = window.open ("/video_player.php?comp="+comp+"&filename="+filename ,"video_player","scrollbars=no,width=650,height=450,top=" + top + ",left=" + left + "toolbar=no,menubar=no,location=no");
}

function SetTeams(countryId, el)
{
	if (allTeams[countryId])
		FillList(allTeams[countryId], el, countryId);
	else
	{
		var method = "sports.GetTeams";

		countryId = Number(countryId);
		new Ajax.RPC ({
			onSuccess: function(req) {
				res = req.responseJSON;
				if( res && res.faultCode ) {
					alert( res.faultString );
				}
				else
				{
					/* add'em for caching */
					allTeams[countryId] = res;
					FillList(res, el, countryId);
				}
			}
		}).send(method, countryId, true) ;
	}
}

/* fill selection list */
function FillList(ar, el, countryId)
{
	el.options[0] = null;
	el.options.length = 0;
	opts = $T("optgroup", el);
	for (i=0; i < opts.length; i++)
		el.removeChild(opts[i]);

	if (countryId == '11' && System.browser.moz)
	{
		if (anational.length)
		{
			var og = $C("optgroup");
			og.label = "- Σούπερ Λίγκα -";
			el.appendChild(og);
			for (var i = 0; i < ar.length; i++)
			{
				if (anational.inArray(mapping[ar[i]['id']]))
				{
					k = el.options.length++;
					el.options[k] = new Option(ar[i]['title'], ar[i]['id']);
					if (selTeam && selTeam == ar[i]['id'] || ( el.name == "teamId2" && typeof selTeam2 != "undefined" && selTeam2 == ar[i]['id']))
						el.options[k].selected = true;
				}
			}
		}
		if (bnational.length)
		{
			var og = $C("optgroup");
			og.label = "- Β Εθνική -";
			el.appendChild(og);
			for (var i = 0; i < ar.length; i++)
			{
				if (bnational.inArray(mapping[ar[i]['id']]))
				{
					k = el.options.length++;
					el.options[k] = new Option(ar[i]['title'], ar[i]['id']);
					if (selTeam && selTeam == ar[i]['id'] || ( el.name == "teamId2" && typeof selTeam2 != "undefined" && selTeam2 == ar[i]['id']))
						el.options[k].selected = true;
				}
			}
		}
		if (cnational.length)
		{
			var og = $C("optgroup");
			og.label = "- Γ Εθνική -";
			el.appendChild(og);
			for (var i = 0; i < ar.length; i++)
			{
				if (cnational.inArray(mapping[ar[i]['id']]) || (!anational.inArray(mapping[ar[i]['id']]) && !bnational.inArray(mapping[ar[i]['id']]) && !cnational.inArray(mapping[ar[i]['id']])))
				{
					k = el.options.length++;
					el.options[k] = new Option(ar[i]['title'], ar[i]['id']);
					if (selTeam && selTeam == ar[i]['id'] || ( el.name == "teamId2" && typeof selTeam2 != "undefined" && selTeam2 == ar[i]['id']))
						el.options[k].selected = true;
				}
			}
		}
		/*
		var og = $C("optgroup");
		og.label = "- Υπόλοιπες ομάδες -";
		el.appendChild(og);

		/* rest teams */
		/* moved up
		for (var i = 0; i < ar.length; i++)
		{
			if (!anational.inArray(mapping[ar[i]['id']]) && !bnational.inArray(mapping[ar[i]['id']]) && !cnational.inArray(mapping[ar[i]['id']]))
			{
				k = el.options.length++;
				el.options[k] = new Option(ar[i]['title'], ar[i]['id']);
				if (selTeam && selTeam == ar[i]['id'] || ( el.name == "teamId2" && typeof selTeam2 != "undefined" && selTeam2 == ar[i]['id']))
					el.options[k].selected = true;
			}
		}
		*/
	}
	else if (countryId == '11' && !System.browser.moz)
	{
		if (anational.length)
		{
			for (var i = 0; i < ar.length; i++)
			{
				if (anational.inArray(mapping[ar[i]['id']]))
				{
					k = el.options.length++;
					el.options[k] = new Option(ar[i]['title'], ar[i]['id']);
					if (selTeam && selTeam == ar[i]['id'] || ( el.name == "teamId2" && typeof selTeam2 != "undefined" && selTeam2 == ar[i]['id']))
						el.options[k].selected = true;
				}
			}
		}
		if (bnational.length)
		{
			for (var i = 0; i < ar.length; i++)
			{
				if (bnational.inArray(mapping[ar[i]['id']]))
				{
					k = el.options.length++;
					el.options[k] = new Option(ar[i]['title'], ar[i]['id']);
					if (selTeam && selTeam == ar[i]['id'] || ( el.name == "teamId2" && typeof selTeam2 != "undefined" && selTeam2 == ar[i]['id']))
						el.options[k].selected = true;
				}
			}
		}
		if (cnational.length)
		{
			el.appendChild(og);
			for (var i = 0; i < ar.length; i++)
			{
				if (cnational.inArray(mapping[ar[i]['id']]) || (!anational.inArray(mapping[ar[i]['id']]) && !bnational.inArray(mapping[ar[i]['id']]) && !cnational.inArray(mapping[ar[i]['id']])))
				{
					k = el.options.length++;
					el.options[k] = new Option(ar[i]['title'], ar[i]['id']);
					if (selTeam && selTeam == ar[i]['id'] || ( el.name == "teamId2" && typeof selTeam2 != "undefined" && selTeam2 == ar[i]['id']))
						el.options[k].selected = true;
				}
			}
		}
		/* rest teams */
		/* moved up
		for (var i = 0; i < ar.length; i++)
		{
			if (!anational.inArray(mapping[ar[i]['id']]) && !bnational.inArray(mapping[ar[i]['id']]) && !cnational.inArray(mapping[ar[i]['id']]))
			{
				k = el.options.length++;
				el.options[k] = new Option(ar[i]['title'], ar[i]['id']);
				if (selTeam && selTeam == ar[i]['id'] || ( el.name == "teamId2" && typeof selTeam2 != "undefined" && selTeam2 == ar[i]['id']))
					el.options[k].selected = true;
			}
		}
		*/
	}
	else
		for (var i = 0; i < ar.length; i++)
		{
			k = el.options.length++;
			el.options[k] = new Option(ar[i]['title'], ar[i]['id']);
			if (selTeam && selTeam == ar[i]['id'] || ( el.name == "teamId2" && typeof selTeam2 != "undefined" && selTeam2 == ar[i]['id']))
				el.options[k].selected = true;
		}
}

var Sports = {
	/**
	 * Toggle a tab
	 *
	 * @param tab A tab to switch to
	 */
	toggleTab :function(tab, e ) {
		// Already here?
		if( tab.parentNode.className == "active") {
			tab.blur();
			return; 
		}
			
		tab = $(tab); // The tab
			
		// We want your parentNode honey, not u
		tab = tab.parentNode;
		if (typeof e == "undefined")
			e = "tabs";
	
		// Gather all tabs
		// And then hide em all but the active one
		var allTabs = $T( "li", $(e));	
		
		
		$A(allTabs).forEachBind( function() { 
			
			var thisId = this.id.toString().replace( /tab-/, "" );
					
			Node.removeClass( this, "active")
			Node.hide( "box-" + thisId );
			
			
			
			// The active one
			if( this == tab ) {
				Node.addClass( this, "active" );
				
				var box = $("box-" + thisId );
				Node.show( box);
			}
			
		});
	}
}

function toggleComments ( toggler )
{
	thisForm= $("comments").getElementsByTagName("form")[0];
	Node.toggle(thisForm);
	display = Node.getStyle(thisForm, "display");
	
	if( toggler)
		thisForm.toggler = toggler;
	
	if( !toggler )
		thisForm = mailContainer.toggler;
	
	
	
	Node.disableSelect( toggler ); // Disable the selection
	
	switch( display ) {
		case "none":
			if( toggler.hasClass("opened") )
				toggler.className = "closed";			
			else
				toggler.innerHTML = toggler.title =  "σχόλια";
		break;
		
		default:
			thisForm.elements[0].focus();
			if( toggler.hasClass("closed") )
				toggler.className = "opened";
			else
				toggler.innerHTML = toggler.title = "Απόκρυψη φόρμας";
		break;
	}

	return false;			
}

function postComment (forma) {
	// Check body
	thisBody = forma.body;
	if(thisBody.value.length < 1 )
	{
		alert( "Παρακαλώ δώστε το σχόλιο σας για να συνεχίσετε");
		thisBody.focus();
		return false;
	}			

	greekWords = thisBody.value.match(/[αάΑ?βΒγΓδΔεέΕΈζΖηήΗΉθΘιίϊΙΊκΚλΛμΜνΝξΞοόΟΌπΠρΡσςΣτΤυύΎΥψφΨχΧωώΩΏ]+/g);
	if( 1 ) {
		
		if (greekWords == null || greekWords.length < 5 )
		{		
			alert ("Για να προχωρήσετε πρέπει το μήνυμά σας να είναι γραμμένο με Ελληνικούς χαρακτήρες και με περισσότερες από 5 ορθά γραμμένες λέξεις.");
			thisBody.focus();
			return false;
		}	
	}
		
	if(thisBody.value.length > 2200 )
	{
		alert( "Το σχόλιο σας δεν μπορεί να υπερβαίνει τους 2.200 χαρακτήρες");
		thisBody.focus();
		return false;
	}			
	
	if(!forma.terms.checked)	{
		alert( "Πρέπει να διαβάσετε και να αποδεχτείτε τους Όρους Χρήσης του Pathfinder για να συνεχίσετε");
		return false;
	}

	var aj = new Ajax('post', {
		url 		: forma.action,
		parameters 	: {
			action 	: "comments",
			body 	: forma.body.value,
			url 	: "/post",
			host  	: "comments.pathfinder.gr",
			articleId : forma.articleId.value,
			login 	: forma.login.value,
			section : forma.section.value,
			forward : self.location
		},
		onSuccess 	: function(req) {
			if( req.responseText )
				alert( req.responseText );

			par = document.createElement("p");
			par.innerHTML = "Το σχολίο σας θα εμφανιστεί στις σελίδες του Pathfinder αμέσως μόλις ελεγχθεί ότι είναι σύμφωνο προς τους Όρους Χρήσης από τη Συντακτική Ομάδα.<br />Ευχαριστούμε για τη συμμετοχή σας!";
			par.className = "success";
			
			$("comments").insertBefore(     par, $("comments").getElementsByTagName("form")[0] ); 
			
			$('mail').toggler.remove();
			
			$("comments").getElementsByTagName("form")[0].remove();

		}	
	});
	aj.send();
	return false;	
}



/* ================= Mailer ================ */
function toggleMail( toggler ) {
	// Define properties
	mailContainer 		= $("mail");

	if( toggler)
		mailContainer.toggler = toggler;
	
	if( !toggler )
		toggler = mailContainer.toggler;
		
	Node.disableSelect( toggler );	
	
	mailContainer.form 	= mailContainer.getElementsByTagName("form")[0];
	Node.toggle(mailContainer)


	switch( Node.getStyle( mailContainer, "display" ) ) {
		case "none":
			if( toggler.hasClass("opened") )
				toggler.className = "closed";
		break;
		
		default:
			mailContainer.form.elements[0].focus();
			if( toggler.hasClass("closed") )
				toggler.className = "opened";
		break;
	}

	return false;
}

function sendMail( forma ) {
	// Check title
	if( forma.to.value.length < 1 || !forma.to.value.isValidEmail() ) {
		alert( "Παρακαλώ δώστε σωστά το e-mail του παραλήπτη για να συνεχίσετε");
		forma.to.focus();
		return false;
	}

	if( forma.fromEmail.value.length < 1 || !forma.fromEmail.value.isValidEmail() ) {
		alert( "Παρακαλώ δώστε σωστά το e-mail σας για να συνεχίσετε");
		forma.fromEmail.focus();
		return false;
	}

	Node.toggle(forma);
	$('mail').addClass ( "loading"); // :

	var ajax = new Ajax('post', {
		parameters : {
			action 	: 'mail',
			articleTitle: forma.articleTitle.value,
			service : forma.service.value,
			articleUrl : forma.articleUrl.value,
			to 	: forma.to.value,
			fromEmail : forma.fromEmail.value,
			fromName  : forma.fromName.value,
			msg 	: forma.msg.value
		},
		onSuccess 	: function( req ) {
			mailContainerForm = $("mail").getElementsByTagName("form")[0];
			
			alertMsg = "Το άρθρο στάλθηκε επιτυχώς στον/στους:\n" + mailContainerForm.to.value;
			if( mailContainerForm.fromCopy.checked )
				alertMsg += "\n\nΑντίγραφο του μηνύματος στάλθηκε και στο e-mail σας";
			alert( alertMsg );      


			$("mail").removeClass ("loading");
			
			
			toggleMail()
			Node.toggle( mailContainerForm);

			mailContainerForm.to.value = "";
		}
	});
	ajax.send();

	return false;
			
}


function popImg( img, width, height ) {
	if ((!window.popup)  || (window.popup && window.popup.closed) )
		window.popup = window.open( "", "popup", "width=" + width  + ", height="+(height + 30));
	else {
		window.popup.resizeTo( width  , (height + 80) );
		window.popup.focus();
	}

		
	doc = window.popup.document;
	var code = "<html>";
	code += "<head><title>" + img.title + "</title>";
	code += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-7\" />\n";
	code += "<style type=\"text/css\">";
	code += "body {margin:0; font: normal 13px arial, serif; text-align: center; background: #eee;}\nimg { margin-bottom: 5px;}\n";
	code += "</style>\n";
	code += "</head><body>\n";
	code += "<img height=\""+height+"\" width=\""+width+"\" border=\"0\" alt=\""+img.title+"\" title=\""+img.title+"\" src=\""+img.href+"\" /><br /><a href=\"#\" onclick=\"self.close();\">[x] κλείσιμο παραθύρου</a>\n";
	code += "</body></html>";
	
	doc.write( code);
	doc.close();
}

Event.add( window, "DOMContentLoaded", function() {
	if (typeof page != "undefined") {
		if (page == "info")
			SetTeams(countryId, $('teamId'));
		else if (page == "compare")
		{
			SetTeams(countryId1, $('teamId1'));
			SetTeams(countryId2, $('teamId2'));
		}
	}
	if ($('rater-my'))
	{
		var myRater = new Rater( $("rater-my"), {
			loginUrl	: loginUrl,
			section		: "sports",
			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);
	}
});

