/* 
	@ Title: McDonald's UK makeupyourownmind global JavaScript file
	@ Client: McDonald's UK
	@ Author: Ben Ellis
	@ Date: 17/01/2007
	@ Version: 1.00
*/

/* 
	Title: toggle()
	Description: Function to toggle objects: plugs into the prototyp/scriptaculous framework
	Arguments: 
		$id: id of object to toggle
		$effect: effect to use
		$duration: length of the effect*/

function toggle(id,effect,effectDuration){
	if(document.getElementById){
		if(!effectDuration) effectDuration = 0.5;
		Effect.toggle(document.getElementById(id),effect,{duration:effectDuration});	
	}
}


/* 
	Title: addLoadEvent()
	Description: Function to add custom functions to the window onload event handler
	Arguments: 
		$func: function as an argument
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}	


/***********************************************
* Random DIVS FOR THE HOMEPAGE

***********************************************/

if (document.getElementById)
document.documentElement.className = 'jsclass'; //hide content for DOM capable browsers


var randomcontentdisplay={
	divholders:new Object(),
	masterclass: "randomcontent",

	init:function(){
		if (!document.getElementById)
			return
		var alldivs=document.getElementsByTagName("div")
		var randomcontentsearch=new RegExp(this.masterclass+"\\s+(group\\d+)", "i") //check for CSS class="randomcontent groupX" (x=integer)
		for (var i=0; i<alldivs.length; i++){
			if (randomcontentsearch.test(alldivs[i].className)){
				if (typeof this.divholders[RegExp.$1]=="undefined") //if array to hold this group of divs doesn't exist yet
					this.divholders[RegExp.$1]=new Array() //create array first
					this.divholders[RegExp.$1].push(alldivs[i]) //add this div to the array
			}
		}
	this.showone()
	},

	showone:function(){
		for (group in this.divholders){ //loop thru each array within object
			var chosenOne=Math.floor(Math.random()*this.divholders[group].length) //randomly pick one entry from array
			this.divholders[group][chosenOne].style.display="block" //display content corresponding to the chosen entry
		}
	}
}



/***********************************************
* Degradable Image gallerys javascript - used on the scout reports pages

***********************************************/

var gal = {
    init : function() {
        if (!document.getElementById || !document.createElement || !document.appendChild) return false;
        if (document.getElementById('gallery')) document.getElementById('gallery').id = 'jgal';
        var li = document.getElementById('jgal').getElementsByTagName('li');
        li[0].className = 'active';
        for (i=0; i<li.length; i++) {
          	//  li[i].style.backgroundImage = 'url(' + li[i].getElementsByTagName('img')[0].src + ')';
            //li[i].style.backgroundRepeat = 'no-repeat';
            li[i].title = li[i].getElementsByTagName('img')[0].alt;
            gal.addEvent(li[i],'click',function() {
                var im = document.getElementById('jgal').getElementsByTagName('li');
                for (j=0; j<im.length; j++) {
                    im[j].className = '';
                }
                this.className = 'active';
            });
        }
    },
    addEvent : function(obj, type, fn) {
        if (obj.addEventListener) {
            obj.addEventListener(type, fn, false);
        }
        else if (obj.attachEvent) {
            obj["e"+type+fn] = fn;
            obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
            obj.attachEvent("on"+type, obj[type+fn]);
        }
    }
}

/***********************************************
* Degradeable toggle (uses sibling tag to toggle the display on or off - currently 
  used for the events page *note calling the function in the page
  -	toggleNextByIdTagAndClassName('byidtagandclass','div','showEvent');
  function searches for all classes showEvent inside #byidtagandclass and hides
  these divs. function then allows the user to toggle the display on and off

***********************************************/

function toggleNext(el) {
	var next=el.nextSibling;
	while(next.nodeType != 1) next=next.nextSibling;
	next.style.display=((next.style.display=="none") ? "block" : "none");
	}

function getElementsByIdTagAndClassName(id,tag,cname) {
	var tags=document.getElementById(id).getElementsByTagName(tag);
	var cEls=new Array();
		
		for (i=0; i<tags.length; i++) {
			var rE=new RegExp("(^|\s)" + cname + "(\s|$)");
		
		if (rE.test(tags[i].className)) {
			cEls.push(tags[i]);
			}
		}
 return cEls;
}
function toggleNextByIdTagAndClassName(id,tag,cname) {
	var ccn="clicker";
		clickers=getElementsByIdTagAndClassName(id,tag,cname);
	for (i=0; i<clickers.length; i++) {
		clickers[i].className+=" "+ccn;
		clickers[i].onclick=function() {toggleNext(this)}
		toggleNext(clickers[i]);
		}
	}
	
/***********************************************

Javascript to pass the audioPlayer.swf the mp3 clip to play

***********************************************/	
function thisMovie(movieName) {
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		return (isIE) ? window[movieName] : document[movieName];
	}
	
function makeCall(str) {
	thisMovie("audioPlayer").asFunc(str);
	}

		
	







