(function(jQuery){
 // We override the animation for all of these color styles
 jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
 jQuery.fx.step[attr] = function(fx){
  if ( fx.state == 0 ) {
   fx.start = getColor( fx.elem, attr );
   fx.end = getRGB( fx.end );
  }
  fx.elem.style[attr] = "rgb(" + [
   Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
   Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
   Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
  ].join(",") + ")";
 }
});

 function getRGB(color) {
  var result;

  if ( color && color.constructor == Array && color.length == 3 ) return color;

  if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
   return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

  if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
   return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

  if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
   return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

  if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
   return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

  return colors[jQuery.trim(color).toLowerCase()];
 }
 
 function getColor(elem, attr) {
  var color;

  do {
   color = jQuery.curCSS(elem, attr);

   if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
    break; 

   attr = "backgroundColor";
  } while ( elem = elem.parentNode );

  return getRGB(color);
 };
 
 var colors = {
  aqua:[0,255,255],
  azure:[240,255,255],
  beige:[245,245,220],
  black:[0,0,0],
  blue:[0,0,255],
  brown:[165,42,42],
  cyan:[0,255,255],
  darkblue:[0,0,139],
  darkcyan:[0,139,139],
  darkgrey:[169,169,169],
  darkgreen:[0,100,0],
  darkkhaki:[189,183,107],
  darkmagenta:[139,0,139],
  darkolivegreen:[85,107,47],
  darkorange:[255,140,0],
  darkorchid:[153,50,204],
  darkred:[139,0,0],
  darksalmon:[233,150,122],
  darkviolet:[148,0,211],
  fuchsia:[255,0,255],
  gold:[255,215,0],
  green:[0,128,0],
  indigo:[75,0,130],
  khaki:[240,230,140],
  lightblue:[173,216,230],
  lightcyan:[224,255,255],
  lightgreen:[144,238,144],
  lightgrey:[211,211,211],
  lightpink:[255,182,193],
  lightyellow:[255,255,224],
  lime:[0,255,0],
  magenta:[255,0,255],
  maroon:[128,0,0],
  navy:[0,0,128],
  olive:[128,128,0],
  orange:[255,165,0],
  pink:[255,192,203],
  purple:[128,0,128],
  violet:[128,0,128],
  red:[255,0,0],
  silver:[192,192,192],
  white:[255,255,255],
  yellow:[255,255,0]
 };
 
})(jQuery);

/**
 * Copyright (c) 2005 - 2009, James Auldridge
 * All rights reserved.
 *
 * Licensed under the BSD, MIT, and GPL (your choice!) Licenses:
 *  http://code.google.com/p/cookies/wiki/License
 *
 * Version 2.0.1
 */
