
/**
 *  Adds ECMA-262 rev. 5 methods to Array prototype, if they are missing
 *  Please note that the global prototypes are updated, so only use this file
 *  if that is acceptable, or if you're running in an iframe
 *
 *  Please not that this file WILL NOT BE COPYRIGHTED, as it merely wraps mozilla implementation
 *  of JavaScript 1.6 / ECMA-262 rev. 5 methods
 *
 *  @author Morgan Roderick - morgan@roderick.dk
 */
 /*jslint evil: false, strict: false, undef: true, white: false, onevar:false, browser:true, plusplus:false, bitwise:false */
(function(){
   "use strict";

   // Mozilla's ECMA-262 rev. 5 compliant implementation
   // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter
   if (!Array.prototype.filter){
       Array.prototype.filter = function(fun /*, thisp*/){
           var len = this.length >>> 0;
           if (typeof fun !== "function"){
               throw new TypeError();
           }

           var res = [];
           var thisp = arguments[1];
           for (var i = 0; i < len; i++){
               if (i in this){
                   var val = this[i]; // in case fun mutates this
                   if (fun.call(thisp, val, i, this)){
                       res.push(val);
                   }
               }
           }

           return res;
       };
   }

    // Mozilla's ECMA-262 rev. 5 compliant implementation
    // see https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf
    if (!Array.prototype.indexOf){
        Array.prototype.indexOf = function( elt ){  
            var len = this.length >>> 0;  
            var from = Number( arguments[1] ) || 0;  
            from = (from < 0) ? Math.ceil(from) : Math.floor(from);  
            if (from < 0){  
                from += len;  
            }

            for (; from < len; from++){  
                if ( from in this && this[from] === elt ){  
                    return from;  
                }
            }  
            return -1;  
        };
    }

    // Mozilla's ECMA-262 rev. 5 compliant implementation
    // see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/lastIndexOf
    if (!Array.prototype.lastIndexOf){
        Array.prototype.lastIndexOf = function(elt /*, from*/){
            var len = this.length;

            var from = Number(arguments[1]);
            if (isNaN(from)){
                from = len - 1;
            } else {
                from = (from < 0) ? Math.ceil(from) : Math.floor(from);
                if (from < 0){
                    from += len;
                } else if (from >= len){
                    from = len - 1;
                }
            }

            for (; from > -1; from--){
                if (from in this && this[from] === elt){
                    return from;
                }
            }
            return -1;
        };
    }

    // Mozilla's ECMA-262 rev. 5 compliant implementation
    // see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/every
    if (!Array.prototype.every) {
        Array.prototype.every = function(fun /*, thisp*/){
            var len = this.length >>> 0;
            if (typeof fun !== "function"){
                throw new TypeError();
            }

            var thisp = arguments[1];
            for (var i = 0; i < len; i++){
                if (i in this && !fun.call(thisp, this[i], i, this)){
                    return false;
                }
            }
            return true;
        };
    }

    // Mozilla's ECMA-262 rev. 5 compliant implementation
    // see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach
    if (!Array.prototype.forEach){
        Array.prototype.forEach = function(fun /*, thisp*/) {
            var len = this.length >>> 0;
            if ( typeof fun !== "function" ){
                throw new TypeError();
            }

            var thisp = arguments[1];
            for (var i = 0; i < len; i++){
                if (i in this){
                    fun.call(thisp, this[i], i, this);
                }
            }
        };
    }
    
    // Mozilla's ECMA-262 rev. 5 compliant implementation
    // see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map
    if (!Array.prototype.map){
        Array.prototype.map = function(fun /*, thisp*/){
            var len = this.length >>> 0;
            if (typeof fun !== "function"){
                throw new TypeError();
            }

            var res = new Array(len);
            var thisp = arguments[1];
            for (var i = 0; i < len; i++){
              if (i in this){
                  res[i] = fun.call(thisp, this[i], i, this);
              }
          }

          return res;
        };
    }

    // Mozilla's ECMA-262 rev. 5 compliant implementation
    // see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/some
    if (!Array.prototype.some){
        Array.prototype.some = function(fun /*, thisp*/){
            var i = 0,
            len = this.length >>> 0;

            if (typeof fun !== "function"){
                throw new TypeError();
            }

            var thisp = arguments[1];
            for (; i < len; i++){
                if (i in this && fun.call(thisp, this[i], i, this)){
                    return true;
                }
            }
            return false;
        };
    }
    
    // Mozilla's ECMA-262 rev. 5 compliant implementation
    // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
    if (!Array.prototype.reduce){
        Array.prototype.reduce = function(fun /*, initial*/){
            var len = this.length >>> 0;
            if (typeof fun !== "function"){
                throw new TypeError();
            }

            // no value to return if no initial value and an empty array
            if (len === 0 && arguments.length === 1){
                throw new TypeError();
            }

            var i = 0;
            var rv;
            if (arguments.length >= 2){
                rv = arguments[1];
            } else {
                do {
                    if (i in this) {
                        rv = this[i++];
                        break;
                    }

                    // if array contains no values, no initial value to return
                    if (++i >= len){
                        throw new TypeError();
                    }
                }
                while (true);
            }

            for (; i < len; i++){
                if (i in this){
                    rv = fun.call(null, rv, this[i], i, this);
                }
            }

            return rv;
        };
    }    

    // Mozilla's ECMA-262 rev. 5 compliant implementation
    // see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight
    if (!Array.prototype.reduceRight){
        Array.prototype.reduceRight = function(fun /*, initial*/){
            var len = this.length >>> 0;
            if (typeof fun !== "function"){
                throw new TypeError();
            }

            // no value to return if no initial value, empty array
            if (len === 0 && arguments.length === 1){
                throw new TypeError();
            }

            var i = len - 1;
            var rv;
            if (arguments.length >= 2){
                rv = arguments[1];
            } else {
                do {
                    if (i in this) {
                        rv = this[i--];
                        break;
                    }

                    // if array contains no values, no initial value to return
                    if (--i < 0){
                        throw new TypeError();
                    }
                } 
                while (true);
            }

            for (; i >= 0; i--) {
                if (i in this){
                    rv = fun.call(null, rv, this[i], i, this);
                }
            }

            return rv;
        };
    }
}());

