/* Custom namespaces */
if (typeof Constec == 'undefined') { Constec = {}; }
if (typeof Constec.Utils == 'undefined') { Constec.Utils = {}; }

jQuery(document).ready(function($) {
  /* Various utilities */
  Constec.Utils = {
  		// Set equal heights for parent containers
  		// Used to vertical-align heading links to bottom
  		eqHeights : function(el,applyToParent) {
  			var $items = $(el);
  			var maxHeight = 0;
  			// Find the tallest element
  			$items.each(function(){
  				if ($(this).height() > maxHeight) {
  					maxHeight = $(this).height();
  				}
  			});

  			// set height
  			if (applyToParent) {
  				$items.parent().height(maxHeight);
  			} else {
  				$items.height(maxHeight);
  			}
  		},
  		// Make element clickable using article title url
  		clickable : function(el) {
  			$(el).click(function(e){
  				e.preventDefault();
  				window.location = $('h2 a', this).attr('href');
  			});
  		}
  };

  // Grid toggle
  if (!location.hostname.match(/(www\.)?constec-tools\.com/)) {
  	$(this).keydown(function(e){
  		var code = (e.keyCode ? e.keyCode : e.which)
  		if (code == 71) { // letter g for grid
  			var $grid = $('#grid');
  			return (_.isElement($grid[0])) ? $grid.remove() : $('#top').before('<div id="grid" />');
  		}
  	});
  }

  // Make all DIVs equal height
  Constec.Utils.eqHeights('#home-categories li.col', false);
  // Correct last column height
  var h = $('#home-categories li.home-contact').height();
  $('#home-categories li.home-contact').height(h + 228);
  
	// Remove last element border
  $('.block-layered-nav li:last-child, #nav-about li:last-child, #wrapper-about .item:last-child').css('border', 'none');
	
	// Homepage remvove empty content
	$('.cms-index-index .std').remove();
	
	// Go to SELECT URLs
  $('select.links').change(function() {
	  window.location = $(this).find("option:selected").val();
	});
  
  // Homepage carousel
  $('.flexslider').flexslider({
		animation: "fade",
		slideDirection: "horizontal",
		directionNav: false,
		controlNav: false,
		});
	
	// Scroll to top
	var winWidth = $(window).width();
	if (winWidth < 715) {
	$(window).scroll(function() {
			if($(this).scrollTop() != 0) {
				$('#scroll-top').fadeIn();	
			} else {
				$('#scroll-top').fadeOut();
			}
		});
	}
	$('#scroll-top').click(function() {
		$('body,html').animate({scrollTop:0},800);
	});
});
