var iCounter = 0;
var sGoNoGo = "stop";
var bStopForced = false;

var tweetCounts=1;
function TweetsLoaded()
{
  var xx = $("[class='twtr-tweet']");
  if (xx.length==0)
  {
    setTimeout('TweetsLoaded()',100);
  }
  else
  {
    var data = {
      action: 'saveWidget',
      profileName: window.profileName,
      content: $("#twtr-widget-1").html()
    };
    $.post('/twitterWidget.php', data);
  }
}

function analyzeResult(data){
  if (data[0].content) {
    jQuery("#twtr-widget-1").html(data[0].content);
  }
  else {
    do_twitter_widget(window.profileName);
  }
}

function get_twitter_widget(profileName){
  window.profileName = profileName;
  $.getJSON("/twitterWidget.php?author_login="+profileName, analyzeResult);
}

function do_twitter_widget(profileName)
{
    new TWTR.Widget({
      version: 2,
      type: 'profile',
      id: 'twtr-widget-1',
      rpp: 20,
      interval: 6000,
      width: 272,
      height: 300,
      theme: {
        shell: {
          background: '#c9c9c9',
          color: '#ffffff'
        },
        tweets: {
          background: '#969696',
          color: '#ffffff',
          links: '#000000'
        }
      },
      features: {
        scrollbar: true,
        loop: false,
        live: false,
        hashtags: true,
        timestamp: true,
        avatars: true,
        behavior: 'all'
      },
      ready: function() {
        // when done rendering...
        setTimeout('TweetsLoaded()',100);
      }
    }).render()
     .setUser(profileName)
     .start();
}

