var Season =
{
	init: function()
	{
		$$('div.guide div.season div.title a').each
		(
			function (a, index)
			{
				var episodes = a.up('div.title').next('div.episodes');
				
				if (index > 1)
				{
					a.addClassName('hidden');
					episodes.hide();
				}
				
				a.setAttribute('href', 'javascript:void(0);');
				a.observe('click', Season.toggle.bind(episodes));
			}
		);
	},
	
	toggle: function()
	{
		var a = this.previous('div.title').down('a');
		
		if (this.visible())
		{
			a.addClassName('hidden');
			this.hide();
			return;
		}
		
		a.removeClassName('hidden');
		this.show();
	}
}

Event.observe(window, 'load', Season.init);;function Cookie(name, duration, path, domain, secure)
{
   this.affix = "";
   
   if( duration )
   {   	  
      var date = new Date();
	  var curTime = new Date().getTime();

	  date.setTime(curTime + (1000 * 60 * duration));
	  this.affix = "; expires=" + date.toGMTString();
   }
   
   if( path )
   {
      this.affix += "; path=" + path;
   }
   
   if( domain )
   {
      this.affix += "; domain=" + domain;
   }

   if( secure )
   {
      this.affix += "; secure=" + secure;
   }
   
      
   function getValue()
   {
      var m = document.cookie.match(new RegExp("(" + name + "=[^;]*)(;|$)"));

      return m ? m[1] : null;   
   }
   
   this.cookieExists = function()
   {
      return getValue() ? true : false;
   }
      
   this.expire = function()
   {
      var date = new Date();
	  date.setFullYear(date.getYear() - 1);
	  document.cookie=name + "=noop; expires=" + date.toGMTString(); 
   }
        
   this.setSubValue = function(key, value)
   {
      var ck = getValue();

      if( /[;, ]/.test(value) )
      {
         //Mac IE doesn't support encodeURI
		 value = window.encodeURI ? encodeURI(value) : escape(value);
      }

      
      if( value )
      {
         var attrPair = "@" + key + value;

         if( ck )
         {
             if( new RegExp("@" + key).test(ck) )
	         {
		        document.cookie =
				   ck.replace(new RegExp("@" + key + "[^@;]*"), attrPair) + this.affix;
	         }
	         else
	         {
		        document.cookie =
				   ck.replace(new RegExp("(" + name + "=[^;]*)(;|$)"), "$1" + attrPair) + this.affix;
	         }
         }
         else
         {
	        document.cookie = name + "=" + attrPair + this.affix;
         }
      }
      else
      {      
	     if( new RegExp("@" + key).test(ck) )
	     {
	        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), "") + this.affix;
	     }
      }
   }

      
   this.getSubValue = function(key)
   {
      var ck = getValue();

      if( ck )
      {
         var m = ck.match(new RegExp("@" + key + "([^@;]*)"));

	     if( m )
	     {
	        var value = m[1];

	        if( value )
	        { 
	           //Mac IE doesn't support decodeURI
			   return window.decodeURI ? decodeURI(value) : unescape(value);
	        }
	     }
      }
   }
};var Watched =
{
	init: function()
	{
		Watched.Cookie = new Cookie('history', 31556926, '/');
		
		if ($('episodeIdToSet'))
		{
			setTimeout(Watched.set.bind($('episodeIdToSet')), 600000);
		}
		
		if (!Watched.Cookie.getSubValue('seen'))
		{
			return;
		}
		
		var ids = Watched.Cookie.getSubValue('seen');
		
		$$('.showItem').each
		(
			function (item)
			{
				var regExp = new RegExp('~' + item.getAttribute('episodeId') + '~', 'i');
				
				if (ids.match(regExp))
				{
					item.down('.watched').show();
				}
			}
		);
	},
	
	set: function()
	{
		var ids = (Watched.Cookie.getSubValue('seen')) ? Watched.Cookie.getSubValue('seen') : '';

		ids = ids + '~' + this.getValue() + '~';
		Watched.Cookie.setSubValue('seen', ids);
	},
	
	unset: function(episodeId, self)
	{
		self.up('.watched').remove();
		
		var ids 			= Watched.Cookie.getSubValue('seen');
		var regExpReplace 	= new RegExp('~' + episodeId + '~', 'i');
		
		Watched.Cookie.setSubValue('seen', ids.replace(regExpReplace, ''));
	}
}

Event.observe(window, 'load', Watched.init);;
