/**
 * Javascript for embedding Flash objects
 * @version 4.0
 * @author pmander, cxilotl, porzechowski
 */

/**
 * A Object representing a Flash Object
 * @param data		The flash source file
 * @param width 	The width of the flash object
 * @param height	The height of the flash object
 * @param bgcolor	The hex code of the background color of the flash object
 */
FlashObject = function(data, width, height, bgcolor)
{
	//fields
	this.data = data;
	this.width = width;
	this.height = height;
	
	
	
	this.params = new Array();
	this.minVersion = 5;
	this.link = "";

	if(!bgcolor || bgcolor == "")
	{
		this.bgcolor = "#ffffff";
	}
	else
	{
		this.bgcolor = bgcolor;
	}
};

/**
 * Adds a <param> tag to the flash object
 * @param name	the paramater's name
 * @parma value	the parameter's value
 */
FlashObject.prototype.addParam = function(name, value)
{
	this.params.push(new Param(name, value));
};

/**
 * Adds a link by appending the query string '?link=url to the data field of this FlashObject
 * @param the url of the link
 */
FlashObject.prototype.setLink = function(url)
{
	this.link = "?clickTAG=" + url;
	//alert(this.link);
};

/**
 * Sets the minimum version of Flash required to play the Flash Object
 * @param minVersion	an integer
 */
FlashObject.prototype.setMinVersion = function(minVersion)
{
	this.minVersion = minVersion;

};

FlashObject.prototype.setAlign = function(setAlign)
{
	this.setAlign = setAlign;

};


/**
 * 
 * set the message for the client depends of flashVersion value.
 *
 */
 
FlashObject.prototype.flashDetector = function()
{

vers = FlashObject.flashVersion();

if ((Number(vers.substring(0,2))) < this.minVersion) {

	this.message = "<div id='logo'>&nbsp;</div><div id='clear'>&nbsp;</div><div style='font-family:Tahoma,Arial,sans-serif; color:#808080;margin-top:260px;margin-left:105px;text-align:center'><strong><font color='red'>Adobe Flash Player " +vers.substring(0,2)+ "</font><br><br>An older version of the Adobe Flash Player  has been detected.</strong<br> <br> This content requires the <strong>Adobe Flash Player  version " 
	+ this.minVersion 
	+ "</strong> or higher <br> To get new version of Adobe Flash Player click the link  <br> <a href='http://www.adobe.com/go/getflashplayer/' style='color:#00a6eb'>Get New Version of Flash</a></div>	<div id='clear'></div>";
} else {
	this.message = "<div id='logo'>&nbsp;</div><div id='clear'>&nbsp;</div><div style='font-family:Tahoma,Arial,sans-serif; color:#808080;margin-top:260px;margin-left:125px;text-align:center'>"+ vers
	+ " </div><div id='clear'></div>"
}
if  (this.minVersion == undefined ) {
	this.message = "<div id='logo'>&nbsp;</div><div id='clear'>&nbsp;</div><div style='font-family:Tahoma,Arial,sans-serif; color:#808080;margin-top:260px;margin-left:125px;text-align:center'><font color='red'><strong>Attention !!!</strong><br><br></font>System noticed that <font color='red'>setMinVersion()</font> is set up without value. <br> Please specify the value of the function.</div><div id='clear'></div>";
	}	

};

/**
 * 
 * display the Flash  otherwise the message to the client - depends of flashVersion() value.
 *
 */
