/* 
-----------------------------------------------------------------
	BRANZ
	BRA.2417
	shared.js
	
	Unique site functions
	This version: 17.04.2008 by AK
	Last Updated: See SVN	
	
-----------------------------------------------------------------
*/

/* 
-----------------------------------------------------------------	
	TRIGGER FUNCTIONS ON LOAD
-----------------------------------------------------------------
*/	
	
function init_branz() {	
		if (whichbrowser.isHiFi) {				
			apply_browser_fixes_for_js_dependent_elements();
			add_textfield_autoclears();	
			css_imagereplace();
			if (!whichbrowser.isIE5x) {
				MenuHoverPseudo(); // hide from ie5x
			}
			tablerowhelper();
			generateFlash('flash_1', '/resources/media/branz_homepage_308x400.swf', 308, 400, 'urlLink=/aboutus', '', ''); // change the link here
			fnLoadPngs();
			// other functions as required...
		}
}	

addEvent(window, 'load', init_branz, false);
	
/* 
-----------------------------------------------------------------	
	FUNCTIONS
-----------------------------------------------------------------
*/	

/* Apply browser based CSS fixes for certain browsers (eg. Safari) */
function apply_browser_fixes_for_js_dependent_elements() {
	if (
				(whichbrowser.isNS && (whichbrowser.versionMinor == 7.1)) || 
				(whichbrowser.isNS && (whichbrowser.versionMinor == 8.1)) || 					
				(whichbrowser.isFirefox && (whichbrowser.versionMinor == 1))							
			)
	{
		//alert('phoenixed');
		document.body.className += ' phoenix'; /* for JS-enabled CSS fixes of JS-generated text-resizer element */
	}	
	if (
				((whichbrowser.isSafari) && (!whichbrowser.isChrome))
			)
	{
		//alert('safari');
		document.body.className += ' safari'; /* for JS-enabled CSS fixes of JS-generated text-resizer element */
	}				
}
	
/* Auto clear forms */
function add_textfield_autoclears() {	
	if (!document.getElementById) return false;
	var inputarray = new Array("header_search","widget_feature","header_login","shop_search");
	for (var a=0; a<inputarray.length; a++) {
//		alert(a);
		if (document.getElementById(inputarray[a])) {
			var header_inputs = document.getElementById(inputarray[a]).getElementsByTagName('input');					
			for (var i=0; i<header_inputs.length; i++) {			
				if (header_inputs[i].type == 'text') {									
					addEvent(header_inputs[i], 'focus', textfield_autoclear, false);
					addEvent(header_inputs[i], 'blur', textfield_autoclear, false);	
				}
			}			
		}
	}
}	
	
function textfield_autoclear(e) {
	// identify 'this'		
	if (!e) {
		var e = window.event; // http://www.quirksmode.org/js/events_properties.html
	}
	// 
	if (e.target) {
		targ = e.target;
	}
	else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {	
		// defeat Safari bug
		targ = targ.parentNode;
	}
	// IE
	if (whichbrowser.isIE5up)	{
		_this = targ;
	} else {
		_this = this;	
	}	
	// interaction behaviours
	if (e.type == 'focus') {		
		if (_this.value == _this.defaultValue) {
			_this.value = '';
		}
	}
	else if (e.type == 'blur') {
		if (!_this.value) {
			_this.value = _this.defaultValue;	
		}	
	}		
}


/* 
-----------------------------------------------------------------	
	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();
	}
}


/*
 * (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);
    }
})();