
// ------------------------ 10util.js starts here -----------------------------
/*              Utility functions                    */

var isIE6 = false;
var isIE7 = false;
/*@cc_on @*/

/*@if (@_jscript_version <= 5.6)
 // The above conditional compilation for IE is equivalent to
 // a conditional comment in HTML of 'if lte IE 6'
 isIE6 = true;
 /*@end @*/

/*@if (@_jscript_version > 5.6)
 // The above conditional compilation for IE is equivalent to
 // a conditional comment in HTML of 'if lte IE 6'
 isIE7 = true;
 /*@end @*/

var loadEventList = [];
function addEvent(obj, eventType, fn){

	if (typeof obj === "string") {
		obj = document.getElementById(obj);
	}

    /* adds an eventListener for browsers which support it
     Written by Scott Andrew: nice one, Scott */
    if (eventType === "load") {
        //hack me
        loadEventList.addLoadEvent(fn);
        return true;
    }
	if (!obj) {
		return null;
	}
    
    if (obj.addEventListener) {
        obj.addEventListener(eventType, fn, false);
        return true;
    }
    else 
        if (obj.attachEvent) {
            var r = obj.attachEvent("on" + eventType, fn);
            return r;
        }
        else {
            return false;
        }
}


var loadEventList = [];
loadEventList.addLoadEvent = function(fn){
    loadEventList[loadEventList.length] = fn;
};

loadEventList.hasFired = false;
loadEventList.fireLoadEvents = function(){
    for (var i = 0; i < loadEventList.length; i++) {
        loadEventList[i]();
    }
    loadEventList.hasFired = true;
};

/* the following is a hack to replicate DOMContentLoaded in browsers
 other than Firefox.  It is basically copied from
 http://dean.edwards.name/weblog/2006/06/again/
 */
if (/WebKit/i.test(navigator.userAgent)) { // Safari
    var _timer = setInterval(function(){
        if (/loaded|complete/.test(document.readyState)) {
            clearInterval(_timer);
            loadEventList.fireLoadEvents(); // call the onload handler
        }
    }, 100);
}
else 
    if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", loadEventList.fireLoadEvents, false);
    }
    else {
    // IE HACK
    /*@cc_on @*/
    /*@if (@_win32)
     document.write("<script id='__ie_onload' defer='defer' src='//:'><\/script>");
     var script = document.getElementById("__ie_onload");
     script.onreadystatechange = function() {
	     if (this.readyState == "complete") {
		     loadEventList.fireLoadEvents(); // call the onload handler
	     }
     };
     /*@end @*/
    }

var safeLoadEventList = [];
var isSafari2 = function () {
	 var userAgentNumber = RegExp("( Safari/)([^ ]+)").exec(navigator.userAgent);
	 if (!userAgentNumber || userAgentNumber.length < 3) {
	 	return false;
	 }
	 var mainVersionNumber = userAgentNumber[2].split('.')[0];
	 if (parseInt(mainVersionNumber) > 500) { //This is Safari 3
	 	return false;
	 }
	 return true;
}
function addSafeLoadEvent(fn) {
	if (!(isIE6 || isIE7)) {
		addEvent(document, 'load', fn);
		return true;
	} else {
		// This is IE6 or 7 and therefore can't have document.body.appendChilds or innerHTMLs until the whole page has loaded.
		// See http://support.microsoft.com/kb/927917 for more information
		safeLoadEventList.push(fn);
		return true;
	}
}

safeLoadEventList.hasFired = false;
safeLoadEventList.fireLoadEvents = function() {
    for (var i = 0; i < safeLoadEventList.length; i++) {
        safeLoadEventList[i]();
    }
    safeLoadEventList.hasFired = true;
};

if ((isIE6 || isIE7)) {
	window.attachEvent("onload", safeLoadEventList.fireLoadEvents);
}

// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
// --------------------- font-sizer.js starts here ----------------------------
/*
	this script adjusts the font-size of the document in 10% increments.
	it checks for the sizing-widget id and then attaches click events 
	it also sets a cookie to rember the font size for 1 year form the last visit

*/

/*
	if javascript is turned off the links goto larger.html and smaller.html
	these pages should explain about what the widget is for and how to adjust 
	the font-size in the browser...
*/

addEvent(window, "load", fontSizer);
//addEvent(window, "load", fontSizerSidebar);
//addEvent(window, "load", setFontSize);


function fontSizer()
{
	if (!document.getElementById) return false;
	
	
	
	if(document.getElementById('larger') && document.getElementById('smaller'))  {
		var increase=document.getElementById('larger');
		var decrease=document.getElementById('smaller');
		
		increase.style.display="inline";
		decrease.style.display="inline";
		
		var myDate= new Date();
		expires=myDate.getFullYear()+1
		myDate.setFullYear(expires);
		expires='; expires='+myDate.toGMTString();
		
		increase.onclick= function() {
			//get the calculated font-size
			if (document.getElementsByTagName('body')[0].style.fontSize)  {
				var currentSize=document.getElementsByTagName('body')[0].style.fontSize;
				
				currentSize=currentSize.match(/([0-9]+)/);
				currentSize=(Number(currentSize[0])+10)+'%';

			}else{//no calculated font size so we are just starting
				var currentSize='110%';
			}
			
			document.getElementsByTagName('body')[0].style.fontSize=currentSize;
			document.cookie="fontSize="+currentSize+expires;
			return false;
		}
		
		decrease.onclick= function() {
			//get the calculated font-size
			if (document.getElementsByTagName('body')[0].style.fontSize)  {
				var currentSize=document.getElementsByTagName('body')[0].style.fontSize;
				currentSize=currentSize.match(/([0-9]+)/);
				currentSize=(Number(currentSize[0])-10);
				if (currentSize<60) currentSize=60;
				currentSize=currentSize+'%';
			}else{//no calculated font size so we are just starting
				var currentSize='90%';
			}
			document.getElementsByTagName('body')[0].style.fontSize=currentSize;
			document.cookie="fontSize="+currentSize+expires;
			return false;
			

		}
		
	
	}
}

