var $data,test;
jQuery(document).ready(function() {
  /**
   * ============= GENERAL FUNCTIONS ==============
   */
  jQuery('.sales_email').click(function() {
    _gaq.push(['_trackPageview', '/mailto/sales']);
    if (jQuery(this).attr("href") == undefined) {
      window.location.href = 'mailto:sales@redbricksmedia.com';
      return false;
    } else {
      return true;
    }
  });
  
  /**
  * ============= CLIENTS PAGE =================
  */
  if (/^\/clients\/.+/.test(location.pathname)) {
    jQuery('#main').height(jQuery("#main").height() + 350);
    jQuery.history.init(function(hash) {
      clientsHash(hash);
    });
    jQuery('.case-study-top .prev').click(clientsGalleryPrev);
    jQuery('.case-study-top .next').click(clientsGalleryNext);
    jQuery('.case-study-navigation').children('a').click(function() {
      clientsGallery(parseInt(jQuery(this).attr('data-index')));
      return false;
    });
    clientsGallery(0);
  }
  
  /**
  * ============== WORK PAGE ================
  */
  if (/^\/clients/.test(location.pathname)) {
    $data = jQuery('#clients').clone();
    $data.children('.hidden').removeClass('hidden');
    jQuery('#sub-menu').children().each(function() {
      var filterType = jQuery(this).attr('data-filter');
      jQuery(this).find('select').change(function() {
        filterWork();
        return false;
      });
    });
    
    function filterWork() {
      var selector = '.client';
      jQuery('#sub-menu').find('option:selected').each(function() {
        if (jQuery(this).attr('data-filter') == undefined) {
          return true;
        }
        selector += "[";
        selector += jQuery(this).parent().attr('data-filter') + "~=" + jQuery(this).attr('data-filter') + ']';
      });
      var linkHash = '';
      if (jQuery("#sub-menu").find("[data-filter=data-services]").find('.selected').attr('data-filter') != undefined)
      linkHash = '#' + jQuery("#sub-menu").find("[data-filter=data-services]").find('.selected').attr('data-filter');
      $data.find('option').each(function() {
        var href = jQuery(this).attr('href');
        if (href.indexOf('#') > 0) href = href.substring(0, href.indexOf('#'));
        jQuery(this).attr('href', href);
      });
      $data.find(selector).each(function() {
        jQuery(this).find('option').each(function() {
          jQuery(this).attr('href', jQuery(this).attr('href') + linkHash);
        });
      });
      if($data.find(selector).length>0) {
        jQuery('#clients').quicksand($data.find(selector), {
          adjustHeight: 'dynamic',
          duration: 800,
          easing: 'easeInOutQuad'
        });
      } else {
        var noResults = jQuery('<div data-id ="searchnoresults" id="searchnoresults">').text("There were no clients that fit this criteria.")
        .addClass('font-size-25').appendTo('#clients').css({'height':'122px','padding-top':'30px','padding-left':'30px'});
        jQuery('#clients').quicksand(jQuery(noResults), {
          adjustHeight: 'dynamic',
          duration: 800,
          easing: 'easeInOutQuad'
        });
      }
    }
  };
});

var disableGallery = false;
function clientsGalleryNext() {
  if (disableGallery) return false;
  toShow = 0;
  if (jQuery('.case-study-gallery').children('img:not(:hidden)').length > 0)
  toShow = parseInt(jQuery('.case-study-gallery').children('img:not(:hidden):first').attr("index")) + 1;
  
  clientsGallery(toShow);
  return false;
}
function clientsGalleryPrev() {
  if (disableGallery) return false;
  toShow = 0;
  if (jQuery('.case-study-gallery').children('img:not(:hidden)').length > 0)
  toShow = parseInt(jQuery('.case-study-gallery').children('img:not(:hidden):first').attr("index")) - 1;
  
  clientsGallery(toShow);
  return false;
}

var clientsGalleryToShow;
function clientsGallery(index) {
  var numImages = jQuery('.case-study-gallery').children('img').length;
  if (numImages < 1) return false;
  if (numImages == 1) {
    index = 0;
  } else {
    if (index > (numImages - 1)) index = (index + 1) % (numImages - 1);
    while (index < 0) index = index + numImages;
  }
  clientsGalleryToShow = index;
  disableGallery = true;
  if (jQuery('.case-study-gallery').children('img:not(:hidden)').length > 0) {
    jQuery('.case-study-gallery').children('img:not(:hidden)').fadeOut(400, clientsGalleryShow);
  } else {
    clientsGalleryShow();
  }
}
function clientsGalleryShow() {
  var toShow = clientsGalleryToShow;
  jQuery('.case-study-navigation').children('a').removeClass('selected');
  jQuery('.case-study-navigation').children('a[data-index=' + toShow + ']').addClass('selected');
  jQuery('.case-study-gallery').children('img').each(function() {
    if (jQuery(this).attr("index") == toShow)
    jQuery(this).fadeIn(400, function() {disableGallery = false;});
  });
}

function clientsHash(hash) {
  if(hash=="") {
    hash=jQuery("li.services:first").find("a").attr("href").substring(1);
  }
  jQuery(".services-content").hide();
  jQuery("#case-study-" + hash).show();
  jQuery("li.services").removeClass("selected");
  jQuery(".services a[href=#" + hash + "]").parent().addClass("selected");
}

/**
 * ============== VERTICAL LANDING PAGE ========================
 */
jQuery(document).ready(function() {
  jQuery.ajaxSetup ({
    cache: false
  }); 
  if (jQuery("#magic-box-submit").length > 0) {
    jQuery("#magic-box-submit").click(function() {
      var field, val;
      
      field = jQuery("[name=contact_name]");
      val = jQuery.trim(field.val());
      if (! /.+/.test(val)) {
        alert("Please enter your name.");
        field.focus();
        return false;
      }
      
      field = jQuery("[name=contact_email]");
      val = jQuery.trim(field.val());
      if (! /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(val)) {
        alert("Please enter a valid email address");
        field.focus();
        return false;
      }
      
      jQuery.post("/wp-content/forms/ajax_contact_us.php", {
        name    : jQuery("[name=contact_name]").val(),
        company : jQuery("[name=contact_company]").val(),
        email   : jQuery("[name=contact_email]").val()
      }, function(data, textStatus, jqXHR) {
        jQuery("#form-container").empty().html("<p id='magic-box-confirm'>Thank you!</p>");
      });
      
      return false;
    });
  }
});

