/* Author: Eric D. Fields / ericdfields@gmail.com / @ericdfields
  
   - assumes jQuery 1.4.2
*/

// Common

var startSlideshow = function(slideshowWrapId) {
  var slideshow = slideshowWrapId ? slideshowWrapId : 'slideshow-wrap';
  $(window).load(function(){
    $('#'+slideshow).nivoSlider({
        effect: 'fade',
      directionNav:false,
      keyboardNav:false,
      captionOpacity: 1,
      pauseTime: 7000,
      animSpeed: 200
    });
  })
}

// All Pages

// Do typography
// if (!$('body').hasClass('home')) {
//   Cufon.replace('h1')
// }    
Cufon.replace('.button strong')

// Init major elements
$header = $('#header')
$main = $('#main')
$subNav = $('#sub-nav')

// move nav out of the semantic list to accomodate design
$header.find('.nav > ul > li').append('<span></span>')
if ($subNav.length) {
  $header.find('.nav li.active ul').removeClass('visuallyhidden')
  $subNav.html($header.find('.nav li ul'))
}

// specific actions for each page
var bodyClasses = $('body').attr('class').split(' ')
for (var i=0; i < bodyClasses.length; i++) {
  switch(bodyClasses[i]) {
    // About
      case 'about-us_overview':
          startSlideshow()
      if (!Modernizr.borderradius) {
         $('#main .highlights').append('<div class="x_corner" />')
      } 
      $(window).load(function() {
        
        $('#main h1').fadeIn('slow')
      })
    break
    case 'careers':
      $('#main h1').fadeIn('slow')
    break
    case 'nonprofits_get-started':
      $('#main h1').fadeIn('slow')
    break
    // Individuals
    case 'individuals_how-it-works':
      startSlideshow()
      // for the down arrow graphic
      $('#main .aside li').not(':last').append('<span />')
    break
    case 'meet-the-team':
      // Cufon.replace('h2');
      $('#main .person img').each(function(){
        var $this = $(this)
        var $tip = $this.next()
        $this.qtip({
          position: { 
            adjust: { 
              x: -150, 
              y: -20 
            } 
          },
          content: $tip,
          show: { 
            solo: true 
          },
          hide: { 
            fixed: true, 
            delay: 500 
          },
          style: {
            tip: {
              corner: 'topLeft', // We declare our corner within the object using the corner sub-option
              color: 'white',
              size: {
                x: 20, // Be careful that the x and y values refer to coordinates on screen, not height or width.
                y : 20 // Depending on which corner your tooltip is at, x and y could mean either height or width!
              }
            },
            background: 'none',
            border: 'inherit',
            padding: '20px'
          }
        })
      })
    break
    case 'nonprofits_why':
      startSlideshow()
    break
    case 'nonprofits_how':

    break
    case 'nonprofits_tour':
      var $header = $('#main .header')
      var $primaryContent = $('#main .primary-content')
      $primaryContent.find('div.visuallyhidden').hide().attr('class','')
      $header.find('.col9').append('<h2 />').find('h2').html($('#step1 h2').text())
      $header.find('ol a').bind('click',function(){
        var $this = $(this)
        var stepHref = $this.attr('href')
        var $activeDiv = $(stepHref)
        $header.find('ol').attr('class',(stepHref.replace('#','')))
        $primaryContent.find('div:visible').fadeOut()
        $header.find('.col9 h2').html($(stepHref).find('h2').text())
        $(stepHref).fadeIn()
        return false
      })
    break
    case 'contact-us1':
      function gMap(lat,lng,id) {
        map_canvas = id+'_canvas'
        $('#'+id).prepend('<div id="'+map_canvas+'" class="map"/>')
        var latlng = new google.maps.LatLng(lat,lng)
        var myOptions = {
          zoom: 15,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          mapTypeControl: false
        }
        var map = new google.maps.Map(document.getElementById(map_canvas),myOptions)
        var marker = new google.maps.Marker({
              position: latlng, 
              map: map
          })
      }
      gMap(42.351991,-71.048345,'x_boston')
      gMap(37.783835,-122.392063,'x_sanfran')
    break
    case 'nonprofits_donation-popup':
      Cufon.replace('h2')
    break   
    case 'boston-marathon-2010':                                                                                       
      Cufon.replace('h2')
      $('.date').after('<img src="images/calendar_icon.png" id="x_datepicker_icon" />')
      $('#x_datepicker_icon').bind('click',function(){
        alert('this should fire the datepicker instead')
      })
      $('.runners').after('<div class="x_stepper"><button class="up">Up</button><button class="down">Down</button></div>')
      $('form').submit(function(e){
        e.preventDefault()
        var success = true // for demonstartion only
        if (success) {                               
          var appendContent = function() {           
            var successString = '<h1>Thank You!</h1><p>Thanks for sending over the date and time of your 2011 Boston Marathon kickoff meeting! We&rsquo;re looking forward to partnering with you and your team to help make your marathon fundraising incredibly successful this year.</p><p>We&rsquo;ll be in touch shortly to confirm that a couple of fundraising experts from Team FirstGiving will attend your meeting. In the meantime, if you have any questions, please contact us at <a href="mailto:BostonMarathon2011@firstgiving.com" title="Email us">BostonMarathon2011@firstgiving.com</a>.</p>'
            $('.primary-content').html(successString) 
            Cufon.replace('h1')
          }
          $('.primary-content > *').fadeOut(function(){
            $(this).remove()
            appendContent()
          })
        }
      })
    break
case 'home':
    startSlideshow();
    var active = false;
    $('ul.quotes li:first-child').addClass('current');
    $('a.dn').bind('click', function (e) {
        e.preventDefault();
        if (!active && !($('.current').is(':last-child'))) {
            active = true;
            $('ul.quotes li:first-child').animate({
                marginTop: '-=' + $('li.current').height()
            }, function () {
                var next = $('li.current').next();
                $('li.current').removeClass('current');
                $(next).addClass('current');
            });
            $('.controls ul li:first-child').animate({
                marginTop: '-=' + $('.controls ul li:first-child').height()
            }, function () {
                active = false;
            })
        }
    });
    $('a.up').bind('click', function (e) {
        e.preventDefault();
        if (!active && !($('li.current').is(':first-child'))) {
            active = true;
            $('ul.quotes li:first-child').animate({
                marginTop: '+=' + $('li.current').height()
            }, function () {
                var prev = $('li.current').prev();
                $('li.current').removeClass('current');
                $(prev).addClass('current');
            });
            $('.controls ul li:first-child').animate({
                marginTop: '+=' + $('.controls ul li:first-child').height()
            }, function () {
                active = false;
            })
        }
    });
    break
case 'get-started':
    startSlideshow('slideshow-wrap-2');
    startSlideshow('slideshow-wrap-1');
    $('#slideshow-2').addClass('visuallyhidden');

    $('a[href="#slideshow-1"]').addClass('active')
    $('h1 a').live('click', function (e) {
        e.preventDefault()
        var $this = $(this)
        $('h1 a').removeClass('active')
        $this.addClass('active')
        $('.slideshow').addClass('visuallyhidden')
        $($this.attr('href')).removeClass('visuallyhidden')
    })
    break
  }
}
// qtip
$('#footer .d a[title]').qtip({
   show: 'mouseover',
   hide: 'mouseout',
   style: {width: 150, padding: 5, background: '#1CA4DD', color: '#fff', fontSize:'12px', textAlign: 'center',
   border: {width: 7, radius: 5, color: '#1CA4DD'},
   tip: 'bottomLeft',
   name: 'dark' // Inherit the rest of the attributes from the preset dark style
   },
   position: {
      corner: {target: 'topMiddle', tooltip: 'bottomLeft'}
   }
})
//sectmenu
$('#searchby').selectmenu({
		width: '95px'
	});
//aboutnav

  
$("#aboutfglink").mouseenter(function() { 
	$(".aboutnav").fadeIn("slow", "linear");
	$("#siteNav").addClass("hoverAbout");
});
$("#mainNav").mouseleave(function() { 
	$(".aboutnav").fadeOut("slow", "linear");
	$("#siteNav").removeClass("hoverAbout");
});


