﻿(function($){
  $.fn.SocialBookmarks = function(options) {
  
    options = jQuery.extend({
      target:"_blank"
    }, options);
    
    return this.each(function() {
      obj = $(this);
      
      //toggle the sublist if set to popup display type
      if( $("div.socialBookmarksShell", obj).hasClass("socialBookmarksToggle"))
      {
        $("div.socialBookmarksButton, div.socialBookmarksShell", obj).hover(function(){
          var btn = $(this).parents("div.widgetSocialBookmarks").find("div.socialBookmarksButton:first");
          var shell = $(this).parents("div.widgetSocialBookmarks").find("div.socialBookmarksShell:first");
          btn.toggleClass("hover");
          shell.toggleClass("hover")
          var btnPosition = btn.position();
          shell.css("left", btnPosition.left).css("top", btnPosition.top - shell.height());
          shell.show();
        },
        function(){
          $(this)
          .parents("div.widgetSocialBookmarks")
          .find("div.socialBookmarksButton, div.socialBookmarksShell")
          .toggleClass("hover")
          .filter(".socialBookmarksShell")
          .hide();
        });
      }
      
      //toggle hovers and apply click events to the services
      $("div.socialBookmarksService", obj)
      .hover(
        function(){
          $(this).toggleClass("hover");
        },
        function(){
          $(this).toggleClass("hover");
        })
      .click(function(){
        //when a service is clicked:
        //- grab the unique info for that service
        //- track the click
        //- load the service's page
        var sServiceId = $(this).attr("id").replace("socialBookmarksServiceId","");
        var iServiceId = parseInt(sServiceId);
        var sServiceURL = "";
        var sServiceTracking = "";
        var iServiceWindowHeight;
        var iServiceWindowWidth;
        switch(iServiceId)
        {
          case 1:
            sServiceURL = "http://delicious.com/save?jump=close&v=4&partner=[partner]&noui&jump=close&url={URL}&title={Title}"
            sServiceTracking = "Delicious Service Clicks";
            iServiceWindowHeight = 550;
            iServiceWindowWidth = 700;
            break;
          case 2:
            sServiceURL = "http://www.facebook.com/sharer.php?u={URL}&t={Title}"
            sServiceTracking = "Facebook Service Clicks";
            iServiceWindowHeight = 450;
            iServiceWindowWidth = 600;
            break;
          case 3:
            sServiceURL = "http://digg.com/submit/?phase=2&partner=[partner]&url={URL}&title={Title}"
            sServiceTracking = "Digg Service Clicks";
            iServiceWindowHeight = 650;
            iServiceWindowWidth = 950;
            break;
          case 4:
            sServiceURL = "http://twitter.com/home?status={URL}"
            sServiceTracking = "Twitter Service Clicks";
            iServiceWindowHeight = 550;
            iServiceWindowWidth = 850;
            break;
          case 5:
            sServiceURL = "http://www.myspace.com/Modules/PostTo/Pages/?u={URL}&t={Title}&c="
            sServiceTracking = "MySpace Service Clicks";
            iServiceWindowHeight = 550;
            iServiceWindowWidth = 850;
            break;
          case 6:
            sServiceURL = "favorites"
            sServiceTracking = "Favorites Clicks";
            iServiceWindowHeight = 550;
            iServiceWindowWidth = 850;
            break;          
          default:
            sServiceURL = "";            
        }
        
        if (sServiceURL != "")
        {
          if (typeof(dcsMultiTrack) != "undefined") {
            dcsMultiTrack("DCSext.socialbookmark", sServiceTracking, "DCSext.socialbookmarkurl", document.title);
          }
          
          if (sServiceURL == "favorites")
          {
            AddBrowserBookmark();
          }
          else
          {
            var sBookmarkTitle = encodeURIComponent(document.title);
            var sBookmarkURL = encodeURIComponent(document.location.href);
            sServiceURL = sServiceURL.replace("{URL}", sBookmarkURL).replace("{Title}", sBookmarkTitle);
            if (options.target == "_blank") {
              window.open(sServiceURL, "Bookmark", "height="+iServiceWindowHeight+", width="+iServiceWindowWidth+", resizable=1, status=1, location=1, toolbar=1, menubar=1");
            } else {
              document.location = sServiceURL;
            }
          }
        } else {
          alert("Invalid bookmark service!");
        }
        
      });
      
      //add a bookmark in the browser itself
      function AddBrowserBookmark()
      {
        var sBookmarkTitle = document.title;
        var sBookmarkURL = document.location.href;
        if (sBookmarkURL.indexOf("?") == -1) {
          sBookmarkURL += "?WT.cg_n=bookmarkreturn";
        }else{
          sBookmarkURL += "&WT.cg_n=bookmarkreturn";
        }
        
        var sManaulMsg = "Sorry! Your browser doesn't support this function.\nPress Ctrl+D to add site to bookmark list.";
        if (window.sidebar) {
          window.sidebar.addPanel(sBookmarkTitle, sBookmarkURL,"");
        }
        else if (window.external) {
          window.external.AddFavorite(sBookmarkURL, sBookmarkTitle);
        }
        else {
          alert(sManaulMsg);
        }
      }
      
    });// end of return this.each
    
  };// end of plugin function
})(jQuery);