// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


// ================================================================================
// = Fllowing 2 functions are for old anchor tag method - remove when comfortable =
// ================================================================================
// Determine url and show correct content
// $(".page").ready(function() {
//   var page = window.location.hash;
//   if (page != '') {
//     $(".current").hide();
//     $(page).show();
//     $(page).addClass('current')
//   };
//   return false;
// });

// Show correct content when nav link is clicked
// $(document).ready(function() {
//   $(".navItem").click(function() {
// 
//   });
//   return false;
// });

// FU IE6
jQuery.each(jQuery.browser, function(i, val) {
  if(i=="msie" && jQuery.browser.version.substr(0,1)=="6")
     window.location = ("off_air.html");
});

// Page Animations
$(document).ready(function() {
  $("#content").fadeIn(speed);
  var speed = 400
  applyNavItemBehavior(speed);

  $("#secret a").click(function() {
    showHeader(speed);
  });

  $("#nav > li > a").click(function () {
    $("#content").fadeTo(speed, 0);
    setTimeout("nav('"+this.href+"')", 600);
    return false;
  });
	
  $("#footerHomeLink").click(function () {
    $("#container").fadeTo(speed, 0);
    setTimeout("nav('"+this.href+"')", 600);
    return false;
  });
	
  $("#showFoot").toggle(
    function () {
      $("#footerWrapper").animate( {
        height:"170px"
      } , 250 )
    },
    function () {
      $("#footerWrapper").animate( {
        height:"24px"
      } , 250 )
    }
    );

  $("#inquiry_form").submit(function(){
    $.post("/inquiries/create",
      $("#inquiry_form").serialize(),
      function(data){
        $("#inquiry_form").hide();
        $(".formThanks").show();
      });
      return false;
  });

  // make links wit .trainLink class change workDetails when clicked
  $(".trainLink").click(function() {
		$("#workDetails").hide();
		$(".trainLink").removeClass("selectedParent")
		$(".trainLink > *").addClass("galImage");
		$(".trainLink > *").removeClass("selectedImage");
		$(this).addClass("selectedParent")
		var kid = $(".selectedParent > .galImage")
		$(kid).removeClass("galImage");
		$(kid).addClass("selectedImage");
    var url = $(this).attr("href");
    $.get(url, { }, function(data) {
      $("#workDetails").html(data);
    });
		$("#workDetails").fadeIn(1000);
    return false;
  });
});

function applyNavItemBehavior(speed) {
  $(".navItem").click(function() {
    hideHeader(speed);
    setTimeout("nav('"+this.href+"')", 400);
    return false;
  });
}

function nav(href){
  location.href=href;
}

function hideHeader(speed) {
  $("#headerWrapper").animate({
    height: "0px",
    opacity: 0.4
  },
  speed);
  $("#header").animate({
    height: "0px",
    marginTop: "0px",
    opacity: 0
  },
  speed);
  $("#headerContent").animate({
    fontSize: "0px",
    height: "0px",
    marginTop: "-250px",
    opacity: 0
  },
  speed);
  $(".line1").animate({
    marginTop: "0px"
  },
  speed);
  $("#secret, #secret a").animate({
    fontSize:"24px",
    height: "55px",
    opacity: 1
  },
  speed);
  $("#navWrapper").animate({
    top:"53px"
  },
  speed);
  $("#navWrapper").removeClass("navWrapperFull");
  $("#navWrapper").addClass("navWrapperZero");
  $("#content").animate({
    marginTop:"130px",
    opacity: 0
  },
  speed);
  $('.navItem').removeClass('navItem');
  return true;
}

function showHeader(speed) {
  $("#headerWrapper").animate({
    height: "430px",
    opacity: 1
  },
  speed);
  $("#headerContent").animate({
    fontSize: "12px",
    marginTop: "215px",
    opacity: 1
  },
  speed);
  $("#header").animate({
    height: "430px",
    opacity: 1
  },
  speed);
  $(".line1").animate({
    marginTop: "3px"
  },
  speed);
  $("#secret, #secret a").animate({
    height: "0px",
    opacity: 0
  },
  100);
  $("#navWrapper").animate({
    top: "435px"
  },
  speed);
  $("#navWrapper").removeClass("navWrapperZero");
  $("#navWrapper").addClass("navWrapperFull");
  $("#content").animate({
    marginTop:"80px"
  },
  speed);
  $("#nav > li > a").addClass("navItem");
  applyNavItemBehavior(speed);
  return true;
}

function processInquiryForm() {

}

// Rollover script
$(document).ready( function()
{
  PEPS.rollover.init();
});
PEPS = {};
PEPS.rollover = 
{
  init: function()
  {
    this.preload();
    $(".ro").hover(
      function () {
        $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) );
      },
      function () {
        $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) );
      }
      );
  },
  preload: function()
  {
    $(window).bind('load', function() {
      $('.ro').each( function( key, elm ) {
        $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) );
      });
    });
  },
  newimage: function( src )
  {
    src = src.replace(/\?[0-9]*$/, '');
    return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_o' + src.match(/(\.[a-z]+\??[0-9]*)$/)[0];
  },
  oldimage: function( src )
  {
    return src.replace(/_o\./, '.');
  }
};

