// flashObject 1.4, Mark Dibbets, Fabrique, update aug 2007
// Customized for drugsweb.nl, Vincent Bovelander, Fabrique, feb 2008
    
// getElement.. wrapper 
function getEl(el){
  return document.getElementById(el);
};

// setClassName
function setClassName(el,className){
  if (!className){
    return;
  }
  if (el.getAttribute('className') != null){
    el.setAttribute('class',className);
  }
  else {
    el.setAttribute('class',className);
  }
};

// add stylesheet
var styleSheetFromScript = false;
function addStyle(selector,properties){
  if (document.styleSheets) {
    if (!styleSheetFromScript){
      styleSheetFromScript = document.createElement('style');
      styleSheetFromScript.setAttribute('type','text/css');
      document.getElementsByTagName('HEAD')[0].appendChild(styleSheetFromScript);
    }
    var lastSheet = document.styleSheets[document.styleSheets.length - 1];
    if(lastSheet && typeof lastSheet.addRule == 'object'){
      lastSheet.addRule(selector, properties);
    }
    else {
      styleSheetFromScript.appendChild(document.createTextNode(selector + ' { ' + properties + ' }'));
    }
 }
};
  
// check if the proper version is installed
function hasMinFlashVersion(versionNumber){
	if (window.ActiveXObject){
		try{
			var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + versionNumber);
			return 1;
		}
		catch(e){
			return 0;
		}
	}
	else {
		if(navigator.plugins.length){
			for (var i=0; i < navigator.plugins.length; i++){
				var pluginIdent = navigator.plugins[i].description.split(" ");
				if(pluginIdent[0] == "Shockwave" && pluginIdent[1] == "Flash"){
					var versionArray = pluginIdent[2].split(".");
					return versionArray[0] >= versionNumber;
				}
			} 	
		}
	}
	return 0;
};

// Class FlashObject
flashObjects = [];
function FlashObject(oArg){

  this.id = oArg.id;
  this.parentId = oArg.parentId;      
  this.uri = oArg.uri;
  this.width = oArg.width;
  this.height = oArg.height;
  this.hasRequiredVersion = hasMinFlashVersion(oArg.requiredVersion);
  this.className = oArg.className ? oArg.className : '';
  this.params = oArg.params;
  this.noFocus = oArg.noFocus;
  if (this.hasRequiredVersion){
    addStyle('#' + oArg.parentId + ' .alternateContent','display:none !important;');
  }
  
  // create the object
  this.create = function(){
    if ((!this.hasRequiredVersion) || (!getEl(this.parentId))){
      return;
    }
    var obj = document.createElement('object');
    obj.setAttribute('type','application/x-shockwave-flash');
    if (this.id){
      obj.setAttribute('id',this.id);
    }
    obj.setAttribute('data',this.uri);
    obj.setAttribute('src',this.uri);
    obj.setAttribute('width',this.width);
    obj.setAttribute('height',this.height);
    if (this.noFocus){
      obj.setAttribute('tabIndex',-1);
    }
    setClassName(obj,this.className);
    for (var i in this.params){
      var param = this.params[i];
      var paramEl = document.createElement('param');
      paramEl.setAttribute('name',i);
      paramEl.setAttribute('value',this.params[i]);
      obj.appendChild(paramEl);
      obj.setAttribute(i,this.params[i]);
    }
    var title = document.title;
    getEl(this.parentId).appendChild(obj);
    document.title = title;
    try {
      obj.loadMovie(0,this.uri);
    }
    catch (e){
    }
  }
  
};

// create a flash object
function createFlashObject(oArg){
  flashObjects[flashObjects.length] = new FlashObject(oArg);
};

// build all the flash objects (onload)
function createFlashObjects(){
  for (var i in flashObjects){
    flashObjects[i].create();
  }
};

// window.onload
if (window.addEventListener){
  window.addEventListener('load', createFlashObjects,false);
}
else if (window.attachEvent){
  window.attachEvent("onload", createFlashObjects);
}

function createTest(testID){
  createFlashObject({
    id:'drugstest_'+testID,
    parentId:'drugstestParent',
    uri:'http://testquiz.jellinekclinics.com/?id='+testID+'&ref=http://www.drugsweb.nl',
    width:500,
    height:400,      
    className:'flashObject',
    requiredVersion:7,
    noFocus:true
  });
}