﻿pageTitle = document.title;

$(window).load(function () {
   document.title = pageTitle;
});

$(document).ready(function () {

   scrollPos = $(window).scrollTop();
   player = scrollDir = scrolling = animating = isMobile = ie = ie7 = ie9 = footerHidden = false;
   section = degree = wheel = map = videoObject = footerTimer = 0;
   english = true;
   lang = 'en';
   R = Raphael("compWheelCanvas", 307, 307);
   $.tbwaga({ ua: "UA-12977082-1" });

   checkBrowser();
   checkHash();

   $(window).resize(function () {
      if ($(window).width() <= 768) {
         isMobile = true;
         windowScroll = window;
         $('#scrollbar').hide();
         $('#mainContainer').css({ 'padding': '0', 'height': $('#mainContent').height() + 'px' })
      } else {
         isMobile = false;
         resizeWindow();
         windowScroll = '#mainContainer';
      };
   });

   $('#mainContainer').scroll(function () {
      scrolling = true;
      $('#background').css({ 'top': '-' + Math.floor($('#mainContainer').scrollTop() * ($('#mainContainer').height() / $('#background').height()) / 5) + 'px' });
   });

   navigation();
   onScroll();
   photoSlider(4000);
   competenciesWheel();
   videoPlayer();
   socialButtons();
   requestForm();

   //$("#Phone").mask("(999) 999-9999");

   $('#GraduationDate').focus(function () {
      if ($(this).val() === $(this).attr('data-val')) {
         $(this).val('');

         if ($('body').hasClass('fr')) {
            $("#GraduationDate").mask("99/99/9999");
         } else {
            $("#GraduationDate").mask("9999/99/99");
         }

      }
   })

   setTimeout(function () {
      footerHide();
   }, 5000);

});


function checkBrowser() {

   if ($('body').hasClass('fr')) {
      english = false;
      lang = 'fr';
      wheelBottom = R.image('/images/wheel-bottom-fr.png', 0, 0, 307, 307);
      wheelTop = R.image('/images/wheel-top-fr.png', 78, 78, 152, 152);
   } else {
      english = true;
      lang = 'en';
      wheelBottom = R.image('/images/wheel-bottom.png', 0, 0, 307, 307);
      wheelTop = R.image('/images/wheel-top.png', 78, 78, 152, 152);
   };


   if ($.browser.msie) {
      ie = true;
      $('body').addClass('ie');
      if (parseInt($.browser.version) == 7) {
         ie7 = true;
         $('body').addClass('ie7');
      } else if (parseInt($.browser.version) == 8) {
         ie8 = true;
         $('body').addClass('ie8');
      } else if (parseInt($.browser.version) == 9) {
         ie9 = true;
         $('body').addClass('ie9');
      };
   };


   if ($(window).width() <= 768) {
      isMobile = true;
      windowScroll = window;
      $('.socialToggles div').hide();
   } else {
      resizeWindow();
      windowScroll = '#mainContainer';
   };

   isiPad();

};


function checkHash() {

   hash = window.location.hash.replace('#', '');

   if (hash == 'competencies' || hash == 'stories' || hash == 'request') {

      if (hash == 'competencies' || hash == 'request') {
         paddingBorder = 10;
      } else {
         paddingBorder = 0;
      };

      setTimeout(function () {
         $(windowScroll).scrollTo($('#page' + hash.charAt(0).toUpperCase() + hash.slice(1)).position().top - paddingBorder, 0, { axis: 'y' });
      }, 10);

      $('#navigationContainer a').removeClass('selected');

      if (!isMobile) {
         $('#nav' + hash.charAt(0).toUpperCase() + hash.slice(1)).addClass('selected');
      };
      animateNavigationSelected($('#nav' + hash.charAt(0).toUpperCase() + hash.slice(1)), 0, false);
      $.tbwaga.trackPageview($('#nav' + hash.charAt(0).toUpperCase() + hash.slice(1)).attr('data-track'));

   } else if (hash.indexOf('region-') != -1) {

      $('#navigationContainer a').removeClass('selected');

      if (!isMobile) {
         $('#communitySelect').addClass('selected');
      };

      setTimeout(function () {

         scrollLocation = $('.regionPage[data-name="' + hash.replace('region-', '') + '"]').position().top;

         if ($('.regionPage[data-name="' + hash.replace('region-', '') + '"]').hasClass('bgTile')) {
            scrollLocation = scrollLocation - 10;
         };

         $.tbwaga.trackPageview('your-region/'+$('option[data-track="' + hash.replace('region-', '') + '"]').attr('data-track'));

      }, 10);

   } else {
      window.location.hash = 'become';

      if (!isMobile) {
         $('#navBecome').addClass('selected');
      };
   };

};


