$(document).ready(function(){



// Home Page
$('#slider li .icon').each(function(){
  var panelCSSColor = $(this).css('background-color'),
      panelCSSImage = $(this).css('background-image'),
      panelClass = $(this).parent().attr('class').split(' '),
      panelID = panelClass[0],
      panelBG = "<div class='bg' id='"+panelID+"' style='background-color:"+panelCSSColor+";background-image:"+panelCSSImage+";'></div>";
  $(panelBG).insertBefore('#bg-gradient');
});
$('#slider').anythingSlider({
  delay: 5000,
  hashTags: false,
  navigationFormatter : function(i, panel){
    var imgBG = panel.find('.icon').attr('src');
    return imgBG;
  }
});
var iconBG = $('.anythingControls li a').text();
$('.anythingControls li a').each(function(){
  var currentTab = $(this),
      iconBG = 'url('+currentTab.text()+')';
  currentTab.css({'background-image':iconBG});
});
$('.home .bg').hide();
$('#panel1').show().addClass('active');
$('#slider').bind('slide_begin', function(e, slider){
  $('.bg').removeClass('next');
  var panelID = '#panel';
  if(slider.currentPage === 5){
    panelID += 1;
  } else if (slider.currentPage === 0){
    panelID += 4;
  } else {
    panelID += slider.currentPage;
  }
  if(panelID.substring(1) != $('.bg.active').attr('id')){
    $(panelID).addClass('next').css({'top':'auto','bottom':'0'}).slideDown();
    $('.bg.active').css({'bottom':'auto','top':'0'}).slideUp();
  }
});

$('#slider').bind('slide_complete', function(e, slider){
  $('.bg.active').removeClass('active');
  $('.bg.next').removeClass('next').addClass('active');
});
$("#tweets").tweet({
  avatar_size: 40,
  count: 4,
  fetch: 20,
  username: ["sethcarl", "elstutzman", "jstutzman", "elevate"],
  loading_text: "searching twitter...",
  refresh_interval: 60,
  template: function(info) { return info.avatar + '<div class="tweet">' + info.text + '<a class="tweet_user" href="' + info.user_url + '">@' + info.screen_name + '</a></div>'; },
  filter: function(t){ return ! /^@\w+/.test(t.tweet_raw_text); }
});



// Work Detail Page
if($('.work-detail').length){
  $(window).load(function(){
    $('.work-detail .blocks.masonry').masonry({
      columnWidth: 196,
      itemSelector: '.block'
    }).animate({'opacity':'1'},200);
    $('header .nav a img').each(function(){
      var navImage = $(this);
      if(navImage.outerWidth() > 357){
        navImage.parent().css({'height':'80px'});
      }
      navImage.css({'opacity':'1','width':'80px'});
    });
    $('header .nav a').hover(function(){
      $(this).find('> span').animate({'bottom':'50px','opacity':'1'},{duration:200, queue:false});
    }, function(){
      $(this).find('> span').animate({'bottom':'35px','opacity':'0'},{duration:200, queue:false});
    })
  });
  $(window).scroll(function(){
    var infoOffset = $('.work-detail .col').offset();
    if($(document).scrollTop() > 500){
      $('.work-detail .col').css({'left':infoOffset.left,'position':'fixed','top':'40px'});
    } else {
      $('.work-detail .col').css({'left':'0','position':'relative','top':'0'});
    }
  });
  $(window).resize(function(){
    var infoOffset = $('.work-detail #main .inner').offset();
    if($(document).scrollTop() > 500){
      $('.work-detail .col').css({'left':infoOffset.left});
    }
  });
}



// Work Overview Page
var speed = 500, $blocks = $('.work .blocks.masonry');
$(window).load(function(){
  $('.work .block img').each(function(i){
    var blocksHeight = null,
        blockImage = $(this),
        blockWidth = null,
        blockHeight = null;
    if(blockImage.outerWidth() < 350){
      blockWidth = 160;
    } else{
      blockWidth = 358;
    }
    if(blockImage.outerHeight() < 350){
      blockHeight = 140;
    } else {
      blockHeight = 358;
    }
    blockImage.parent().width(blockWidth).height(blockHeight);
  });
  $blocks.masonry({
    itemSelector: '.block:not(.invis)',
    animate: true,
    animationOptions: {
      duration: speed,
      queue: false
    }
  }).animate({'opacity':'1'},500);
  $('.work .what-we-do a').click(function(){
    if($(this).siblings('.active').length > 0){
      $(this).toggleClass('active');
      var activeBoxes = '';
      $(this).parent().children('.active').each(function(){
        activeBoxes += '.block.'+$(this).attr('class').split(' active');
      });
      $blocks.children(activeBoxes).fadeIn(speed).removeClass('invis');
      $blocks.children('.block').not(activeBoxes).fadeOut(speed).addClass('invis');
      $blocks.masonry({ itemSelector: '.block:not(.invis)' });
    }
    return false;
  });
});
// $('.work .block').click(function(){
//   window.location = $(this).find('a').attr('href');
//   return false;
// });



// Method Page
$('.method .bar div').each(function(i){
  $(this).delay(i*300).animate({'width':'110%'}, 500, function(){ $(this).animate({'width':'100%'}, 200); });
});
$('.method .col, .method .bar').hover(function(){
  targetElement = null;
  targetElement = '.'+$(this).attr('data-rel');
  $(targetElement).addClass('active');
}, function(){
  targetElement = null;
  targetElement = '.'+$(this).attr('data-rel');
  $(targetElement).removeClass('active');
});



// Connect Page
$('#main form .check').live('click', function(){
  checkBox = $(this);
  checkBox.toggleClass('active');
  if(checkBox.is('.active')){
    checkBox.find('input').attr('checked','checked');
  } else {
    checkBox.find('input').removeAttr('checked');
  }
});
$('#main form').validate({
  errorPlacement: function(error, element) {
    error.appendTo( element.prev('label') );
  },
  rules: {
    Name: 'required',
    Company: 'required',
    Email: {
      email: true,
      required: true
    },
    Phone: {
      required: true
    },
    ProjectName: 'required',
    Message: 'required'
  },
  messages: {
    Name: 'Please enter your name',
    Company: 'Please enter your company name',
    Email: {
      email: 'Please enter a valid email address',
      required: 'Please enter your email address'
    },
    Phone: {
      required: 'Please enter your phone number'
    },
    ProjectName: 'Please enter your project name',
    Message: 'Please enter a brief message'
  },
  submitHandler: function(form){
    var dataString = $(form).serialize();
    $.post("/forms/processor.php", dataString, function(data){
      $('#main form').html('<h2>'+data+'</h2>');
    });
    return false;
  }
});




});

