/*
SANTIAGO JAVASCRIPT FILE

FUNCTIONS, ETC IN FILE
email_link()	- adds a message inviting comments by email - moved to JQuery
open_window()	- opens a url in a new window - used by Postcard script
JQUERY 			- a JavaScript Framework used by the postcard script
POSTCARD SCRIPT- at end of file (has to be after JQUERY), puts a message on pages
					  inviting users to send postcard. When an image is clicked, opens
					  new window with postcard_process.php and variables for image etc.
*/



// opens a new window, used by postcard script
function open_window(url, winname)
{
	var width = 450;
	var height = 680;
	display_image = window.open(url, winname, "location=0,resizable=yes,scrollbars=yes,width="+width+",height="+height);
}





$(document).ready(function(){

	//GET LOCATION AND FILE NAME
	//var sFilename = new String(document.location);	//file name, converted to string
	var sDocUrl = new String(document.URL);
	var sFilename = "";
	var nLen = "";				//length of string
	var i = "";					//integer counter
	var day = /\d\d/;			//match two digits
	var english = /en/;		//language code used in file names
	var german = /de/;		// "
	var spanish = /es/;
	var french = /fr/;
	var gallego = /ga/;
	var italian = /it/;
	var japanese = /ja/;
	var korean = /ko/;
	var dutch = /nl/;			// "
	var portuguese = /pt/;
	var swedish = /sw/;
	var chinese = /zh/;
	
	//get the file name from URL
	nLen = sDocUrl.length
	for (i = nLen; i > 0; i--) 
	{
		sChar = sDocUrl.substring(i,i+1)
		if (sChar == "/" ) {break}
			//document.write('<hr>' + sDocUrl.substring(i,nLen));
			sFilename = sDocUrl.substring(i,nLen);
	}
	
	//SET MESSAGE VARIABLES
	//default email message (in 3 parts)
	var sEmailMsg1 = "If you have a comment or update which would help other pilgrims on this section of the Camino, please use this ";
	var sEmailMsg2 = "Email link";
	var sEmailMsg3 = " to tell us about it.";
	//default Postcard message (message, plus "New")
	var sNew = "New";
	var sPostcardMsg = "Send a photo to a friend - just click on the photo of your choice";
	
	//alternative language(s)
	if(sFilename.match(day))
	{
		if (sFilename.match(german))
		{
			sEmailMsg1 = " Wenn Sie Anmerkungen oder Aktualisierungen haben, die anderen Pilgern auf diesem Abschnitt des Camino helfen koennten, verwenden Sie bitte diese ";
			sEmailMsg2 = "Email-Adresse";
			sEmailMsg3 = ", um uns dies wissen zu lassen.";
			
			sNew = "Neu";		//re postcard message
			sPostcardMsg = "Schicken Sie ein Foto an einen Freund. Klicken Sie auf das Foto Ihrer Wahl";	
		}
		if (sFilename.match(french))
		{
			sEmailMsg1 = " Si vous avez une observation ou un mis &agrave; jour qui pourrait int&eacute;resser d'autres p&egrave;lerins sur cet &eacute;tape du Chemin, veuillez utiliser ce lien ";
			sEmailMsg2 = "courriel";
			sEmailMsg3 = " pour nous informer.";
			
			sNew = "Nouveau";		//re postcard message
			sPostcardMsg = "Envoyez une photo &agrave; un ami - simplement cliquez sur la photo choisie";
		}
		
		if (sFilename.match(spanish))
		{
			sEmailMsg1 = "";
			sEmailMsg2 = "Si tienes algo comentario o actualización que consideras de utilidad para otros pregrinos en esta sección del Camino, por favor sigue este enlace y cuéntanos acerca de ello";
			sEmailMsg3 = "";
			
			sNew = "Nuevo";		//re postcard message
			sPostcardMsg = "Envía una fotografía a un amigo - sólo haz click en la foto";
		}
		if (sFilename.match(gallego))
		{
			sEmailMsg1 = " ";
			sEmailMsg2 = "Se tes algún comentario que poda axudar a outos Peregrinos non dudes encomentarnoslo";
			sEmailMsg3 = " ";
			
			sNew = "Novo";		//re postcard message
			sPostcardMsg = "Envia unha foto a un amigo, so tes que Click na fotos que mais che guste.";	
		}
		if (sFilename.match(italian))
		{
			sEmailMsg1 = "Se hai commenti o aggiornamenti che potrebbero essere d'aiuto ad altri pellegrini lungo questo tratto del camino, per favore  segnalaceli a questa ";
			sEmailMsg2 = " link Email";
			sEmailMsg3 = " ";
			
			sNew = "Novita";		//re postcard message
			sPostcardMsg = "Invia una foto ad un amico - basta cliccare sulla foto che preferisci";	
		}
		if (sFilename.match(dutch))
		{
			sNew = "Nieuw";		//re postcard message
			sPostcardMsg = "Stuur een foto naar een vriend(in) - je hoeft alleen maar op de goede foto te klikken";
		}
		
		//DISPLAY EMAIL LINK
		if ( sFilename.match(english) || sFilename.match(german) || sFilename.match(french) || sFilename.match(gallego) || sFilename.match(italian))
		{
			
			//display email link
			$("#photos").before("<div id='email_link'><p>"+sEmailMsg1 + "<a href='mailto:updates&#64;santiago-compostela.net?subject=Comment_on_"+sDocUrl+"'>"+sEmailMsg2+"</a>"+sEmailMsg3+"</p></div>");
		}
		
		//ELECTRONIC POSTCARD
		if ( sFilename.match(english) || 
			 sFilename.match(spanish) ||
			 sFilename.match(german)  ||
			 sFilename.match(french)  ||
			 sFilename.match(gallego) ||
			 sFilename.match(italian) ||
			 sFilename.match(japanese) ||
			 sFilename.match(korean)  ||
			 sFilename.match(dutch)   ||
			 sFilename.match(portuguese) ||
			 sFilename.match(swedish) ||
			 sFilename.match(chinese))
		{
			//display postcard message
			$("#photos").before("<p id='postcard_message' ><span>"+sNew+"</span> "+sPostcardMsg+" <span>"+sNew+"</span></p>");

			//make images with "pix" in path clickable
			$("img[@src*=pix]").click(function() {
			var image_path = $(this).src();
			var hd1 = $("h1:first").text();
			var hd2 = $("h2:first").text();
			var doc_path = document.URL;
			var doc_title = document.title;
			
			//http://www.santiago-compostela.net
			//alert("sFilename: "+sFilename);	
								open_window('../postcard_process.php?image_path='+image_path+'&doc_path='+doc_path+'&doc_fn='+sFilename+'&doc_title='+doc_title+'&hd1='+hd1+'&hd2='+hd2, 'postcard');
			});
		}//end if sFilename matches language
		
		
		
	}//end sFilename.match(day)
	
	/*//CHANGE FLAG BAR TO HIGHLIGHT CURRENT LANGUAGE
	//test for third char = '_' (a 2-letter prefix indicates a language page)
	//beginning of string, exactly 2 alpha chars followed by an underscore
	var lang_prefix = /^[a-z]{2}_/;		// "
	var lang_suffix = /_[a-z]{2}\.html$/
	//document.write('<hr>' + sDocUrl.substring(i,nLen));
	if (sFilename.match(lang_prefix))
	{
		//document.write('<hr>' + sFilename);
		$("#flag_bar").find("img").each(function(i) {
		    // $(this).append( " BAM! " + i );
			//$(this).attr("src","../uk3_dim.gif");
				var filename = $(this).attr("src");
				//String.replace(pattern,string)
				var newfilename = filename.replace(/\.gif/, "_dim.gif");
				//document.write('<hr>newfilename: ' + newfilename);
				$(this).attr("src", newfilename);
		   });
		
			}*/
});




