/*============================
  Toolbar was written for use on GuildPortal.com sites, but feel free to use it on any site
  @author:  Rob G. (aka Mottie)
  @version: 1.01 (8/18/2009) - Search: fixed popup & added width variable, Popup: added move icon & adjusted CSS to prevent it from resizing images inside the popup
  @version: 1.00 (8/14/2009)

  GFeed Plugin by M. Alsup was incorporated into this file
 =============================
Add new icons & links to the toolbar using this template:

 addToolbarLink.push(["(1)","(2)","(3)","(4)","(5)"]);
 (1) Location on the toolbar: "left", "center" or "right"
 (2) Type of Link: "link", "popup", "feed", "frame", "gallery", "code" or "spacer" (see below)
 (3) Icon Title: "My Facebook!"
 (4) Icon Image: "facebook.gif"
 (5) Link: use the URL to site: 
     Popup: add any HTML here:
     Feed: Add the URL to the RSS feed: http://www.facebook.com/pages/GuildPortal/126252409947
     Frame: 
     Gallery: Add the feed URL to your image gallery here to display pictures instead of text
     Code: Add any javascript (inside the script tag)
     Spacer: Use text or <img> to separate, e.g. " | " or "<img src='myspacer.gif'>"

Add new search choices using this template:

 addToolbarSearch.push (["(A)","(B)","(C)"]);
 (A) Site name
 (B) Site Icon
 (C) Site Search URL (please refer to this post on how to determine this URL ->
     http://guildportal.com/Guild.aspx?GuildID=258012&TabID=2168224&ForumID=1233963&TopicID=7589246&Page=1#37846839
*/
var tbLinks = new Array();
var tbSearch = new Array();

// Set default variables, if not defined
if (typeof(tbStartShowing)=="undefined") var tbStartShowing = true;
if (typeof(tbStartBottom)=="undefined") var tbStartBottom = true;
if (typeof(tbPopupFrameWidth)=="undefined") var tbPopupFrameWidth = 400;
if (typeof(tbPopupFrameHeight)=="undefined") var tbPopupFrameHeight = 300;
if (typeof(tbOpenLinksInNewWindow)=="undefined") var tbOpenLinksInNewWindow = true;
if (typeof(enableGuildPortalChat)=="undefined") var enableGuildPortalChat = true;
if (typeof(enableFacebookChat)=="undefined") var enableFacebookChat = true;
if (typeof(enableGoogleChat)=="undefined") var enableGoogleChat = true;
if (typeof(hideToolbarInfo)=="undefined") var hideToolbarInfo = false;
if (typeof(tbGalleryRows)=="undefined") var tbGalleryRows = 3;
if (typeof(tbGalleryBkgdStyle)=="undefined") var tbGalleryBkgdStyle = "dark";
if (typeof(tbGalleryBkgdColor)=="undefined") var tbGalleryBkgdColor = "#333333";
if (typeof(tbGalleryWidth)=="undefined") var tbGalleryWidth = 800;
if (typeof(tbGalleryHeight)=="undefined") var tbGalleryHeight = 600;
if (typeof(tbSearchBoxWidth)=="undefined") var tbSearchBoxWidth = 100;
if (typeof(tbSearchIcon)=="undefined") var tbSearchIcon = "http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/search-1.png";
if (typeof(tbHideIcon)=="undefined") var tbHideIcon = '<img src="http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/close-2.png">';
if (typeof(tbShowIcon)=="undefined") var tbShowIcon = '<img src="http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/show.png">';
if (typeof(tbMoveIcon)=="undefined") var tbMoveIcon = '<img src="http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/move.png">';
if (typeof(tbPopupMove)=="undefined") var tbPopupMove = '<img src="http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/move-1.png">';
if (typeof(tbPopupClose)=="undefined") var tbPopupClose = '<img src="http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/close-2.png">';

