jQuery.noConflict();

(function($) {
  var ltie7 = $.browser.msie && $.browser.version < 7;
  var ieAll = $.browser.msie;
  var operaAll = $.browser.opera;
  
  $.fn.hover = function() {
    if (!ltie7) {
      return this;
    }

    this.mouseover(function() {
      $(this).addClass('hover');
    }).mouseout(function() {
      $(this).removeClass('hover');
    });

    return this;
  };

  $.fn.weatherWidget = function() {
    var that = this;
    var t;

    this.find('.forecast-toggle a').click(function() {
      if (!ltie7) {
        that.find('.forecast').slideToggle("300"); 
      } else {
        that.find('.forecast').toggle();
      }
      if ($(this).parent().toggleClass('forecast-collapse').hasClass('forecast-collapse')) {
        $(this).text(SiteVars.modules.Weather.phrases.collapse);
      } else {
        $(this).text(SiteVars.modules.Weather.phrases.forecast);
      }
      return false;
    });

    this.find('.dropdown').click(function() {
      $(this).find('.dropdown-list').toggle().parent().mouseout(function() {
        var that = $(this);
        t = setTimeout(function() {
          that.find('.dropdown-list').hide().parent().unbind('mouseout');
        }, 300);
      }).mouseover(function() {
        clearTimeout(t);
      });
      return false;
    });

    this.find('.dropdown a').click(function() {
      $(this).parent().parent().hide().prev().text($(this).text()).parent().unbind('mouseout');
      if (that.find('.selected .forecast').is(':hidden')) {
        that.find('.forecast').hide();
      }
      that.find('.dwidget-weather-container').removeClass('selected').filter('#dropdown_' + $(this).attr('class')).addClass('selected');
      return false;
    });

    return this;
  };
  
  $.fn.addEvent = function(){
    var that = this;
    
    this.find('.dropdown').click(function() {
        if(!$(this).hasClass('disabled')){
          var t;
          $(this).css('z-index', 1000);
          $(this).find('.dropdown-list').toggle().parent().mouseout(function() {
            var that = $(this);
            t = setTimeout(function() {
              that.css('z-index', 100).find('.dropdown-list').hide().parent().unbind('mouseout');
            }, 300);
          }).mouseover(function() {
            clearTimeout(t);
          });
        }
      return false;
    });
      
    this.find('.disabled').click(function() {
        return false;
    });
    
    this.find('.dropdown a').click(function() {
      var inputSt = $(this).parent().parent().parent().parent().find('input');
      
      $(this).parent().parent().prev().text($(this).text()).parent().mouseout();
      inputSt.val($(this).attr('class'));
      return false;
    });
    
    
    this.find('.checkbox-option').click(function() {
        
      var input = $(this).toggleClass('selected').find('input');
      if(input[1]){
        input[1].checked = (input[1].checked) ? false : true;
        input[0].value = (input[1].checked) ? 'true' : 'false';
      }
      else{
        input[0].checked = (input[0].checked) ? false : true;
        var parentList = input.parent().parent().parent();
        parentList.find('.master-checkbox')[0].value = getMultiValueOverride(input[0]);
        //console.log(parentList.find('.master-checkbox')[0]);
      }
      
    });
  }
  
  $.fn.searchFilter = function() {
    var that = this;
    
    //console.log(this.find('.dropdown'));
    
    this.find('.dropdown').click(function() {
    		
      var t;
      $(this).css('z-index', 10000);
      $(this).find('.dropdown-list').toggle().parent().mouseout(function() {
        var that = $(this);
        t = setTimeout(function() {
          that.css('z-index', 100).find('.dropdown-list').hide().parent().unbind('mouseout');
        }, 300);
      }).mouseover(function() {
        clearTimeout(t);
      });
      return false;
    });
      
    this.find('.dropdown a').click(function() {
    		//console.log('in dropdown click to a href');
      var inputSt = $(this).parent().parent().parent().parent().find('input');
      $(this).parent().parent().prev().text($(this).text()).parent().mouseout();
      inputSt.val($(this).attr('class'));
      	//console.log('inputSt value: '+inputSt[0].value);
      	//console.log('inputSt id: '+inputSt[0].id);
      Event.fire(inputSt[0], 'dropdown:change');
      
      return false;
    });
    
    this.find('.checkbox-list li').click(function() {
      var input = $(this).toggleClass('selected').find('input');
      input[0].checked = (input[0].checked) ? false : true;
      search.runTypeCheck($(this));
    }).hover();
    
    this.find('.radio-list li').click(function() {
      var input = $(this).toggleClass('selected').find('input');
      input[1].checked = (input[1].checked) ? false : true;
      input[0].checked = (!input[1].checked) ? true : false;
      /*search.runTypeCheck($(this));*/
    }).hover();
    
    
    
    var sDate = new Date();
    var eDate = new Date(sDate.getFullYear(), sDate.getMonth() + 4, sDate.getDate());
    
    var defaultSDateString = SiteVars.modules.Vacation.month_phrases[sDate.getMonth()] + ' ' + sDate.getDate() + ', ' + sDate.getFullYear();
    var defaultEDateString = SiteVars.modules.Vacation.month_phrases[eDate.getMonth()] + ' ' + eDate.getDate() + ', ' + eDate.getFullYear() 
    
    var sDateField = this.find('.input-date input').eq(0);
    var eDateField = this.find('.input-date input').eq(1);
    
    //Added after discussion started in FLY-906
    //removed for MVN-1089
    //sDateField.val(defaultSDateString);
    //eDateField.val(defaultEDateString);
    
    var nextYear = sDate.getFullYear()+1;
    var useYearRange = sDate.getFullYear()+':'+nextYear;
    
    this.find('.input-date input').eq(0).datepicker({dateFormat: 'MM d, yy', currentText:SiteVars.modules.Vacation.thismonth, buttonImage: '/template_images/hvcb/dwidget/calendar.png', buttonImageOnly: true, changeYear: true, yearRange: useYearRange , showOn: 'both', showButtonPanel: true, onSelect: function(dateText) {    
    }});
    this.find('.input-date input').eq(1).datepicker({dateFormat: 'MM d, yy', currentText:SiteVars.modules.Vacation.thismonth, buttonImage: '/template_images/hvcb/dwidget/calendar.png', buttonImageOnly: true, changeYear: true,  yearRange: useYearRange, showOn: 'both', showButtonPanel: true, defaultDate: +7});
    this.find('.input-date .calendar_icon').click(function() {
      $(this).prev().datepicker('show');
    });

  };
  
  $.fn.directoryWidget = function() {
    var that = this;
    
    /*this.find('#directory_act_island, #directory_act_category, #directory_lodging_island, #directory_events_island').val();
    this.find('#activity-category .dropdown').hide().filter('#activity-category-all').show();
    this.find('#activity-types .field, #lodging-types .field, #events-types .field').hide().filter('#activity-types-all-all, #lodging-types-all, #events-types-all').show();*/
    
    this.find('#directory_act_island').val(this.find('#directory_act_island').prev().find('strong').attr('class'));
    this.find('#directory_lodging_island').val(this.find('#directory_lodging_island').prev().find('strong').attr('class'));
    this.find('#directory_events_island').val(this.find('#directory_events_island').prev().find('strong').attr('class'));
    this.find('#directory_act_category').val(this.find('#activity-category-' + this.find('#directory_act_island').val()).find('strong').attr('class'));
    this.find('#activity-category .dropdown, #activity-types .field, #lodging-types .field, #events-types .field').hide().filter('#activity-category-' + this.find('#directory_act_island').val() + ', #activity-types-' + this.find('#directory_act_island').val() + '-' + this.find('#directory_act_category').val() + ', #lodging-types-' + this.find('#directory_lodging_island').val() + ', #events-types-' + this.find('#directory_events_island').val()).show();
      
    
     $('#activity-types').show();
    
    /* this.find('#nav-dir a').click(function() {
      $(this).parent().parent().attr('class', '').addClass($(this).parent().attr('id'));
      that.find('.dwidget-directory-tab').hide().filter('#directory-activities').find('fieldset:not(.intro)').hide();
      that.find('#' + $(this).attr('href').replace('#','')).show();
      return false;
      }); */ //AT:commenting code that is not on qa
      
    this.find('#nav-dir a').click(function() {
      $(this).parent().parent().attr('class', '').addClass($(this).parent().attr('id'));
      //[AT: commenting this out so that the sub type selection will be displayed when clicking back and forth between tabs]
      that.find('.dwidget-directory-tab').hide();//.filter('#directory-activities').find('fieldset:not(.intro)').hide(); 
      //that.find('#' + $(this).attr('href').replace('#','')).show();
      //var url = that.find('#' + $(this).attr('href'));
      var url = $(this).attr('href');
      if (url.indexOf('http') > -1) {
        url = url.substring(url.indexOf('#'));  
      }
      
      that.find('#' + url).show();
      
      
      
      
      return false;
    });
    
    this.find('.dropdown').click(function() {
      var t;
      
      $(this).css('z-index', 10000);
      
      if ($(this).parent().attr('id') === 'activity-category') {
        $(this).parent().parent().siblings().show();
      }
      
      $(this).find('.dropdown-list').toggle().parent().mouseout(function() {
        var that = $(this);
        t = setTimeout(function() {
          that.css('z-index', 100).find('.dropdown-list').hide().parent().unbind('mouseout');
        }, 300);
      }).mouseover(function() {
        clearTimeout(t);
      });
      return false;
    });
    
    /* this.find('.dropdown a').click(function() {
      var inputSt = $(this).parent().parent().parent().parent().find('input');
      
      $(this).parent().parent().prev().text($(this).text()).parent().mouseout();
      inputSt.val($(this).attr('class'));
      
      if (inputSt.attr('id') === 'directory_act_island') {
        $('#activity-category .dropdown').hide();
        $('#activity-category-' + inputSt.val()).show();
        $('#activity-types .field').hide();
        $('#activity-types-' + inputSt.val() + '-' + $('#directory_act_category').val()).show();
      } else if (inputSt.attr('id') === 'directory_act_category') {
        $('#activity-types .field').hide();
        $('#activity-types-' + $('#directory_act_island').val() + '-' + inputSt.val()).show();
      } else if (inputSt.attr('id') === 'directory_lodging_island') {
        $('#lodging-types .field').hide();
        $('#lodging-types-' + inputSt.val()).show();
      } else if (inputSt.attr('id') === 'directory_events_island') {
        $('#events-types .field').hide();
        $('#events-types-' + inputSt.val()).show();
      }
      
      return false;
      }); */ //AT:not on qa
      
    this.find('.dropdown a').click(function() {
      var inputSt = $(this).parent().parent().parent().parent().find('input');
      
      $(this).parent().parent().prev().text($(this).text()).parent().mouseout();
      inputSt.val($(this).attr('class'));
      
      if (inputSt.attr('id') === 'directory_act_island') {
        // get original value
        var orig = $('#directory_act_category');
        
        
        
        $('#activity-category .dropdown').hide();
        $('#activity-category-' + inputSt.val()).show();
        $('#activity-types .field').hide();
        $('#activity-types-' + inputSt.val() + '-' + $('#directory_act_category').val()).show();
        
       // console.log('set activity-category to correct value ' + orig.val());
        
        
        if (orig.val() != 'all') {
          // reset the whole thing
          var new_dropdown = $('#activity-category-' + inputSt.val());
          
          
          $('#directory_act_category').val('');
          
          if (!$('#activity-category').is(':hidden')) {
        
            var open_field = $('#activity-category').find('.dropdown').not(':hidden');
            var first_field = open_field.find('.dropdown-list li a');
            
            open_field.find('strong.all').eq(0).text(first_field.eq(0).text());
           // $('#activity-types').hide();
             $('#activity-types').show();
            
          }
          
          
          /*
          // now switch to right category
          var new_dropdown = $('#activity-category-' + inputSt.val());
          var dd = new_dropdown.find('.dropdown-list li a.' + orig.val()).eq(0);
          
          if (dd.text() != '') {
            //console.log('switching.... ' + dd.text());
            new_dropdown.find('strong.all').eq(0).text(dd.text());
          } else {
            // reset to all categories
            $('#directory_act_category').val('');
          }
          */
        } 
        
        search.setMId(inputSt);
        
      } else if (inputSt.attr('id') === 'directory_act_category') {
        $('#activity-types .field').hide();
        $('#activity-types-' + $('#directory_act_island').val() + '-' + inputSt.val()).show();
      } else if (inputSt.attr('id') === 'directory_lodging_island') {
        $('#lodging-types .field').hide();
        $('#lodging-types-' + inputSt.val()).show();
        
        search.setMId(inputSt);
        
      } else if (inputSt.attr('id') === 'directory_events_island') {
        $('#events-types .field').hide();
        $('#events-types-' + inputSt.val()).show();
        
        search.setMId(inputSt);
      }
      
      return false;
    });
    
    this.find('.dwidget-checkbox-list li').click(function() {
      var input = $(this).toggleClass('selected').find('input');
      input[0].checked = (input[0].checked) ? false : true;
      //console.log(input);
      //console.log(input[0].checked);
      search.runTypeCheck($(this));
    }).hover();
    
    
    var sDate = new Date();
    var eDate = new Date(sDate.getFullYear(), sDate.getMonth() + 4, sDate.getDate());
    
    var defaultSDateString = SiteVars.modules.Vacation.month_phrases[sDate.getMonth()] + ' ' + sDate.getDate() + ', ' + sDate.getFullYear();
    var defaultEDateString = SiteVars.modules.Vacation.month_phrases[eDate.getMonth()] + ' ' + eDate.getDate() + ', ' + eDate.getFullYear() 
    
    var sDateField = this.find('.input-date input').eq(0);
    var eDateField = this.find('.input-date input').eq(1);
    
    sDateField.val(defaultSDateString);
    eDateField.val(defaultEDateString);
    
    var nextYear = sDate.getFullYear()+1;
    var useYearRange = sDate.getFullYear()+':'+nextYear;
    
    this.find('.input-date input').eq(0).datepicker({dateFormat: 'MM d, yy', currentText:SiteVars.modules.Vacation.thismonth, buttonImage: '/template_images/hvcb/dwidget/calendar.png', buttonImageOnly: true, changeYear: true, yearRange: useYearRange , showOn: 'both', showButtonPanel: true, onSelect: function(dateText) {    
    }});
    this.find('.input-date input').eq(1).datepicker({dateFormat: 'MM d, yy', currentText:SiteVars.modules.Vacation.thismonth, buttonImage: '/template_images/hvcb/dwidget/calendar.png', buttonImageOnly: true, changeYear: true,  yearRange: useYearRange, showOn: 'both', showButtonPanel: true, defaultDate: +7});
    this.find('.input-date .calendar_icon').click(function() {
      $(this).prev().datepicker('show');
    });

    
    this.find('a.reset').click(function() {
      
      var el = $(this).parent().parent();
      el.parent()[0].reset();
      
      sDateField.val(defaultSDateString);
      eDateField.val(defaultEDateString);
      
      var defaultVal = '';
      
      el.siblings().andSelf().find('div.dropdown').each(function() {
        defaultVal = $(this).find('a.' + $(this).find('strong').attr('class')).text();
        try {
          defaultClass = $(this).find('a.' + $(this).find('strong').attr('class'))[0].className;
          
          if (defaultVal != '') {
            $(this).find('strong').text(defaultVal);
          }
        } catch(e) {}
        
      });
      el.siblings().andSelf().find('ul.dwidget-checkbox-list li').removeClass('selected');
      

      
      // find open category
      if (!$('#activity-category').is(':hidden')) {
        
        var open_field = $('#activity-category').find('.dropdown').not(':hidden');
        var first_field = open_field.find('.dropdown-list li a');
        
        open_field.find('strong.all').eq(0).text(first_field.eq(0).text());
        
        
        $('#activity-types .field').hide();
        $('#activity-types #activity-types-all-' + defaultClass.toLowerCase()).find('.dropdown-list').show();
      }
      
      else if(!$('#lodging-types').is(':hidden')){
        var open_field = $('#lodging-types').find('.field').not(':hidden');
        open_field.hide();
        $('#lodging-types-'+defaultClass.toLowerCase()).show();
      }
      else if(!$('#events-types').is(':hidden')){
        var open_field = $('#events-types').find('.field').not(':hidden');
        open_field.hide();
        $('#events-types-'+defaultClass.toLowerCase()).show();
      }
      
      var island = $('#directory-activities').find('.field .dropdown strong').eq(0);
      
      $('#activity-category .dropdown').hide();
      // now show island
      $('#activity-category-' + island.attr('class')).show(); 
      var all = $('#activity-category-' + island.attr('class')).find('ul li a').eq(0);
      $('#activity-category-' + island.attr('class') + ' strong.all').eq(0).text(all.text()); 
      
      
      // show 
      $('#activity-types').show();
      
      return false;
    });
  };
  
  $.fn.landingPages = function() {
    if (!this[0]) {
      return false;
    }
    
    var curHash = window.location.hash.substr(2);
    var hashMod = $('.content-brand')[0] ? 'brand_' : '';
    
    if (curHash.length > 0 && $.trim(curHash) !== '') {
      $.ajax({
        dataType: 'xml',
        type: 'get',
        url: 'xml/landing_page_' + hashMod + curHash.toLowerCase() + '.xml',
        success: function(xml) {
          updateContentFromXML(xml);
        }
      });
    }
    
    var updateContentFromXML = function(xml) {
      var lp = $('#landing-page');
      
      $('h2', lp).html($('title', xml).text());
      $('.content-full', lp).html($('content', xml).text());
      lp.find('.collapse a').text('More').parent().removeClass('collapse').addClass('expand').prev().hide();
      
      $('p.more', lp).empty();
      $('links link', xml).each(function() {
        $('p.more', lp).append('<a href="' + $(this).attr('href') + '">' + $(this).text() + '</a><br />');
      });
      
      $('sidebar', xml).each(function() {
        $('#sidebar_two').empty().append($(this).text());
      });
            
      $('.island-offers ul.row').remove();
      var i = 0;
      var l = $('offers offer', xml).length;
      $('.island-offers').show();
      $('offers offer', xml).each(function() {
        if (i % 4 === 0) {
          $('.island-offers-inner').append('<ul class="row"></ul>');
        }
        $('.island-offers-inner ul.row:last').append('<li><table><tr><td><a href="' + $(this).attr('href') + '" target="_blank"><img src="' + $(this).attr('img_src') + '" alt="' + $(this).attr('img_alt') + '" height="' + $(this).attr('img_height') + '" width="' + $(this).attr('img_width') + '" /></a></td></tr></table></li>');
        i++;
      });
      if (l === 0) {
        $('.island-offers').hide();
      }
      
      $('meta[name=title]').attr('content', $('fb_share', xml).attr('title'));
      $('meta[name=description]').attr('content', $('fb_share', xml).text());
      $('link[rel=image_src]').attr('href', $('fb_share', xml).attr('img'));
    };
    
    checkHash = function() {
      var newHash = window.location.hash.substr(2);
      if (newHash.length > 0 && $.trim(newHash) !== '' && newHash !== curHash) {
        curHash = newHash;
        $.ajax({
          dataType: 'xml',
          type: 'get',
          url: 'xml/landing_page_' + hashMod + curHash.toLowerCase() + '.xml',
          success: function(xml) {
            updateContentFromXML(xml);
          }
        });
      };
      window.setTimeout("checkHash()", 100);
    };
    
    var t = window.setTimeout("checkHash()", 100);
    
    return this;
  };

  $(function() {
    /* $('.dwidget-weather').weatherWidget();
    $('.dwidget-directory').directoryWidget(); */
    $('.hvcbsitesearchform').searchFilter();
    $('#landing-page').landingPages();
    $('#event-form').addEvent();
    
    
    
    /* $(function() {
      $('#search-filter').find('.input-date input').eq(0).datepicker({dateFormat: 'MM d, yy', buttonImage: 'template_images/hvcb/icon-calendar.png', buttonImageOnly: true, showOn: 'both', showButtonPanel: true, onSelect: function(dateText) {
        $('#search-filter').find('.input-date input').eq(1).val(dateText);
      }});
      $('#search-filter').find('.input-date input').eq(1).datepicker({dateFormat: 'MM d, yy', buttonImage: 'template_images/hvcb/icon-calendar.png', buttonImageOnly: true, showOn: 'both', showButtonPanel: true, defaultDate: +7});
    }); */
  });
})(jQuery);

