//*****Replace align attribute with class**************************************************//

function replaceTarget() {
  if (!document.getElementsByTagName('a')) return false;
  $('a[target]').addClass('newwindow').removeAttr('target');
}

// NEWSLETTER POPUP
function newsletter() {
  // Set Height of Overlay
  $('#overlayLoad').css('height',$(document).height());
  // Fade In Overlay
  $('#overlayLoad').fadeIn();
  $('#signUp').center();
  $('#signUp .closeSignUp').click(function() {
    $('#signUp').fadeOut();
    $('#overlayLoad').fadeOut();
  });
}

function popUp() {
  
  // LANDSCAPING
  $('h2.landscapingPopUpLink').click(function() {
    // Create Overlay
    var overlay = '<div id="overlay" style="background: #fff; width: 100%; position: absolute; top: 0; left: 0; z-index: 999; opacity: .4; -moz-opacity: .4; filter: alpha(opacity=40);>"';
        overlay += '</div>';
    $('body').append(overlay);
    // Set Height of Overlay
    $('#overlay').css('height',$(document).height());
    // Fade In Overlay
    $('#overlay').fadeIn();
    $("#loading").ajaxStart(function(){
      $(this).show();
    }).ajaxStop(function(){
      $(this).hide();
    });
    // Display Correct Pop Up
    $('.popUp').fadeOut();
    $('.landscapingPopUp').fadeIn();
    // Close the Pop Up
    $('.close').click(function() {
      $('.popUp').fadeOut();
      $('#overlay').fadeOut();
    });
    // Disable default action
    return false;
  });
  
  // CONSTRUCTION
  $('h2.constructionPopUpLink').click(function() {
    // Create Overlay
    var overlay = '<div id="overlay" style="background: #fff; width: 100%; position: absolute; top: 0; left: 0; z-index: 999; opacity: .4; -moz-opacity: .4; filter: alpha(opacity=40);>"';
        overlay += '</div>';
    $('body').append(overlay);
    // Set Height of Overlay
    $('#overlay').css('height',$(document).height());
    // Fade In Overlay
    $('#overlay').fadeIn();
    $("#loading").ajaxStart(function(){
      $(this).show();
    }).ajaxStop(function(){
      $(this).hide();
    });
    // Display Correct Pop Up
    $('.popUp').fadeOut();
    $('.constructionPopUp').fadeIn();
    // Close the Pop Up
    $('.close').click(function() {
      $('.popUp').fadeOut();
      $('#overlay').fadeOut();
    });
    // Disable default action
    return false;
  });
  
  // MAINTENANCE
  $('h2.maintenancePopUpLink').click(function() {
    // Create Overlay
    var overlay = '<div id="overlay" style="background: #fff; width: 100%; position: absolute; top: 0; left: 0; z-index: 999; opacity: .4; -moz-opacity: .4; filter: alpha(opacity=40);>"';
        overlay += '</div>';
    $('body').append(overlay);
    // Set Height of Overlay
    $('#overlay').css('height',$(document).height());
    // Fade In Overlay
    $('#overlay').fadeIn();
    $("#loading").ajaxStart(function(){
      $(this).show();
    }).ajaxStop(function(){
      $(this).hide();
    });
    // Display Correct Pop Up
    $('.popUp').fadeOut();
    $('.maintenancePopUp').fadeIn();
    // Close the Pop Up
    $('.close').click(function() {
      $('.popUp').fadeOut();
      $('#overlay').fadeOut();
    });
    // Disable default action
    return false;
  });
  
  // SNOW REMOVAL
  $('h2.snowRemovalPopUpLink').click(function() {
    // Create Overlay
    var overlay = '<div id="overlay" style="background: #fff; width: 100%; position: absolute; top: 0; left: 0; z-index: 999; opacity: .4; -moz-opacity: .4; filter: alpha(opacity=40);>"';
        overlay += '</div>';
    $('body').append(overlay);
    // Set Height of Overlay
    $('#overlay').css('height',$(document).height());
    // Fade In Overlay
    $('#overlay').fadeIn();
    $("#loading").ajaxStart(function(){
      $(this).show();
    }).ajaxStop(function(){
      $(this).hide();
    });
    // Display Correct Pop Up
    $('.popUp').fadeOut();
    $('.snowRemovalPopUp').fadeIn();
    // Close the Pop Up
    $('.close').click(function() {
      $('.popUp').fadeOut();
      $('#overlay').fadeOut();
    });
    // Disable default action
    return false;
  });
  
  // DIY
  $('h2.diyPopUpLink').click(function() {
    // Create Overlay
    var overlay = '<div id="overlay" style="background: #fff; width: 100%; position: absolute; top: 0; left: 0; z-index: 999; opacity: .4; -moz-opacity: .4; filter: alpha(opacity=40);>"';
        overlay += '</div>';
    $('body').append(overlay);
    // Set Height of Overlay
    $('#overlay').css('height',$(document).height());
    // Fade In Overlay
    $('#overlay').fadeIn();
    $("#loading").ajaxStart(function(){
      $(this).show();
    }).ajaxStop(function(){
      $(this).hide();
    });
    // Display Correct Pop Up
    $('.popUp').fadeOut();
    $('.diyPopUp').fadeIn();
    // Close the Pop Up
    $('.close').click(function() {
      $('.popUp').fadeOut();
      $('#overlay').fadeOut();
    });
    // Disable default action
    return false;
  });
  
  $('img.greenGuysLogo').click(function() {
    // Create Overlay
    var overlay = '<div id="overlay" style="background: #fff; width: 100%; position: absolute; top: 0; left: 0; z-index: 999; opacity: .4; -moz-opacity: .4; filter: alpha(opacity=40);">';
        overlay += '</div>';
    $('body').append(overlay);
    // Set Height of Overlay
    $('#overlay').css('height',$(document).height());
    // Fade In Overlay
    $('#overlay').fadeIn();
    // Display Correct Pop Up
    $('.popUp').fadeOut();
    $('.greenGuysPopUp').fadeIn();
    // Close the Pop Up
    var close = '<p class="close">;'
        close += '</p>';
    $('.greenGuysPopUp .popUpContent').append(close);
    $('.close').click(function() {
      $('.popUp').fadeOut();
      $('#overlay').fadeOut();
    });
    // Disable default action
    return false;
  });
}