if (enableGuildPortalChat) tbLinks.push(["right","code","Guild Chat","http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/chat-guildportal.png","openChat();"]);
if (enableFacebookChat) tbLinks.push(["right","frame:450,500","Facebook Chat","http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/chat-facebook.png","http://www.facebook.com/presence/popout.php"]);
if (enableGoogleChat) tbLinks.push(["right","frame:320,451","Google Chat","http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/chat-google.png","http://www.gmodules.com/ig/ifr?url=http://www.google.com/ig/modules/googletalk.xml&amp;synd=open&amp;w=320&amp;h=451&amp;title=Google+Chat&amp;lang=en&amp;country=US&amp;border=%23ffffff%7C0px%2C0px+solid+%23999999&amp;"]);
if (!hideToolbarInfo) tbLinks.push(["about","popup:200","About this Toolbar","http://i144.photobucket.com/albums/r176/8r9/Aion/QMark.png","<h5>Toolbar v1.0</h5>This toolbar was written by Mottie<br>"]);
if (typeof(addToolbarLink)!="undefined") { tbLinks = addToolbarLink.concat(tbLinks) }

tbSearch.push (["Google","http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/google-1.png","http://www.google.com/search?q="]);
tbSearch.push (["Wikipedia","http://i201.photobucket.com/albums/aa236/Mottie1/testsite/icons/wiki.png","http://en.wikipedia.org/w/index.php?search="]);
if (typeof(addToolbarSearch)!="undefined") { tbSearch = tbSearch.concat(addToolbarSearch) }

var tbfeeds = 0;
if (typeof(tbSearchDefault)=="undefined") { var tbSearchDefault = tbSearch[0][0] }

/*============================
  Start Feed Plugin
 =============================*/
/*
 *  Plugin which uses the Google AJAX Feed API for creating feed content
 *  @author:  M. Alsup (malsup at gmail dot com)
 *  @version: 1.0.2 (5/11/2007)
 *  Documentation and examples at: http://www.malsup.com/jquery/gfeed/
 *  Free beer and free speech. Enjoy!
 */
(function($) {
if (!window.google)  {
 alert('You must include the Google AJAX Feed API script');
 return;
}    
if (!google.feeds) google.load("feeds", "1");
$.fn.gFeed = function(options) {
 var opts = jQuery.extend({
  target: this,
  max:   5   // max number of items per feed
 }, options || {});
 var g = new google.feeds.FeedControl();
 this.each(function() {
  var url = this.href || opts.url;
  var title = opts.title || this.title || $(this).text();
  g.addFeed(url, title);
  g.setNumEntries(opts.max);
 });
 $(opts.target).each(function() {
  g.draw(this, opts.tabs ? { drawMode: google.feeds.FeedControl.DRAW_MODE_TABBED } : null );
 });
 return this;
};
})(jQuery);

/*============================
  Start Toolbar Script
 =============================*/
