window.addEvent('load', function() {
	insertYoutube();
	insertJWplayer();
});


/**
* insertYoutube
*
* replaces hyperlinks to Youtube movies with the Youtube object to that movie,
* adopting the width and height of the placeholder image inside the hyperlink
*
* @author Klaas Dieleman <klaas{AT}efocus.nl>
* 
*/

function insertYoutube() {
	var requiredFlashVersion = 9;
	if (Browser.Plugins.Flash.version < requiredFlashVersion) return false;
	
	var youtubelinks = new Array;
	document.getElements('a').each(function(item) {
		if (item.getProperty('href') && item.getProperty('href').substr(0,31) == "http://www.youtube.com/watch?v=" && item.getElement('img'))
			youtubelinks.include(item);
	});
	
	constructMovieObject = function(item) {
		var youTubeCode = item.getProperty('href').substr(31, item.getProperty('href').length - 1);
		var youTube = new Element('span');
		
		var youTubeObject = '<object ';
		youTubeObject += 'width="' + item.getElement('img').getSize().x + '" ';
		youTubeObject += 'height="' + item.getElement('img').getSize().y + '">\n';
		youTubeObject += '<param name="movie" value="http://www.youtube.com/v/' + youTubeCode + '&hl=en&fs=1&rel=0"></param>\n';
		youTubeObject += '<param name="allowFullScreen" value="true"></param>\n';
		youTubeObject += '<param name="allowscriptaccess" value="always"></param>\n';
		youTubeObject += '<embed src="http://www.youtube.com/v/' + youTubeCode + '&hl=en&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + item.getElement('img').getSize().x + '" height="' + item.getElement('img').getSize().y + '"></embed>\n';
		youTubeObject += '</object>';
		
		youTube.set('html', youTubeObject);
		return youTube;
	};
	
	youtubelinks.each(function(item) {
		var movie = constructMovieObject(item);
		movie.inject(item, 'after');
		item.destroy();
	});
	
}


/**
* insertJWplayer
*
* replaces hyperlinks to flv media (in the Sitecore medialibrary) with instance of JWplayer,
* adopting the source videofile plus the width and height of the placeholder image inside the hyperlink
*
* @author Klaas Dieleman <klaas{AT}efocus.nl>
* 
*/

function insertJWplayer() {
	var requiredFlashVersion = 9;
	if (Browser.Plugins.Flash.version < requiredFlashVersion) return false;
	
	var jwlinks = new Array;
	document.getElements('a').each(function(item) {
		if (item.getProperty('href') && item.getProperty('href').toLowerCase().indexOf("/video/") > 0 && item.getElement('img'))
		    jwlinks.include(item);
	});
	
	constructMovieObject = function(item) {
		var jwFile = item.getProperty('href');
		jwContainer = new Element('span');
		jwContainer.inject(item, 'after');			
		var s1 = new SWFObject("/swf/player.swf", "player", item.getElement('img').getSize().x, item.getElement('img').getSize().y, requiredFlashVersion);
		s1.addParam("allowfullscreen", "true");
		s1.addParam("allowscriptaccess", "always");
		s1.addVariable("file", jwFile);
		s1.addVariable("type", "video");
		s1.write(jwContainer);
		
		item.destroy();
	};
	
	jwlinks.each(function(item) {
		constructMovieObject(item);
	});
}




/* --- mogelijkheid om meerdere functies door onload te laten triggeren: addLoadEvent(naamFunctie); --- */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/* --- mogelijkheid om meerdere functies door onresize te laten triggeren: addResizeEvent(naamFunctie); --- */
function addResizeEvent(func) {
  var oldonresize = window.onresize;
  if (typeof window.onresize != 'function') {
    window.onresize = func;
  } else {
    window.onresize = function() {
      if (oldonresize) {
        oldonresize();
      }
      func();
    }
  }
}

function getUrlForPrint() {
    var url = location.href;
    if (url.indexOf('#') > 0) url = url.substring(0, url.indexOf('#'));
    var sep = url.indexOf('?') > 0 ? '&' : '?';
    return url + sep + 'print=true';
}
