/** 
 *  @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;


//THIS HAS BEEN PASTED IN HERE INSTEAD OF HAVING IT ON THE PAGE, SINCE WE ARE USING IT FOR THE HOMEPAGE ONLY, THIS IS ACCEPTABLE.
function changecss(myclass,element,value) {
	var CSSRules
	if (document.all) 
	{
		CSSRules = 'rules'
	}
	else if (document.getElementById) 
	{
		CSSRules = 'cssRules'
	}
	for (var i=0; i<document.styleSheets[0][CSSRules].length; i++) 
	{
//		if (document.styleSheets[0][CSSRules][i].selectorText == myclass) 
//		{
//			document.styleSheets[0][CSSRules][i].style[element] = value;
//		}
	}	
}










function makeQuickLinkSmall(currentSize)
{
  var quickLinks = document.getElementById('ddwrap');
  
  //DEfault values for the normal page.
   quickLinks.style.marginLeft = '50px';
//    quickLinks.style.width = '150px';
    
    //changecss('#dropdown ul .top','width','130px');
    //changecss('#dropdown ul .item, #dropdown ul .last, #dropdown ul .first','width','130px');
    //changecss('#dropdown ul .first','background','Transparent');

  
  if(currentSize >= 17)
  {      
    quickLinks.style.marginLeft = '0px';
  }
  
//  if(currentSize >= 25)
//  {
////    quickLinks.style.marginLeft = '-50px';
////    quickLinks.style.width = '250px';
////    changecss('#dropdown ul .top','width','170px');
////    changecss('#dropdown ul .item, #dropdown ul .last, #dropdown ul .first','width','170px');
//    changecss('#dropdown ul .first','background','White');
//  } 
//  
//  if(currentSize >= 30)
//  {
//    quickLinks.style.marginLeft = '-50px';
//    quickLinks.style.width = '250px';
//    changecss('#dropdown ul .top','width','230px');
//    changecss('#dropdown ul .item, #dropdown ul .last, #dropdown ul .first','width','230px');
//    changecss('#dropdown ul .first','background','White');
//  } 
}

function makeCampusesWider(currentSize)
{
  var campuses = document.getElementById('camps');
  var quickLinks = document.getElementById('ddwrap');
  if(currentSize >= 23)
  {
    campuses.style.width = '405px';
    campuses.style.width = '26em';
    quickLinks.style.marginLeft = '-40px';
  }
  else
  {
    campuses.style.width = '350px'; //Default
  }
}

function init()
{
    var iBase = TextResizeDetector.addEventListener(onFontResize,null);
    //alert( "The base font size = " + iBase );
    makeQuickLinkSmall(iBase);
    //makeCampusesWider(iBase);
}

function onFontResize( e, args )
{
  var msg = "\nThe base font size in pixels: " + args[0].iBase;
  msg +="\nThe current font size in pixels: " + args[0].iSize;
  msg += "\nThe change in pixels from the last size:" + args[0].iDelta;
  //alert(msg);
  makeQuickLinkSmall(args[0].iSize);  
  //makeCampusesWider(args[0].iSize);
}

/* id of element to check for and insert test SPAN into */
TextResizeDetector.TARGET_ELEMENT_ID = 'header';
/* function to call once TextResizeDetector was initialized */
TextResizeDetector.USER_INIT_FUNC = init;

