/*
Script Name: Digg Query
Script URI: http://stylizedweb.com
Description: Allows for dynamic insertion
Version: 0.1
Author: Leon Chevalier
Author URI: http://aciddrop.com/
Copyright (c) 2008 Leon Chevalier
Released under the GNU General Public License (GPL)
http://www.gnu.org/licenses/gpl.txt
0.1 - Initial Release
*/

var diggQuery = Class.create();
diggQuery.prototype = {
	   /**
	   * Constructor, called when class instantiated
	   **/
	   initialize: function() {
		var options = Object.extend({
		  opacity:60,						
		  pad_links:false,
		  link_css:'',
		  link_scope:'',
		  link_rel:'',
	      box_type:'',
		  proxy:'/proxy.php'
		}, arguments[0] || {});
					
	    this.options = options;
		this.do_links();
					
	   },
	   
	   /**
	   * Adds digg info to any element passed to it
	   **/
	   add_digg_info: function(element,info) {
					   	  
	   this.create_info_box(info,this.options.box_type);
	   Element.insert(element,{after:this.content});
	   
	   //Get inserted span
	   this.inserted = element.next();
	   this.inserted.setOpacity(this.options.opacity/100); 
	   
	   //On hover
	   Event.observe(this.inserted,"mouseover",function(event) { 
	   this.setOpacity(1);	   
	   });
	   
	   //Mouse out
	   Event.observe(this.inserted,"mouseout",function(event) { 
	   element = Event.findElement(event,'span');
	   element.setOpacity(this.options.opacity/100); 
	   }.bind(this));
	   
	   
	   },
	   
	   /**
	   * Creates our digg box. Both with and without images
	   **/	   
	   create_info_box: function(info,type) {
	   	   	   
		   this.content = "<span class='digg_span'>";
		   
		   	   this.content += "<a href='" + info.digg_url + "' title='" + info.diggs + " diggs. Click to digg: " + this.htmlspecialchars(info.description) + "' alt='" + info.diggs + " diggs.  to digg: " + this.htmlspecialchars(info.description) + "'>";
  			
			this.content +=  "<img src='http://stylizedweb.com/digg_query/digg-badges/10x10-digg-thumb.gif' width=10 height=10 border=0 style='valign:middle' />";
   								   this.content += info.diggs;
								   if(type!='image_only') {	   
								  this.content += " diggs";
								   }
									   
								   
				this.content += "</a>"
				
		  this.content += "</span>"
		  
	   if(this.options.pad_links == true) {
		   this.content += "<span class='hidden_span'><img src='http://stylizedweb.com/digg_query/digg-badges/10x10-digg-thumb.gif' width=10 height=10 border=0 style='valign:middle' /> ";
		   if(type!='image_only') {	   
		   this.content += info.diggs + " diggs";
		   } else {
		   this.content += "&nbsp;";
		   }
		   this.content += "</span>";
	   }
	   
	   
	   return this.content; 
	   
	   
	   },
	   
	   /**
	   * The AJAX request. Pass the url about which info is required and the element
	   * to show the info next to
	   **/
	   show_diggs: function(url,element) {
				
		new Ajax.Request(this.options.proxy, {
		  method: 'get',
		  parameters: {url:url,func:'echo_story_json',element:element},
		  onSuccess: function(transport) {
			this.element = transport.request.parameters.element;
			this.response = (transport.responseText).evalJSON();
			if(this.response.stories) {
				this.diggs = this.response.stories[0].diggs;		
				this.digg_url = this.response.stories[0].href;						
				this.description = this.response.stories[0].description;										
				this.add_digg_info(this.element,{diggs:this.diggs,thumbnail:this.thumbnail,digg_url:this.digg_url,description:this.description});
			} 
		  }.bind(this)
		});

	  },
	  
	  /**
	  * Gets the links beside which to show the digg info
	  * 
	  **/	  
	  do_links: function () {
	  
		$$(this.options.link_css+' a').each(function(item){
		if(this.options.link_scope == 'internal') {
		regex = new RegExp(document.location.host,"i");
		} else {
		regex = new RegExp("/*/","i");
		}
			if(regex.test(item.href)) {
				if(this.options.link_rel) {			
				 if(this.options.link_rel == item.rel) {
				 this.show_diggs(item.href,item);
				 }				
				} else {				
				 this.show_diggs(item.href,item);
				}
			
			}
		}.bind(this));	  	  
	  
	  },
	  
	/**
	* The digg description needs to be special characterified
	* 
	**/	  
	htmlspecialchars: function (str) {
	 if (typeof(str) == "string") {
	  str = str.replace(/&/g, "&amp;"); /* must do &amp; first */
	  str = str.replace(/"/g, "&quot;");
	  str = str.replace(/'/g, "&#039;");
	  str = str.replace(/</g, "&lt;");
	  str = str.replace(/>/g, "&gt;");
	  }
	 return str;
	 }
	  
}