FlashObject.prototype.print = function(flashContainerID)
{
	var data = this.data;
	var width = this.width;
	var height = this.height;
	var bgcolor = this.bgcolor;
	var link = this.link;
	var align = this.setAlign;
	
	var Q = "\"";
	var markup = "";
	var smarkup = "";
	var flashContainer = document.getElementById(flashContainerID);

 /**
 * Checks width and height of FlashObject
 * Set value to 100% for width and height when the width and height is empty otherwise set the value from the Flash object
 */

	
	
	
	var flashVersion = FlashObject.flashVersion();

	if ((Number(flashVersion.substring(0,2)) >= this.minVersion))
	{		
		//get params
		var params = this.params;
		var paramsString = "";
		
		for(var i = 0;  i < params.length; i++)
		{
			var paramName = params[i].name;
			var paramValue = params[i].value;
			paramsString += "<param name=" + Q + paramName + Q + " value=" + Q + paramValue + Q + " />";
		}
		
		//get markup
		markup = "<object type=\"application/x-shockwave-flash\" "
					+ "data=" +  Q + data + link + Q
					+ "width=" + Q + width + Q
					+ "height=" + Q + height + Q + ">"
					+ "<param name=\"movie\" value=" + Q + data + link + Q + "/>"
					+ "<param name=\"quality\" value=\"high\" />"
					+ "<param name=\"bgcolor\" value=" + Q + bgcolor + Q + " />"	
					+ paramsString
					+ "<embed src=" + Q + data + link + Q
						+ "type=\"application/x-shockwave-flash\" "
						+ "width=" + Q + width + Q
						+ "height=" + Q + height + Q
						+ "quality=\"high\" "
						+ "bgcolor=" + Q + bgcolor + Q + " >"
					+ "</embed>"
				  + "</object>";
		flashContainer.style.background = "none";
		
		flashContainer.innerHTML = markup;	
	} else {
if ((!width || width==" ") && (!height || height== " ") ) {

	height = "100%"
	width = "100%"
	flashContainer.style.height = height;
	flashContainer.style.width = width;
    } else {
	flashContainer.style.height = height;
	flashContainer.style.width = width;
	}
//alert(width.indexOf("%"));
if ((width.indexOf("%") != "-1") && (height.indexOf("%")) != "-1"  && (align == "left" || align=="center" || align =="right")){
	this.message = "<div id='logo'>&nbsp;</div><div id='clear'>&nbsp;</div><div style='font-family:Tahoma,Arial,sans-serif; color:#808080;margin-top:260px;margin-left:125px;text-align:center'><font color='red'><strong>Attention !!!</strong><br><br></font>System noticed that property of Flash Object has been given % value during the  <font color='red'>setAlign()</font>  can work properly just only with  pixels values. <br> Please specify the required values.</div><div id='clear'></div>";
	height = "100%"
	width = "100%"
	flashContainer.style.height = height;
	flashContainer.style.width = width;
	
	} else {
/**
 * 
 * set the flash container to the left.
 *
 */
	if (align == "left")  {

	JSS({
		'html': {
		   	'body': {
				'text-align': 'left',
				'float': 'left'
					}
				}
		});

	} 
 
/**
 * 
 * set the flash container to the center.
 *
 */
	if (align == "center")  {

	JSS({
		'html': {
			'body': {
				'text-align': 'center'
					},
			'div': {
				'margin-left': 'auto',
				'margin-right': 'auto'
					}
				}
		});

    } 
/**
 * 
 * set the flash container to the right.
 *
 */	
	if (align == "right")  {

	JSS({
		'html': {
			'body': {
				'text-align': 'right',
				'float': 'right'
					}
				}
		});
	} 	

	}

		flashContainer.innerHTML = this.message;	
	}

};
 
/** 
 * An object representing a parameter
 */
Param = function(name, value)
{
	this.name = name;
	this.value = value;
};


/**
 * Checks Visibility of  Adobe Flash against  IE, Firefox browser
 * @return Returns the latest version of Adobe Flash Player or if Adobe Flash Player is not installed 
 */


FlashObject.checkVersion = function()
{

var i,flash;
		if (window.ActiveXObject){
			for(i=10;i>0;i--){
				try{
					flash=new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+i);
					return (i+".0");
					
				}
				catch(e){
				}
			}
		}
		else if (navigator.plugins&&navigator.plugins.length){
			for (i=0;i<navigator.plugins.length;i++){
				if (navigator.plugins[i].name.indexOf('Shockwave Flash')!=-1){
					return (navigator.plugins[i].description.split(" ")[2]);
				}
			}
		}
		return ("No Adobe Flash Player Plugin has been detected in your browser <br><a href='http://www.adobe.com/go/getflashplayer/' style='color:#00a6eb'>Get Adobe Flash Player</a>");

}
/** 
 * 
 * Gets the Adobe Flash Version 
 * 
 **/