var jaaulde = window.jaaulde || {};
jaaulde.utils = jaaulde.utils || {};
jaaulde.utils.cookies = (function()
{
 var cookies = [];

 var defaultOptions = {
  hoursToLive: null,
  path: '/',
  domain:  null,
  secure: false
 };
 var resolveOptions = function(options)
 {
  var returnValue;

  if(typeof options !== 'object' || options === null)
  {
   returnValue = defaultOptions;
  }
  else
  {
   returnValue = {
    hoursToLive: (typeof options.hoursToLive === 'number' && options.hoursToLive > 0 ? options.hoursToLive : defaultOptions.hoursToLive),
    path: (typeof options.path === 'string' && options.path != '' ? options.path : defaultOptions.path),
    domain: (typeof options.domain === 'string' && options.domain != '' ? options.domain : defaultOptions.domain),
    secure: (typeof options.secure === 'boolean' && options.secure ? options.secure : defaultOptions.secure)
   };
  }

  return returnValue;
 };
 var assembleOptionsString = function(options)
 {
  options = resolveOptions(options);

  return (
   (typeof options.hoursToLive == 'number' ? '; expires='+expiresGMTString(options.hoursToLive) : '') +
   '; path=' + options.path +
   (typeof options.domain === 'string' ? '; domain=' + options.domain : '') +
   (options.secure === true ? '; secure' : '')
  );
 };
 var expiresGMTString = function(hoursToLive)
 {
  var dateObject = new Date();
  dateObject.setTime(dateObject.getTime() + (hoursToLive*60*60*1000));

  return dateObject.toGMTString();
 };
 var splitCookies = function()
 {
  cookies = [];
  var pair, name, separated = document.cookie.split(';');
  for(var i = 0; i < separated.length; i++)
  {
   pair = separated[i].split('=');
   name = pair[0].replace(/^\s*/, '').replace(/\s*$/, '');
   value = decodeURIComponent(pair[1]);
   cookies[name] = value;
  }
  return cookies;
 };

 var constructor = function(){};
 
 constructor.prototype.get = function(cookieName)
 {
  var returnValue;
  
  splitCookies();
  
  if(typeof cookieName === 'string')
  {
   returnValue = (typeof cookies[cookieName] !== 'undefined') ? cookies[cookieName] : null;
  }
  else if(typeof cookieName === 'object' && cookieName !== null)
  {
   returnValue = [];
   for(var item in cookieName)
   {
    returnValue[cookieName[item]] = (typeof cookies[cookieName[item]] !== 'undefined') ? cookies[cookieName[item]] : null;
   }
  }
  else
  {
   returnValue = cookies;
  }

  return returnValue;
 };
 constructor.prototype.set = function(cookieName, value, options) //hoursToLive, path, domain, secure
 {
  if(typeof value === 'undefined' || value === null)
  {
   if(typeof options !== 'object' || options === null)
   {
    options = {};
   }
   value = '';
   options.hoursToLive = -8760;
  }
  
  var optionsString = assembleOptionsString(options);

  document.cookie = cookieName + '=' + encodeURIComponent(value) + optionsString;
 };
 constructor.prototype.del = function(cookieName, options) //path, domain
 {
  if(typeof options !== 'object' || options === null)
  {
   options = {};
  }
  this.set(cookieName, null, options);
 };
 constructor.prototype.test = function()
 {
  var returnValue = false, testName = 'cT', testValue = 'data';

  this.set(testName, testValue);

  if(this.get(testName) == testValue)
  {
   this.del(testName);
   returnValue = true;
  }

  return returnValue;
 };
 constructor.prototype.setOptions = function(options)
 {
  if(typeof options !== 'object')
  {
   options = null;
  }

  defaultOptions = resolveOptions(options);
 }

 return new constructor();
})();


(function()
{
 if(typeof jQuery !== 'undefined' )
 {
  jQuery.cookies = jaaulde.utils.cookies;

  var extensions = {
   cookify: function(options)
   {
    return this.each(function()
    {
     var name = '', value = '', nameAttrs = ['name', 'id'], iteration = 0, inputType;

     while(iteration < nameAttrs.length && (typeof name !== 'string' || name === ''))
     {
      name = jQuery(this).attr(nameAttrs[iteration]);
      iteration++;
     }

     if(typeof name === 'string' || name !== '')
     {
      inputType = jQuery(this).attr('type').toLowerCase();
      if(inputType !== 'radio' && inputType !== 'checkbox')
      {
       value = jQuery(this).attr('value');
       if(typeof value !== 'string' || value === '')
       {
        value = null;
       }
       jQuery.cookies.set(name, value, options);
      }
     }

     iteration = 0;
    });
   },
   cookieFill: function()
   {
    return this.each(function()
    {
     var name = '', value, nameAttrs = ['name', 'id'], iteration = 0, nodeType;

     while(iteration < nameAttrs.length && (typeof name !== 'string' || name === ''))
     {
      name = jQuery(this).attr(nameAttrs[iteration]);
      iteration++;
     }

     if(typeof name === 'string' && name !== '')
     {
      value = jQuery.cookies.get(name);
      if(value !== null)
      {
       nodeType = this.nodeName.toLowerCase();
       if(nodeType === 'input' || nodeType === 'textarea')
       {
         jQuery(this).attr('value', value);
       }
       else
       {
        jQuery(this).html(value);
       }
      }
     }

     iteration = 0;
    });
   },
   cookieBind: function(options)
   {
    return this.each(function(){
     $(this).cookieFill().change(function()
     {
      $(this).cookify(options);
     });
    });
   }
  };

  jQuery.each(extensions, function(i)
  {
   jQuery.fn[i] = this;
  });
 }
})();