function fontSizerSidebar()
{
	if (!document.getElementById) return false;
	
	
	
	if(document.getElementById('larger-sidebar') && document.getElementById('smaller-sidebar'))  {
		var increase=document.getElementById('larger-sidebar');
		var decrease=document.getElementById('smaller-sidebar');
		
		increase.style.display="inline";
		decrease.style.display="inline";
		
		var myDate= new Date();
		expires=myDate.getFullYear()+1
		myDate.setFullYear(expires);
		expires='; expires='+myDate.toGMTString();
		
		increase.onclick= function() {
			//get the calculated font-size
			if (document.getElementsByTagName('body')[0].style.fontSize)  {
				var currentSize=document.getElementsByTagName('body')[0].style.fontSize;
				
				currentSize=currentSize.match(/([0-9]+)/);
				currentSize=(Number(currentSize[0])+10)+'%';

			}else{//no calculated font size so we are just starting
				var currentSize='110%';
			}
			
			document.getElementsByTagName('body')[0].style.fontSize=currentSize;
			document.cookie="fontSize="+currentSize+expires;
			return false;
		}
		
		decrease.onclick= function() {
			//get the calculated font-size
			if (document.getElementsByTagName('body')[0].style.fontSize)  {
				var currentSize=document.getElementsByTagName('body')[0].style.fontSize;
				currentSize=currentSize.match(/([0-9]+)/);
				currentSize=(Number(currentSize[0])-10);
				if (currentSize<60) currentSize=60;
				currentSize=currentSize+'%';
			}else{//no calculated font size so we are just starting
				var currentSize='90%';
			}
			document.getElementsByTagName('body')[0].style.fontSize=currentSize;
			document.cookie="fontSize="+currentSize+expires;
			return false;
			

		}
		
	
	}
}

/*
	Get the cookie and set the adjusted font-size
*/

function setFontSize ()  {
	var cookies=document.cookie;
	var cookieList=cookies.split(';');
	var fontSize='';

	for ( var i=0; i<cookieList.length; i++)  {
		if (cookieList[i].match('fontSize')) fontSize=cookieList[i];
	}
		if (fontSize)  {	
			fontSize=fontSize.match((/([0-9]+\%)/))[0];
			document.getElementsByTagName('body')[0].style.fontSize=fontSize;
		}
		
}
// ---------------------- font-sizer.js ends here -----------------------------

// ----------------- textresizedetector.js starts here ------------------------
/** 
 *  @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
 *
 *	REQUIRED BY: zones-navigation-rollover
 */

/**
 * @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;



// ------------------ textresizedetector.js ends here -------------------------
// ------------------- textresizeinit.js starts here --------------------------
/*
	detect the users font size and adjust the layout appropriately
	
	REQUIRED BY: zones-navigation-rollover
*/
 	function init()  {
	   var iBase = TextResizeDetector.addEventListener(onFontResize,null);
	   var bodyTag = document.getElementById('wrapper');
	   if(bodyTag)  {
		   if (iBase>27)  {
				bodyTag.className='large-type';
			}
		}
	}
	//id of element to check for and insert control
	TextResizeDetector.TARGET_ELEMENT_ID = 'wrapper';
	//function to call once TextResizeDetector has init'd
	TextResizeDetector.USER_INIT_FUNC = init;
		function onFontResize(e,args) {
			// to aid navigation roll-overs
			//zone_navigation_rollover.simpleNavResizer();
			// end of aid to navigaiton roll-overs
			var bodyTag = document.getElementById('wrapper');
			if(bodyTag){
				//27 is the last font size for the standard layout
				if (args[0].iSize>27)  {
					bodyTag.className='large-type';
				}
				if (args[0].iSize<26)
				{
					bodyTag.className='';
				}
			}
		}
// -------------------- textresizeinit.js ends here ---------------------------


function addComment() {
	//$('comment_form').show();
	Effect.BlindDown('comment_form');
	Effect.ScrollTo('comment_anchor', {afterFinish:function() {$('comment_comment').focus()
	}});
	
	
}


function replyTo(id, by) {
	$('comment_parent_id').value = id
	$('in_reply_to').update('In reply to <b>'+by+'</b>&hellip;')
	Effect.ScrollTo('comment_form');
	$('comment_comment').focus()
}


function openLogin() {
	Effect.BlindDown('login');
	$('login_box').morph('background:#FFF;');
	
}

function closeLogin() {
	Effect.BlindUp('login');
	$('login_box').morph('background:#F0F0F0;');
}


function insertAtCursor(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }

  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == 0) {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
                  + myValue
                  + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }

}