function addItem(quantity, maxLines)
{
  if (sGoNoGo == "stop")
  {
    return;
  }

  var cellCounter = iCounter;
  var rowCounter = Math.floor(cellCounter/quantity);
  var rowToHideCounter = rowCounter - maxLines;
  var cellID = "cell" + cellCounter;
  var rowID = "row" + rowCounter;
  var rowToHideID = "row" + rowToHideCounter;

  var cellWidth = 84;
  var cellHeight = 84;

  iCounter++;

  if (cellCounter/quantity == Math.floor(cellCounter/quantity)) {
    // New row
    var rowtext = '<ul class="thumb" id="'+rowID+'" style="width:'+cellWidth*quantity+'px;height=0px;">';
    if (rowCounter > 0) {
      rowtext = '</ul>'+rowtext;
    }
    jQuery("#animContainer").prepend(rowtext);
    jQuery("#"+rowID).animate(
    {
      height: cellHeight + 'px' //height: '+=' + cellHeight + 'px'
    }, 500);

    // For IE7+
    $("#"+rowID).css(
    {  'overflow': 'visible'
    });
  }

  $.getJSON("/feed.php?position="+cellCounter, function(data)
  {
    var url = data[0].image;
    var text = data[0].title;
    text = text.replace(/\"/g,"''"); // To replace all occurences, use .replace("find","replace"), and change "find" to /find/g
    var author_uri = data[0].author_uri;
    var author_name = author_uri.substring(author_uri.lastIndexOf("/")+1);

    var boxtext = '<li id="'+cellID+'"><a href="javascript:get_twitter_widget(\''+author_name+'\')"><img alt="'+text+'" src="/thumb.php?src='+encodeURIComponent(url)+'"></a></li>';
    jQuery("#"+rowID).append(boxtext);
    jQuery("#"+cellID).toggle();

    jQuery("#"+cellID).delay(0).fadeIn(500, function()
    {
      // Do nothing
    });

    $("#"+cellID).Zoomer({speedView:200,speedRemove:400,altAnim:true,speedTitle:0,debug:false});
  });

  if (rowToHideCounter >= 0)
  {
    jQuery("#"+rowToHideID).animate(
    {
      height: '0px'
    }, 500, function()
    {
      jQuery("#"+rowToHideID).remove();
    });
  }
}

function start_slideshow()
{
  if (sGoNoGo == "stop" && !bStopForced)
  {
    sGoNoGo = "go";
  }
}

function stop_slideshow()
{
  if (sGoNoGo == "go" && !bStopForced)
  {
    sGoNoGo = "stop";
  }
}

function togglePlayPause()
{
  if (sGoNoGo == "go")
  {
    stop_slideshow();
    bStopForced=true;
    $("#playpause").attr("src","/images/interface/play.gif");
  }
  else
  {
    bStopForced=false;
    start_slideshow();
    $("#playpause").attr("src","/images/interface/pause.gif");
  }
}

(function($)
{
  $.fn.Zoomer=function(b)
  {
    var c=$.extend(
    {
      speedView:200,     //The speed of the main animation
      speedRemove:400,   //The speed at which we remove the main animation
      altAnim:false,     //Whether we animate our alt tags or not
      speedTitle:400,    //The speed of our title animation
      debug:false        //A quick switch between debug mode
    },b);
    var d=$.extend(c,b);

    function e(s)
    {
      if(typeof console!="undefined"&&typeof console.debug!="undefined")
      {
        console.log(s)
      }
      else
      {
        alert(s)
      }
    }

    if(d.speedView==undefined||d.speedRemove==undefined||d.altAnim==undefined||d.speedTitle==undefined)
    {
      e('speedView: '+d.speedView);
      e('speedRemove: '+d.speedRemove);
      e('altAnim: '+d.altAnim);
      e('speedTitle: '+d.speedTitle);
      return false
    }

    if(d.debug==undefined)
    {
      e('speedView: '+d.speedView);
      e('speedRemove: '+d.speedRemove);
      e('altAnim: '+d.altAnim);
      e('speedTitle: '+d.speedTitle);
      return false
    }

    if(typeof d.speedView!="undefined"||typeof d.speedRemove!="undefined"||typeof d.altAnim!="undefined"||typeof d.speedTitle!="undefined")
    {
      if(d.debug==true)
      {
        e('speedView: '+d.speedView);
        e('speedRemove: '+d.speedRemove);
        e('altAnim: '+d.altAnim);
        e('speedTitle: '+d.speedTitle)
      }

      $(this).hover(function()
      {
        stop_slideshow();

        $(this).css(
        {
          'z-index':'10'
        });

        $(this).find('img').addClass("hover").stop().animate(
        {
        /*
          marginTop:'-110px',
          marginLeft:'-110px',
          top:'50%',
          left:'50%',
          */
          top:'-53px',
          left:'-50px',
          width:'175px',
          height:'181px'/*,
          padding:'20px'*/
        }
        ,d.speedView);

        if(d.altAnim==true)
        {
          var a=$(this).find("img").attr("alt");
          var text=a;
          if(a.length!=0)
          {
            $(this).prepend('<div class="title"><p>'+text+'</p></div>');
            $('.title').animate(
            {
              /*marginLeft:'-49px',
              marginTop:'49px'*/
              left:'-47px',
              top:'51px'
            }
            ,d.speedTitle).css(
            {
              'z-index':'10',
              'position':'absolute',
              'float':'left'
            })
          }
        }
      },function()
      {
        start_slideshow();

        $(this).css(
        {
          'z-index':'0'
        });

        $(this).find('img').removeClass("hover").stop().animate(
        {
          //marginTop:'0',
          //marginLeft:'0',
          top:'0',
          left:'0',
          width:'76px',
          height:'76px'
          //,padding:'2px'
        },d.speedRemove);

        $(this).find('.title').remove();
      })
    }
  }
})(jQuery);

function genHex()
{
  colors = new Array(14);
  colors[0]="0";
  colors[1]="1";
  colors[2]="2";
  colors[3]="3";
  colors[4]="4";
  colors[5]="5";
  colors[5]="6";
  colors[6]="7";
  colors[7]="8";
  colors[8]="9";
  colors[9]="a";
  colors[10]="b";
  colors[11]="c";
  colors[12]="d";
  colors[13]="e";
  colors[14]="f";

  digit = new Array(5);
  color="";
  for (i=0;i<6;i++)
  {
    digit[i]=colors[Math.round(Math.random()*14)];
    color = color+digit[i];
  }

  return color;
}

