/*
* Recommended usage: Activate javascript based features on pages.
* How-To : Add a setup call in the appropriate flow sections below.
* The motivation is to move towards 'externalized javascript'.
* Fully seperates where code is written from where it is called.
* Ofter relies on the data passing technique 'data-' that's put directly onto html elements by the JSP pages.
 */
APP = {
//    Note that this app calls itself starting with $(document).ready(UTIL.loadEvents)
//    Thus, it only requires inclusion on the page to self-attach to the activated DOM elements.
//    The flow control is based on the fdl populating the flowTemplate.jsp file.
//    corporate\modules\tlc-webapp\src\main\webapp\templates\flowTemplate.jsp
//    <body class="bgBody <%=fdl.getFlowName()%>"  data-role="page"  data-flow="<%=fdl.getFlowName() %>" data-flow-state="<%=fdl.getCurrentStateFlowName()
//    Control elements are pulled out of the class="" and the id="" attributes of the body tag.
    common : {
        init : function() {
            TLPopup.setupPopups();
            TLUtils.setupMobileToggle();
            TLUtils.activateSearchFlightTypeRadios(); // Note this is required for both desktop and mobile. TODO : localize in search pages only.
        }
    },
    FltSearch :{
        init : function() { },
        FGSearchFlight : function() {
            if (TLUtils.isMobileClientJS()) {
                MobileCalendar.initSearchReturn($('#dtaBMonthYear'), $('#dtaBDay'), $('#dtaEMonthYear'), $('#dtaEDay'));             
            }
            TLUtils.activateChildAge();                     
        }
    },
    FltAddTraveller : {
    	init : function() { },
    	FGSearchFlight : function() {
    		if(TLUtils.isMobileClientJS()) {
    			TLUtils.addTravellerChildAges();
    		}
    	}
    },
    TripAvail :{
        init : function() { },
        FGSearchFlight : function() {
            TLUtils.activateChildAge();            
            TLUtils.refreshMobileForm();
            
        }
    },
    HtlSearch :{
        init : function() { },
        FGSearchHotel  : function() {
            TLUtils.activateChildAge();
        }
    },
//    HtlAvail : {
//    	init : function() {},
//    	FGSearchHotel : function() {
//    		TLUtils.activateChildAge();
//    	}
//    },
    PkgSearch :{
        init : function() { },
        FGPackageSearch   : function() {
            TLUtils.activateChildAge();
        }
    },
    CarSearch : {
    	init : function() { },
    	FGCarSeacrh : function() {
    		TLUtils.activateChildAge();
    	}
    }
    
//    PkgAvail : {
//    	init : function() {},
//    	FGPackageSearch : function() {
//    		TLUtils.activateChildAge();
//    	}
//    }
};




var searchPage = searchPage || {};
searchPage.calendars = function(json) {
    alert(json.currentText);
};

UTIL = {

    fire : function(func, funcname, args) {

        var namespace = APP;  // indicate your obj literal namespace here

        funcname = (funcname === undefined) ? 'init' : funcname;
        if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function') {
            namespace[func][funcname](args);
        }

    },

    loadEvents : function() {

        var bodyId = document.body.id;
        // hit up common first.
        UTIL.fire('common');
        // do all the classes too.
//    $.each(document.body.className.split(/\s+/),function(i,classnm){
        if ($(document.body).attr("data-flow-state")) {
            $.each($(document.body).attr("data-flow-state").split(/\s+/), function(i, classnm) {
                UTIL.fire(classnm);
                UTIL.fire(classnm, ($(document.body).attr("data-flow-group")));
            });
        }
        UTIL.fire('common', 'finalize');
    }

};
// kick it all off here
$(document).ready(UTIL.loadEvents);