function makeToolbar(){
 var tbposition = (tbStartBottom) ? 'tbBarBottom' : 'tbBarTop';
 var tbar = '<div id="toolbar" class="' + tbposition + '"><table width="100%"><tr><td class="tbSearch" nowrap></td><td class="tbleft"></td>' +
 '<td class="tbcenter"></td><td class="tbright"></td><td width="1%" nowrap><span class="tbabout tbMain"/><span id="tbHide" onmouseover="' +
 'ddrivetip(\'Hide toolbar\',\'\',\'100\')" onmouseout="hideddrivetip()">' + tbHideIcon + '</span><span id="tbMove" onmouseover="' +
  'ddrivetip(\'Move toolbar\',\'\',\'100\')" onmouseout="hideddrivetip()">'+ tbMoveIcon + '</span></td></tr></table></div><div id="tbShow" ' +
 'class="' + tbposition + '" onmouseover="ddrivetip(\'Show toolbar\',\'\',\'100\')" onmouseout="hideddrivetip()">' + tbShowIcon + '</div>';
 $('.ContentWrapper').append(tbar);

/****************
 Add Search Box
 ****************/
 var searchbox = '<input id="tbSearchInput" type="text" maxlength="256" name="search" onKeyPress="return disableEnterKey(event)" />' +
  '<span class="tbIcon tbMain" title="Click Me"><img id="tbSearchChoice" src="';
 var tmp = '';
 for (j=0;j<tbSearch.length;j++) {
  tmp += '<div class="searchIcons tbMain" onClick="tbSetSearchIcon(' + j + ')"><img src="' + tbSearch[j][1] + '">' + tbSearch[j][0] + '</div>';
  if (tbSearch[j][0] == tbSearchDefault){ searchbox += tbSearch[j][1] + '" name="' + j + '"'}
 }
 searchbox += '"/><div style="display:none" width="' + tbSearchBoxWidth + '" rel="popup" class="tbMain">' + tmp + '</div></span>' +
  '<img id="tbSearchIcon" onmouseover="ddrivetip(\'Search\',\'\',\'100\')" onmouseout="hideddrivetip()" src="' + tbSearchIcon + '">';
 $('#toolbar .tbSearch').append(searchbox);

/****************
 Add Custom Icons
 ****************/
 for (j=0;j<tbLinks.length;j++){
  addToolbarIcon(j);
 }

/****************
 Add click functions to toolbar
 ****************/
// hide the bar

 if (tbStartShowing){
  $('#tbShow').hide();
 } else {
  $('#toolbar').hide();
 }
 $('#tbHide').click(function(){
  $('#toolbar').slideToggle();
  $('#tbShow').slideToggle();
 })
// restore the bar
 $('#tbShow').click(function(){
  $('#tbShow').slideToggle();
  $('#toolbar').slideToggle();
 })
// move the bar
 $('#tbMove').click(function(){
  if ($('#toolbar').hasClass('tbBarTop')){
   $('#toolbar, #tbShow').removeClass('tbBarTop').addClass('tbBarBottom')
   $('#tbPopup').removeClass('tbPopupTop').addClass('tbPopupBottom');
  } else {
   $('#toolbar, #tbShow').removeClass('tbBarBottom').addClass('tbBarTop')
   $('#tbPopup').removeClass('tbPopupBottom').addClass('tbPopupTop');
  }
 })
// close the popup
 $('#tbPopup .close').live('click', function() {
  $(this).fadeOut('fast', function() { $(this).parent().remove(); });
 });
// Enable search
 $('#tbSearchIcon').click(function(){
 	var tmp = $("#tbSearchInput").attr("value");
 	if (tmp == '') return;
  var searchtxt = tbSearch[$("#tbSearchChoice").attr("name")][2] + tmp;
  if (tbOpenLinksInNewWindow) {
   window.open(searchtxt);
  } else {
   document.location.href = searchtxt;
  }
 })
// Enable gallery
 $('.tbgalleryfeed').click(function(){
  tbGallery($(this).attr('title'),$(this).attr('rel'),$(this).attr('name')); // title, url, size
 })

/****************
 Popup windows for toolbar
 ****************/
 $('.tbIcon').live('click', function(){
  $('#tbPopup').remove();
  var tmp = $(this).find('div:first');
  if (!tmp.length) return; // no popup or code
// run code
  if (tmp.hasClass('tbCode')){
   eval(tmp.attr('rel'));
   return;
  }
// make popup
  var $tbp = $('<div>').attr('id','tbPopup')
   .html($(this).find('div').html())
  $(this).after($tbp);
  if ( $('#toolbar').hasClass('tbBarTop')){
   $tbp.addClass('tbPopupTop')
  } else {
   $tbp.addClass('tbPopupBottom')
  }
// Retrieve the feed
  if (tmp.attr('rel').match('feed')){
   var tmp1 = $tbp.find('#'+tmp.attr('rel'));
   tmp1.gFeed({ url : tmp1.attr('rel'), 'title' : tmp1.attr('title'), 'max' : tbNumberOfFeeds });
   setTimeout(addPreview,1000); // Add an image preview *** need to find a better way to determine when the feed is loaded ***
  }
// Load the Frame
  if (tmp.attr('rel')=='frame'){
   $tbp.find('iframe').attr('src', tmp.attr('name'));
  }
// Position popup
  var tbPW = (tmp.attr('width')||400);
  var tbleft = ( $(this).position().left + (tbPW - 40) < $(window).width() ) ? $(this).position().left : $(window).width() - tbPW - 40;
  $('#tbPopup').css({'left' : tbleft + 'px', 'width' : tbPW + 'px' }).fadeIn('slow');
  $('#tbPopup .content').css({'width' : tbPW + 'px' });
  if ($('#tbPopup').find('.move').length) $('#tbPopup').draggable({containment:'window', axis:'x', handle:'.move'}).find('.move').css({'cursor':'move'});
 });
}