function onScroll() {

   setInterval(function () {

      if (scrolling && !animating) {
         scrolling = false;

         if (scrollPos - $(window).scrollTop() > 0) {
            scrollDir = 'up';
         } else {
            scrollDir = 'down';
         };

         scrollPos = $(window).scrollTop();
         firstRegion = false;

         $('.page').each(function () {
            if (inNewView($(this))) {
               if ($(this).hasClass('regionPage')) {
                  animateScroll = 'resources';
                  region = $(this).prop('id');
                  $('#communitySelect').val($(this).find('h1:first').html());
               } else {
                  region = false;
                  animateScroll = $(this);
               };
               animateNavigationSelected(animateScroll, 500, region);
               return false;
            };
         });

         $('.regionPage').each(function () {
            if ((inNewView($(this)) && !isMobile) || isiPad()) {
               initSocial('#' + $(this).prop('id'), $(this).attr('data-twitter'), parseInt($(this).attr('data-facebook')), parseInt($(this).attr('data-linkedin')), $(this).attr('data-flickr'));
            };
         });

      };

   }, 150);

};


function navigation() {

   $('#navigationContainer a').click(function (e) {

      animating = true;
      //e.preventDefault();

      if (isMobile) {
         thisTop = $('#' + $(this).prop('id').replace('nav', 'page')).offset().top - 55;
         scrollTimer = 0;
      } else {
         thisTop = $('#' + $(this).prop('id').replace('nav', 'page')).position().top;
         scrollTimer = 1000;
      }

      if ($(this).prop('id') == 'navCompetencies' || $(this).prop('id') == 'navRequest') {

         thisTop = thisTop - 10;

      } else if ($(this).prop('id') == 'navBecome') {

         thisTop = 0;

         if (isMobile) {
            thisTop = $('#pageBecome').offset().top - 55;
         }

      } else {

         if (isMobile) {
            thisTop = thisTop - 20;
         }

      }

      $(windowScroll).scrollTo(thisTop, scrollTimer, { axis: 'y' });

      animateNavigationSelected($(this), 1000, false);

      setTimeout(function () {
         animating = false;
      }, 1200);

      return false;

   });


   $('#communitySelect').change(function () {

      animating = true;
    
      if (isMobile) {
         $(windowScroll).scrollTo($('#' + $(this).find('option:selected').attr('data-id')).offset().top - 55, 0, { axis: 'y' });
      } else {

         scrollLocation = $('#' + $(this).find('option:selected').attr('data-id')).position().top;
     
         if ($('#' + $(this).find('option:selected').attr('data-id')).hasClass('bgTile')) {
            scrollLocation = scrollLocation - 10;
         }

         $('#mainContainer').scrollTo(scrollLocation, 1000, { axis: 'y' });

      }

      animateNavigationSelected('resources', 1000, $(this).find('option:selected').attr('data-id'));
     
      setTimeout(function () {
         animating = false;
      }, 1000);

   });


   $('.pageResources').click(function (e) {

      //e.preventDefault();

      if (isMobile) {
         $(windowScroll).scrollTo($('.regionPage:first').offset().top - 55, 0, { axis: 'y' });
      } else {
         $('#mainContainer').scrollTo($('.regionPage:first').position().top, 1000, { axis: 'y' });
      };

      window.location.hash = 'resources';

      return false;

   });


   $('a[href="#"], a[href=""]').click(function (e) {
      //e.preventDefault();
      return false;
   });

   $('a[rel="external"]').click(function () {
      $.tbwaga.trackEvent('cma', 'external-link', $(this).prop('href'));
   });

   $('.socialShare a').click(function () {

      switch ($(this).prop('id').replace('taglineSocial', '').replace('footerSocial', '').toLowerCase()) {
         case 'facebook':
            $.tbwaga.trackSocial('facebook', 'share', $(this).prop('href'));
            break;
         case 'twitter':
            $.tbwaga.trackSocial('twitter', 'share', $(this).prop('href'));
            break;
         case 'linkedin':
            $.tbwaga.trackSocial('linkedin', 'view', $(this).prop('href'));
            break;
      };

   });

   $('.cmaLogo').click(function () {
      if (isMobile) {
         $.scrollTo(0, 0, { axis: 'y' });
      }
   });

};


