﻿/*
* File:        gr8_menu_01.js
* Version:     1.0
* Author:      Rob du Preez (www.pandasam.co.za)
* 
* Copyright 2011 Pandasam cc (www.pandasam.co.za), all rights reserved.
*
*/
(function($) {
	$.fn.gr8_menu = function(options) {

		// Define default options
		// ----------------------
		var defaults = {

			width: 'auto',	// 'auto' makes parent container sized to fit menu
							// otherwise pass width as '500px'

			corners: '5px 5px transparent'		// show rounded corners with attr
												// '' does not show rounded corners
		};
		var options = $.extend(defaults, options);

		return this.each(function() {

		  // Find the parent that has a class
		  // --------------------------------
		  var papa=$(this).parent();
		  var parentclass='.'+papa.attr('class');
		  while (parentclass=='.') {
			papa=papa.parent(); 
			if(papa.html()===null) { parentclass=$(this).parent(); break; }
			parentclass='.'+papa.attr('class');
		  }

 		  var menu=$(this);
		  var mxh=menu.find('li a').outerHeight(true);

		  if (options.width=='auto') {
			// Calculate the size for the parent container by computing the
			// total width of all the li a items.
			// ------------------------------------------------------------
			var padtop=parseFloat($(this).css('padding-left').replace(/px/gi,''))+parseFloat($(this).css('padding-right').replace(/px/gi,''));
			var margtop=parseFloat($(this).css('margin-left').replace(/px/gi,''))+parseFloat($(this).css('margin-right').replace(/px/gi,''));
			var totwidth=padtop+margtop;
			var mxh=0;
			menu.find('li').each(function(i,ele) {
					var padding=parseFloat($(this).css('padding-left').replace(/px/gi,''))+parseFloat($(this).css('padding-right').replace(/px/gi,''));
					totwidth+=padding;
				}
			);
			var rmarg=0;
			menu.find('li a').each(function(i,ele) {
					rmarg=parseFloat($(this).css('margin-right').replace(/px/gi,''));
					totwidth+=$(this).outerWidth(true);
					if ($(this).height()>mxh) mxh=$(this).height();
				}
			);
			$(parentclass).width(totwidth+rmarg);
		  } else {
			$(parentclass).width(options.width);
		  }

          // Create the lavaLamp menu
          // ------------------------
var gr8_noclicks=false; 
          $(this).lavaLamp({fx: "backout", speed: 700
              , click: function(event, menuItem) {
					if (gr8_noclicks) { gr8_noclicks=false; return false; }
//					alert(event.srcElement.href);
//					return false;
                }
          });
            
/*
		  gr8_ui_mods();
		  
		  if (options.corners!='') {
			// Give the parent div rounded corners
            // -----------------------------------
            $(parentclass).corners(options.corners);
		  }			
*/
		});
    };
	
})(jQuery); 