if (window.location.href.indexOf('twihoo.com')!=-1) {
 var months = new Array('january','february','march','april','may','june','july','august','september','october','november','december');
 var timezone = 0;
 var timelang = 'at';
} else {
 var months = new Array('января','февраля','марта','апреля','мая','июня','июля','августа','сентября','октября','ноября','декабря');
 var timezone = 10800;
 var timelang = 'в';
}

function getDateText(date) {
 var tDate = new Date((date+timezone) *1000);
 var cDate = new Date();
 return ' ' +
  ( ( (tDate.getDate() == cDate.getDate()) && (tDate.getMonth() == cDate.getMonth()) && (tDate.getYear() == cDate.getYear()) ) ? '' : ( tDate.getDate() +' '+ months[tDate.getMonth()] ) ) 
  + 
  ' ' + timelang + ' ' + twoDig(tDate.getHours()) + ':' + twoDig(tDate.getMinutes());
}

function twoDig(val) {
 return ( (val*1) < 10 ) ? '0' + val : val;
}

function correctPNG() {
 $("#sb").hide();
 var arVersion = navigator.appVersion.split("MSIE")
 var version = parseFloat(arVersion[1])
 if ((version >= 5.5) && (document.body.filters)) {
  for(var i=0; i<document.images.length; i++) {
   var img = document.images[i]
   var imgName = img.src.toUpperCase()
   if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
    var imgID = (img.id) ? "id='" + img.id + "' " : ""
    var imgClass = (img.className) ? "class='" + img.className + "' " : ""
    var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
    var imgStyle = "display:inline-block;" + img.style.cssText 
    if (img.align == "left") imgStyle = "float:left;" + imgStyle
    if (img.align == "right") imgStyle = "float:right;" + imgStyle
    if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
    var strNewHTML = "<span " + imgID + imgClass + imgTitle
    + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
    + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
    + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
    img.outerHTML = strNewHTML
    i = i-1
   }
  }
 }  
}

