/*
 * Array Functionality
 * script: array.js
 * for: browsers not implementing the full array functionality of ECMA262-3
*/

if(typeof Array.prototype.copy=='undefined')
    Array.prototype.copy=function(a){
        var
            i=0,
            b=[];
        for(i;i<this.length;i++)
            b[i]=(typeof this[i].copy!='undefined')?
                this[i].copy():
                this[i];
        return b
    };

if(typeof Array.prototype.concat=='undefined')
    Array.prototype.concat=function(a){
        var
            i=0,
            b=this.copy();
        for(i;i<a.length;i++)
            b[b.length]=a[i];
        return b
    };
    
if(typeof Array.prototype.pop=='undefined')
    Array.prototype.pop=function(){
        var
            b=this[this.length-1];
        this.length--;
        return b
    };

if(typeof Array.prototype.push=='undefined')
    Array.prototype.push=function(){
        var
            i=0,
            b=this.length,
            a=arguments;
        for(i;i<a.length;i++)
            this[b+i]=a[i];
        return this.length
    };

if(typeof Array.prototype.shift=='undefined')
    Array.prototype.shift=function(){
        var
            i=0,
            b=this[0];
        for(i;i<this.length-1;i++)
            this[i]=this[i+1];
        this.length--;
        return b
    };

if(typeof Array.prototype.slice=='undefined')
    Array.prototype.slice=function(a,c){
        var
            i=0,
            b,
            d=[];
        if(!c)
            c=this.length;
        if(c<0)
            c=this.length+c;
        if(a<0)
            a=this.length-a;
        if(c<a){
            b=a;
            a=c;
            c=b
        }
        for(i;i<c-a;i++)
            d[i]=this[a+i];
        return d
    };

if(typeof Array.prototype.splice=='undefined')
    Array.prototype.splice=function(a,c){
        var
            i=0,
            e=arguments,
            d=this.copy(),
            f=a;
        if(!c)
            c=this.length-a;
        for(i;i<e.length-2;i++)
            this[a+i]=e[i+2];
        for(a;a<this.length-c;a++)
            this[a+e.length-2]=d[a-c];
        this.length-=c-e.length+2;
        return d.slice(f,f+c)
    };

if(typeof Array.prototype.unshift=='undefined')
    Array.prototype.unshift=function(a){
        var
            b;
        this.reverse();
        b=this.push(a);
        this.reverse();
        return b
    };

	firsts = new Array();
	toppers = new Array();
	var firstsPath = "images/philly_firsts";
	var toppersPath = "images/header_images";
	var numFirsts = 19;
	var numToppers = 44;	
	
	while(numFirsts--){
	firsts.push(firstsPath + "/" + pad(numFirsts) + ".gif");
	}
	
	while(numToppers--){
	toppers.push(toppersPath + "/" + pad(numToppers) + ".jpg");
	}	

	function pad(num) {
	return (num < 10)? "0" + num : num;
	}

	function getRandomNum(lbound, ubound) {
	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
	}

	function imagetag(f,n,c){
	document.write("<img class=\""+c+"\" src=\""+f+"\" name=\""+n+"\" />");
	}

	function show(id) {
		var p = document.getElementById(id);
		p.style.display = "block";
	}

	function hide(id) {
		var p = document.getElementById(id);
		p.style.display = "none";
	}

	function setBody(id,content,classname) {
		var l = document.layers?  document[id]:document.getElementById? document.getElementById(id):document.all? document.all[id]:0;
		if (document.layers) {
		var spanStr = classname?'<span class="'+classname+'">':'<span>';
		l.document.write(spanStr+content+'</span>');l.document.close();
		} else if (document.getElementById || document.all) {
		l.innerHTML = content; l.className = classname;
		}
	}


	function getRandomImage(arr){
	var selected= getRandomNum(0, arr.length);
	var imagePath =  arr.splice(selected,1);
	return imagePath;
	}


	function getQueryVariable(variable) {
	 var query = window.location.search.substring(1);
	 var vars = query.split("&");
	 for (var i=0;i<vars.length;i++) {
	 var pair = vars[i].split("=");
	 if (pair[0] == variable) {
	 return pair[1];
	 }
	 }
	 //alert('Query Variable ' + variable + ' not found');
	}


	function replace(string,text,by) {
	// Replaces text with by in string
	    var strLength = string.length, txtLength = text.length;
	    if ((strLength == 0) || (txtLength == 0)) return string;

	    var i = string.indexOf(text);
	    if ((!i) && (text != string.substring(0,txtLength))) return string;
	    if (i == -1) return string;

	    var newstr = string.substring(0,i) + by;

	    if (i+txtLength < strLength)
	        newstr += replace(string.substring(i+txtLength,strLength),text,by);

	    return newstr;
	}

	function buildGallery(p, n){
		var j = Number(n);
			for(var i=1;i<=j;i++){
			k =  imagetag("images/gallery/"+p+"/"+i+".jpg", "galleryitem_"+i, "galleryitem");
			}
	}

	function go(d){
		location.href = d;
	}

	function MM_swapImgRestore() { //v3.0
	  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}

	function MM_preloadImages() { //v3.0
	  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}

	function MM_findObj(n, d) { //v4.01
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	  if(!x && d.getElementById) x=d.getElementById(n); return x;
	}

	function MM_swapImage() { //v3.0
	  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}

	function stopError() {
	  return true;
	}

	var timeDelay = 6000; // change delay time in seconds
	var counter = 0;
	
	function startPix() {
	setInterval("slideshow()", timeDelay);
	}

	function slideshow() {
	var v = (toppers[getRandomNum(0, toppers.length)]);
	var m = (firsts[getRandomNum(0, firsts.length)]);
	(counter%2)?	document.image_one.src = v : 	document.image_two.src = v;
	counter++
	}

	window.onerror = stopError;
	startPix();
	