/* css switcher: http://alistapart.com/d/alternate/styleswitcher.js
 */
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) {
        a.disabled = false;
      }
    }
  }
  setBWImgs(); // ;;tp;; //
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();

  doclang = getDocumentLang(); // `lang' is global (thus w/o var)

  createCSSpanel();

  // dodane:
  if (document.getElementById("imgPlcHldr")) { random_image() ; }

	display_photo_icon();

  setActiveStyleSheet(title);

}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

//=======================================================================
// http://www.sitepoint.com/print/expressions-javascript
// http://www.pixline.net/2006/09/javascript-non-intrusivo-2di6/
// http://www.pageresource.com/jscript/jstring1.htm

var magic_attr = "longdesc"; // magiczny atrybut

function setBWImgs() {
  var i, a, title;
  var filename, ne, rr, old_name, new_name;

  // imgs zawiera wszystkie 
  var imgs=document.getElementsByTagName('img');

  // przeszukaj wszystkie elementy img, jeżeli są oznaczone atrybutem 'rep[laceable]'
  // sprawdź bierzący styl i jeżeli jest równy 'NS' podmień grafikę na b-w

  for(i=0; i < imgs.length; i++) {

    a = imgs[i];

    // element img posiada atrybut 'magic_attr'
    if(a.getAttribute(magic_attr) != null ) {
			filename = a.getAttribute(magic_attr); // pełna nazwa (URI) pliku

      // testujemy czy magiczny atrybut jest magiczny, tj. zaczyna się od ##
      if (( filename.indexOf("##") == 0) || (filename.indexOf("[[") == 0) ) {
        // alert ("OK"); // --debug--
				title = getActiveStyleSheet();
				filename = filename.substring(2); // usuń dwa pierwsze ##
 
	    	ne = filename.split("."); // ne[0] nazwa, ne[1] ext
				old_name = a.getAttribute("src"); // stara nazwa

				if( title == "NS") {  
					rr = new RegExp(filename); // stara nazwa 
					new_name = old_name.replace(rr, ne[0] + "-+bw." + ne[1]); // zamień starą nazwę na nową
					//a.src = new_name; // wstaw nową nazwę 
          a.setAttribute("src", new_name);
				}
				else {
        	// W stylu BW ikona ma suffix -+bw przed rozszerzeniem, usuwamy go
        	// metodą `brute force' (pamiętać o nazwach plików):
					new_name = old_name.replace(/-\+bw\./, "\."); // usuń bw-+
					//a.src = new_name; // ditto
          a.setAttribute("src", new_name);
				}
   		}
		}
 	}
}

//=======================================================================
// dodaj panel wyboru stylu (wersja poprawiona):

function createCSSpanel() {

  var body = document.getElementsByTagName("body")[0]; // pierwsze body z tablicy
  
  var ele1 = body.firstChild; // pierwszy węzeł po 'body', po nim dokleimy panel navigacyjny

  nvg = body.insertBefore (document.createElement ('div'), ele1);
  nvg.setAttribute('class', 'styleswitchpanel');

  //alert ( lang );

  var plLang="pl"; var enLang="en"; var frLang="fr";
  var styleSwitchTxt = ""; var modifTxt = "";

  // wybierz napisy w zaleznosci od jezyka dokumentu:
  switch (doclang) {

    case plLang: styleSwitchTxt = '>> wybierz styl >> '; 
                 modifTxt = 'ostatnia zmiana';
                 break;
    case enLang: styleSwitchTxt = '>> switch style >> ';
                 modifTxt = 'last modified';
                 break;
    case frLang: styleSwitchTxt = '>> changement de style >> '; 
                 modifTxt = 'derniére modification';
                 break;
  }

  nvg.appendChild (document.createTextNode (styleSwitchTxt));

	var s1 = nvg.appendChild(document.createElement('a'));

  s1.setAttribute('onClick', 'setActiveStyleSheet("ES");return false;');
	s1.setAttribute('href', '#');
	s1.innerHTML = 'es';

  nvg.appendChild ( document.createTextNode (' :: '));

	var s2 = nvg.appendChild(document.createElement('a'));
  s2.setAttribute('onClick', 'setActiveStyleSheet("NS");return false;');
	s2.setAttribute('href', '#');
	s2.innerHTML = 'ns';

  nvg.appendChild ( document.createTextNode (' :: '));

	var s3 = nvg.appendChild(document.createElement('a'));
  s3.setAttribute('onClick', 'setActiveStyleSheet("BS");return false;');
	s3.setAttribute('href', '#');
	s3.innerHTML = 'bs';

  // Pobierz datę ostatniej modyfikacji z odpowiedniego elementu meta

  var moddate = getMetaValue("DC.date");

  // Wypisz co trzeba przed końcowym '/body'

	body.appendChild( document.createElement('hr') );

  addr = body.appendChild( document.createElement('address') );

  addr0 = addr.appendChild( document.createElement('a') );
	addr0.setAttribute('href', 'http://validator.w3.org/check/referer');

  addr.appendChild( document.createTextNode( "(c) T. Przechlewski; " + modifTxt + ": " + moddate ) );

  addr1 = addr0.appendChild( document.createElement('img') );

	addr1.setAttribute('src', 'http://gnu.univ.gda.pl/~tomasz/icons/valid-xhtml11.png');
	addr1.setAttribute('longdesc', '[[valid-xhtml11.png');
	addr1.setAttribute('alt', 'Valid XHTML 1.1!');

	addr1.setAttribute('height', '31');
	addr1.setAttribute('width', '88');

};