/* Illegal copying of this code prohibited by real patsan's law! */
function twihoo() {
 this.globalTop = 0;
 this.topic = 1;
 this.wait = 0;
 this.page = 0;
 this.pause = 0;
 this.blink = 1;
 this.speed = 3000;
 this.lenta = (window.location.href.indexOf('/all')==-1) ? 1 : 0;
 this.saytmt, this.movetmt;
 this.onlynew = 0;
 this.order = 0;
 this.lastid = 0;

 this.getTop = function() {
  t.globalTop = $(window).height();
  $.each($("li.screen"), function(i,item) {
   t.globalTop-=$(item).height()+40;
  });
  return t.globalTop;
 }
  
 this.say = function(topic){
  if (topic) {
   clearTimeout(t.saytmt);
   topic = (topic=='r') ? 1 : ( (topic=='g') ? 2 : 3);
   topic = topic;
  } else {
   topic = t.topic;
  }

  if ($("li").length < 40 || t.topic!=topic || t.page!=0 ) {
   t.wait = 1;
   var datacount = 0;
   if (!t.lenta) {
    $('#load').css('display','block');
    $('#next').css('display','none');
   }
   $.getJSON(feedUrl 
     + topic 
     + ( t.order ? '&order=asc' : '&order=desc' )
     + ( t.onlynew ? ('&since=' + t.lastid ) : '' ) 
     + ( (t.page!=0) ? ('&page='+t.page ) : '' ) 
     + '&' + Math.random(),
    function(data,textStatus){
     if (textStatus!='success') return;
     if (t.topic!=topic) $("li.headline").remove();
     t.topic=topic;
     $.each(data, function(i,item){
      if (navigator.userAgent.indexOf('Opera Mini')!=-1) {
       img = '<b>'+ item[2] +'</b>';
      } else {
       img = '<img src="'+ item[3] +'" border="0">';
      }
      $("<li>").attr("class","headline").html('<table width="100%"><tr><td width="60"><a href="http://twitter.com/'+ item[1] +'" target="_blank">'+ img +'</a></td><td>'+ item[4].replace(/<strong>/g,'<strong class="s'+ topic +'">') + '<br /><small><a href="http://twitter.com/'+ item[1] +'/status/'+ item[0] +'" target="_blank">'+ getDateText(item[5]) +'</a></small></td></tr></table>').hover(
       function () {
        $(this).css('border-color','#aaaaaa');
        if (t.pause==0) t.paused();
       },
       function () {
        $(this).css('border-color','transparent');
        if (t.pause==1) t.paused();
       }
      ).appendTo("#scrollup");
      datacount++;
      t.lastid = item[0];
     });
     data = null;
     t.wait = 0;
     if (!t.lenta) {
      $('#load').css('display','none');
      $('#next').css('display',(datacount==40) ? 'block' : 'none');
     }
   });
  }
  if (t.lenta) t.saytmt = setTimeout('t.say()',10000);
 };

 this.next = function() {
  t.page++;
  return t.say();
 }
 
 this.move = function() {
  clearTimeout(t.movetmt);
  if ($("li.headline").length && t.pause==0) {
   $("li.headline:first").attr("class","screen");
   $("#scrollup").animate( { top: t.getTop() }, { queue: false, duration: 700, complete:function(){ 
    if (t.globalTop < -100) {
     t.globalTop+=($("li.screen:first").height()+40); 
     $("li.screen:first").remove();
     $("#scrollup").css('top',t.globalTop+'px');
    };
   }});
  }
  t.movetmt = setTimeout('t.move()',t.speed);
 };

 this.sized = function() { 
  if (t.lenta) $("#monitor").height($(window).height()); 
 }

 this.paused = function() {
  if (t.pause==0) {
   $('li#sactive').attr("id","");
   if(!$.browser.msie) $('#sp').show();
   $('li.s0').attr("id","sactive");
   t.pause=1;
  } else {
   $('li.s0').attr("id","");
   if(!$.browser.msie) $('#sp').hide();
   $('li.'+((t.speed==6000) ? 's1' : ( (t.speed==3000) ? 's2' : 's3' ))).attr("id","sactive");
   t.pause=0;
  }
  return false;
 }
  
 this.changeFont = function(button) {
  if (button.id!="factive"){
   if ($("#factive")) $("#factive").attr("id","");
   $("#scrollup").animate( { fontSize: (button.className=='sm')?'100%':'140%'}, { queue: false, duration: 1000, complete:function(){ }});
   button.id="factive";
   $.cookies.set('f',button.className);
  }
 }

 this.pauseBlink = function() {
  if ( t.pause == 0 ) t.blink = 0;
  $("#sp").animate( { opacity: (t.blink==0) ? 0.3 : 0.7 }, { queue: false, duration: 1000, complete:function(){ t.blink = 1-t.blink; return t.pauseBlink() } });
 }
 
 this.changeBackground = function(button) {
  if (button.id!="cactive"){
   if ($("#cactive")) $("#cactive").attr("id","");
   
   $("#tw").animate( { color: (button.className=='wh')?'#000000':'#cccccc', backgroundColor: (button.className=='wh')?'#ffffff':'#000000' }, { queue: false, duration: 1000, complete:function(){ }});

//   $("#f > *, #sp").animate( { color: (button.className=='wh')?'#ffffff':'#000000', backgroundColor: (button.className=='wh')?'#000000':'#ffffff' }, { queue: false, duration: 1000, complete:function(){ }});
   $("#f > *").animate( { color: (button.className=='wh')?'#ffffff':'#000000', backgroundColor: (button.className=='wh')?'#000000':'#ffffff' }, { queue: false, duration: 1000, complete:function(){ }});

   if($.browser.msie) {
    if (button.className=="wh") $("#sb").show();
    else $("#sb").hide();
   } else $("#sb").animate( { opacity: (button.className=='wh')?1.0:0.0}, { queue: false, duration: 1000, complete:function(){ }});
   button.id="cactive";
   
   $.cookies.set('c',button.className);
  }
 }

 this.changeSpeed = function(button) {
  if (button.id!="sactive"){
   if ($("#sactive")) $("#sactive").attr("id","");

   if (button.className=="s0") {
   	t.paused();
   }
   else {
   	t.speed = (button.className=="s1") ? 6000 : ( (button.className=="s2") ? 3000 : 1500 );
    if(!$.browser.msie) $('#sp').hide();
    t.pause=0;
    $.cookies.set('s',button.className);
   }

   button.id="sactive";
  }
 }
 
 if (this.lenta) $("#scrollup").css('top',$(window).height()+'px');
 
 if (window.location.href.indexOf('/all')==-1) $("<div>").attr("id","sp").html('<img src="/i/pause.gif" height="284" width="284" />').appendTo("#monitor");

 $("#w > *").click(function(){if (this.id!="wactive" && t.wait==0){
  if ($("#wactive")) {
   $("#wactive").attr("class", ($("#wactive").attr("class") + "").replace(/a/,''));
   $("#wactive").attr("id","");
  }
  t.say(this.className.replace(/a/,''));
  if (this.className.indexOf('a')==-1) this.className+='a';
  this.id="wactive";
 }});
  
 $("#w > *").hover(
  function () {
   if (this.id!='wactive' && this.className.indexOf('a')==-1) this.className+='a';
  }, 
  function () {
   if (this.id!='wactive') this.className=this.className.replace(/a/,'');
  }
 );

 $("#f > *,#c > *,#s > *,p.p").hover(
  function () {
   if (this.id=="") this.id="border";
  },
  function () {
   if (this.id=="border") this.id="";
  }
 );
/*
 $("#scrollup").hover(
  function () {
   if (t.pause==0) t.paused();
  },
  function () {
   if (t.pause==1) t.paused();
  }
 );
*/
 $("#c > *").click(function(){return t.changeBackground(this)});

 $("#f > *").click(function() {return t.changeFont(this)});
 
 $("#s > *").click(function(){return t.changeSpeed(this)});

 $("p.p").click(function(){return t.paused()});
};

