/*
Author: RBurnie
Requirements: mootools (including DOM package)
"TextResizeDetector" - included at bottom of this script
*/
var florey_swf = Class({
	
	initialize: function(wrapper_name,swf_name,blurb_name){
		//this.bug_tracer = new Bug_tracer();
		// param names
		this.wrapper_name = wrapper_name;
		this.swf_name = swf_name;
		this.blurb_name = blurb_name;
		// get objects and elements
		this.swf = this.get_swf_object(this.swf_name); //the actual object
		this.wrapper = $(this.wrapper_name);
		this.blurb = $(this.blurb_name);
		this.wrapper_parent = this.wrapper.getParent();
		// get initial sizes - we will adjust sizes via js
		this.swf_init_height = this.swf.height;
		this.swf_init_width = this.swf.width;
		this.blurb_init_height = this.blurb.getStyle('height');
		this.wrapper_init_height = this.wrapper.getStyle('height');
		// all_init_height to be set to blurb_init_height if > swf_init_height
		this.all_init_height = (this.blurb_init_height.toInt() > this.swf_init_height.toInt() ? this.blurb_init_height : this.swf_init_height);
		// set initial styles
		// this includes setting both swf wrapper and blurb div to position absolute
		// and adjusting heights of swf wrapper, its parent, and blurb to the same as blurb height
		// or if blurb height less than swf init height the init height
		this.wrapper_parent.setStyles({
			'height':this.all_init_height,
			// For some reason if you've set the margin for the wrapper it stops working after resize
			// so we will give the margin to the wrapper parent
			'margin':this.wrapper.getStyle('margin')
		});
		this.wrapper.setStyles({
			width: this.swf_init_width,
			height: this.all_init_height,
			'background':'transparent',
			'background-image':'none',
			'margin':0
		});
		// We need to also make the swf wrapper position absolute
		// But only in IE6
		if(window.navigator.appVersion.indexOf('MSIE 6') > -1){
			this.wrapper.setStyles({
				'position': 'absolute',
				'top': '0px',
				'left': '0px'
			});
		}
		this.swf.height = this.all_init_height;
		
		this.blurb.setStyles({
			'position': 'absolute',
			'top': '0px',
			'left': '0px'
		});
	
	},
	
	
	// Method to get the actual flash object
	get_swf_object: function(movie_name){
		if (window.document[movie_name]){
			return window.document[movie_name];
		}
		if (navigator.appName.indexOf("Microsoft Internet")==-1){
			if (document.embeds && document.embeds[movie_name])
			 return document.embeds[movie_name]; 
		}else{
			// if (navigator.appName.indexOf("Microsoft Internet")!=-1)
			return document.getElementById(movie_name);
		}
	},
	
	
	adjust_height: function(dir){
		var swf = this.get_swf_object(this.swf_name);
		//NB: Internet Explorer wont accept .getStyle or .setStyle on the swf object
		//    so we need to use the specific 'height' attribute of the OBJECT tag
		switch(dir){
			case 'plus': 	var h = this.blurb.getStyle('height');			
							if(h.toInt() > this.all_init_height.toInt()){
								this.wrapper.setStyle('height',h);
								this.wrapper_parent.setStyle('height',h);
								swf.height = h;
							}
							break;
							
			case 'minus':	var h = this.blurb.getStyle('height');		
							if( (h.toInt() < this.swf.height) && (h.toInt() > this.all_init_height.toInt()) ) {	
								this.wrapper.setStyle('height',h);
								this.wrapper_parent.setStyle('height',h);
								swf.height = h;
							}else if( (h.toInt() < this.swf.height) && (h.toInt() < this.all_init_height.toInt()) ){
								this.wrapper.setStyle('height',this.all_init_height);
								this.wrapper_parent.setStyle('height',this.all_init_height);
								swf.height = this.all_init_height.toInt();
							}
							break;
							
			default:		swf.height = this.all_init_height;		
							this.wrapper.setStyle('height',this.all_init_height+'px');
							this.wrapper_parent.setStyle('height',this.all_init_height+'px');
							break;
		}
		
	}
	
});