function animateNavigationSelected($this, speed, region) {

   $('#navigationContainer a').removeClass('selected');
   $('#communitySelect').removeClass('selected');

   if ($this != 'resources') {

      var thisPage = $this.prop('id').replace('nav', '').replace('page', '').toLowerCase();

      if (window.location.hash.replace('#', '') != thisPage) {
         window.location.hash = thisPage;
         if (thisPage != 'stories') {
            $('.selected .filmClose').click();
            $.tbwaga.trackPageview('stories/' + $('#pageStories .selected').attr('data-track') + '/pause');
         };
         $.tbwaga.trackPageview($('#' + $this.prop('id').replace('page', 'nav')).attr('data-track'));
      };

      switch (thisPage) {
         case 'become':
            animateTop = '115px';
            break;
         case 'competencies':
            animateTop = '161px';
            break;
         case 'stories':
            animateTop = '205px';
            break;
         case 'request':
            animateTop = '251px';
            break;
      };

      $('#navSelected').stop().animate({
         top: animateTop,
         right: '0px'
      }, speed, function () {
         animating = false;
         if (thisPage == 'request') {
            setTimeout(function () {
               $('#Region').focus();
            }, 500);
         }
      });

      $('#communitySelect').val($('#communitySelect').find('option:first').val());

      if (!isMobile) {
         $('#nav' + $this.prop('id').replace('nav', '').replace('page', '')).addClass('selected');
      };

   } else {

      regionPage = region.replace('pageRegions', 'region-').toLowerCase();

      if (window.location.hash.replace('#', '') != regionPage) {
         window.location.hash = regionPage;
         $('.selected .filmClose').click();
         $.tbwaga.trackPageview('stories/' + $('#pageStories .selected').attr('data-track') + '/pause');
         $.tbwaga.trackPageview('your-region/'+$('option[data-id="' + region + '"]').attr('data-track'));
      };

      if (!isMobile) {
         $('#communitySelect').addClass('selected');
      };

      arrowTop = '345px';

      $('#navSelected').stop().animate({
         top: arrowTop,
         right: '-10px'
      }, speed, function () {
         animating = false;
      });

   };

};


function footerHide() {

   if (!isMobile) {

      $('#taglineContainer').animate({
         top: '-100px',
         paddingTop: '20px'
      }, 250);

      $('#taglineLinks').animate({
         top: '85px'
      }, 250);

      $('#taglineSocial').hide().css({ 'top': '3px', 'margin-left': '380px' }).fadeIn().find('a').css({
         'width': '16px',
         'height': '14px',
         'margin': '0 0 0 15px',
         'background-position-y': '-70px'
      });

      $('#navigationContainer').animate({
         top: '20px'
      }, 250);

      $('#mainContainer').animate({
         top: '20px',
         height: ($(window).height() - 20) + 'px'
      }, 250);

      $('#scrollbar').animate({
         height: '20px'
      }, 250);

      $('#taglineBackground').fadeIn(500);

      setTimeout(function () {
         footerHidden = true;
      }, 300);

      setTimeout(function () {
         if (footerHidden) {
            $('#mainContainer').css({ 'height': ($(window).height() - 20) + 'px' });
         }
      }, 1000);

   };

};


