social = {
  currentChoice: null,
  choiceCache: {},
  choices: [],
  newChoice: null,
  inTransition: false,
  
  changeChoice: function(newChoice, direction){
    $.log('changeChoice() - '+ social.choices[newChoice])
    $("#"+social.currentChoice).fadeOut()
    $("#"+social.choices[newChoice]).fadeIn()
    social.currentChoice = social.choices[newChoice]
    social.newChoice = null
  },
  
  flickr: {
    init: function(){
      if($('#flickr').length != 0){
        $("#refreshLink,#photo").click(social.flickr.getImage)
        social.flickr.getImage()
      }
      $("#refreshPhoto").show()
    },
    getImage: function(e){
      if(e){ e.preventDefault() }
      $('#photo').fadeOut()
      $.getJSON('/api/flickr/', social.flickr.receivedImage)
    },
    receivedImage: function(d, s){
      var p = $('#photo')
      p.empty()
      $("<img>").attr('src', d.src).appendTo(p).load(function(){ $("#photo").fadeIn() })
      $("<p><strong>"+d.title+"</strong></p>").appendTo(p)
      $("#photo img").wrap("<a href=\""+d.url+"\" target=\"_blank\" title=\"View this image on flickr\"></a>")
    }
  },
  
  init: function(){
    // populate the social choices
    $("div.social").each(function(){
      social.choices[social.choices.length] = this.getAttribute('id')
      if($(this).css("display") == "block"){ social.currentChoice = this.getAttribute("id") }
    })
  },
  
  left: function(){
    var ix = social.choices.indexOf(social.currentChoice)
    if(ix > 0){ ix-- }
    else { ix = social.choices.length - 1 }
    // window.console.log("left to "+ ix)
    social.changeChoice(ix, "left")
  },
  
  right: function(){
    var ix = social.choices.indexOf(social.currentChoice)
    if(ix < (social.choices.length - 1)){ ix++ }
    else { ix = 0 }
    // window.console.log("right to "+ ix)
    social.changeChoice(ix, "right")
  }
}
function initFlickrLinks(e){
  if($(this).attr("checked") == true){
    window.console.log('test')
    $("#flickr_badge_wrapper a").attr('target', '_blank')
  }
  else {
    $("#flickr_badge_wrapper a").attr('target', '_self')
  }
}
$(document).ready(function(){
  if($(".social")){
    social.init()
    social.flickr.init()
    $("#socialLeft > a").click(function(e){
      e.preventDefault()
      social.left()
    })
    $("#socialRight > a").click(function(e){
      e.preventDefault()
      social.right()
    })
  }
  $("#newWindows").change(initFlickrLinks)
  initFlickrLinks()
})