//*****Replace align attribute with class**************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName('img')) return false;
  $('img[align="left"]').addClass('left').removeAttr('align');
  $('img[align="right"]').addClass('right').removeAttr('align');
  $('img[align="middle"]').addClass('middle').removeAttr('align');
}
//*****Replace target attribute with class**************************************************//
function replaceTarget() {
  if (!document.getElementsByTagName('a')) return false;
  $('a[target]').addClass('newwindow').removeAttr('target');
}
//*****The following function make it possible to have web standard popups**************************************************//
function strictNewWindow() {
  if (!document.getElementsByTagName('a')) return false;
  $('a.newwindow').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
}
//*****Clear search text out search box on focus**************************************************//
function clearSearchBox() {
  if (!document.getElementById('txtSearch')) return false;
  $('#txtSearch').focus(function() {
    if ($(this).val() == 'SEARCH') {
      $(this).val('');
    }
  });
}

function ContactUsWrap() {
  $('.toggle').hide();
  $('.contactUsWrap h5').addClass('inactive');
  $('.contactUsWrap h5').click(function () {
    if($(this).hasClass('inactive')) {
      $('.toggle').slideUp();
      $('.contactUsWrap h5').removeClass('active');
      $('.contactUsWrap h5').addClass('inactive');
      $(this).removeClass('inactive');
      $(this).addClass('active');
      $(this).parent().find('.toggle').slideDown();
    } else {
      if($(this).hasClass('active')) {
        $('.toggle').slideUp();
        $('.contactUsWrap h5').removeClass('active');
        $('.contactUsWrap h5').addClass('inactive');
      }
    }
  });
}

function switchDivisions() {
  $('#currentlyViewing #toggle').hide();
  $('#currentlyViewing').click(function() {
    $('#currentlyViewing #toggle').slideToggle();
    $(this).toggleClass('active');
  });
}

//*****Clear search text out search box on focus**************************************************//
function clearSearchBox() {
  if (!document.getElementById('text')) return false;
  $('#text').focus(function() {
    if ($(this).val() == 'SEARCH') {
      $(this).val('');
    }
  });
}
//*****Clear search text out search box on focus**************************************************//
function animateSecondaryNav() {
  $("ul#secondaryNav li a").hover(function () {
    $(this).stop().animate({'top' : '12px'});
  }, function() {
    $(this).stop().animate({'top' : '0px'});
  });
}

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

  /***** TOGGLE FEATURED PROJECTS *****/
function toggleFeaturedProjects() {
  $('.projectWrap .toggle').hide();
 // $('.luft .toggle').show();
  $('.projectWrap h3').addClass('inactive');
//  $('.luft h3').removeClass('inactive');
//  $('.luft h3').addClass('active');
  $('.projectWrap h3').click(function () {
    if($(this).hasClass('inactive')) {
      $('.projectWrap .toggle').slideUp();
      $('.projectWrap h3').removeClass('active');
      $('.projectWrap h3').addClass('inactive');
      $(this).removeClass('inactive');
      $(this).addClass('active');
      $(this).parent().find('.toggle').slideDown();
    } else {
      if($(this).hasClass('active')) {
        $('.projectWrap .toggle').slideUp();
        $('.projectWrap h3').removeClass('active');
        $('.projectWrap h3').addClass('inactive');
      }
    }
  });
}

  /***** TOGGLE FORMS *****/
function toggleForms() {
  // set up the page, hide all forms, disable their controls
  $('.contactus #main h3').hide();
  $('.contactus #main form').hide()
                 .find(":input")
                 .attr("disabled", "disabled");
  $("#reasonforContacting").change(function() {
    // hide all forms, disabled their inputs
    $("form:not(#" + this.value + ")").hide()
                                      .find(":input")
                                      .attr("disabled", "disabled");
    // reveal the form who's ID is this selected option's value, enable all controls
    $("#" + this.value).animate({"height": "toggle", "opacity": "toggle"}, "slow")
                       .find(":input")
                       .removeAttr("disabled");
  });
}

  /***** ROTATE PHOTOS *****/
function rotatePhotos() {
  $('.gallery a img').each(function() {
    $(this)
           .css({
             "-webkit-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg)',
             "-moz-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg)'
           })
           .hover(function() {
                $(this).css({
                    "-webkit-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg) scale(1.05)',
                    "-moz-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg) scale(1.05)'
                })
            }, function() {
                $(this).css({
                    "-webkit-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg) scale(1)',
                    "-moz-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg) scale(1)'
                })
            });
           
           
  });
}

  /***** TOOLTIPS *****/
function tooltips() {
  $('.trigger').tooltip();
}

/*
function formMessages() {
  $('#sendbutton14').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();
    if($('.success').length != 0 {
      console.log('success exists');
    } else {
      console.log('success does not exist');
    });
  });
    
  $('.close').click(function() {
    $('.success').fadeOut();
    $('#overlay').fadeOut();
  });
}
*/
//*****Load all functions**************************************************//
$(document).ready(function(){
  replaceAlign();
  replaceTarget();
  strictNewWindow();
  clearSearchBox();
  ContactUsWrap();
  switchDivisions();
  animateSecondaryNav();
  toggleBuckets();
  toggleFeaturedProjects();
  toggleForms();
  // rotatePhotos();
  tooltips();
  //formMessages();
});
