/* Copyright 2003 - Pierre Saslawsky - pierre at photobiker.com */


function postCount(ignored) {
	document.write('Comments');
}


/*==============================================*/
/*  utilities                                   */
/*==============================================*/

function strParamVal(str, paramName) {
	var paramList = str.substr(0,str.length);
	var aParam = paramList.split("&")
	for (i=0; i<aParam.length; i++) {
		if (paramName == aParam[i].split("=")[0]) {

			return aParam[i].split("=")[1]
		}
	}
	return "";
}	



function paramVal(paramName) {
	return strParamVal(location.search.substring(1), paramName);
}	



var siteBaseUrl = "";
function getSiteBaseUrl() {
	if (siteBaseUrl.length > 0)
		return siteBaseUrl;
	siteBaseUrl = location.protocol + '//' + location.host;
	if (location.protocol == "file:") {
		/* generic debug code */
		var folderStr = 'photobiker';
		var folderLoc = location.href.indexOf(folderStr);
		if (folderLoc != -1) {
			siteBaseUrl = location.href.substr(0, folderLoc + folderStr.length);
			siteBaseUrl += '/';
			return siteBaseUrl;
		}
		/* default hard-coded debug code */
		if (navigator.appVersion.toLowerCase().indexOf('macintosh') != -1) {
			siteBaseUrl = siteBaseUrl + '/Users/pierre/Sites/photobiker/';
		}
		else {
			siteBaseUrl = 'Y:\\photobiker/';
		}
	}
	else {
		if (location.host == "127.0.0.1")
			siteBaseUrl += '/~pierre/photobiker/';	// local host
		else
			siteBaseUrl += '/';		// live code
	}
	return siteBaseUrl;
}


function getDocUrl() {
	var docUrl = location.protocol + '//' + location.host + location.pathname;
	return docUrl;
}


function addStyleSheet(styleSheet) {
	if (styleSheet.length != 0) {
		var styleStr = '<'
			 + 'link rel="stylesheet" '
			 + 'href="' + styleSheet + '" '
			 + 'type="text/css"'
			 + '/>';
		document.write(styleStr);
	}
}


/*==============================================*/
/*  cookies                                     */
/*==============================================*/

function getCookie(Name) {
   var search = Name + "=";
   if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search);
      if (offset != -1) { // if cookie exists 
         offset += search.length;
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset);
         // set index of end of cookie value
         if (end == -1) 
            end = document.cookie.length;

         return unescape(document.cookie.substring(offset, end));
      } 
   }
   return "";
}


function getCookieParam(paramName) {
	var cookie = getCookie("Photobiker");
	if (cookie.length == 2 && paramName == 'lang')
		return cookie;

	var cookieTemp = getCookie("PhotobikerTemp");
	if (cookieTemp.length > 0)
		cookie += '&' + cookieTemp;
	return strParamVal(unescape(cookie), paramName);
}


function writeCookieParam(paramName, value, isPersistent) {
	var cookieName = (isPersistent ? "Photobiker" : "PhotobikerTemp");
	var cookie = getCookie(cookieName);
	if (cookie.length == 2)
		cookie = "lang=" + cookie; // reformat old-style cookie

	cookie = unescape(cookie);
	var offset = cookie.indexOf(paramName);
	if (offset != -1) {
		// remove the old param/value pair
		var end = cookie.indexOf('&', offset);
		if (end == -1) {
			// the pair was last in the cookie
			cookie = cookie.substring(0, offset);
		}
		else {
			// the pair was inside the cookie
			cookie = cookie.substring(0, offset-1) + cookie.substring(end+1);
		}
	}

	value = String(value);
	if (value.length > 0) {
		if (cookie.length > 0 && cookie.charAt(cookie.length-1) != '&')
			cookie += '&';
		cookie += paramName + '=' + value;
	}
	
	var cookieString = cookieName + "=" + escape(cookie) + ";path=/";
	if (isPersistent) {
		var today = new Date();
		var expires = new Date();
		expires.setTime(today.getTime() + 1000*60*60*24*365);
		cookieString += ";expires=" + expires.toGMTString();

	}
	document.cookie = cookieString;

}


function writeLanguageCookie(language) {
	writeCookieParam('lang', language, true);
}



function readLanguageCookie() {
	return getCookieParam('lang');
}



function getLangBaseUrl() {
	return getSiteBaseUrl() + 'lang/';
}


