/* 
-----------------------------------------------------------------
	CRESA - Good Homes for Good Lives
	CRE.2583
	shared.js
	
	Unique site functions
	This version: 14.10.2008 by AK
	Last Updated: See SVN	
	
-----------------------------------------------------------------
*/

/*
 * (c)2006 Jesse Skinner/Dean Edwards/Matthias Miller/John Resig
 * Special thanks to Dan Webb's domready.js Prototype extension
 * and Simon Willison's addLoadEvent
 * For more info, see:
 * http://www.thefutureoftheweb.com/blog/adddomloadevent
 * http://dean.edwards.name/weblog/2006/06/again/
 * http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 */
 
addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;

            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }

        load_events.push(func);
    }
})();

/* 
-----------------------------------------------------------------	
	TRIGGER FUNCTIONS ON LOAD
-----------------------------------------------------------------
*/	
	
function init_goodhomes() {	
		if (whichbrowser.isHiFi) {				
			apply_browser_fixes_for_js_dependent_elements();
			tablerowhelper();
			add_print_button();
			// other functions as required...
		}
}	

addDOMLoadEvent(init_goodhomes);
	
/* 
-----------------------------------------------------------------	
	FUNCTIONS
-----------------------------------------------------------------
*/	

	
function add_print_button()	{
	if (!document.getElementById || !document.getElementsByTagName || !document.createElement || !document.replaceChild) {
		return;
	}		
	
	var displayPrint = true;
	var printLabel = 'Print Page';		
	
	if (document.getElementById('tools_footer')) {		
		var li = document.createElement('li');
				li.className = 'tool_print';			
				
		var button = document.createElement('a');
				button.setAttribute('title', printLabel);
				button.setAttribute('href', "#");	
		var buttontext = document.createTextNode(printLabel);
				button.appendChild(buttontext);
				li.appendChild(button);		
				addEvent(button, 'click', print_page, false);
				
		var next_sibling = document.getElementById('tools_footer').getElementsByTagName('li')[0];			
				next_sibling.parentNode.insertBefore(li, next_sibling);	
	}
}
	
function print_page() {
	window.print();
	return false;
}


/* 
-----------------------------------------------------------------	
	HELPER FUNCTIONS
-----------------------------------------------------------------
*/	

// CSS helper
function cssjs(a,o,c1,c2) {
		switch (a){
			case 'swap':
				o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				var found=false;
				var temparray=o.className.split(' ');
				for(var i=0;i<temparray.length;i++){
					if(temparray[i]==c1){found=true;}
				}
				return found;
			break;
		}
}

// Events helper
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}
// Events helper
function removeEvent(elm, evType, fn, useCapture) {
	if (elm.removeEventListener) {
		elm.removeEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.detachEvent) {
		var r = elm.detachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}
	
function cancelClick(e) {
	if (window.event){
		window.event.cancelBubble = true;
		window.event.returnValue = false;
		return;
	}
	if (e){
		e.stopPropagation();
		e.preventDefault();
	}
}


