

/* Image Caption */

//Get Element by Class Name from: http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}


//Function for converting title tags on images into caption

function addCaption(xClass) {
	var allImages = getElementsByClassName(document, "img", xClass);
	for ( var i=0; i < allImages.length; i++) {
		var imageCaption = document.createTextNode(allImages[i].title);
		var imageContainer = document.createElement("div");
		var imagePara = document.createElement("p");
		var imageWidth = allImages[i].getAttribute("width");
		var spareSpan = document.createElement("span"); //This adds an extra span. Useful for curved corners
		imagePara.appendChild(imageCaption);
		allImages[i].parentNode.insertBefore(imageContainer, allImages[i]);
		imageContainer.appendChild(allImages[i]);
		if ( allImages[i].title != "" ) {
		imageContainer.appendChild(imagePara); 
		}
		imageContainer.appendChild(spareSpan);		
		imageContainer.className = xClass
		spareSpan.className = "spareSpan"
		allImages[i].className = "imgCaption"
		imageContainer.style.width = imageWidth + "px";
		
    }
}

// Runs all the listed functions on the loading of the window

window.onload=function(){
addCaption("imgLeft");
addCaption("imgRight");
}

/* FAQs */
$.fn.orphans = function(){
  var ret = [];
  this.each(function(){$.each(this.childNodes, function() {
    if (this.nodeType == 3 && $.trim(this.nodeValue)) ret.push(this)
    })}); 
   return $(ret);
}
//http://www.learningjquery.com/2008/02/simple-effects-plugins
jQuery.fn.blindToggle = function(speed, easing, callback) {
  var h = this.height() + parseInt(this.css('paddingTop')) + parseInt(this.css('paddingBottom'));
  return this.animate({marginTop: parseInt(this.css('marginTop')) < 0 ? 0 : -h}, speed, easing, callback); 
};
jQuery.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};
$(document).ready(function() {
//s/jq/a
    $('.expand').css('cursor','pointer').orphans().wrap('<a href="#expand/collapse" title="expand/collapse"></a>');
    $('.collapse').hide(); 
    
    $('#show .expand').click(function() {
        $(this).toggleClass("arrow-up");
        $(this).next('.normal').toggle();
        $(this).next('.slow').toggle('slow');
    });
    
    $('#slide .expand').click(function() {
        $(this).toggleClass("arrow-up");
        $(this).next('.normal').slideToggle();
        $(this).next('.slow').slideToggle('slow');
    });
    
    $('#tabbed .expand').click(function() {
        $(this).toggleClass("arrow-up");
        $(this).next('.normal').fadeToggle();
        $(this).next('.slow').fadeToggle('slow');
    });
});

/* New Window */
$(function(){
$('a.external').click(function(){
window.open(this.href);
return false;
});
});

/* New Window */
$(function(){
$('ul.sizes li a').click(function(){
window.open(this.href);
return false;
});
});

// Adds a class focus to input text when focused
function focusfix(selector, className) {
$(selector).focus(function() {
$(this).addClass(className);
});
// Removes class when focus is lost
$(selector).blur(function() {
$(this).removeClass(className);
});
}

jQuery(document).ready(function($) {
focusfix('input.text', 'focus');
});

jQuery(document).ready(function($) {
focusfix('textarea', 'focus');
});

/* New Window */
$(function(){
$('a.external').click(function(){
window.open(this.href);
return false;
});
});