/****************
 Add preview for image only feeds (e.g. photobucket)
 ****************/
function addPreview(){
 $('#tbPopup a.gf-title').each(function(){
  var imgsrc = $(this).attr('href');
  if (imgsrc.match('.gif|.jpg|.png|.bmp')){
   var feedimg = '<img style="float:right;height:4em !important;width:4em !important;" src="' + imgsrc +
    '" onclick="jQuery.facebox(\'<center><img src=' + imgsrc + '></center>\')">';
   $(this).prepend(feedimg);
  }
 })
}

/****************
 Function to add toolbar icons
 tbLinks.push(["(1)location","(2)type","(3)title","(4)image tag","(5)content/url"]);
 ****************/
function addToolbarIcon(j){
 var tbEl = tbLinks[j];
 var t = tbEl[1].toLowerCase();
 if (t=="spacer"){
  var tbicon = tbEl[4];
 } else {
  var icon = '<img src="' + tbEl[3] + '" onmouseover="ddrivetip(\'' + tbEl[2].replace(/\'/,'&#146;') +
   '\',\'\',\'100\')" onmouseout="hideddrivetip()">';
  var tbicon = '<span class="tbIcon tbMain">';
 }
 // popup = display contents (HTML)
 if (t.match('popup')){
  tbicon += icon + '<div style="display:none" width="' + t.split(':')[1] + '" rel="popup"><div class="close tbMain">' + tbPopupClose +
   '</div><div class="move tbMain">' + tbPopupMove + '</div><div class="content">' + tbEl[4] + '</div></div></span>';
 }
 // link = make icon into a link
 if (t=="link"){
 	var tmp = (tbOpenLinksInNewWindow) ? '_blank' : '';
 	tbicon += '<a target="' + tmp + '" href="' + tbEl[4] + '">' + icon + '</a>';
 }
 // feed = add feed to popup
 if (t.match('feed')){
  tbfeeds++;
  tbicon += icon + '<div style="display:none" width="' + t.split(':')[1] + '" rel="feed' + tbfeeds + '"><div class="close tbMain">' +
   tbPopupClose + '</div><div class="move tbMain">' + tbPopupMove + '</div><div id="feed' + tbfeeds + '" title="' + tbEl[2] +
   '" rel="' + tbEl[4] + '"></div></div></span>';
 }
 // frame = open a popup with a frame (frame:width,height)
 if (t.match('frame')){
  var size = t.split(':')[1];
  var frameWidth = size.split(',')[0];
  var frameHeight = size.split(',')[1];
  tbicon += icon + '<div style="display:none" width="' + frameWidth + '" rel="frame" name="' + tbEl[4] + '"><div class="close tbMain">' +
   tbPopupClose + '</div><div class="move tbMain">' + tbPopupMove + '</div><div class="tbPopupTitle">' + tbEl[2] + '</div><br><iframe src="" width="' +
   frameWidth + '" height="' + frameHeight + '" allowtransparency="true" align="top" frameborder="0" scrolling="auto"></iframe></div></span>';
 }
 // gallery = open up facebox w/ Cooliris to view gallery
 if (t.match('gallery')){
  var size = t.split(':')[1];
  tbicon = '<span class="tbgalleryfeed tbMain" title="' + tbEl[2] + '" rel="' + tbEl[4] + '" name="' + size + '">' + icon + '</span>';
 }
 // code = run code w/ popup
 if (t=="code"){
  tbicon += icon + '<div style="display:none" class="tbCode" rel="' + tbEl[4] + '"></div></span>';
 }
 $('#toolbar .tb' + tbEl[0].toLowerCase()).append(tbicon);
}

/****************
 Search functions
 ****************/
function tbSetSearchIcon(j){
 $('#tbSearchChoice').attr('src',tbSearch[j][1]).attr("name",j);
 $('#tbPopup').remove();
}
function disableEnterKey(e){
 var key = (window.event) ? e.keyCode : key=e.which;
 if ( key == 13 ) {
  return false
 } else {
  return true
 }
}

/****************
 Add Cooliris viewer for image only feeds (e.g. photobucket)
 ****************/
function tbGallery(gtitle,gfeed,size){
 var tmp1 = gfeed.split('/');
 var tmp2 = tmp1.length;
 var tmp = '';
/* FLICKR: example Photostream URLs
 root: http://www.flickr.com/photos/thomashawk/
       http://www.flickr.com/photos/27734279@N02/
  set: http://www.flickr.com/photos/thomashawk/sets/
       http://www.flickr.com/photos/thomashawk/sets/72157594515875921/
*/
// Extract out user name or album
 if (gfeed.match('flickr')){
  for (j=0;j<tmp2;j++){
   if (tmp1[j] == "photos") {
    var user = tmp1[j+1];
    var ptype = tmp1[j+2];
    var album = tmp1[j+3];
   }
  }
  if (user != '') tmp = 'api://www.flickr.com/?user=' + user;
  if (album != '' && ptype == 'sets') tmp = 'api://www.flickr.com/?album=' + album;
 }
// PICASA: example URL = http://picasaweb.google.com/jajonet/BLIJDORP/#
// Extract out user name or album
 if (gfeed.match('picasa')){
  for (j=0;j<tmp2;j++){
   if (tmp1[j].match("picasaweb")){
    var user = tmp1[j+1];
    var album = tmp1[j+2];
   }
  }
  if (user != '') tmp = 'api://picasaweb.google.com/?user=' + user;
  if (album != '') tmp = 'api://picasaweb.google.com/?user=' + user + '&album=' + album;
 }
 // OTHER Media Feeds - let the plugin sort it out
 if (tmp == '') tmp = gfeed;
 tbLoadGallery(gtitle,tmp,size);
}
function tbLoadGallery(gtitle,gfeed,size){
 var gWidth = size.split(',')[0];
 jQuery.facebox('<div class="tbPopupTitle">' + gtitle + '</div><br><div id="tbgallerybox"></div>');
 var flashvars = { feed: escape(gfeed), numRows: tbGalleryRows, style: tbGalleryBkgdStyle, backgroundColor: escape(tbGalleryBkgdColor) };
 var params = { allowscriptaccess: "always" };
 swfobject.embedSWF(
  "http://apps.cooliris.com/embed/cooliris.swf",
  "tbgallerybox",
  gWidth,
  size.split(',')[1],
  "9.0.0",
  "",
  flashvars,
  params
 );
 $('#facebox').draggable({containment: 'parent'}).find('.body').css({ 'width' : gWidth, 'cursor': 'move' });
}

// Load Toolbar
$(document).ready(function(){
 // load extra scripts (control for cooliris)
 $('head').append( $('<script>').attr({ 'type':'text/javascript', 'src':'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'}) );
 makeToolbar()
})