var Toggler = {
	init : function(){	
		$('#toggle-box').hide(); // Hide even though it's already hidden
		$('#extra-toggle a').click(function() {
      $('#toggle-box').slideToggle("medium") // First click should toggle to 'show'
	  	return false;
   	})
	}
};

var Defuscator = {
	init : function(){
		jQuery.fn.defuscate = function( settings ) {
 		   settings = jQuery.extend({
    		    link: true
    		}, settings);
    		var regex = /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;
    		return this.each(function() {
       		 if ( $(this).is('a[@href]') ) {
           		 	// If it's an <a> element, defuscate the href attribute
            		$(this).attr('href', $(this).attr('href').replace(regex, '$1@$2'));
            		// Make sure that the element's contents is not made into a link
            		var is_link = true;
            		//alert($(this).attr('href'));
       		 }
        		// Defuscate the element's contents
        		$(this).html($(this).html().replace(regex, (settings.link && !is_link ? '<a href="mailto:$1@$2">$1@$2</a>' : '$1@$2')));
  			});
		}
		$('p.email').defuscate();
	}
};

var Glider = {
	init : function(){
		featuredcontentglider.init({	
			gliderid: "slideshow", //ID of main glider container
			contentclass: "slide-content", //Shared CSS class name of each glider content
			togglerid: "slide-select", //ID of toggler container
			remotecontent: "", //Get gliding contents from external file on server? "filename" or "" to disable
			selected: 0, //Default selected content index (0=1st)
			persiststate: false, //Remember last content shown within browser session (true/false)?
			speed: 500, //Glide animation duration (in milliseconds)
			direction: "rightleft", //set direction of glide: "updown", "downup", "leftright", or "rightleft"
			autorotate: true, //Auto rotate contents (true/false)?
			autorotateconfig: [3000, 1] //if auto rotate enabled, set [milliseconds_btw_rotations, cycles_before_stopping]
		})
	}
};

var Blockquotes = {
	init : function(){	
	function prepareBlockquotes() {
     if (!document.getElementsByTagName || !document.createElement || !document.appendChild) return;
     var quotes = document.getElementsByTagName("blockquote");
     for (var i=0; i<quotes.length; i++) {
          var source = quotes[i].getAttribute("title");
			 var url = quotes[i].getAttribute("cite");
          if (!source) continue;
          	if (url){
			 		var para = document.createElement("p");
          		var link = document.createElement("a");
          		para.className = "attribution";
          		link.setAttribute("href",url);
          		link.appendChild(document.createTextNode("- "+source+" -"));
          		para.appendChild(link);
          		quotes[i].appendChild(para);
     			}else{
			 		var para = document.createElement("p");
          		para.className = "attribution";
          		para.appendChild(document.createTextNode("- "+source+" -"));
          		quotes[i].appendChild(para);
			}
	  }
 }
 
 prepareBlockquotes();
	}
};

/*  Onload Functions    //---------------------------*/
$(document).ready(function(){
	Toggler.init();
	Defuscator.init();
	Glider.init();
	Blockquotes.init();
});