function footerShow() {

   if (!isMobile) {

      $('#taglineContainer').animate({
         top: '0',
         paddingTop: '0'
      }, 250);

      $('#taglineLinks').animate({
         top: '53px'
      }, 250);

      $('#taglineSocial').hide().css({ 'top': '45px', 'margin-left': '380px' }).fadeIn().find('a').css({
         'width': '22px',
         'height': '21px',
         'margin': '0 0 0 8px',
         'background-position-y': '-1px'
      });

      $('#navigationContainer').animate({
         top: '100px'
      }, 250);

      $('#mainContainer').animate({
         top: '100px',
         height: ($(window).height() - 100) + 'px'
      }, 250);

      $('#scrollbar').animate({
         height: '100px'
      }, 250);

      $('#taglineBackground').fadeOut(150);

      setTimeout(function () {
         footerHidden = false;
      }, 300);

   };

};


twitterOptions = {
   version: 2,
   type: 'profile',
   rpp: 30,
   interval: 30000,
   width: 370,
   height: 111,
   theme: {
      shell: { background: '#ffffff', color: '#004b8d' },
      tweets: { background: '#ffffff', color: '#717175', links: '#3b3b3b' }
   },
   features: { scrollbar: true, loop: false, live: true, hashtags: true, timestamp: true, avatars: false, behavior: 'all' }
};


function initSocial(id, twitter, facebook, linkedin, flickr) {

   if ((inNewView($(id)) && $(id).attr('data-init') == '0') || isiPad()) {

      $(id).attr('data-init', '1');

      switch (id) {
         case '#pageRegionsAlberta':
            var twitterFeed = twitterAlberta;
            break;
         case '#pageRegionsBritishColumbia':
            var twitterFeed = twitterBC;
            break;
         case '#pageRegionsManitoba':
            var twitterFeed = twitterManitoba;
            break;
         case '#pageRegionsNewBrunswick':
            var twitterFeed = twitterNB;
            break;
         case '#pageRegionsOntario':
            var twitterFeed = twitterON;
            break;
         case '#pageRegionsQuebec':
            var twitterFeed = twitterQB;
            break;
         default:
            var twitterFeed = 0;
            break;
      };

      var twitterRender = false;
      var feedContainer = $(id).find('.socialFeedFacebook');
      
      var twitterTimer = setTimeout(function () {

         if (inNewView($(id)) || isiPad()) {

            $(id).find('.socialLink:first').click();

            if (twitter != 0 && twitterFeed != 0) {
               twitterFeed.render().setUser(twitter).start();
               $(id).find('.twitterWrap').html($('<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//platform.twitter.com/widgets/follow_button.html?screen_name=' + twitter + '&amp;lang=' + lang + '"></iframe>'));
            };

            if (facebook != 0) {
               $(id).find('.facebookWrap').html($('<iframe src="//www.facebook.com/plugins/like.php?href=' + encodeURI(feedContainer.attr('data-href')) + '&amp;send=false&amp;layout=button_count&amp;width=200&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21&amp;lang=' + lang + '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:21px;" allowTransparency="true"></iframe>'));
               if ($('body').hasClass('ie')) {
                  $(id).find('.socialFeedFacebook').html($('<iframe class="fb-like-box" src="//www.facebook.com/plugins/likebox.php?href=' + encodeURI(feedContainer.attr('data-href')) + '&amp;width=380&amp;colorscheme=light&amp;show_faces=false&amp;border_color=ffffff&amp;stream=true&amp;header=false&amp;height=455&amp;lang=' + lang + '" scrolling="no" frameborder="0" allowTransparency="true"></iframe>'));
               } else {
                  $(id).find('.socialFeedFacebook').html($('<iframe class="fb-like-box" src="//www.facebook.com/plugins/likebox.php?href=' + encodeURI(feedContainer.attr('data-href')) + '&amp;width=380&amp;colorscheme=light&amp;show_faces=false&amp;border_color=ffffff&amp;stream=true&amp;header=false&amp;height=455&amp;lang=' + lang + '" scrolling="no" frameborder="0" allowTransparency="true"></iframe>'));
               };
            };

            if (flickr != 0) {
               $(id + ' .feedFlickr').jflickrfeed({ limit: 8, qstrings: { id: flickr }, itemTemplate: '<a href="http://www.flickr.com/photos/' + flickr + '" title="{{title}}" target="_blank"><img alt="{{title}}" src="{{image_s}}" /></a>' });
            };

            twitterRender = true;
         } else {
            $(id).attr('data-init', '0');
         }

      }, 1000);

   };

};


