// #@(#)jquery.converttotab.js	1.1 14:25:12,11/12/30 (yy/mm/dd)
/**
 * Convert To Tabs (Created AM Dev '11)
 * 
 * Version 1.0
 * 
 * JQuery Plugin to convert specific code to work with jquery.basictabs.1.2.js and above
 * 
 * Revisions
 * 1.0 Initial Release
 * 
 * Requires 
 * * jquery 1.6+
 * * jquery.basictabs.1.2.js
 * 
 * To Do
 * Complete documentation, implement options, create dynamic holder (allow for passing in)
 */

(function($){
	/**
	 * Core set of methods which are exposed as part of the plugin
	 */
	var methods = {
		
		/**
		 * The main/default constructor of the plugin
		 * @param {Object} inOptions An Object containing the custom set of options to be applied to each 
		 * of the instances of the plugins.
		 */
		init : function (inOptions) {
			var options = $.extend({}, $.fn.converttotab.defaults, inOptions),		// Combine the options and defaults into a new object
				initOptions = options;
			
			return this.each(function () {
				
				// Hide from external
				var	jstab	= this,
					tabs 	= [],	// Store the data for the tabs
					thistab,
					tabSection	= $('.tab-section'),
					tc,th,tb,id,html,body,content,height,cleanid,tclass;
					
				$(jstab).children(".single-tab").each(
					function(){
						thistab = $(this);
						tabs.push($(thistab));
					}
				)
				
				// Apply the skeleton to the jstab
				$("<div class='tab-container'><div class='tab-head'><ul></ul></div><div class='tab-body'></div>").appendTo($(tabSection));
				
				// Loop through and add the titles to the tab-head
				tc = $(tabSection).find('.tab-container');
				th = $(tc).find(".tab-head ul");
				tb = $(tc).find(".tab-body");
				
				for(var i=0; i<tabs.length; i++) {
					thistab = $(tabs[i]);
					
					content = $(thistab).find(".tab-title").html();
					
					body	= $(thistab).find(".tab-body").html();
					height 	= $(thistab).find(".tab-body").height();
					
					id 		= $(content).attr("href");
					html 	= $(content).html();
						
					cleanid = id.substring(id.indexOf('#'));
					cleanid = cleanid.substr(1);
					
					tclass	= (i==tabs.length-1) ? "last" : ""; 
					tclass	+= (i==0) ? "first" : "";
					
					$("<li class='"+tclass+"'><a href='#"+cleanid+"'>"+html+"</a></li>").appendTo($(th));
					
					$("<div class='js-tab' style='height:"+height +"px;position:relative;' id='"+cleanid+"'>"+body+"</div>").appendTo($(tb));
				}
				
				$(this).parent().remove();
			});
		}
	};
	
	
	/**
	 * Main Constructor which then goes on to use the method object.
	 */
	$.fn.converttotab = function (method) {
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
	    } else if (typeof method === 'object' || !method ) {
			return methods.init.apply(this, arguments);
	    } else {
			$.error('Method ' +  method + ' does not exist on jQuery.converttotab');
	    }
	};
	
	/**
	 * Defaults which can be passed in as over-ride
	 */
	$.fn.converttotab.defaults = {	};
	
	/** 
	 * Automatic call to instiate basic functionality
	 */
	$(document).ready(function() {
		
	});
	
})(jQuery)

