// GLOBAL

$('nav#sub_nav li:last-child').addClass('last');
jQuery.waypoints.settings.scrollThrottle = 30;
if(!jQuery('body').hasClass('contact-us'))
{
  jQuery('.single article footer').waypoint(function(event, direction){
    jQuery(this).toggleClass('sticky', direction === "down");
    event.stopPropagation();
  });
}
// CONTACT US page
if(window.location.href.indexOf('?thanks')>0)
{
  // jQuery('form').remove();
  jQuery('article header h1').html("Thanks!");
  jQuery('.content').html("<p>Thank you for sending your message.  We'll be in touch shortly with a reply.</p>");
}


// the EMPLOYEE page

$('#employee #photo_collection div img').imgCenter();

// the BLOG page

if($('#blog aside.sidebar').height() > $('#blog #main .contain').height()){  $('#blog #main .contain').height($('#blog aside.sidebar').height()); }

$('li:contains("RSS"), li:contains("rss")').addClass('rss');
$('li:contains("acebook")').addClass('facebook');
$('li:contains("witter")').addClass('twitter');
$('li:contains("inked")').addClass('linkedin');

$('#blog  a:contains("Sign Up")').addClass('button');

if(window.location.href.indexOf('our-thinking/category/newsletter')>0 || $('#blog.single p.tags a.tag:contains("newsletter")').length > 0)
{
  $('#main_nav li').removeClass('parent_of_current');
  $('#nav_newsletter, #nav_blog').addClass('parent_of_current');
  $('h3:contains("Categories"), ul.tags').remove();
}

if(window.location.href.indexOf('our-thinking/category/News%20and%20events')>0)
{
  $('#main_nav li').removeClass('parent_of_current');
  $('#nav_news-and-events, #nav_blog').addClass('parent_of_current');
}

$('form#search').bind('submit', function(){
  var q = $(this).find('input').val();
  $(this).find('input').val('site:copernicusmarketing.com '+q);
});

// the HOME page
$('#home_slides').anythingSlider({
  buildArrows         : false,
  startStopped        : false,
  delay               : 12500,
  onInitialized: function(e, slider) {
    slider.$controls
      .prepend('<a class="prev">Previous</a>')
      .append('<a class="next">Next</a>')
      .find('.prev, .next').click(function(){
        if ($(this).is('.prev')) {
          slider.goBack();
        } else {
          slider.goForward();
        }
      });
    $('.start-stop').remove();
  }
});
$('#home #promos .photo img').imgCenter();

var bg_top_offset = $('.anythingSlider-default').offset();
if (bg_top_offset)
{
  var bg_top = bg_top_offset.top;
  if(bg_top > 0){ $('#home').css('background','#fff url("/images/bg_home.png") center 291px no-repeat'); }
}


// the OUR WORK page
$('.msie ul li:nth-child(4n+4)').addClass('fourth-child');
$('.msie .top_employees li:nth-child(2n+2)').addClass('last-child');
$('h2:contains("List")').next('ul').attr('id', 'client_list');
$('h2:contains("ndustries")').next('ul').attr('id', 'industries');
$('h2:contains("Select Clients"), h2:contains("Select International Clients"), h2:contains("Alguns Clientes"), h2:contains("Experiência por parte da indústria")').next('ul').attr('id', 'select_clients');
$('ul#case_studies figure').append('<div class="over">'); // add a non-semantic div to accomplish the overlay effect
$('ul#case_studies figcaption').equalHeights();
$('ul#client_list li img').vAlign();
$('ul#industries, ul#select_clients').easyListSplitter({ colNumber: 4 });
$('.listContainer1 ul').equalHeights();
$('#book_list figcaption').equalHeights();
$('#book_list li').equalHeights();
$('.msie #book_list li:nth-child(2n+2)').addClass('second');

more_lis = 4 - ($('ul#client_list li').length % 4);
if(more_lis < 4)
{
  for(var i=0; i<more_lis; i++)
  {
    $('ul#client_list').append('<li></li>');
  }
}

// the SERVICES page
nxt = $('nav.services li.current').next();
if(nxt.length)
{
  $('#services .shadowed').after('<div class="next"><a href="'+ nxt.find('a').attr('href') +'"><strong>Next:</strong>'+nxt.find('a').html()+'</a></div>');
}

// the CASE STUDY page
nxt = $('nav.case_studies li.current').next()
if(nxt.length)
{
  $('#our_work .shadowed').after('<div class="next"><a href="'+ nxt.find('a').attr('href') +'"><strong>Next:</strong>'+nxt.find('a').html()+'</a></div>');
}

function activatePlaceholders() {
  var detect = navigator.userAgent.toLowerCase();
  if (detect.indexOf('safari') > 0) { return false; }

  var inputs = document.getElementsByTagName('input');
  for (var i=0;i<inputs.length;i++) 
  {
    if (inputs[i].getAttribute('type') == 'text' || inputs[i].getAttribute('type') == 'search') 
    {
      if (inputs[i].getAttribute('placeholder') && inputs[i].getAttribute('placeholder').length > 0) 
      {
        inputs[i].value = inputs[i].getAttribute('placeholder');
        inputs[i].onclick = function() 
        {
          if (this.value == this.getAttribute('placeholder')) 
          {
            this.value = '';
          }
          return false;
        }
        inputs[i].onblur = function()
        {
          if (this.value.length < 1) 
          {
            this.value = this.getAttribute('placeholder');
          }
        }
      }
    }
  }
  return true;
}

