//==============================================================
// CUSTOM SCRIPTS
// Author: Last Door Solutions (http://lastdoorsolutions.com)
// 2011
// ==============================================================


$(document).ready(function() {

//==============================================================
// Superfish
// ==============================================================
if( $('ul.main-nav').length>0 ){
	$('ul.main-nav').superfish({
		delay		: 0
	}); 
	$('span.sf-sub-indicator').remove(); 
}

//==============================================================
// Maintain Equal Height
// ==============================================================
var greatestHt = 0;
var block = jQuery('.equal-height-cols .col'); 
block.each( function(){	
	var curHt = jQuery(this).height();	
	if( curHt > greatestHt ){
		greatestHt = curHt;	
	}	
});
block.css('height',greatestHt);

//==============================================================
// get Widget title as a widget class
// ==============================================================

$('.widget_text').each( function(){
var widgetTitle = $(this).find('.widget-title').text();
var widgetTitleSlug = widgetTitle.replace(/ /gi, "-");
widgetTitleSlug = widgetTitleSlug.toLowerCase();
widgetTitleSlug = "widget-" + widgetTitleSlug;
$(this).addClass(widgetTitleSlug);
});

});
// end ready function here.

//====================================================================
// function to make the main-nav dropdown content within the wrapper.
// REQUIREMENTS/NOTES:
// Integreating with superfish menu.
// Containre id: primary-nav
// menu ul class: main-nav
// ===================================================================

$(function() {
    var $mainNav = $('#primary-nav ul'),
    navWidth = $mainNav.width();
	navWidth = navWidth - 28; // only for this theme
	
    $mainNav.children('li').hover(function(ev) {
        var $this = $(this);
		if($this.parent().hasClass('main-nav')){  // condition for only first level / immediate drop down
        	$dd = $this.children('ul');
		}
		else{
			$dd2 =	$this.children('ul');
		}
        // get the left position of this tab
		// condition if menu item [a tag] has class=sf-with-ul
		// condition if menu item [li tag] parent [ul tag] has class=main-nav (ie. root level)
		if( $(this).find('a').hasClass('sf-with-ul') ){

			var leftPos = $this.find('.sf-with-ul').position().left;

			// get the width of the dropdown
			var ddWidth = $dd.width(),
			leftMax = navWidth - ddWidth;
			//alert("leftPos :" + leftPos +" , ul Width : " + ddWidth +" , navWidth : " + navWidth +" , leftMax : " + leftMax);
			
			if( $(this).parent().hasClass('main-nav') ){
			// position the dropdown
				$dd.css('left', Math.min(leftPos, leftMax) );
			
			}
			else{
				var ddLeftRoot = $(this).parent().position().left,
				ddWidthRoot = $(this).parent().width(),
				ddWidthChild = $dd2.width(),				
				leftMaxChild = ddLeftRoot + ddWidthRoot + ddWidthChild,
				rightPos = leftPos - ddWidthChild;
				
				var ddTop = $this.find('.sf-with-ul').position().top;	
				$dd2.css('left', (leftMaxChild < navWidth) ? ddWidthRoot : rightPos );
				$dd2.css('top', ddTop );
			}
        
        	// show the dropdown
        	//$this.addClass('main-nav-item-active');

		}
    }, function(ev) {

        // hide the dropdown
        //$(this).removeClass('main-nav-item-active');
    });
});

