jQuery.fn.extend({
	socialBookMarks: function(o) { 
	/*
	*  Funcao que serve para gerar o html dos social bookmarks
	*  Adiciona o html ao objecto evocado 
	*
	*  Ex: jQuery("#socialbookmarksVisiveis").socialBookMarks();
	*  Escreve o seguinte html dentro do objecto #socialbookmarksVisiveis:
	*    
	*    <div class="sbookmarks">
	*      adicionar aos favoritos
	*      <div class="channel"><a href="http://tags.sapo.pt/post?v=3&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'" title="Adicionar ao Sapo Tags"><img src="' + imgFolder + '/sapotags_trans.gif" title="Adicionar ao Sapo Tags"/></a></div>
	*      <div class="channel"><a href="javascript:window.open(\'http://digg.com/submit?phase=2&url=\'+encodeURIComponent(location.href)+\'&bodytext=&tags=&title=\'+encodeURIComponent(document.title));return false;" title="Adicionar ao Digg"><img src="' + imgFolder + '/digg_trans.gif" alt="Adicionar ao Digg"/></a></div>
	*      <div class="channel"><a href="javascript:window.open(\'http://digg.com/submit?phase=2&url=\'+encodeURIComponent(location.href)+\'&bodytext=&tags=&title=\'+encodeURIComponent(document.title));return false;" title="Adicionar ao Digg"><img src="' + imgFolder + '/digg_trans.gif" alt="Adicionar ao Digg"/></a></div>
	*      <div class="channel"><a href="javascript:window.open(\'http://www.google.com/bookmarks/mark?op=add&hl=pt-PT&bkmk=\'+encodeURIComponent(location.href)+\'&annotation=&labels=&title=\'+encodeURIComponent(document.title));return false;" title="Adicionar ao Google"><img src="' + imgFolder + '/google_trans.gif" alt="Adicionar ao Google"/></a></div>
	*      <div class="channel"><a href="javascript:window.open(\'http://myweb2.search.yahoo.com/myresults/bookmarklet?t=\'+encodeURIComponent(document.title)+\'&d=&tag=&u=\'+encodeURIComponent(location.href));return false;" title="Adicionar ao Yahoo"><img src="' + imgFolder + '/yahoo_trans.gif" alt="Adicionar ao Yahoo"/></a></div>
	*      <div class="channel"><a href="javascript:window.open(\'http://technorati.com/faves?add=\'+encodeURIComponent(location.href)+\'&tag=\');return false;" title="Adicionar ao Technorati"><img src="' + imgFolder + '/technorati_trans.gif" alt="Adicionar ao Technorati"></a>
	*    </div>
	*
	*
	*  Ha dois parametros de configuracao opcionais, o 'text' e o 'visibility',
	*  por defeito o text = 'adicionar aos favoritos' e o visibility = 'visible',
	*  para mudar os valores, passar os parametros ao evocar a funcao, ex.:
	*
  *    jQuery("#socialbookmarksEscondidos").socialBookMarks({
  *      text: '<u>adicionar aos favoritos</u>',
  *      visibility: 'hidden'
  *    });
	*
	*  neste caso aparece o texto adicionar aos favoritos sublinhado e so onmouseover e que mostra os socialbookmarks (o div .sbookmarks)
	*
	*/
		o = jQuery.extend({ // definicoes default
			text: 'adicionar aos favoritos',
			visibility: 'hidden',
			iconsFolder: '/images/bookmarks'
		}, o || {});
		
		var obj = jQuery(this);

		var channels = {
		  "items": [
		    {
		      "title": "Sapo Tags",
		      "icon": "sapotags.gif",
		      "domain": "http://tags.sapo.pt",
		      "path": "'http://tags.sapo.pt/post?v=3&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)"
		    },
		    {
		      "title": "Do Melhor",
		      "icon": "domelhor.gif",
		      "domain": "http://domelhor.net",
		      "path": "'http://domelhor.net/submit.php?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)"
		    },
		    {
		      "title": "Del.icio.us",
		      "icon": "delicious.gif",
		      "domain": "http://del.icio.us",
		      "path": "'http://del.icio.us/post?v=2&url='+encodeURIComponent(location.href)+'&notes=&tags=&title='+encodeURIComponent(document.title)"
		    },
		    {
		      "title": "Digg",
		      "icon": "digg.gif",
		      "domain": "http://digg.com",
		      "path": "'http://digg.com/submit?phase=2&url='+encodeURIComponent(location.href)+'&bodytext=&tags=&title='+encodeURIComponent(document.title)"
		    },
		    {
		      "title": "Google",
		      "icon": "google.gif",
		      "domain": "http://www.google.com",
		      "path": "'http://www.google.com/bookmarks/mark?op=add&hl=pt-PT&bkmk='+encodeURIComponent(location.href)+'&annotation=&labels=&title='+encodeURIComponent(document.title)"
		    },
		    {
		      "title": "Yahoo",
		      "icon": "yahoo.gif",
		      "domain": "http://www.yahoo.com",
		      "path": "'http://myweb2.search.yahoo.com/myresults/bookmarklet?t='+encodeURIComponent(document.title)+'&d=&tag=&u='+encodeURIComponent(location.href)"
		    },
		    {
		      "title": "Technorati",
		      "icon": "technorati.gif",
		      "domain": "http://www.technorati.com",
		      "path": "'http://technorati.com/faves?add='+encodeURIComponent(location.href)+'&tag='"
		    },
		    {
		      "title": "StumbleUpon",
		      "icon": "stumbleupon.gif",
		      "domain": "http://www.stumbleupon.com",
		      "path": "'http://www.stumbleupon.com/submit?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)"
		    }
		  ]
		}
		
		
		var sbmk = jQuery('<div class="sbookmarks"></div>');
		jQuery('<div class="sbookmarkstit">Partilhar este artigo em:</div>').appendTo(sbmk);
		
		jQuery(channels.items).each(function(i,channel){
		  jQuery('<div class="channel"></div>').append(
		    jQuery("<a href="+ channel.domain +"><img src="+ o.iconsFolder +"/"+ channel.icon +" alt="+channel.title+" title='Adicionar ao "+channel.title+"' /> "+ channel.title +"</a>").click(function() {
		      window.open(eval(channel.path));
		      return false;
		    })
		  ).appendTo(sbmk)
		});
		
		
    obj.append(
      o.text, // mensagem que aparece no topo
      sbmk // socialBookMarksHTML
    ).css("cursor", "pointer");
    
    if(o.visibility == 'hidden'){ // se for suposto esconder o div .sbookmarks
      jQuery(sbmk).hide(); // esconde o div .sbookmarks ao carregar
			
      obj.click(function() { // click do elemento que agrega todo o html dos socialbookmarmks, mostra o div .sbookmarks
        jQuery(sbmk).show("fast");
      });
      jQuery(sbmk).mouseover(function() {
        clearTimeout(hidesbmk);
        jQuery(sbmk).show("fast")
      });
      jQuery(obj).mouseout(function() {
        hidesbmk = setTimeout(function(){
          jQuery(sbmk).hide("fast")
        },500)
      })
    }
  },
	
	
	
	browserBookMarks: function(o) {
	/*
	*  Funcao que serve para gerar o html dos bookmarks para o browser
	*  Adiciona o html ao objecto evocado 
	*
	*  Ex: jQuery("#adFavoritos").browserBookMarks();
	*  Escreve o seguinte html dentro do objecto #adFavoritos:
	*  
	*  ** TODO: 
	*  *  - Colocar link correcto, no caso de ser homePage, contruido por javascript com base no endereco da pagina actual	 
	*  **	
	*	
	*  ** PARAMETROS:
	*  *	homepageText: texto do link para adicionar a homepage
	*  *	homepageLink: 'homePage', 'currentPage' ou um url - link que sera adicionado aos favoritos
	*  *	favoritsText: texto do link para adicionar aos favoritos
	*  *	favoritsLink: 'homePage', 'currentPage' ou um url - link que sera adicionado aos favoritos
	*  **	
	*/
	
		o = jQuery.extend({ // definicoes default
			homepageText: 'Faça deste site a sua homepage',
			homepageLink: 'homePage',
			favoritsText: 'Adicione este site aos seus favoritos',
			favoritsLink: 'homePage'
		}, o || {});
		
		documentLink = function(link){
			if (link == 'homePage'){
				return location.href.slice(0, location.href.indexOf(document.domain)) + document.domain;
			} else if (link == 'currentPage'){
				return location.href;
			} else {
				return link;
			}
		}
		documentTitle = function(link){
			if (link == 'homePage'){
				return document.title.slice(0, document.title.indexOf('-'));
			} else if (link == 'currentPage'){
				return document.title;
			} else {
				return link;
			}
		}
		
		homepageLink = documentLink(o.homepageLink);
		favoritsLink = documentLink(o.favoritsLink);
		favoritsTitle = documentTitle(o.favoritsLink);
				
		isExplorer = ((navigator.appName=="Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=4)) ? true : false;
		
		
		jQuery('<ul />')
			.append(jQuery('<li class="bbmkHomepage" />')
				.append(jQuery('<a href="'+homepageLink+'" />')
					.click(function(){
						if (isExplorer) {
							this.style.behavior='url(#default#homepage)'; 
							this.setHomePage(homepageLink);
						} else {
							alert('Para fazer deste site a sua home page arraste este link para cima do botão "home" do seu navegador');
						}
						return false;
					})
					.html(o.homepageText)
				)
			)
			.append(jQuery('<li class="bbmkFavorits" />')
				.append(jQuery('<a href="'+favoritsLink+'" />')
					.click(function(){
						if (window.sidebar&&window.sidebar.addPanel) {
							window.sidebar.addPanel(favoritsTitle, favoritsLink,"");
						} else {
							window.external.AddFavorite(favoritsLink, favoritsTitle);
						}
						return false;
					})
					.html(o.favoritsText)
				)
			)
			.appendTo(this);
	}
	
});