function socialButtons() {

   $('.socialLink').click(function () {

      selectedNetwork = $(this).prop('title');
      socialToggles = $(this).parent();
      socialToggles.find('.twitterWrap').hide();
      socialToggles.find('.facebookWrap').hide();
      socialToggles.find('.flickrView').hide();
      socialToggles.find('.linkedInView').hide();

      switch (selectedNetwork) {
         case 'Facebook':
            socialToggles.find('.facebookWrap').show();
            break;
         case 'Twitter':
            socialToggles.find('.twitterWrap').show();
            break;
         case 'Flickr':
            socialToggles.find('.flickrView').show();
            break;
         case 'LinkedIn':
            socialToggles.find('.linkedInView').show();
            break;
      };

      $.tbwaga.trackPageview('your-region/' + $(this).parents('.regionPage').attr('data-name') + '/' + selectedNetwork.toLowerCase());
      socialToggles.find('.socialLink').removeClass('socialFacebookSelected').removeClass('socialTwitterSelected').removeClass('socialLinkedInSelected').removeClass('socialFlickrSelected');
      $(this).addClass('social' + selectedNetwork + 'Selected');
      $(this).parents('.regionPage').find('.socialFeeds').hide().parent().find('.socialFeed' + selectedNetwork).show();

      return false;

   });

};


function photoSlider(timeout) {

   setTimeout(function () {

      $('#photoSlider li').show();
      var removeSlide = $('#photoSlider').find('li:last');
      removeSlide.fadeOut(500);
      setTimeout(function () {
         removeSlide.remove();
         $('#photoSlider').prepend(removeSlide);
         photoSlider(3000);
      }, 500);

   }, timeout);

};


function pauseVideos() {

   if (english) {
      $f("videoAtul").pause();
      $f("videoShawn").pause();
      $f("videoRachel").pause();
      $f("videoRobert").pause();
      $f("videoElaine").pause();
      $f("videoDan").pause();
      $f("videoGeorge").pause();
      $f("videoGeeta").pause();
      $f("videoKatie").pause();
      $f("videoMark").pause();
   } else {
      $f("videoMartial").pause();
      $f("videoJosee").pause();
   };

};


function videoPlayer() {

   $('.film').click(function () {

      var selectedFilm = $(this);

      if (isMobile || isiPad()) {
         window.open($(this).find('.filmVideo a').prop('href').replace('.mp4', '_mobile.mp4'));
      } else {

         pauseVideos();

         if (!selectedFilm.hasClass('selected')) {

            $('.film').css({ 'width': '320px', 'height': '197px' }).removeClass('selected');

            selectedFilm.animate({
               width: '625px',
               height: '352px'
            }, 600).addClass('selected');

            setTimeout(function () {
               $('#mainContainer').scrollTo(selectedFilm.position().top, 500, { axis: 'y' });
            }, 300);

            $.tbwaga.trackPageview('stories/' + selectedFilm.attr('data-track'));

         };
      };

   });

   $('.filmClose').click(function () {

      var selectedFilm = $(this).parent();
      var film = $(this).parent().prop('id').replace('film', 'video');

      pauseVideos();

      selectedFilm.animate({
         width: '320px',
         height: '197px'
      }, 600);

      setTimeout(function () {
         selectedFilm.removeClass('selected');
      }, 600);

   });

};