if (navigator.userAgent.indexOf('Opera Mini')!=-1 || navigator.userAgent.indexOf('mobile')!=-1 || navigator.userAgent.indexOf('Symbian')!=-1) {
 prj = feedUrl.split('p=')[1].split('&')[0];
 window.top.location="/m/?p=" + prj;
}

$(document).ready(function(){
 if (navigator.userAgent.indexOf('iPhone')!=-1) {
  $('#legend small, #legend div').hide();
  $('#legend ul, #legend p, #legend div#logo').css('display','block').css('width','150px').css('position','150px').css('margin','0px 0px 30px 0px').css('padding','0px');
  $('#monitor').css('left','20%').css('width','80%');
  $('ul#w').css('height','200px');
  $("#scrollup").css('font-size','200%');
 }

 t=new twihoo()

 if (window.location.href.indexOf('forest')!=-1) {
  t.onlynew = 1;
  t.order = 1;
 }

 if ($('#f').length && $.cookies.get('f') ) t.changeFont($('li.'+$.cookies.get('f')).get(0));
 if ($('#s').length && $.cookies.get('s') ) t.changeSpeed($('li.'+$.cookies.get('s')).get(0));
 if ($('#c').length && $.cookies.get('c') ) t.changeBackground($('li.'+$.cookies.get('c')).get(0));
 
 t.pauseBlink();
 t.sized();
 t.say();
 if (t.lenta) t.move();
});

if($.browser.msie) window.attachEvent("onload", correctPNG);

$(window).resize(function() {t.sized()});

$(window).keypress(function (e) {if (e.which==32) {return t.paused()}});