FlashObject.flashVersion = function() {

	var flashVer = FlashObject.checkVersion();
	
	return flashVer;
}	


/** 
 * JSS
 * 
 * Nested CSS via JavaScript - to sort problem in IE for setAlign();
 **/
var JSS = function(o, options) {
  // set up the base options
  this.options = {
    minified: true,
    apply: true
  }
  
  // set up the data hash
  this.data = {};
  
  // m()
  // 
  // Merges properties of s into o and returns o
  // 
  this.m = function(o, s) {
    for (var p in s) {
      o[p] = s[p];
    }
    
    return o;
  }
  
  // parse()
  // 
  // Parses the object o and stores the information in this.data{}
  // 
  this.parse = function(o, p) {
    if (typeof o != 'object') {
      throw new Error('CSS::parse: No valid CSS object passed.');
    }
    
    if (!p) {
      p = '';
    }
    
    for (var prop in o) {
      if (typeof o[prop] == 'object') {
        var rule = getPrefix(p) + prop;
        if (!this.data[rule]) {
          this.data[rule] = [];
        }

        this.parse(o[prop], this.getPrefix(p) + prop);

      } else {
        this.data[p][this.data[p].length] = (this.options.minified) ? prop + ':' + this.minify(o[prop]) + ';' : '  ' + prop + ': ' + o[prop] + ';\n';
      }
    }
  }
  
  // build()
  // 
  // Builds the information stored in this.data{} into valid CSS text
  // 
  this.build = function() {
    var css = '';
    
    for (var rule in this.data) {
      if (this.data[rule] != []) {
        css += (this.options.minified) ? rule + '{' : rule + ' {\n';

        css += this.data[rule].join('');

        css += (this.options.minified) ? '}' : '}\n\n';
      }
    }

    return css;
  }
  
  // getPrefix()
  // 
  // Shortcut to return the prefix for stacking
  // 
  this.getPrefix = function(p) {
    if (p) {
      return p + ' ';

    } else {
      return '';
    }
  }
  
  // minify()
  // 
  // Minifies the string passed
  // 
  this.minify = function(s) {
    // html/css colour names that can be replaced by shorter hex codes
    var colours = [
      [
        /black/,
        '#000'
      ],
      [
        /white/,
        '#fff'
      ],
      [
        /fuchsia/,
        '#f0f'
      ],
      [
        /yellow/,
        '#ff0'
      ]
    ];
    
    // loop through taking out any long colour names
    for (var i = colours.length - 1; i >= 0; i--){
      s = s.replace(colours[i][0], colours[i][1]);
    }
    
    // if we've got any 6 char hex codes
    if (s.match(/#[0-9a-f]{6}/)) {
      // see if we can shrink them
      var matches = s.match(/#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])/);
      
      if ((matches[1] == matches[2]) && (matches[3] == matches[4]) && (matches[5] == matches[6])) {
        s = s.replace(matches[0], '#' + matches[1] + matches[3] + matches[5]);
      }
    }
    
    return s;
  }
  
  // addToPage()
  // 
  // Applies the generated stylesheet to the page
  // 
  this.addToPage = function(s) {
    // create a style element
    var style = document.createElement('style');
    // set it to a text/css block
    style.setAttribute('type', 'text/css');

    // append the style to the body
    if (style.styleSheet) {
      // in IE
      style.styleSheet.cssText = s;

    } else {
      // in the other browsers
      style.appendChild(document.createTextNode(s));
    }

    // put it in the document
    document.getElementsByTagName('head')[0].appendChild(style);
  }
  
  // set the options
  this.options = this.m(this.options, options || {});
  
  // start parsing the object
  this.parse(o);
  
  if (this.options.apply) {
    // if we're supposed to apply the stylesheet, do it!
    this.addToPage(this.build());
    
  } else {
    // just return the text
    return this.build();
  }
}