/** 
 *  @fileoverview TextResizeDetector
 * 
 *  Detects changes to font sizes when user changes browser settings
 *  <br>Fires a custom event with the following data:<br><br>
 * 	iBase  : base font size  	
 *	iDelta : difference in pixels from previous setting<br>
 *  	iSize  : size in pixel of text<br>
 *  
 *  * @author Lawrence Carvalho carvalho@uk.yahoo-inc.com
 * @version 1.0
 */

/**
 * @constructor
 */
TextResizeDetector = function() { 
    var el  = null;
	var iIntervalDelay  = 200;
	var iInterval = null;
	var iCurrSize = -1;
	var iBase = -1;
 	var aListeners = [];
 	var createControlElement = function() {
	 	el = document.createElement('span');
		el.id='textResizeControl';
		el.innerHTML='&nbsp;';
		el.style.position="absolute";
		el.style.left="-9999px";
		var elC = document.getElementById(TextResizeDetector.TARGET_ELEMENT_ID);
		// insert before firstChild
		if (elC)
			elC.insertBefore(el,elC.firstChild);
		iBase = iCurrSize = TextResizeDetector.getSize();
 	};

 	function _stopDetector() {
		window.clearInterval(iInterval);
		iInterval=null;
	};
	function _startDetector() {
		if (!iInterval) {
			iInterval = window.setInterval('TextResizeDetector.detect()',iIntervalDelay);
		}
	};
 	
 	 function _detect() {
 		var iNewSize = TextResizeDetector.getSize();
		
 		if(iNewSize!== iCurrSize) {
			for (var 	i=0;i <aListeners.length;i++) {
				aListnr = aListeners[i];
				var oArgs = {  iBase: iBase,iDelta:((iCurrSize!=-1) ? iNewSize - iCurrSize + 'px' : "0px"),iSize:iCurrSize = iNewSize};
				if (!aListnr.obj) {
					aListnr.fn('textSizeChanged',[oArgs]);
				}
				else  {
					aListnr.fn.apply(aListnr.obj,['textSizeChanged',[oArgs]]);
				}
			}

 		}
 		return iCurrSize;
 	};
	var onAvailable = function() {
		
		if (!TextResizeDetector.onAvailableCount_i ) {
			TextResizeDetector.onAvailableCount_i =0;
		}

		if (document.getElementById(TextResizeDetector.TARGET_ELEMENT_ID)) {
			TextResizeDetector.init();
			if (TextResizeDetector.USER_INIT_FUNC){
				TextResizeDetector.USER_INIT_FUNC();
			}
			TextResizeDetector.onAvailableCount_i = null;
		}
		else {
			if (TextResizeDetector.onAvailableCount_i<600) {
	  	 	    TextResizeDetector.onAvailableCount_i++;
				setTimeout(onAvailable,200)
			}
		}
	};
	setTimeout(onAvailable,500);

 	return {
		 	/*
		 	 * Initializes the detector
		 	 * 
		 	 * @param {String} sId The id of the element in which to create the control element
		 	 */
		 	init: function() {
		 		
		 		createControlElement();		
				_startDetector();
 			},
			/**
			 * Adds listeners to the ontextsizechange event. 
			 * Returns the base font size
			 * 
			 */
 			addEventListener:function(fn,obj,bScope) {
				aListeners[aListeners.length] = {
					fn: fn,
					obj: obj
				}
				return iBase;
			},
			/**
			 * performs the detection and fires textSizeChanged event
			 * @return the current font size
			 * @type {integer}
			 */
 			detect:function() {
 				return _detect();
 			},
 			/**
 			 * Returns the height of the control element
 			 * 
			 * @return the current height of control element
			 * @type {integer}
 			 */
 			getSize:function() {
	 				var iSize;
			 		return el.offsetHeight;
		 		
		 		
 			},
 			/**
 			 * Stops the detector
 			 */
 			stopDetector:function() {
				return _stopDetector();
			},
			/*
			 * Starts the detector
			 */
 			startDetector:function() {
				return _startDetector();
			}
 	}
 }();

TextResizeDetector.TARGET_ELEMENT_ID = 'doc';
TextResizeDetector.USER_INIT_FUNC = null;