function logoFader() {
  // LANDSCAPING
  $('h2.landscapingPopUpLink').hover(function() {
    $('img.logo').stop().animate({'opacity' : 0});
    $('img.landscapingLogo').stop().animate({'opacity' : 1});
    $('h1.title').stop().animate({'opacity': 0 });
    $('h1.landscaping').stop().animate({'opacity' : 1});
  }, function() {
    $('img.logo').stop().animate({'opacity' : 0});
    $('img.greenGuysLogo').stop().animate({'opacity' : 1});
    $('h1.title').stop().animate({'opacity': 0 });
    $('h1.greenGuys').stop().animate({'opacity' : 1});
  });
  // CONSTRUCTION
  $('h2.constructionPopUpLink').hover(function() {
    $('img.logo').stop().animate({'opacity' : 0});
    $('img.constructionLogo').stop().animate({'opacity' : 1});
    $('h1.title').stop().animate({'opacity': 0 });
    $('h1.construction').stop().animate({'opacity' : 1});
  }, function() {
    $('img.logo').stop().animate({'opacity' : 0});
    $('img.greenGuysLogo').stop().animate({'opacity' : 1});
    $('h1.title').stop().animate({'opacity': 0 });
    $('h1.greenGuys').stop().animate({'opacity' : 1});
  });
  // MAINTENANCE
  $('h2.maintenancePopUpLink').hover(function() {
    $('img.logo').stop().animate({'opacity' : 0});
    $('img.maintenanceLogo').stop().animate({'opacity' : 1});
    $('h1.title').stop().animate({'opacity': 0 });
    $('h1.maintenance').stop().animate({'opacity' : 1});
  }, function() {
    $('img.logo').stop().animate({'opacity' : 0});
    $('img.greenGuysLogo').stop().animate({'opacity' : 1});
    $('h1.title').stop().animate({'opacity': 0 });
    $('h1.greenGuys').stop().animate({'opacity' : 1});
  });
  // SNOW REMOVAL
  $('h2.snowRemovalPopUpLink').hover(function() {
    $('img.logo').stop().animate({'opacity' : 0});
    $('img.snowRemovalLogo').stop().animate({'opacity' : 1});
    $('h1.title').stop().animate({'opacity': 0 });
    $('h1.snowRemoval').stop().animate({'opacity' : 1});
  }, function() {
    $('img.logo').stop().animate({'opacity' : 0});
    $('img.greenGuysLogo').stop().animate({'opacity' : 1});
    $('h1.title').stop().animate({'opacity': 0 });
    $('h1.greenGuys').stop().animate({'opacity' : 1});
  });
  // DIY
  $('h2.diyPopUpLink').hover(function() {
    $('img.logo').stop().animate({'opacity' : 0});
    $('img.diyLogo').stop().animate({'opacity' : 1});
    $('h1.title').stop().animate({'opacity': 0 });
    $('h1.diy').stop().animate({'opacity' : 1});
  }, function() {
    $('img.logo').stop().animate({'opacity' : 0});
    $('img.greenGuysLogo').stop().animate({'opacity' : 1});
    $('h1.title').stop().animate({'opacity': 0 });
    $('h1.greenGuys').stop().animate({'opacity' : 1});
  });
}

function secondaryNav() {
  $("ul#secondaryNav li a").hover(function () {
    $(this).stop().animate({'left' : '10px'});
  }, function() {
    $(this).stop().animate({'left' : '0'});
  });
}

  /***** CLEAR SEARCH *****/
function clearSearchBox() {
  if (!document.getElementById('text')) return false;
  $('#text').focus(function() {
    if ($(this).val() == 'SEARCH') {
      $(this).val('');
    }
  });
}

  /***** INITIALIZE CYCLE *****/
function initializeCycle() {
  if (!$('.pics').length > 0) return false;
  $('.pics').cycle('fade');
}

  /***** TOGGLE BUCKETS *****/
function toggleBuckets() {
  $('.toggle').show();
  $('.toggleWrap h4').addClass('active');
  $('.toggleWrap h4').click(function () {	
	$(this).toggleClass('active');
	$(this).toggleClass('inactive');
  	$(this).parent().find('.toggle').slideToggle();
  });
}

//*****Load all functions**************************************************//
$(document).ready(function(){
  replaceTarget();
  newsletter();
  popUp();
  logoFader();
  secondaryNav();
  $('#loading').fadeOut();
  clearSearchBox();
  initializeCycle();
  toggleBuckets();
});
