// Javascript voor pagina's op rotterdam.sp.nl

// Testje met web 3.6-links onderaan berichten -- klevie@sp.nl 20090627
// Dit is JavaScript omdat PHP de benodigde variabelen niet kent met alle oneindige virtuals en Marko-nieuwsmoduleshizzle...

function shareLinks()
{
	var title = document.title;
	var tinytitle = title.replace("SP Rotterdam :: ","").replace("SP afdeling Rotterdam :: ", "").replace(" - SP afd. Rotterdam","").replace("Nieuws :: ","");
	title = escape(title); tinytitle= escape(tinytitle);

	// Korte URL indien nieuwsbericht (nieuwe shortener t.sp.nl). Vooralsnog alles.
	var url = location.href;
	var urlarray = location.href.split("/");
	var ncode = (urlarray[4]-0).toString(36);
	var tinyurl = "http://sp.nl/9n"+ncode;

	// Beschikbare links
	var services = {
		"mail" : "mailto:?subject="+tinytitle+"&body="+title+": "+url,
		"gmail" : "http://www.addtoany.com/add_to/gmail?linkurl="+url+"&linkname="+title,
		"hotmail" : "http://www.addtoany.com/add_to/hotmail?linkurl="+url+"&linkname="+title,
		"hyves" : "http://rotterdam.sp.nl/myinclude/hyvesapi.php?a=www&p1=Lees [url="+url+"]"+tinytitle+"[/url]&p2=rotterdam.sp.nl",
		"facebook" : "http://www.facebook.com/share.php?src=bm&u="+url+"&amp;t="+title+"&amp;v=3",
		"twitter" : "http://twitter.com/home?status=RT @SPRotterdam "+tinytitle+" "+tinyurl,
		"google_bookmarks" : "http://www.google.com/bookmarks/mark?op=edit&bkmk="+url+"&title="+title,
		"delicious" : "http://del.icio.us/post?v=4;url="+url+";title="+title,
		"nujij" : "http://nujij.nl/jij.lynkx?t="+tinytitle+"&u="+url,
		"msn_reporter" : "http://reporter.msn.nl/?f=contribute&Title="+tinytitle+"&URL="+url
	};

	// title-beschrijvingen bij links
	var descriptions = {
		"mail" : "Verstuur dit bericht via je e-mailprogramma",
		"gmail" : "Verstuur dit bericht via Gmail",
		"hotmail" : "Verstuur dit bericht via Hotmail",
		"hyves" : "Voeg toe als WWW op Hyves",
		"facebook" : "Deel dit bericht via Facebook",
		"twitter" : "Retweet dit bericht",
		"google_bookmarks" : "Voeg dit bericht toe aan Google Bookmarks",
		"delicious" : "Voeg dit bericht toe aan Delicious",
		"nujij" : "Voeg dit bericht toe aan NUjij.nl",
		"msn_reporter" : "Voeg dit bericht toe aan MSN Reporter"
	};

	// Poept u maar!
	var tekst = "<ul class='addtoany_list'>\n";
	for(key in services)
	{ tekst += "<li><a href='" + services[key] + "' title='" + descriptions[key] + "' target='_blank' rel='nofollow'><img src='/mypictures/illu/" + key + ".png' alt='' /></a></li>"; }
	tekst += "</ul>\n";
	tekst += "<div id='linknotes' class='printOnly'></div>\n";
	if(ncode) tekst += "<p class='klein' style='margin-top:2px;'>Shortlink voor dit bericht: <a href='" + tinyurl + "'>" + tinyurl + "</a></p>\n";
	document.write(tekst);
}

// Nog een mooie nieuwe toevoeging: voetnoten met links onderaan print-pagina's
// Afkomstig van http://www.alistapart.com/articles/improvingprint/
// Gewoon Omdat Het Kan.- klevie@sp.nl 20090905
 