//=======================================================================

function getMetaValue(d) {
  var i, a;

  for(i=0; (a = document.getElementsByTagName("meta")[i]); i++) {
		 if ( a.getAttribute("name") == d ) { return a.getAttribute("content"); }
  }
  return dateNotSet;

}


//=======================================================================

function getDocumentLang() {
  var theBody = document.getElementsByTagName('body')[0];

  if ( theBody.getAttribute("lang") ) { 
     return theBody.getAttribute("lang"); }
  else { 
      if ( theBody.getAttribute("xml:lang") ) { 
          return theBody.getAttribute("xml:lang") }
            else { return "pl"; } // default is PL
  }
}


//
// http://www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/
// google: remove element javascript
// http://www.howtocreate.co.uk/tutorials/javascript/dombasics
//
// <img jest wew <a> ... </a> ; atrybut href zawiera link do oryginału
//
function random_image () {
  images = new Array(9);
  image_uris = new Array(9);
  var zdjecie = document.getElementById("imgPlcHldr");

  // adresy URL poprawne na dzien 3-10-2007:
  images[0] = "lr_charge_01"; 
  image_uris[0]='http://www.rugbymatters.net/RFUW/2007-08/Premiership/Clifton%20v%20Saracens/Gallery/1aa_543-4316_fm.htm';
 
  images[1] = "lr_lineout_01";  
  image_uris[1]= 'http://www.hno.harvard.edu/gazette/2002/04.18/photos/07-rugby-1-275.jpg';
  
  images[2] = "lr_scrum_01";
  image_uris[2]='http://rugby.stanford.edu/cgi-bin/spgm-1.4.7/index.php?spgmGal=2004-2005&spgmPic=12#spgmPicture';
 
  images[3] = "lr_scrum_02"; 
  image_uris[3]='http://www.news.harvard.edu/gazette/2006/10.26/photos/15-rugby4-450.jpg';

  images[4] = "lr_scrum_03";
  image_uris[4]='http://www.rugbymatters.net/RFUW/2007-08/Premiership/Clifton%20v%20Saracens/Gallery/1aa_543-4327_fm.htm';

  images[5] = "lr_try_01";
  image_uris[5]='http://www.rwcwomens.com/NR/rdonlyres/C52C2DBF-B8DA-4C4E-A37B-D6B67B051EE1/0/fracan225.jpg';

  images[6] = "lr_try_02";
  image_uris[6]='http://www.bcrugbynews.com/images/burnaby_velox_may52007.jpg';

  images[7] = "mr_scrum_01";
  image_uris[7]='http://www.flickr.com/photos/tasman/298672339/';

  images[8] = "lr_charge_02";
  image_uris[8] = 'http://www.irishrugby.ie/images/news/INPHO_LADIES_SCvIR_03.jpg';

  var index = Math.floor(Math.random() * images.length);
  var z = "http://gnu.univ.gda.pl/~tomasz/rysunki/" + images[index] + ".jpg";
  var ld = "[[" + images[index] + ".jpg";

  zdjecie.setAttribute('src', z);
  zdjecie.setAttribute('longdesc', ld);
  zdjecie.setAttribute('width', "300px");

  // <img jest wew. <a href=''>
  if (zdjecie.parentNode.getAttribute("href")) {
     zdjecie.parentNode.setAttribute('href', image_uris[index]);
  }

}

// ======================================================================
// Dodanie ikonek `foto-aparat' do elementów span oznaczonych jako <span class='photoicon' .../>
function display_photo_icon() {
   var e = document.getElementsByTagName('span');


   for ( var i = 0; i < e.length; i++ ) { var a = e[i].getAttribute('class');
			 if ( a == 'photoicon' ) { 
					 var pi = document.createElement('img'); 
					 pi.setAttribute('src', 'http://gnu.univ.gda.pl/~tomasz/icons/photoicon.png');
					 pi.setAttribute('alt', '[icon]');
					 pi.setAttribute('title', '[icon]');
					 e[i].parentNode.insertBefore(pi, e[i]); 
			 }
       else if ( a == 'googleearth' ){ 
					 var pi = document.createElement('img'); 
					 pi.setAttribute('src', 'http://gnu.univ.gda.pl/~tomasz/icons/50px-Gnome-globe-svg-+bw.png');
					 pi.setAttribute('alt', '[Google Maps]');
					 pi.setAttribute('title', '[Google Maps]');
					 pi.setAttribute('width', '20px');
           pi.setAttribute('longdesc', "[[50px-Gnome-globe-svg.png");
					 e[i].parentNode.insertBefore(pi, e[i]);
			 }
	 }
}

//=======================================================================
//Local variables:
//tab-width:2
//End:
