// Cross-browser event binding
window.EMAddEvent=function(e,ev,f,c){if(e.addEventListener){e.addEventListener(ev,f,c);}else if(e.attachEvent){var r=e.attachEvent('on'+ev,f);return r;}else{e['on'+ev]=f;}};
// Remove events bound with window.EMAddEvent
window.EMRemoveEvent=function(e,ev,f){if(e.removeEventListener){e.removeEventListener(ev,f,false);}else if(e.detachEvent){e.detachEvent("on"+ev,e[ev+f]);e[ev+f]=null;e["e"+ev+f]=null;}else{e['on'+ev]=null;}};

// Leaderboard
var EMLeaderBoard = {
	// Id of the element to replace
	id: "EMLeaderBoardId",
	// The base directory
	baseDir: 'http://www.eddymerckx.be/leaderboards/assets/',
	// An object containing the banners
	leaderBoards: {
		EMRegular: {
			url: 'swf/eddyBanner_840x150',
			width: 840,
			height: 150
		},
		EMSpecial: {
			url: 'swf/eddyBannerNoc_840x150',
			width: 840,
			height: 150
		}
	},
	// The leaderboard id
	leaderBoard: false,
	// The language
	language: 'nl',
	// The width of the banner, dynamically set
	width: 0,
	// The height of the banner, dynamically set
	height: 0,
	// The url to go to when clicking the banner
	clickTag: 'http://www.eddymerckx.be',
	// The background color of the banner
	backgroundColor: '#FFFFFF',
	// The minimum flash version
	flashVersion: '8',
	// The id of the flash object
	flashId: 'leaderBoard',
	// Path to ExpressInstall
	expressInstall: "expressInstall.swf",
	// Boolean to check if the leaderboard is already loaded
	loaded: false,
	// SWFObject instance
	so: new Object(),
	// The path to swfobject.js
	SWFObjectURL: "swfobject.js",

	/**
	 * Initialize the leaderboard
	 */
	initialize: function ()
	{
		// Check if an element with id=this.id exists
		if (!document.getElementById(this.id)) {
			return false;
		}

		// Detect SWFObject is already loaded
		this.detectSWFObject();
	},

	/**
	 * Load the flash banner
	 */
	load: function ()
	{
		// If the banner is already loaded, return
		if (this.loaded) {
			return;
		}

		// Support for SWFObject 1.5
		if (typeof SWFObject != 'undefined') {
			this.so = new SWFObject(this.getLeaderBoard(), this.flashId, this.width, this.height, this.flashVersion, this.backgroundColor);
			this.so.addVariable("clickTAG", this.clickTag);
			this.so.write(this.id);
		}
		// Support for SWFObject 2.x
		else if (typeof swfobject != 'undefined') {
			var flashvars = {
				clickTAG: this.clickTag
			};
			var params = false;
			var attributes = false;

			this.so = new swfobject.embedSWF(this.getLeaderBoard(), this.id, this.width, this.height, this.flashVersion, this.expressInstall, flashvars, params, attributes);
		}

		// Set the loaded variable
		this.loaded = true;
	},

	/**
	 * Returns the path to the flash banner
	 */
	getLeaderBoard: function ()
	{
		// Get the element that will be replaced with the flash banner
		var lbPlace = document.getElementById(this.id);
		// Loop through the available leaderboards
		for (var leaderBoard in this.leaderBoards) {
			// Set the default leaderboard (first in array)
			if (!this.leaderBoard) {
				this.leaderBoard = this.leaderBoards[leaderBoard].url;
				this.width = this.leaderBoards[leaderBoard].width;
				this.height = this.leaderBoards[leaderBoard].height;
			}

			// If the name of this leaderboard is defined as a classname
			// Set this as the banner
			if (this.hasClass(lbPlace, leaderBoard)) {
				this.leaderBoard = this.leaderBoards[leaderBoard].url;
				break;
			}
		}

		// Return the path to the banner
		return this.baseDir + this.leaderBoard + '_' + this.language.toLowerCase() + '.swf';
	},

	/**
	 * Check if SWFObject is already loaded, if not, load it dynamically
	 */
	detectSWFObject: function ()
	{
		if (typeof SWFObject == 'undefined' && typeof swfobject == 'undefined') {
			// Load SWFObject dynamically
			var script= document.createElement("script");
			script.type= "text/javascript";

			var me = this;
			script.onreadystatechange= function () {
				if (this.readyState == 'complete' || this.readyState=='loaded') {
					// If the script is loaded, load the flash banner
					me.load();
				};
			}

			script.onload = function () {
				// If the script is loaded, load the flash banner
				me.load();
			};

			// Set the script source to start loading SWFObject
			script.src = this.baseDir + this.SWFObjectURL;
			// Add the script to the headers
			document.getElementsByTagName("head")[0].appendChild(script);
			return;
		}

		// SWFObject is already defined
		this.load();
	},

	/**
	 * Set the id of the container
	 */
	setId: function (id)
	{
		if (document.getElementById(id)) {
			this.id = id;
			return true;
		}

		return false;
	},

	/**
	 * Set the background color of the container
	 */
	setColor: function (color)
	{
		this.backgroundColor = color;
	},

	/**
	 * Set the language of the flash banner
	 */
	setLanguage: function (language)
	{
		// Check if there's any language defined
		if (typeof language == 'string') {
			switch (language.toLowerCase()) {
				// Available languages
				case 'nl':
				case 'fr':
					this.language = language.toLowerCase();
					break;
				// Default language
				default:
					this.language = 'nl';
					break;
			}
		}
		else {
			// Default language
			this.language = 'nl';
		}
	},

	// Get elements by class name
	getElementsByClassName: function (cns,stn,el){if(!stn){stn='*'}if(!el){el=document.body}var arrElements=(stn=='*'&&el.all)?el.all:el.getElementsByTagName(stn);var arrReturnElements=new Array();var arrRegExpClassNames=new Array();if(typeof cns=='object'){for(var i=0;i<cns.length;i++){arrRegExpClassNames.push(new RegExp("(^|\\s)"+cns[i].replace(/\-/g,"\\-")+"(\\s|$)"));}}else{arrRegExpClassNames.push(new RegExp("(^|\\s)"+cns.replace(/\-/g,"\\-")+"(\\s|$)"));}var oElement;var bMatchesAll;for(var j=0;j<arrElements.length;j++){oElement=arrElements[j];for(var k=0;k<arrRegExpClassNames.length;k++){if(arrRegExpClassNames[k].test(oElement.className)){arrReturnElements.push(oElement);}}}return (arrReturnElements)},
	// Returns true if an element has a class
	hasClass: function (v,cn){var regex=RegExp("(^|\\s)"+cn.replace(/\-/g,"\\-")+"(\\s|$)");return regex.test(v.className)}
}

// Load the leaderboard
window.EMAddEvent(window, 'load', function() {
	try {
		var EMElements = EMLeaderBoard.getElementsByClassName('EMLeaderBoard');
		if (EMElements.length > 0) {
			EMElement = EMElements[0];
			var EMElementId = EMElement.getAttribute('id');

			if (EMElementId==null || EMElementId == 'undefined' || EMElementId == '') {
				EMElementId	= 'EMLeaderBoardId';
				EMElement.id = EMElementId;
			}

			EMLeaderBoard.setId( EMElementId );
			EMLeaderBoard.setLanguage( EMElement.getAttribute('lang') );
			EMLeaderBoard.initialize();
		}
	}
	catch (e) {}
}, false);