jQuery.noConflict();

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function resetHash(){
  var curLocation = new String(location.pathname);
  var hashTest = /(brand|blitz)\//gi;
  location.href = curLocation.replace(hashTest, '$1#/');
}

function checkLastLocation(){
  var curLocation = new String(location.pathname);
  var hashTest = /(.*)(brand|blitz)\/(.*)/gi;
  var cId = curLocation.replace(hashTest, '$3')
  return cId;
}

function updateContentFromXML(req) {
  var lp = $('landing-page');
  
  var responseHandler = $('response-handler');
  
  var introContent = $$('.IntroContent')[0];
  var mainContent = $$('.MainContent')[0];
  var rightContent = $$('.RightContent')[0];
  
  //responseHandler.innerHTML = req.responseText;
  try{
    introContent.innerHTML = Element.select(responseHandler, '.IntroContent')[0].innerHTML;
  }catch(e){
    //console.log(e);
  }
  try{
    mainContent.innerHTML = Element.select(responseHandler, '.MainContent')[0].innerHTML;
  }catch(e){
    //console.log(e);
  }
  try{
    //console.log(rightContent);
    //console.log(Element.select(responseHandler, '.RightContent')[0].innerHTML);
    //rightContent.innerHTML = ''
    rightContent.innerHTML = Element.select(responseHandler, '.RightContent')[0].innerHTML;
  }catch(e){
    //console.log(e);
  }
  
  responseHandler.innerHTML = '';
  
  if ('cmsMenusUI' in window) cmsMenusUI.build();
}


(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.landingPages = function() {
    if (!this[0]) {
      return false;
    }
    
    var curHash = window.location.hash.substr(2);
    var hashMod = ''//'/hvcb';
	
	if (curHash.length > 0 && $.trim(curHash) !== '') {
      modules.sendAsync({
        updateElem:'response-handler',
        url:SiteVars.path_to_top + hashMod +  SiteVars.this_url + '/' + curHash.toLowerCase(), //+ '?async=true&parent_path_to_top='+SiteVars.path_to_top,
        onComplete: function(req) {
          updateContentFromXML(req);
          setActiveStyleSheet(curHash); // Function to change stylesheet
        }
      });
    }
    
    else{
      var spoof = checkLastLocation();
      setActiveStyleSheet(spoof);
    }
    
    checkHash = function() {
      var newHash = window.location.hash.substr(2);
      if (newHash.length > 0 && $.trim(newHash) !== '' && newHash !== curHash) {
        curHash = newHash;
        modules.sendAsync({
          updateElem:'response-handler',
          url:SiteVars.path_to_top + hashMod +  SiteVars.this_url + '/' + curHash.toLowerCase(), // + '?async=true&parent_path_to_top='+SiteVars.path_to_top,
          onComplete: function(req) {
            updateContentFromXML(req);
            setActiveStyleSheet(curHash); // Function to change stylesheet
          }
        });
      };
      window.setTimeout("checkHash()", 100);
    };
    
    var t = window.setTimeout("checkHash()", 100);
    
    return this;
  };
  
  //{{{ $.fn.landingPagesBAK = function() 
  $.fn.landingPagesBAK = 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);
		  setActiveStyleSheet(curHash); // Function to change stylesheet
        }
      });
    }
    
    var x_updateContentFromXML = function(xml) {
      var lp = $('#landing-page');
      
      $('h1', 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><tbody><tr><td class="row1"><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><tr><td class="row2"><p>' + $(this).attr('text_blurb') + '</p></td></tr><tr><td class="row3"><a href="' + $(this).attr('href') + '" title="View Offer" target="_blank">View Offer</a></td></tr></tbody></table></li>');
        i++;
      });
      if (l === 0) {
        $('.island-offers').hide();
      }
	  
    };
    
    x_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);
			setActiveStyleSheet(curHash); // Function to change stylesheet
          }
        });
      };
      window.setTimeout("checkHash()", 100);
    };
    
    var t = window.setTimeout("checkHash()", 100);
    
    return this;
  }; //}}}


  $(window).load(function(){
    $('#landing-page').landingPages();
  });
  
  $(function() {
    //$('.dwidget-weather').weatherWidget();
    //$('.dwidget-directory').directoryWidget();
    //$('#landing-page').landingPages();
    
    /* $(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);