function addLangStyleSheet(styleSheet) {
	if (styleSheet.length != 0) {
		addStyleSheet(getLangBaseUrl() + styleSheet);
	}
}


/*==============================================*/
/*  language buttons                            */
/*==============================================*/

var langEn = false;
var langFr = false;

var lastLangEn = langEn; /* avoid recursion of onload events */
var lastLangFr = langFr;

function updateLangButtons() {
	var pic = "";
	if (lastLangEn != langEn) {
		lastLangEn = langEn;
		if (langEn) {
			pic = "langactive-en.gif";
		}
		else {
			pic = "lang-en.gif"
		}
		var elt = document.getElementById("lang-en");
		if (elt) { elt.setAttribute("src", getLangBaseUrl() + pic); }
	}

	if (lastLangFr != langFr) {
		lastLangFr = langFr;
		if (langFr) {
			pic = "langactive-fr.gif";
		}
		else {
			pic = "lang-fr.gif"
		}
		elt = document.getElementById("lang-fr");

		if (elt) { elt.setAttribute("src", getLangBaseUrl() + pic); }
	}
}


function clicklang(lang) {
	var reload = false;
	if (lang == "en") {
		reload = true;
		langEn = !langEn;
		if (langFr) {
			langFr = false;
		}
		updateLangButtons();
	}
	else if (lang == 'fr') {
		reload = true;
		langFr = !langFr;
		if (langEn) {
			langEn = false;
		}
		updateLangButtons();
	}
	if (reload) {
		if (langEn == langFr) {
			writeLanguageCookie("");
		}
		else {
			writeLanguageCookie(lang);
		}
		if (langEn == langFr) {
			window.location = getDocUrl();
		}
		else {
			var searchStr = "";
			if (langEn) {
				searchStr = 'lang=en';
			}
			else {
				searchStr = 'lang=fr';
			}
			window.location = getDocUrl() + '?' + searchStr;
		}
	}
}


function showLangButtons(path) {
	showLangButtons();
}


function showLangButtons() {
	LANGPATH = getLangBaseUrl();
	document.write('<div class=\"sidebar navlinks langButtons\">');
	document.write('<span style=\"position:relative; left:10px;\">');
	document.write('<img id=\"lang-en\" src=\"' + LANGPATH + 'lang-en.gif\" onClick=\"clicklang(\'en\');\" onDblClick=\"clicklang(\'en\');\">');
	document.write('</span>');
	document.write('<span style=\"position:relative; left:55px;\">');
	document.write('<img id=\"lang-fr\" src=\"' + LANGPATH + 'lang-fr.gif\" onClick=\"clicklang(\'fr\');\" onDblClick=\"clicklang(\'fr\');\" onload=\"updateLangButtons()\">');
	document.write('</span>');

	document.write('<center style=\"margin-top:4px; font-weight:bold;\">');
	if (langFr)
		document.write('Choisir une langue');
	else
		document.write('Select your language');
	document.write('</center>');

	document.write('</div>');
}



/*==============================================*/
/*  runs on load                                */
/*==============================================*/

var langStr = paramVal("lang");
if (langStr == 'en' || langStr == 'fr') {
	writeLanguageCookie(langStr)
}
else {
	langStr = readLanguageCookie();
}

var styleSheet = "";
if (langStr == "en") {
	styleSheet = "layout-en.css";
	langEn = true;
}
else if (langStr == "fr") {
	styleSheet = "layout-fr.css";
	langFr = true;
}
addLangStyleSheet(styleSheet);

/*==============================================*/

if (paramVal("print") == "1") {
	addLangStyleSheet("layout-print.css");
}

/*==============================================*/

if (navigator.appVersion.toLowerCase().indexOf('macintosh') != -1) {
	addLangStyleSheet("fontmac.css");
}
else if (screen.fontSmoothingEnabled) {
	addLangStyleSheet("fontwin.css");
}

/*==============================================*/

document.write('<script src=\"' + getSiteBaseUrl() + 'continents/' + 'continents.js' + '\"></script>');
document.write('<script src=\"' + getSiteBaseUrl() + 'lang/' + 'ui.js' + '\"></script>');

/*==============================================*/

document.write('<script src="http://www.google-analytics.com/ga.js"  type="text/javascript"></script>');
document.write('<script type="text/javascript">');
document.write('var pageTracker = _gat._getTracker("UA-3786055-3");');
document.write('pageTracker._initData();');
document.write('pageTracker._trackPageview();');
document.write('</script>');
