/*
 * Social Limit - Only the social you care about.
 *
 * Enables your site to know which social bookmarking badges to display to your
 * visitors. It tells you all social sites the user has gone to, or you can 
 * query for a specific one.
 * 
 * For example:
 * 
 *    var sl = SocialHistory();
 *    alert( sl.doesVisit("Digg") ); // Returns true/false, -1 if unknown.
 *    var listOfVisitedSites = sl.visitedSites();
 *    var checkedSites = sl.checkedSites();
 *
 * If you want to add more sites to check, you can pass that in as a dictionary
 * to History:
 *
 *    var more = { "Humanized": "http://humanized.com",
 *                 "Azarask.in": ["http://azarask.in", "http://azarask.in/blog"]
 *               };
 *    var sl = SocialHistory(more);
 *    alert( sl.doesVisit("Humanized") );
 *
 * For a list of built-in sites, see the sites variable below.
 *
 * Copyright (c) 2008 Aza Raskin (http://azarask.in/blog)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

var SocialHistory = function( moreSites ){

  var sites = {
    "Digg": ["http://digg.com", "http://digg.com/login"],
    "LinkedIn": ["http://www.linkedin.com", "https://www.linkedin.com/secure/login"],
    "Scribd": ["http://www.scribd.com"],
    "hi5": ["http://www.hi5.com"],
    "GetSatisfaction": ["http://www.getsatisfaction.com"],
    "Twitter": ["http://twitter.com"],
    "CastTV": ["http://www.casttv.com"],
    "Mugshot": ["http://mugshot.org"],
    "Naymz": ["http://www.naymz.com"],
    "Yelp": ["http://www.yelp.com"],
    "Yonja": ["http://www.yonja.com"],
    "Bookmooch": ["http://www.bookmooch.com"],
    "Plurk": ["http://www.plurk.com"],
    "Youtube": ["http://www.youtube.com"],
    "Upcoming": ["http://upcoming.yahoo.com"],
    "Tribe": ["http://people.tribe.net"],
    "Photoshop": ["http://www.photoshop.com"],
    "YouNoodle": ["http://www.younoodle.com"],
    "Zivity": ["http://beta.zivity.com"],
    "Rethos": ["http://www.rethos.com"],
    "Dopplr": ["http://www.dopplr.com"],
    "Friendfeed": ["http://www.friendfeed.com"],
    "Popsugar": ["http://www.teamsugar.com"],
    "a.viary": ["http://a.viary.com"],
    "Etsy": ["http://www.etsy.com"],
    "Kiva": ["http://www.kiva.org"],
    "Consumerist": ["http://www.consumerist.com"],
    "Ziki": ["http://www.zkiki.com"],
    "Tumblr": ["http://www.tumblr.com"],
    "Bebo": ["http://www.bebo.com"],
    "43Things": ["http://www.43things.com"],
    "Flickr": ["http://www.flickr.com"],
    "Pownce": ["http://www.pownce.com"],
    "eBay": ["http://www.ebay.com"],
    "PeekYou": ["http://www.peekyou.com"],
    "Rapleaf": ["http://www.rapleaf.com"],
    "Wesabe": ["http://www.wesabe.com"],
    "Spock": ["http://www.spock.com"],
    "StumbleUpon": ["http://stumbleupon.com"],
    //"Facebook": ["http://facebook.com/home.php", "http://facebook.com", "https://login.facebook.com/login.php"],
    "Del.icio.us": ["https://secure.del.icio.us/login", "http://del.icio.us/"],
    "Last.fm": ["http://www.last.fm/", "https://www.last.fm/login/"],
    "Mixx": ["https://www.mixx.com/login/dual", "http://www.mixx.com"],
    "Google Bookmarks": ["http://www.google.com/bookmarks", "http://www.google.com/ig/add?moduleurl=bookmarks.xml&hl=en"]
  };
  
  for( var site in moreSites ) {
    // If we don't have the site, create the URL list.
    if( typeof( sites[site] ) == "undefined" ) sites[site] = [];
    
    // If the value is string, just push that onto the URL list.
    if( typeof( moreSites[site] ) == "string" )
      sites[site].push( moreSites[site] );
    else
      sites[site] = sites[site].concat( moreSites[site] );
  }
  
  var visited = {};

  function getStyle(el, scopeDoc,styleProp) {
    if (el.currentStyle)
      var y = el.currentStyle[styleProp];
    else if (window.getComputedStyle)
      var y = scopeDoc.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
    return y;
  }
  
  function remove( el ) {
    el.parentNode.removeChild( el );
  }
  
  // Code inspired by:
  // bindzus.wordpress.com/2007/12/24/adding-dynamic-contents-to-iframes
  function createIframe() {
    var iframe = document.createElement("iframe");
    iframe.style.position = "absolute";
    iframe.style.visibility = "hidden";
    iframe.style.height = "0";

    document.body.appendChild(iframe);

    // Firefox, Opera
    if(iframe.contentDocument) iframe.doc = iframe.contentDocument;
    // Internet Explorer
    else if(iframe.contentWindow) iframe.doc = iframe.contentWindow.document;

    // Magic: Force creation of the body (which is null by default in IE).
    // Also force the styles of visited/not-visted links.
    iframe.doc.open();
  	iframe.doc.write('<style>');
  	iframe.doc.write("a{color: #000000; display:none;}");  	
  	iframe.doc.write("a:visited {color: #FF0000; display:inline;}");  	
  	iframe.doc.write('</style>');
    iframe.doc.close();
    
    // Return the iframe: iframe.doc contains the iframe.
    return iframe;
  }  

  var iframe = createIframe();
  
  function embedLinkInIframe( href, text ) {
    var a = iframe.doc.createElement("a");
    a.href = urls[i];
    a.innerHTML = site;
    iframe.doc.body.appendChild( a );
    //console.log( a.href );
  }
  
  for( var site in sites ) {
    var urls = sites[site];
    for( var i=0; i<urls.length; i++ ) {
      // You have to create elements in the scope of the iframe for IE.
      embedLinkInIframe( urls[i], site );
      
      // Automatically try variations of the URLS with and without the "www"
      if( urls[i].match(/www\./) ){
        var sansWWW = urls[i].replace(/www\./, "");
        embedLinkInIframe( sansWWW, site );
      } else {
        // 2 = 1 for length of string + 1 for slice offset
        var httpLen = urls[i].indexOf("//") + 2;
        var withWWW = urls[i].substring(0, httpLen ) + "www." + urls[i].substring( httpLen );
      }
      
    }
  }
    
  var links = iframe.doc.body.childNodes;
  for( var i=0; i<links.length; i++) {
    // Handle both Firefox/Safari, and IE (respectively)
    var displayValue = getStyle(links[i], iframe.doc, "display");
    var didVisit = displayValue != "none";
      
    if( didVisit ){
      visited[ links[i].innerHTML ] = true;
    }
  }
  
  //remove( iframe );
  
  return new (function(){
    var usedSites = [];
    for( var site in visited ){
      usedSites.push( site );
    }
    
    // Return an array of visited sites.
    this.visitedSites = function() {
      return usedSites;
    }
    
    // Return true/false. If we didn't check the site, return -1.
    this.doesVisit = function( site ) {
      if( typeof( sites[site] ) == "undefined" )
        return -1;
      return typeof( visited[site] ) != "undefined";
    }
    
    var checkedSites = [];
    for( var site in sites ){
      checkedSites.push( site );
    }
    // Return a list of the sites checked.
    this.checkedSites = function(){
      return checkedSites;
    }
  })();
}


var mapSites = [];
   mapSites["Digg"] = "http://digg.com/users/MaiaB";
   mapSites["LinkedIn"] = "http://www.linkedin.com/pub/4/221/310";
   mapSites["Scribd"] = "http://www.scribd.com/people/view/519467-maiabittner";
   mapSites["hi5"] = "http://maiabittner.hi5.com";
   mapSites["GetSatisfaction"] = "http://getsatisfaction.com/people/maia";
   mapSites["Twitter"] = "http://twitter.com/maiab";
   mapSites["CastTV"] = "http://www.casttv.com/users/maia/favorites";
   mapSites["Mugshot"] = "http://mugshot.org/person?who=G3RkVVLTlrq911";
   mapSites["Naymz"] = "http://www.naymz.com/search/maia/bittner/1975714";
   mapSites["Yelp"] = "http://maiab.yelp.com";
   mapSites["Yonja"] = "http://www.yonja.com/maia";
   mapSites["Bookmooch"] = "http://www.bookmooch.com/m/bio/maiabittner";
   mapSites["Plurk"] = "http://www.plurk.com/user/maia";
   mapSites["Youtube"] = "http://www.youtube.com/user/maiabittner";
   mapSites["Upcoming"] = "http://upcoming.yahoo.com/user/226904";
   mapSites["Tribe"] = "http://people.tribe.net/ef46330a-2ca7-4e5f-ac99-3edd6401974b";
   mapSites["Photoshop"] = "http://maiab.photoshop.com";
   mapSites["YouNoodle"] = "http://www.younoodle.com/people/maia_bittner";
   mapSites["Zivity"] = "http://beta.zivity.com/users/maia";
   mapSites["Rethos"] = "http://www.rethos.com/maia";
   mapSites["Dopplr"] = "http://www.dopplr.com/traveller/maiabittner";
   mapSites["Friendfeed"] = "http://www.friendfeed.com/mbittner";
   mapSites["Popsugar"] = "http://www.teamsugar.com/user/Maia+Bittner";
   mapSites["a.viary"] = "http://a.viary.com/profile/maia";
   mapSites["Etsy"] = "http://www.etsy.com/profile.php?user_id=5706949";
   mapSites["Kiva"] = "http://www.kiva.org/lender/maia";
   mapSites["Consumerist"] = "http://www.consumerist.com/commenter/Maia-Bittner";
   mapSites["Ziki"] = "http://www.ziki.com/en/people/maiabittner";
   mapSites["Tumblr"] = "http://maia.tumblr.com";
   mapSites["Bebo"] = "http://www.bebo.com/maiabittner";
   mapSites["43Things"] = "http://www.43things.com/person/maiabittner";
   mapSites["Flickr"] = "http://www.flickr.com/people/maia";
   mapSites["Pownce"] = "http://www.pownce.com/maia";
   mapSites["eBay"] = "http://feedback.ebay.com/ws/eBayISAPI.dll?ViewFeedback2&userid=maia!&ftab=AllFeedback&items=25";
   mapSites["PeekYou"] = "http://www.peekyou.com/USA/California/San_Francisco/Maia_Bittner/76323254";
   mapSites["Rapleaf"] = "http://www.rapleaf.com/pub/1/Maia-Bittner";
   mapSites["Wesabe"] = "http://www.wesabe.com/users/show/maia-bittner";
   mapSites["Spock"] = "http://www.spock.com/maia";
   mapSites["Last.fm"] = "http://www.last.fm/user/maiabittner";
