/**
 * Compact labels plugin
 * Takes one option
 *  - labelOpacity [default: true] - set to false to disable label opacity change on empty input focus
 */
(function($){$.fn.compactize=function(options){var defaults={labelOpacity:true};options=$.extend(defaults,options);return this.each(function(){var label=$(this),input=$('#'+label.attr('for'));input.focus(function(){if(options.labelOpacity){if(input.val()===''){label.css('opacity','0.5');}} else{label.hide();}});if(options.labelOpacity){input.keydown(function(){label.hide();label.css('opacity',1);});} input.blur(function(){if(input.val()===''){label.show();} if(options.labelOpacity){label.css('opacity',1);}});window.setTimeout(function(){if(input.val()!==''){label.hide();}},50);});};})(jQuery);

/**
 * (v) Equal Height plugin v. 1.11
 */

(function($){$.fn.equalHeights=function(){$(window).resize($.proxy(function(){var tallest=0;$(this).css('min-height',0);$(this).each(function(){if($(this).outerHeight()>tallest){tallest=$(this).outerHeight();}});$(this).each(function(){var padding=$(this).outerHeight()-$(this).height();var height=tallest-padding;$(this).css({'min-height':height});if($.browser.msie&&parseInt($.browser.version,10)==6)$(this).height(height);});},this)).triggerHandler('resize');return this;};})(jQuery);

/*
 * hrefID jQuery extention - returns a valid #hash string from link href attribute in Internet Explorer
 */
(function($){$.fn.extend({hrefId:function(){return $(this).attr('href').substr($(this).attr('href').indexOf('#'));}});})(jQuery);

/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		enhancements : {
			blockLinks : function() {
				$('.block-link').click(function(){
					window.location = $('a:first', this).attr('href');
					return false;
				});
				
				if($.browser.msie && $.browser.version == '6.0') {
					$('.block-link').hover(
						function(){
							$(this).css('backgroundColor', '#d0d6e4');
						},
						function(){
							$(this).css('backgroundColor', 'transparent');
						}
					);
				}
			},
			equalHeights : function() {
				var primary = $('#content .primary'),
					aside = $('#content .aside');
			
				primary.add(aside).equalHeights();
				
				$('> div', primary).each(function(index){
					$('> div:eq(' + index + ')', primary).add('> div:eq(' + index + '):not(.auto-height)', aside).equalHeights();
				});

				$('.section-c', primary).equalHeights();
				$('.cols-two, .cols-two .col', primary).equalHeights();
				$('.callouts .callout').equalHeights();
				
				//
				$('.cols-two .col, .aside .section-a').equalHeights();
				$('body#home .rotator, body#home .aside .hfeed').equalHeights();
				
			},
			stickyContent : function() {
				var container = $('.sticky-content'),
					padding = $('.sticky-bottom', container).outerHeight();
				
				container.each(function(){
					$(this).css('paddingBottom', padding);
				});
			}
		},
		
		fixes : {
			dropdown : function() {
				if(!$.browser.msie || $.browser.version != '6.0') {
					return;
				}
				
				$('#nav .dropdown').hover(
					function(){
						$(this)
							.addClass('dropdown-hover')
							.find('ul').show();
					},
					function(){
						$(this)
							.removeClass('dropdown-hover')
							.find('ul').hide();
					}
				);
			},
			lastChild : function() {
				if(!$.browser.msie) {
					return;
				}
				
				$('.aside > div:last-child, .primary > div:last-child, .callouts:last-child, .callouts .callout:last-child, .aside .hentry:last-child, .col .section-contact:last-child, .block-link:last-child').addClass('last-item');
				$('.cols-two:last').addClass('cols-last');
				$('.primary .section-b .section-c:last-child').addClass('section-c-last');		
			}
		},
		
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			}
		}
	};

	Engine.fixes.dropdown();
	Engine.fixes.lastChild();
	Engine.enhancements.blockLinks();
	Engine.enhancements.stickyContent();
	Engine.enhancements.equalHeights();
	Engine.utils.links();
	Engine.utils.mails();
});

//
$(document).ready(function() {

  // Setup cycling of home page banners
  if( typeof $.fn.cycle == 'function' ) {
    $('#rotator-items').cycle({
      timeout:            10000,
  		fx:                 'fade',
  		pager:			        '.rotator-controls ul',
  		pagerAnchorBuilder: pagerBuild,
  		activePagerClass:   'active'
  	});
  }
	
	// Colorbox setup
	if( typeof $.fn.colorbox == 'function' ) {
  	$('.illustration-box .view-image').colorbox();
  }
	
});
//

function pagerBuild(index, elem) {
	return "<li><a href='#'>" + (index + 1) + "</a></li>";
}