function competenciesWheel() {

   $('.wheelBtn').click(function () {

      requestedSection = $(this).attr('data-section');
      requestedDegree = parseInt($(this).attr('data-degree'));
      requestedButton = $(this);

      if (section != 0) {
         compWheelCollapse(section);
         setTimeout(function () {
            compWheelRotate(requestedSection, requestedDegree, requestedButton, true);
         }, 500);
      } else {
         compWheelRotate(requestedSection, requestedDegree, requestedButton, true);
      };

      return false;

   });


   $('.compWheelSection, .compWheelTopSection, #pageCompetencies').click(function () {
      if (section != 0) {
         compWheelCollapse(section);
      };

      return false;
   });


   $('#competenciesWheelLogo').click(function () {
      if (section != 0) {
         compWheelCollapse(section);
      };
      setTimeout(function () {
         compWheelRotate('A', 0, $(this), false);
         compWheelRotate('B', 0, $(this), false);
         $('#compWheelButtons').removeClass().addClass('RV');
         $('#compWheelTopButtons').removeClass().addClass('AS');
      }, 450);
   });
};


function compWheelExpand(requestedSection) {

   $.tbwaga.trackPageview('competencies/' + $('.wheelBtn[data-section="' + requestedSection + '"]').attr('data-track'));
   
   if (requestedSection == 'A' || requestedSection == 'M' || requestedSection == 'S') {
      width = '450px';
   } else {
      width = '250px';
   };

   if (requestedSection == 'RV' && $('body').hasClass('fr')) {
      $('#wheelRVtitle').hide();
      $('#wheelRVspecial').show();
      $('#wheelRVspecialC').show();
   } else {
      $('#wheelRVtitle').show();
      $('#wheelRVspecial').hide();
      $('#wheelRVspecialC').hide();
   };

   $('#wheel' + requestedSection).show();
   $('#wheel' + requestedSection + 'body').animate({
      width: width
   }, 250, function () {
      $('#wheel' + requestedSection + ' .compWheelFade').fadeIn(500);
   });

   section = requestedSection;

};


function compWheelCollapse(requestedSection) {

   $('#wheel' + requestedSection + ' .compWheelFade').fadeOut(250);

   setTimeout(function () {
      $('#wheel' + requestedSection + 'body').animate({
         width: '0'
      }, 200, function () {
         $('#wheel' + requestedSection).hide();
         $('#wheelRVspecialC').hide();
      });
      setTimeout(function () {
         section = 0;
      }, 250);
   }, 250);

};


function compWheelRotate(requestedSection, requestedDegree, requestedButton, continueOn) {

   if (requestedSection == 'A' || requestedSection == 'M' || requestedSection == 'S') {
      wheel = '#competenciesWheelTop';
      wheelObject = wheelTop;
   } else {
      wheel = '#competenciesWheelBottom';
      wheelObject = wheelBottom;
   };

   if (degree == requestedDegree && continueOn) {
      compWheelExpand(requestedSection);
   } else {
      wheelTop.stop();
      wheelBottom.stop();
      wheelObject.stop().animate({ transform: "r" + requestedDegree }, 500, "<>", function () {
         if (continueOn) {

            compWheelExpand(requestedSection);

            if (requestedButton.hasClass('wheelBottomBtn')) {
               $('#compWheelButtons').removeClass().addClass(section);
            } else if (requestedButton.hasClass('wheelTopBtn')) {
               $('#compWheelTopButtons').removeClass().addClass(section);
            };

         };
         degree = requestedDegree;
      });
   };

};


