// General Purpose Functions

function convert_size(input, engine){
	var bytes = new Array("B", "KB", "MB", "GB", "TB");
	for(val in bytes){
		if(input > 1024) input = input / 1024; else break;
	}
	
	return Math.round(input*100)/100+" "+bytes[val];
}


//	Adds a target="_blank" attribute to all anchor tags
//	found with an attribute with rel="external" at runtime.
//
//	*	Thanks to Marius Stanciu for the helpful technique.
//		URL:	http://marius.code-purity.com/archives/2009/4/13/opening_pages_in_a_new/
function addExternalLinks(){
	var links;
	
	//	Prototype Framework
	if(typeof(Prototype) != "undefined"){
		links	= $$('a[rel="external"]');
		links.each(function(link){
			if(link.readAttribute("href") != "" && link.readAttribute("href") != "#")
				link.writeAttribute("target", "_blank");
		});
	}
	
	//	jQuery Framework
	else if(typeof(jQuery) != "undefined"){
		jQuery("a[rel='external']").attr("target", "_blank");
	}
}


//	Replaces an anchor's title text with a designed value.
//	*	Intended for use alongside Lightbox script, to replace tooltip text with lengthier descriptions viewed when opened.
function setLightboxInfo(id, text){
	var fullString		=	text;
	var node			=	(typeof(id) == "string") ? $(id) : id;
	node.writeAttribute("title", fullString);
}


function placeholderText(remtitle){
	
	//	Prototype Method
	if(typeof(Prototype) != "undefined"){
		
		//	Register form listeners to empty text fields still holding default text upon submission.
		$$("form").each(function(form){
			
			//	Triggered when form's being submitted.
			form.observe("submit", function(e){
				var target	= e.target;
				var value	= "";
				var dtext	= "";
				form.getElements().each(function(f){
					value	= f.getValue();
					dtext	= f.readAttribute("dtext");
					if(f.hasClassName("prevtxt") && value == dtext)
						f.setValue("");
				});
				
			});
		});
		
		var fields	=	$$(".prevtxt");
		var title	=	"";
		fields.each(function(link){
			title	=	link.readAttribute("title");
			//	Determine whether to add placeholder text/listeners based on Title attribute
			if(title != ""){
				link.setValue(title);
				link.writeAttribute("dtext", title);
				link.addClassName("inactive");
				
				//	Triggered when element gains focus
				link.observe("focus", function(e){
					var target = e.target;
					
					// If current value matches placeholder text, clear field
					if(target.readAttribute("dtext") == target.getValue()){
						target.removeClassName("inactive");
						target.setValue("");
					}
				});
				
				//	Triggered when element loses focus
				link.observe("blur", function(e){
					var target	= e.target;
					var dtext	= target.readAttribute("dtext");
					var value	= target.getValue();
					//	If value was empty and/or left at placeholder value, restore styles
					if(dtext == value || !value){
						target.addClassName("inactive");
						target.setValue(dtext);
					}
				});
				
				//	If remtitle is TRUE, remove the Title attribute from source markup
				if(remtitle)
					link.writeAttribute("title", null);
			}
		});
	}
	
	
	
	
	//	jQuery Method
	else if(typeof(jQuery) != "undefined"){
		
		//	Clear the values from any fields that're left at their placeholder values before submitting.
		$("form").submit(function(){
			$(this).find("input").each(function(index, val){
				var text	=	$(val).data("text");
				if($(val).val() == text)
					$(val).val("");
			});
		});
		
		
		$("input").each(function(index, val){
			var obj		=	$(val);
			var title	=	obj.attr("title");
			var value	=	obj.val();
			
			
			//	Check if a Title attribute is present, and the initial value
			//	is currently either blank or matching the given title.
			if(title && (!value || value == title)){
				
				obj.data("text", title);
				obj.val(title);
				obj.addClass("inactive");
				
				//	Triggered when field recieves Input focus
				obj.focusin(function(){
					var obj		=	$(this);
					var temp	=	obj.data("text");
					var value	=	obj.val();
					if(value == temp){
						obj.removeClass("inactive");
						obj.val("");
					}
				});
				
				//	Triggered when the field loses input focus
				obj.focusout(function(){
					var obj		=	$(this);
					var temp	=	obj.data("text");
					var value	=	obj.val();
					if(value == temp || !value){
						obj.addClass("inactive");
						obj.val(temp);
					}
				});
			}
		});
	}
}


//	jQuery scrollTo method. Credit goes to Aaron Schmidt.
//	*	URL: http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12/comment-page-1#comment-13214
jQuery.fn.extend({
	scrollTo : function(speed, easing){
		return this.each(function(){
			var targetOffset = $(this).offset().top;
			$('html,body').animate({scrollTop: targetOffset}, speed, easing);
		});
	}
});

//	Adds Carousels and prettyPhoto galleries
function addGalleries(){
	
	//	jCarousel
	if(typeof($.jcarousel) != "undefined"){
		$("#carousel").jcarousel({
			auto:		4,
			scroll:		1,
			wrap:		"both",
			animation:	"slow"
		});
	}
	
	//	Lightboxes / Prettyphotos
	if(typeof($.prettyPhoto) != "undefined")
		$(".wp_gallery a[href]").attr("rel", "prettyPhoto[gallery]").prettyPhoto({theme:"dark_rounded"});
}

// Fired when document's finished loading
$(document).ready(function(){
	addExternalLinks();
	placeholderText(true);
	addGalleries();
});
