/*
 * Nithin Meppurathu Date Manager
 *
 * Included Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 */

(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

  // The base Class implementation (does nothing)
  this.Class = function(){};
 
  // Create a new Class that inherits from this class
  Class.extend = function(prop) {
    var _super = this.prototype;
   
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
   
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
           
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
           
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);       
            this._super = tmp;
           
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
   
    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
   
    // Populate our constructed prototype object
    Class.prototype = prototype;
   
    // Enforce the constructor to be what we expect
    Class.prototype.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;
   
    return Class;
  };
	/*

	var dm = new dateManager({
		'fetchUrl' : 'http://bhg.sweeps.nithin.resolute.com/prize'
	});

	/*

	// add custom data
	
	x.push('2011-11-22', '{"data":"today"}');
	x.push('2011-11-23', '{"data":"tomorrow"}');

	console.log(x.get());
	console.log(x.next());
	console.log(x.next());
	console.log(x.prev());
	console.log(x.prev());
	console.log(x.get('2011-11-24'));
	console.log(x.get());

	console.log(x.get('2011-11-28').title);
	console.log(x.get('2011-11-28').description);

	*/

})();

(function ($) {
	$.event.special.load = {
		add: function (hollaback) {
			if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
				// Image is already complete, fire the hollaback (fixes browser issues were cached
				// images isn't triggering the load event)
				if ( this.complete || this.readyState === 4 ) {
					hollaback.handler.apply(this);
				}

				// Check if data URI images is supported, fire 'error' event if not
				else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
					$(this).trigger('error');
				}
				
				else {
					$(this).bind('load', hollaback.handler);
				}
			}
		}
	};
}(jQuery));

var dateManager = Class.extend({

	init: function(obj)
	{
		this.manager 	= [];
		this.obj 	 	= obj;
		var todaysDate 	= new Date();
		this.index	 	= this.encode(todaysDate);
	},

	push: function(key, body)
	{
		this.manager[key] = body;
	},

	add_channel_id: function(channel_id)
	{
		this.obj.channel_id = channel_id;
	},

	next: function()
	{
		var tomorrow = new Date(this.decode(this.index).getTime() + (24 * 60 * 60 * 1000));
		var nextDate = this.encode(tomorrow);
		return this.get(nextDate);
	},

	prev:function()
	{
		var yesterday = new Date(this.decode(this.index).getTime() - (24 * 60 * 60 * 1000));
		var prevDate = this.encode(yesterday);
		return this.get(prevDate);	
	},

	decode: function(string)
	{
		var ds = string.split('-');
		var temp = new Date(ds[0],(parseInt(ds[1])-1),ds[2]);
		return temp;
	},

	encode: function(date)
	{
		return date.getFullYear()+'-'+(date.getMonth() + 1)+'-'+date.getDate();
	},

	get: function(key)
	{
		if(key)
		{
			this.index	 = key;
			
			if(this.manager[key])
			{
				return this.manager[key];
			}
			else
			{
				return this.fetch(key);
			}
		}
		else
		{
			return this.manager;
		}
	},

	fetch: function(key)
	{
		return this.manager[key] = jQuery.parseJSON(
		 $.ajax({
		 	async: false,
	  		url: this.obj.fetchUrl + '/' + key + '/' + this.obj.channel_id
		}).responseText);
	},

	key: function(key)
	{
		if(key)
			this.index = key;

		return this.index;
	}
});