function requestForm() {

   $('#requestSubmit').click(function (e) {

      //e.preventDefault();
      formErrors = false;

      $('#pageRequest .required').each(function () {

         if ($(this).prop('id') == 'GraduationDate') {

            if ($('#GraduationDate').val() == $('#GraduationDate').attr('data-val')) {
               $('#GraduationDate').css({ 'border': '1px solid #ff0000' }).next('.formInputError').animate({
                  opacity: 1
               });
               formErrors = true;
            } else {
               $('#GraduationDate').css({ 'border': '1px solid #717175' }).next('.formInputError').animate({
                  opacity: 0
               });
            }

         } else {

            if ($(this).val() == '') {

               $(this).css({ 'border': '1px solid #ff0000' }).next('.formInputError').animate({
                  opacity: 1
               }, 500);
               formErrors = true;

            } else {

               $(this).css({ 'border': '1px solid #717175' }).next().animate({
                  opacity: 0
               }, 500);

            }

         }

      });

      if (!/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/g.test($('#Email').val())) {
         $('#Email').css({ 'border': '1px solid #ff0000' });
         formErrors = true;
      };

      /*
      if ($('#Region').val() == $('#Region option:first').val()) {
      $('#Region').css({ 'border': '1px solid #ff0000' });
      formErrors = true;
      } else {
      $('#Region').css({ 'border': '1px solid #717175' });
      }
      */

      if (!formErrors) {

         $.ajax({
            type: "POST",
            url: "/Contacts/Create",
            data: $("form").serialize(),
            success: function (msg) {

               $('.formSuccess').fadeIn(500);
               $('.formResponse').click(function () {
                  $(this).fadeOut(500);
               });
               /*
               setTimeout(function () {
                  $('.formResponse').fadeOut(500);
               }, 5000);
               */
               $('.input').val('');
               //alert($('.formSuccess').text());
               $.tbwaga.trackPageview('form-submitted');
            },
            error: function (a, b, c) {
               //alert($('.formError').text());

               $('.formError').fadeIn(500);
               $('.formResponse').click(function () {
                  $(this).fadeOut(500);
               });
               /*
               setTimeout(function () {
                  $('.formResponse').fadeOut(500);
               }, 5000);
               */
            }
         });
         return false;
      };

   });


   $('#pageRequest .required').keyup(function () {

      if ($(this).val() != '') {
         $(this).css({ 'border': '1px solid #717175' }).next().stop().animate({
            opacity: 0
         }, 500);
      };

      if ($(this).prop('name') == 'Email' && !/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/g.test($('#Email').val())) {
         $('#Email').css({ 'border': '1px solid #ff0000' }).next('.formInputError').stop().animate({
            opacity: 1
         }, 500);
         formErrors = true;
      };

   });

};


function resizeWindow() {

   if (footerHidden) {
      taglineHeight = 20;
   } else {
      taglineHeight = 100;
   };

   $('#scrollbar').show().css({ 'width': getScrollBarWidth() + 'px' });
   $('#mainContainer').css({ 'height': ($(window).height() - taglineHeight) + 'px' }).css({ 'padding': '0 ' + (($(window).width() / 2) - 480) + 'px 0 0' });

};


function getScrollBarWidth() {

   var inner = document.createElement('p');
   inner.style.width = "100%";
   inner.style.height = "200px";

   var outer = document.createElement('div');
   outer.style.position = "absolute";
   outer.style.top = "0px";
   outer.style.left = "0px";
   outer.style.visibility = "hidden";
   outer.style.width = "200px";
   outer.style.height = "150px";
   outer.style.overflow = "hidden";
   outer.appendChild(inner);

   document.body.appendChild(outer);
   var w1 = inner.offsetWidth;
   outer.style.overflow = 'scroll';
   var w2 = inner.offsetWidth;
   if (w1 == w2) w2 = outer.clientWidth;

   document.body.removeChild(outer);

   return (w1 - w2);

};


function inNewView(elem) {

   var docViewTop = $('#mainContainer').scrollTop() + 150;
   var docViewBottom = docViewTop + $('#mainContainer').height();

   var elemTop = $(elem).position().top;
   var elemBottom = elemTop + $(elem).height();

   return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));

};


function isiPad() {

   if ((navigator.platform.indexOf("iPad") != -1)) {
      $('html').addClass('iPad');
   };
   return (navigator.platform.indexOf("iPad") != -1);

};
