// Author: Jason Levitt Date: December 7th, 2005
// Constructor -- pass a REST request URL to the constructor
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

/*
    The global object JSON contains two methods.

    JSON.stringify(value) takes a JavaScript value and produces a JSON text.
    The value must not be cyclical.

    JSON.parse(text) takes a JSON text and produces a JavaScript value. It will
    return false if there is an error.
*/
var JSON = function () {
var m = {
  '\b': '\\b',
  '\t': '\\t',
  '\n': '\\n',
  '\f': '\\f',
  '\r': '\\r',
  '"' : '\\"',
  '\\': '\\\\'
  },
  s = {
      'boolean': function (x) {
          return String(x);
      },
      number: function (x) {
          return isFinite(x) ? String(x) : 'null';
      },
      string: function (x) {
          if (/["\\\x00-\x1f]/.test(x)) {
              x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                  var c = m[b];
                  if (c) {
                      return c;
                  }
                  c = b.charCodeAt();
                  return '\\u00' +
                      Math.floor(c / 16).toString(16) +
                      (c % 16).toString(16);
              });
          }
          return '"' + x + '"';
      },
      object: function (x) {
          if (x) {
            var a = [], b, f, i, l, v;
            if (x instanceof Array) {
              a[0] = '[';
              l = x.length;
              for (i = 0; i < l; i += 1) {
                v = x[i];
                f = s[typeof v];
                if (f) {
                    v = f(v);
                    if (typeof v == 'string') {
                      if (b) {
                          a[a.length] = ',';
                      }
                      a[a.length] = v;
                      b = true;
                    }
                  }
                }
                a[a.length] = ']';
              } else if (x instanceof Object) {
                  a[0] = '{';
                  for (i in x) {
                      v = x[i];
                      f = s[typeof v];
                      if (f) {
                          v = f(v);
                          if (typeof v == 'string') {
                              if (b) {
                                  a[a.length] = ',';
                              }
                              a.push(s.string(i), ':', v);
                              b = true;
                          }
                      }
                  }
                  a[a.length] = '}';
              } else {
                  return;
              }
              return a.join('');
          }
          return 'null';
      }
    };
  return {
    copyright: '(c)2005 JSON.org',
    license: 'http://www.JSON.org/license.html',
/*
    Stringify a JavaScript value, producing a JSON text.
*/
  stringify: function (v) {
    var f = s[typeof v];
    if (f) {
      v = f(v);
      if (typeof v == 'string') {
          return v;
      }
    }
    return null;
  },
/*
    Parse a JSON text, producing a JavaScript value.
    It returns false if there is a syntax error.
*/
    parse: function (text) {
      try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + text + ')');
      } catch (e) {
        return false;
      }
    }
  };
}();

/****************************************************************************************************
End JSON parser. Begin preppermint script
****************************************************************************************************/

//*****************************
//AddEvent stuff
//*****************************
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	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] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);
/* End addEvent stuff */

function displayStories(){
  var content = "";
  var pagenum = "page=1";
  var valueArray = new Array();
  
  //Store page number if there is one.
  if(document.location.hash != ""){
    valueArray = document.location.hash.toString().replace(/#/,'').split('&');
    pagenum = valueArray[0];
  }
  
  //var request = "http://localhost/publishing/publishjson.aspx?feed=chrisboylan.com&" + pagenum.toString();
  var request = "http://www.preppermint.net/publishing/publishjson.aspx?id=26&sppc=3&" + pagenum.toString();
  
  //Get JSON text
  aObj = new JSONscriptRequest(request);
  // Build the script tag
  aObj.buildScriptTag();
  // Execute (add) the script tag
  aObj.addScriptTag();
  
  //Everything else from here will be executed by the returned script. 
  //That returned script will parse template, publishTemplate.js.
}

//Set things in motion...
addEvent(window,'load',displayStories);