jQuery(document).ready(function ($) {    
    window.commonLeftMenuIE = new function () {
        
        this.selected_section = false;
        this.selected_continent = false;
        this.selected_country = false;
        this.selected_state = false;
        this.selected_city = false;
        this.selected_category = false;
        
        this.no_geo_params = false;
        
        this.stored_selects = [];
        
        this.init_passed = false;
        
        this.callurl = jsBaseURL + '/commonpanels/dmenus/common_menu_handler.php';

        this.reloadSelect = function (args, target_cont, target_id,default_selected) {
            jQuery('#' + target_cont + ' select').empty();
            this.blockMenu();
            jQuery.post(this.callurl, args, function (resp) {
                if(jQuery('#' + target_cont + ' select#'+target_id) .length == 0) {                
                    jQuery('#' + target_cont).append('<select id="' + target_id + '"></select>');
                }
                jQuery(resp.results).each(function (idx, opt_elt) {
                    jQuery('#' + target_id).append($('<option></option>').val(opt_elt.opt_val).text(opt_elt.opt_text).data('opturl', opt_elt.opt_url));
                });
                
                //let's check for default selected value
                if (jQuery('#' + target_id + ' option[value="'+default_selected+'"][text!="All/Any"]').length > 0 ) {
                    jQuery('#' + target_id + ' option[value="'+default_selected+'"][text!="All/Any"]').attr('selected','selected');
                } 
                
                window.commonLeftMenuIE.unblockMenu();
                
                //Make sure that only selects with searchable values are enabled
                jQuery('#left-menu-ie-cont select').each(function(idx,elt){                   
                    //check for normal items in select
                    if( jQuery(elt).children('option').not('[value="n/a"]').length == 0 ) {
                        jQuery(elt).attr('disabled','disabled');
                        jQuery(elt).addClass('disabled');
                    } else {                       
                        jQuery(elt).removeAttr('disabled');
                        jQuery(elt).removeClass('disabled');
                    }
                   
                });
                
            });
        };       
        
        this.getDefaultArgs = function() {
            var args = {
                'section_id':this.selected_section,
                'continent_id':this.selected_continent,  
                'country_id':this.selected_country,
                'state_id':this.selected_state,
                'city_id':this.selected_city,
                'category_id':this.selected_category
            };
            if(this.no_geo_params) {
                args.continent_id = '';
                args.country_id = '';
                args.state_id = '';
                args.city_id = '';
            }
            return args;
        }
                
        this.initSection = function () {
            var args = this.getDefaultArgs();
            args.act = 'get_sections';
            this.reloadSelect(args,'left_menu_step1_cont','common_section_select',this.selected_section);
        }
        
        this.initContinent = function () {
            var args = this.getDefaultArgs();
            args.act = 'get_continents';
            this.reloadSelect(args,'left_menu_step2_cont','common_continent_select',this.selected_continent);
        }
        
        this.initCountry = function () {
            var args = this.getDefaultArgs();
            args.act = 'get_countries';
            this.reloadSelect(args,'left_menu_step3_cont','common_country_select',this.selected_country);
        }
        
        this.initState = function () {
            var args = this.getDefaultArgs();
            args.act = 'get_states';
            this.reloadSelect(args,'left_menu_step4_cont','common_state_select',this.selected_state);
        }
        this.initCity = function () {
            var args = this.getDefaultArgs();
            args.act = 'get_cities';
            this.reloadSelect(args,'left_menu_step5_cont','common_city_select',this.selected_city);
        }
        this.initCategory = function () {
            var args = this.getDefaultArgs();
            args.act = 'get_categories';
            this.reloadSelect(args,'left_menu_step6_cont','common_category_select',this.selected_category);
        }
        
        //Blocking menu selection
        this.blockMenu = function() {
            jQuery('#left-menu-ie-cont select').each(function (idx,elt) {
                jQuery(elt).attr('readonly','readonly');
            });
        }
        
        //Unblocking menu selection
        this.unblockMenu = function() {
            jQuery('#left-menu-ie-cont select').each(function (idx,elt) {
                jQuery(elt).removeAttr('readonly');
            });
        }
        
        this.doChangeProcess = function (evt) {                                        
            
            if(evt.currentTarget.id == 'common_section_select') {
                //Is new section differ from selected ?
                if(this.selected_section != jQuery('#left_menu_step1_cont select option:selected').val()) {
                    //Clearing other dropdowns selection
                    this.selected_continent = '';
                    this.selected_country = '';
                    this.selected_state = '';
                    this.selected_city = '';
                //this.selected_category = '';
                }
                
                this.selected_section = jQuery('#left_menu_step1_cont select option:selected').val();
                
                this.initContinent();
                this.initCountry();
                this.initState();
                this.initCity();
                this.initCategory();
            }
            
            if(evt.currentTarget.id == 'common_continent_select') {
                
                if(this.selected_continent != jQuery('#left_menu_step2_cont select option:selected').val()) {
                    this.selected_country = '';
                    this.selected_state = '';
                    this.selected_city = '';
                //this.selected_category = '';
                }
                this.selected_continent = jQuery('#left_menu_step2_cont select option:selected').val();
                
                this.initCountry();
                this.initState();
                this.initCity();
                this.initCategory(); 
            }
            
            if(evt.currentTarget.id == 'common_country_select') {
                if(this.selected_country != jQuery('#left_menu_step3_cont select option:selected').val()) {
                    this.selected_state  = '';
                    this.selected_city = '';
                //this.selected_category = '';
                }
                this.selected_country = jQuery('#left_menu_step3_cont select option:selected').val();
                
                this.initState();
                this.initCity();
                this.initCategory(); 
            }
            
            if(evt.currentTarget.id == 'common_state_select') {
                
                if(this.selected_state != jQuery('#left_menu_step4_cont select option:selected').val()) {
                    this.selected_city = '';
                //this.selected_category = '';
                }
                this.selected_state = jQuery('#left_menu_step4_cont select option:selected').val();
                
                this.initCity();
                this.initCategory(); 
            }
            
            if(evt.currentTarget.id == 'common_city_select') {
                if(this.selected_city != jQuery('#left_menu_step5_cont select option:selected').val()) {
                //this.selected_category = '';
                }
                this.selected_city = jQuery('#left_menu_step5_cont select option:selected').val();
                
                this.initCategory();
            }
            
            if(evt.currentTarget.id == 'common_category_select') {
                if(this.selected_category != jQuery('#left_menu_step6_cont select option:selected').val()) {                    
                    //need to reload using new category only (no geo-params)
                    this.no_geo_params = true;
                    
                    //Reloading
                    this.initContinent();
                    this.initCountry();
                    this.initState();
                    this.initCity();
                    
                    this.no_geo_params = false;
                }
                
                this.selected_category = jQuery('#left_menu_step6_cont select option:selected').val();
                
                if(this.selected_category == 'n/a' || this.selected_continent == 'n/a') {
                    this.initContinent();
                    this.initCountry();
                    this.initState();
                    this.initCity();                    
                }
            }
            
            this.checkSelectedParams();
        }
        
        this.checkSelectedParams = function () {
            var can_proceed = false;
            if(this.selected_section == 'extreme') {
                //at least country and/or category must be selected
                if(this.selected_country != 'n/a' || this.selected_category != 'n/a' ) {
                    can_proceed = true;
                }
            } else if(this.selected_section == 'sightseeing') {
                // at least country and/or category
                if(this.selected_country != 'n/a' || this.selected_category != 'n/a' ) {
                    can_proceed = true;
                }
            } else if (this.selected_section == 'adventure') {
                //at least continent must be selected
                if(this.selected_continent != 'n/a')  {
                    can_proceed = true; 
                }
            } else if (this.selected_section == 'volunteering') {
                //at least continent must be selected
                if(this.selected_continent != 'n/a')  {
                    can_proceed = true; 
                }
            }
            
            if(can_proceed) {
                //enabling "go" button
                jQuery('#left_menu_ie_find').removeAttr('disabled');
                jQuery('#left_menu_ie_find').removeClass('disabled');
            } else {
                //disabling "go" button
                jQuery('#left_menu_ie_find').attr('disabled','disabled');
                jQuery('#left_menu_ie_find').addClass('disabled');
            }
            
            return can_proceed;
        }
        
        this.doFindProcess = function () {
            //Validating selected values
            var can_proceed = this.checkSelectedParams();
            
            if(can_proceed) {
                var args = this.getDefaultArgs();
                args.act = 'get_target_url';
                jQuery.post(this.callurl,args,function(resp) {        
                    if(resp.results.length) {                                               
                        window.location = resp.results[0];
                    }
                });
            }
        }
        
        this.initMenu = function () {            
            //Processing the detected values (if any)
            if(window.commonLeftMenuIE_detect.detect_section != undefined) {
                this.selected_section = window.commonLeftMenuIE_detect.detect_section;
            }
            
            if(window.commonLeftMenuIE_detect.detect_continent != undefined) {
                this.selected_continent = window.commonLeftMenuIE_detect.detect_continent;
            }
            
            if(window.commonLeftMenuIE_detect.detect_country != undefined) {
                this.selected_country = window.commonLeftMenuIE_detect.detect_country;
            }
            
            if(window.commonLeftMenuIE_detect.detect_state != undefined) {
                this.selected_state = window.commonLeftMenuIE_detect.detect_state;
            }
            
            if(window.commonLeftMenuIE_detect.detect_city != undefined) {
                this.selected_city = window.commonLeftMenuIE_detect.detect_city;
            }
            
            if(window.commonLeftMenuIE_detect.detect_category != undefined) {
                this.selected_category = window.commonLeftMenuIE_detect.detect_category;
            }
            
            //Performing menu init
            this.initSection();
            this.initContinent();
            this.initCountry();
            this.initState();
            this.initCity();
            this.initCategory();
            
            this.checkSelectedParams();
            
            
            if(!this.init_passed) {
                //Initializing event handlers
                //Setting event handlers
                jQuery('#left_menu_step1_cont').delegate('select','change',function(evt){
                    window.commonLeftMenuIE.doChangeProcess(evt);
                });
                jQuery('#left_menu_step2_cont').delegate('select','change',function(evt){
                    window.commonLeftMenuIE.doChangeProcess(evt);
                });
                jQuery('#left_menu_step3_cont').delegate('select','change',function(evt){
                    window.commonLeftMenuIE.doChangeProcess(evt);
                });
                jQuery('#left_menu_step4_cont').delegate('select','change',function(evt){
                    window.commonLeftMenuIE.doChangeProcess(evt);
                });
                jQuery('#left_menu_step5_cont').delegate('select','change',function(evt){
                    window.commonLeftMenuIE.doChangeProcess(evt);
                });
                jQuery('#left_menu_step6_cont').delegate('select','change',function(evt){
                    window.commonLeftMenuIE.doChangeProcess(evt);
                });
            
                //onclick handler
                jQuery('#left_menu_ie_find').click(function(){
                    window.commonLeftMenuIE.doFindProcess();
                });
                
                this.init_passed = true;
            }
              
            
        };
    }();
    window.commonLeftMenuIE.initMenu();
});