activatePlaceholders();


function Quiz(questions, selector) {
  this.questions  = questions;
  this.score      = 0;
  this.currently  = 0;
  this.selector   = selector;
  this.quiz_container = $(this.selector);
  
  this.answer_with = function(user_answer) {
    answer_result = this.score_and_respond_to(user_answer);
    this.render_status(answer_result);
    // increment it
    this.currently++;
  };
  this.score_and_respond_to = function(user_answer) {
    if(this.questions[this.currently] && this.questions[this.currently].answer == user_answer) {
      this.score += 16;                                 // if user_answer matches with question's answer => score += 16
      return "Correct!";
    } else if(user_answer == 'dunno') {
      this.score += 12;                                  // if user answer is "dunno" => score += 12
      return "You're not sure.";
    } else {
      this.score += 8;                                  // if user answer is wrong => score += 8
      return "Incorrect.";
    }
  }

  this.current_question_string = function() {
    if(this.questions[this.currently])
    {
      return this.questions[this.currently].question
    }
  }

  this.render_question = function() {
    if(this.currently < questions.length) {
      var curr = this.currently;
      var curr_question = this.current_question_string();

      this.quiz_container.find('p.question').classname = "question";
      this.quiz_container.find('p.question')
        .addClass("number_"+curr)
        .html(curr_question);
    } else {
      this.render_score_total();
      return;
    }

  };

  this.show_related_links = function(){
    links = this.questions[this.currently].links;
    if(links.length > 0) {
      for(i=0; i<links.length; i++) {
        $('ul.links').append('<li><a href="'+links[i][1]+'">'+links[i][0]+'</a></li>');
      }
      $('ul.links').before('<h2 class="related_links">Boost your Marketing IQ with...</h2');
    }

  }

  this.render_status = function(result) {
    this.quiz_container.find('input:radio').attr('disabled', 'true');
    this.quiz_container.find('.status').html(result)[0].className="status";
    if(result=="Correct!"){ this.quiz_container.find('.status').addClass('correct'); }
    if(result=="You're not sure."){ this.quiz_container.find('.status').addClass('dunno'); }
    if(result=="Incorrect."){ this.quiz_container.find('.status').addClass('incorrect'); }
    this.quiz_container.find('.links').after('<input type="button" id="next" value="Next Question" class="button" />')
    this.show_related_links();
  };

  this.final_user_title = function() {
    if (this.score > 149) {
      return '- a Marketing Genius.';
    } else if (this.score > 129) {
      return '- A guru. A maven.';
    } else if (this.score > 109) {
      return '- An up-and-coming consultant--ready for a partnership with McKinsey or BCG.';
    } else if (this.score > 89) {
      return '- A seasoned professional--probably working for a CPG company.';
    } else if (this.score > 69) {
      return '- A \'gut\' marketer--if it feels good, it must be right--few of your marketing programs are successful.';
    } else if (this.score > 49) {
      return '- A deathwish marketer--ask for help before you kill another brand.';
    } else if (this.score > 29) {
      return '- An incompetent--seriously, get some marketing help.';
    } else if (this.score > 10) {
      return '- Dangerous to your company--your best bet is to resign immediately.';
    } else {
      return '- Guilty of malpractice--get out of marketing.';
    }
  }

  this.render_score_total = function() {
    this.quiz_container.html('\
        <p class="total_score">You scored <strong>'+ this.score +'</strong> -- you are <strong>'+ this.final_user_title() +'</strong></p>\
        <p class="share"><a href="http://twitter.com/home?status=I scored a '+ this.score +' on the Copernicuz Marketing Quiz. Try it for yourself - http://copernicusmarketing.com/quiz">share on twitter.</a></p>');
  };

  this.jQ_build_dom = function() {

    this.quiz_container.html('\
        <form id="question" name="question">\
          <p class="question number_0"></p>\
          <label for="true">true <input type="radio" value="true" name="answer" /></label>\
          <label for="false">false <input type="radio" value="false" name="answer" /></label>\
          <label for="dunno">don\'t know <input type="radio" value="dunno" name="answer" /></label>\
        </form>\
        <p class="status"></p>\
        <ul class="links"></ul>')

    var the_quiz = this;
    this.quiz_container.find('input').bind('click', function(e){
      e.preventDefault();
      the_quiz.answer_with(this.value);
    });

    this.quiz_container.find('input#next').live('click', function(){
      $('input:radio').attr('checked', false).removeAttr('disabled');
      $('input#next, .related_links').remove();
      $('p.status').html('');
      $('ul.links').html('');
      the_quiz.render_question();
    });
  }

  this.start = function() {
    this.jQ_build_dom();
    this.render_question();
  }

  var the_quiz = this;
  this.quiz_container.find('.start').bind('click', function(e){
    e.preventDefault();
    the_quiz.start();
  });

}

function Question(question, answer, links) {
  this.question = question;
  this.answer   = answer;   
  this.links    = links;   
}

