// ==UserScript==
// @name           Show Flickr Contact Context
// @namespace      http://husk.org/code/
// @description    Show links to your Flickr contacts on photo pages
// @include        http://*flickr.com/photos/*/*
// @exclude        http://*flickr.com/photos/organize*
// ==/UserScript==

// Version 0.20
// Copyright (c) Paul Mison
// GPL licenced: http://www.gnu.org/copyleft/gpl.html

// main
window.console.log("running Flickr context script");
if (context()) {
  window.console.log("worth checking context");

  photo_id = window.page_photo_id;

  var listener = {
    flickr_photos_getContactsPhotos_onLoad: function(success, responseXML, responseText, params){
      try{
        var rsp = responseText.replace(/jsonFlickrApi\(/,'');
        rsp = eval('('+rsp);
        if(rsp.stat == 'ok') {
          window.console.log("got listener function");
          
          var i=0;
          var html = '';
          for(i=0;i<rsp.photos.photo.length;i++) {
            var photo = rsp.photos.photo[i];
            var id = rsp.photos.photo[i].id;
            if (id == photo_id) {
              window.console.log(id);
              context = "contacts"+window.global_nsid;

              var contextHTML = '<table cellspacing="0" cellpadding="0" border="0"> <tbody><tr> <td valign="bottom" class="LilIcon"><img width="18" height="25" id="nextprev_button_stream45864284@N00" class="nextprev_button" style="border: 0pt none ;" src="http://l.yimg.com/g/images/context_open.gif"/></td> <td colspan="2"> <h3 class="contextTitleOpen" id="contextTitle_stream45864284@N00"> <a id="contextLink_stream45864284@N00" class="currentContextLink" href="'
              contextHTML += '/photos/contacts/">Photos from your contacts</a> </h3> </td> </tr> <tr class="contextThumbsRow" id="nextprev_tr_stream45864284@N00"> <td valign="bottom" class="context_crap_td" rowspan="2"><img width="18" height="104" alt="" class="crap" src="http://l.yimg.com/g/images/context_crap.gif"/></td> <td><div class="nextprev_contextThumbsDiv" id="nextprev_div_stream45864284@N00"><div class="contextThumbsMoverWrapperDiv"><div id="nextprev_mover_div_stream45864284@N00" class="contextThumbsMoverDiv">'
              
              // left is back, so we increase the index; if you can't increase it, "last photo"
              if (i == (rsp.photos.photo.length)-1) {
                contextHTML += '<a style="position: absolute; left: 0px;"><img width="75" height="75" alt="" src="http://l.yimg.com/g/images/en-us/placeholder_last_photo.gif" class="nextprev_thumb" id="nextprev_thumb_stream45864284@N002929539007" style="visibility: visible;"/></a>';
              } else {
                prev = rsp.photos.photo[i+1];
                contextHTML += '<a style="position: absolute; left: 0px;" href="/photos/'+prev.owner+'/'+prev.id+'/?contact=in" class="contextThumbLink"><img width="75" height="75" alt="" title="Go to the previous item from your contacts" src="http://farm'+prev.farm+'.static.flickr.com/'+prev.server+'/'+prev.id+'_'+prev.secret+'_s.jpg" class="nextprev_thumb" id="nextprev_thumb_'+context+'" style="visibility: visible;"/></a>';
              }
              
              if (i == 0) {
                contextHTML += '<a style="position: absolute; left: 78px;"><img width="75" height="75" alt="" src="http://l.yimg.com/g/images/en-us/placeholder_last_photo.gif" class="nextprev_thumb" id="nextprev_thumb_'+context+'" style="visibility: visible;"/></a>';
              } else {
                next = rsp.photos.photo[i-1];
                contextHTML += '<a style="position: absolute; left: 78px;" href="/photos/'+next.owner+'/'+next.id+'/?contact=in" class="contextThumbLink"><img width="75" height="75" alt="" title="Go to the next item from your contacts" src="http://farm'+next.farm+'.static.flickr.com/'+next.server+'/'+next.id+'_'+next.secret+'_s.jpg" class="nextprev_thumb" id="nextprev_thumb_'+context+'" style="visibility: visible;"/></a>';
              }
                
              //  <a class="contextThumbLink" style="position: absolute; left: 78px;"><img width="75" height="75" alt="" src="http://l.yimg.com/g/images/en-us/placeholder_last_photo.gif" class="nextprev_thumb" id="nextprev_thumb_stream45864284@N00" style="visibility: visible;"/></a>'
                
              // contextHTML += '</div></div><div id="nextprev_arrows_div_stream45864284@N00" class="nextprev_contextThumbsArrowsDiv"><img width="37" height="10" border="0" title="" src="http://l.yimg.com/g/images/context_arrow_more_left_out.gif?v1" class="nextprev_arrows_img_prev" id="nextprev_arrows_prev_img_stream45864284@N00" style="cursor: pointer; visibility: visible;" alt=""/><a class="contextThumbLink" href="/photos/" style="cursor: pointer;"><img width="79" height="10" border="0" title="" src="http://l.yimg.com/g/images/en-us/context_view_page_out.gif?v1" class="nextprev_view_page_img" id="nextprev_view_page_img_stream45864284@N00" style="visibility: visible;" alt=""/></a><img width="37" height="10" border="0" title="" src="http://l.yimg.com/g/images/context_arrow_more_right_out.gif?v1" class="nextprev_arrows_img_next" id="nextprev_arrows_next_img_stream45864284@N00" style="cursor: pointer;" alt=""/></div></div>'
              contextHTML += '</div></div><div id="nextprev_arrows_div_'+context+'" class="nextprev_contextThumbsArrowsDiv"></div></div>'
              contextHTML += '</td> <td> '
              contextHTML += '<div class="contextInfo"> <div id="contextInfoInner_'+context+'" class="contextInfoInner"> <div id="photosNum_'+context+'" class="photosNum"><!-- contacts --></div> <div id="photosText_'+context+'" class="photosText"></div> </div> </div>'
              contextHTML += '</td> </tr>'
              contextHTML += '<tr id="nextprev_crumbs_tr_'+context+'"> <td colspan="2"><div class="contextCrumbsDiv"/></td> </tr> </tbody></table>';

              var hook = document.getElementById('otherContexts_div');
              hook.innerHTML += contextHTML;
            }
          }
        }
      } catch (e) {
        window.console.log("error: "+responseText);
        window.console.log(e);
      }
    }
  };

  window.F.API.callMethod('flickr.photos.getContactsPhotos', {
    count: 48,
    extras: '',
    single_photo:1,
    format: 'json',
  }, listener);

  window.console.log("done ajax call");
}

function context() {
  r = document.referrer;
  regex = 'flickr.com/photos/friends'
  if (r.match(regex)) {
    return true;
  }
  
  if (get_param('contact')) {
    return true;
  }

  return false;
}

function get_param(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

