/*******************************************************************
 * This file and the namespace it represents hold the primaryNavigation
 * object. The primaryNavigation object has a couple public methods for 
 * setting up interaction with the primaryNavigation component. 
 *
 * @author primmee
 *
 * JavaScript File Dependencies  (in order):
 *   jQuery
 *   cfc.js
 *******************************************************************/
//Create Namespace
cfc.namespace("primaryNavigation");

/*
 * Description of static object which in turn is actually
 * a property of the namespace you created with "yourNamespace"
 */
cfc.primaryNavigation = (function(objSpec) {

    /****************************************
     * PRIVATE Members -- Dependencies
     ****************************************/
    // state any objects this object is dependent upon like jQuery
    var $ = objSpec.$;
	
    /****************************************
     * PRIVATE Members
     ****************************************/
    // Except this object's dependencies, place all private functions, variables, etc. here 
    
    /****************************************
     * PUBLIC API
     ****************************************/
    // create obj to hold the public API and will be returned to the namespace
    var publicApi = {};
    
    // assign public members to publicApi

	// adds a class to mark the last list item in the primaryNavigation list
    publicApi.markLast = (function(){
        $('#c-mainnav > ul > li:last').addClass('c-lastNavItem');
    });
		
	// defines the animation and binds this action to events on the page
    publicApi.animate = (function(){
        $("#c-mainnav ul li").hover(function(){

            var ele = $(this);
            var ani = ele.data('ani');
            function adown(){
                clearTimeout(ani);
                ani = false;
                var par = ele.closest('ul.c-inner'); // second level menu
                var ch = par ? ele.children('ul.c-inner') : null;
                if (ch) {
                    var navItem = par.closest('li.c-lastNavItem'); // the last mainnav item
                    if (navItem.length && !ch.hasClass('c-leftItem')) { // move to the left so it's not off the page
                        ch.addClass('c-leftItem');
                    }
                }
                //Stop previous animations and drop down the subnav on hover
                ele.children("ul.c-inner").fadeIn(200).show();
            }
            if (ani) {
                clearTimeout(ani);
            }
            ani = setTimeout(adown, 150);
            ele.data('ani', ani);
        }, function(){
            var ele = $(this);
            var ani = ele.data('ani');
            function aup(){
                clearTimeout(ani);
                ani = false;
                //When the mouse hovers out of the subnav, stop previous animations and move it back up
                ele.children("ul.c-inner").hide();
            }
            if (ani) {
                clearTimeout(ani);
            }
            ani = setTimeout(aup, 150);
            ele.data('ani', ani);
        });
    }); 
	
	publicApi.initPage = (function(){
		
		publicApi.markLast();
		publicApi.animate();
		
    }); 	
	
    return publicApi;
 
/*
 * Optional literal objSpec parameter used to set all necessary intenral parameters.
 * All dependencies should be provided through this objSpec parameter.
 */ 
})(
{
	$: jQuery

});

/*******************************************************************
 Load scripts in page when DOM ready
 *******************************************************************/
$(document).ready(function(){
    cfc.primaryNavigation.initPage();
});


//alert("cfc.primaryNavigation");