/* Eerst functies (uit jsUtilities) dan echte code */
    if(Array.prototype.push == null) {
      Array.prototype.push = function(item) {
        this[this.length] = item;
        return this.length;
      };
    };
    // ---------------------------------------------------------------------
    //                  function.apply (if unsupported)
    //           Courtesy of Aaron Boodman - http://youngpup.net
    // ---------------------------------------------------------------------
    if (!Function.prototype.apply) {
      Function.prototype.apply = function(oScope, args) {
        var sarg = [];
        var rtrn, call;
        if (!oScope) oScope = window;
        if (!args) args = [];
        for (var i = 0; i < args.length; i++) {
          sarg[i] = "args["+i+"]";
        };
        call = "oScope.__applyTemp__(" + sarg.join(",") + ");";
        oScope.__applyTemp__ = this;
        rtrn = eval(call);
        oScope.__applyTemp__ = null;
    	return rtrn;
      };
    };
    function inArray(needle) {
      for (var i=0; i < this.length; i++) {
        if (this[i] === needle) {
          return i;
        }
      }
      return false;
    }
    function addClass(theClass) {
      if (this.className != '') {
        this.className += ' ' + theClass;
      } else {
        this.className = theClass;
      }
    }
    function lastChildContainingText() {
      var testChild = this.lastChild;
      var contentCntnr = ['p','li','dd'];
      while (testChild.nodeType != 1) {
        testChild = testChild.previousSibling;
      } 
      var tag = testChild.tagName.toLowerCase();
      var tagInArr = inArray.apply(contentCntnr, [tag]);
      if (!tagInArr && tagInArr!==0) {
        testChild = lastChildContainingText.apply(testChild);
      }
      return testChild;
    }

/* Hier Komt Het Dus */
function linkNotes()
{
      var container = document.getElementById('berichttekst');
      var target = document.getElementById('linknotes');

      var h2        = document.createElement('h2');
      var h2_txt    = document.createTextNode('Links');
      h2.appendChild(h2_txt);
      var coll = container.getElementsByTagName('*');
      var ol   = document.createElement('ol');
      var myArr = [];
      var thisLink;
      var num = 1;
      for (var i=0; i<coll.length; i++) {
        if ( coll[i].getAttribute('href') ||
             coll[i].getAttribute('cite') ) { 
          thisLink = coll[i].getAttribute('href') ? coll[i].href : coll[i].cite;
          var note = document.createElement('sup');
	  addClass.apply(note,['printOnlyInline']);
          var note_txt;
          var j = inArray.apply(myArr,[thisLink]);
          if ( j || j===0 ) { // if a duplicate
            // get the corresponding number from 
            // the array of used links
            note_txt = document.createTextNode(j+1);
          } else { // if not a duplicate
            var li     = document.createElement('li');
            var li_txt = document.createTextNode(thisLink);
            li.appendChild(li_txt);
            ol.appendChild(li);
            myArr.push(thisLink);
            note_txt = document.createTextNode(num);
            num++;
          }
          note.appendChild(note_txt);
          if (coll[i].tagName.toLowerCase() == 'blockquote') {
            var lastChild = lastChildContainingText.apply(coll[i]);
            lastChild.appendChild(note);
          } else {
            coll[i].parentNode.insertBefore(note, coll[i].nextSibling);
          }
        }
      }
      target.appendChild(h2);
      target.appendChild(ol);
}

// Easy Youtube-embedcode met alle juiste parameters
// rode franje, fullscreen, weet ik veel wat
// 368x232 is precies goed voor 16:9 video
// We zouden ooit nog eens een echte custom player kunnen maken...

function joetjoeb(videocode)
{
	document.write(' \
	<object type="application/x-shockwave-flash" style="width:368px; height:232px;" data="http://www.youtube.com/v/' + videocode + '&fs=1&color1=0xFF0000&color2=0xFF0000&border=0&showinfo=0&rel=0"> \
	<param name="movie" value="http://www.youtube.com/v/' + videocode + '&hd=1&color1=0xFF0000&color2=0xFF0000&border=0&showinfo=0&rel=0"></param> \
	<param name="allowFullScreen" value="true"></param> \
	</object>');
}

// ALGEMEEN voor alle pagina's: althans op mijn telefoon doen de Nokia-browser en Opera zich helemaal niks aantrekken van een eventuele handheld-stylesheet (nog los van hoe goed die is)
// Dus bieden we op deze manier een mobiele versie aan. Zie ook http://wiki.forum.nokia.com/index.php/How_to_switch_CSS_styles
/*
if(screen.width<350 && screen.height<350) {
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
		if(a.getAttribute("title") == "Mobile") a.disabled = false;
}
*/