// APL Smart GA Tracker script
//
// Include this javascript at end of body, if you want to
// render the ga_debug div, otherwise can go anywhere.
//

function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);
}

function isempty(x){
if(x!=="")
    return true;
}

function escapeHtml (html) {
var replacements = {
	'&': '&amp;',
	'<': '&lt;',
	'>': '&gt;'
	};
return html.replace(/[&<>]/g, function ($0) {return replacements[$0];});
}


/* library.austintexas.gov google analytics */

  var _gaq = _gaq || [];
  var aplHosts = new Object;
  /* all APL Domains used for tracking in Google Analytics */
  aplHosts['library.austintexas.gov'] = 'UA-105868-11';
  aplHosts['www.austinlibrary.com'] = 'UA-105868-3';
  aplHosts['austinlibrary.com'] = 'UA-105868-3';
  aplHosts['www.connectedyouth.com'] = 'UA-105868-8';
  aplHosts['connectedyouth.com'] = 'UA-105868-8';
  aplHosts['www.connectedyouth.org'] = 'UA-105868-8';
  aplHosts['connectedyouth.org'] = 'UA-105868-8';
  aplHosts['www.wiredforyouth.org'] = 'UA-105868-8';
  aplHosts['wiredforyouth.org'] = 'UA-105868-8';
  aplHosts['www.wiredforyouth.com'] = 'UA-105868-8';
  aplHosts['wiredforyouth.com'] = 'UA-105868-8';
  aplHosts['library.austintexas.gov'] = 'UA-105868-11';
  aplHosts['library.austintexas.gov'] = 'UA-105868-11';
  aplHosts['www.ci.austin.tx.us'] = 'UA-105868-11';
  aplHosts['libro.coacd.org'] = 'UA-28149773-1';
  aplHosts['libro'] = 'UA-28149773-1';
  aplHosts['austinsummerreading.org'] = 'UA-105868-6';
  aplHosts['www.austinsummerreading.org'] = 'UA-105868-6';


  var this_host= window.location.hostname;
  var this_href= window.location.href;
 
  // alert(this_host + ' ==> ' + aplHosts[this_host]);
  // alert(this_href);
  
  var o = parseUri.options;
  var items = parseUri(this_href);
  var query = items[o.q.name];
		
  for (var i = 0; i < o.key.length; i++) {
		items[o.key[i]] = escapeHtml(items[o.key[i]]);
		// alert(o.key[i] + ' => ' + items[o.key[i]]);
}

	var dirs = items['directory'].split('/');
	var this_dirs = dirs.filter(isempty);	 // remove 

// This case will let you override host tracker codes based upon
// directories in array this_dirs[0..n] like austinlibrary.com/ahc/
// so you can create custom tracker codes for very specific cases.

switch(this_host) {

case 'libro','libro.coacd.org':
	//  alert('case libro');
    if (this_dirs[0] == 'stage') { 
		alert('true');
	}
	break;
case 'www.austinlibrary.com','austinlibrary.com':
  	// alert('case austinlibrary.com');
	switch(this_dirs[0]) {
		case 'ahc':
			aplHosts[this_host]= 'UA-105868-3'; // push different ga prop for /ahc if we want to. 
		  	break;
		case 'ga':
//			alert('austinlibrary ga dir');
			break;
		default:
			break;
	}
	break;
case 'www.austintexas.gov':
  	// alert('case austinlibrary.com');
	switch(this_dirs[0]) {
		case 'library':
			aplHosts[this_host]= 'UA-105868-10'; // www.austintexas.gov /library tracker
		  	break;
		default:
			break;
	}
	break;
default:

//  alert('host case default');

}

// render ga_debug div if found
var ga_div=document.getElementById("ga_debug");
if (ga_div != null) {
	ga_div.innerHTML='';
	for (var i = 0; i < o.key.length; i++) {
		ga_div.innerHTML=ga_div.innerHTML + '<li style="margin-left:12px;">' + o.key[i] + ' :: ' + items[o.key[i]] + '</li>';
	}

	ga_div.innerHTML=ga_div.innerHTML+ '<li style="margin-left:12px;"> GA Tracker :: ' + aplHosts[this_host] + ' for ' + this_host + '</li>';
	ga_div.innerHTML=ga_div.innerHTML+'';
}

_gaq.push(['_setAccount', aplHosts[this_host]]);
_gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); 
	ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


