Cufon.replace('h1');
Cufon.replace('td.contactLeft');
Cufon.replace('td.contactRight');
Cufon.replace('#navigation a', {hover: true});
Cufon.replace('span.footerHeader');
(function($){
	$(function(){
	    cycleManager.init();
	    fixFooter();
	    
	    $(window).resize(function() {
		    $('#cycle-bg-wrapper').resizeCycle();
		    fixFooter();
	    });
	});
	
	var cycleManager = {
		cycle: null,
		numImgs: 0,
		numLoaded: 0,
		
		init: function() {
			var self = this;
			this.cycle = $('#cycle-bg-wrapper');
			this.numImgs = $('img', this.cycle).length;
			$('img', this.cycle).css('visibility', 'hidden');
			$('img', this.cycle).imagesLoaded(function() {
				self.start();
			});
		},
		start: function() {
			$('img', this.cycle).css('visibility', 'visible');
			this.cycle.resizeCycle();
			this.cycle.addClass('cycling');
			this.cycle.cycle({
		    	containerResize: false,
		    	pager: '#cycle-nav-inner',
		    	pagerAnchorBuilder: function (idx, slide) {
					return '#cycle-nav a:eq(' + idx + ')';
				},
				slideExpr: 'div.cycle-bg',
		        slideResize: false,
		        speed: 1000,
		        sync: false,
		        timeout: 11000
		    });
		}
	};
	   
	// inspired by http://www.localwisdom.com/blog/2010/03/creating-a-resizeable-proportional-background-image-in-jquery/
	$.fn.resizeCycle = function() {
		//get browser dimensions
	    var winWidth = $(window).width();
	    var winHeight = $(window).height();
	    var winRatio = winHeight/winWidth;
	    
	    return $(this).each(function() {
	    	$(this).width(winWidth).height(winHeight);
	    	$('div.cycle-bg', this).each(function() {
	    		var div = $(this);
	    		var img = $('img', this);
	    		// attempt to get the already calculated image ratio. 
	    		// calculating the image ration should only happen the first time this is run on an image
	    		var ratio = img.data('ratio');
	    		if (ratio == undefined) {
	    			var imgWidth = img.attr('width');
	    		    var imgHeight = img.attr('height');
	    		    //define image ratio
	    		    ratio = imgHeight/imgWidth;
	    		    img.data('ratio', ratio);
	    		}
	    		
	    		//resize the image and container div
	    	    if (winRatio > ratio) {
	    	        div.height(winHeight);
	    	        div.width(winHeight / ratio);
	    	        img.height(winHeight);
	    	        img.width(winHeight / ratio);
	    	    } else {
	    	        div.width(winWidth);
	    	        div.height(winWidth * ratio);
	    	        img.width(winWidth);
	    	        img.height(winWidth * ratio);
	    	    }
	    	}).width(winWidth).height(winHeight);
	    });
	};
	
	function fixFooter() {
		var winHeight = $(window).height();
		var footer = $('div.footer');
		var offset = footer.offset();
		var footerHeight = footer.height();
		var bottom = offset.top + footerHeight;
		var diff = winHeight - bottom;
		if (diff > 0) {
			var container = $('div.bodyContainer');
			container.height(container.height() + diff);
		}
	}
	
	
})(jQuery);
function setupLandingPage() {
	$('table.boxes div.category').each(function() {
		var div = $(this);
		var link = $('a.readmoreBox', div);
		if (link.length > 0) {
			div.click(function(e) {
				var target = link.attr('target');
				if (target == undefined || target != '_blank') {
					window.location = link.attr('href');
				} else {
					window.open(link.attr('href'));
				}
				
			});
		} else {
			div.css('cursor', 'default');
		}
	});
}
function setupContactForm() {
	 $('#captchaReload').click(function() {
       $.getJSON(this.href, function(data) {
          try{
             $('input[name="ap-form-captcha[id]"]').val(data.captchaId);
             $('#captcha-img').attr('src', data.src);
          }catch(e){
             alert('There was a problem refreshing the captcha image. ' + e);
          }
       });
       return false;
    });
	$('button').hover(
		function() {$('span.readmoreAlt', this).addClass('readmoreAltHover');},
		function() {$('span.readmoreAlt', this).removeClass('readmoreAltHover');}
	);
    $('input,textarea').example(function() {
    	return $(this).attr('title');
    });
	$('#contact-form').validate({ 
		rules : {
			'ap-form-captcha[input]' : 'required'
		},
    	errorContainer: '#contact-warning', 
		focusInvalid: false,
		submitHandler: function(form) { 
    		$('input[type=text], textarea', form).each(function() {
	    	    if ($(this).val() == $(this).attr('title')) {
	    	    	$(this).val('');
	    	    }
    		});

    		$('#contact-form').ajaxSubmit({
				dataType: 'json',
    			success: function (response) {
    				if (response.status == 'error') {
    					$.each(response.errorList, function(field, msg) {
    						if (field == 'ap-form-captcha') {
    							$('input[name="ap-form-captcha[input]"]').addClass('error');
    							$('#contact-warning-captcha').show();
    						} else {
    							$('input[name="' + field + '"]').addClass('error');
    							$('#contact-warning').show();
    						}
    					});
    				} else {
		    			var c = $('#contact-thanks').show().detach();
		        		$('#interior-left').height($('#interior-left').height()).empty().append(c);
    				}
    			}
    		}); 
    		
    	}, 
    	showErrors: function(errorMap, errorList) {
    		for ( var i = 0; this.errorList[i]; i++ ) {
    			var error = this.errorList[i];
    			this.settings.highlight && this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass );
    		}

    		if( this.errorList.length ) {
    			this.toShow = this.toShow.add( this.containers );
    		}
    		if (this.settings.unhighlight) {
    			for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
    				this.settings.unhighlight.call( this, elements[i], this.settings.errorClass, this.settings.validClass );
    			}
    		}
    		this.toHide = this.toHide.not( this.toShow );
    		this.hideErrors();
    		this.addWrapper( this.toShow ).show();
    		$('input[type=text], textarea', this.currentForm).each(function() {
    			if (this != document.activeElement) {
    				var e = $(this);
    				if (e.val() == '' && e.attr('title').length > 0) {
    					e.val(e.attr('title')).addClass('example');
    				}
    			}
    		});
    	}
   });
}

//$('img.photo',this).imagesLoaded(myFunction)
//execute a callback when all images have loaded.
//needed because .load() doesn't work on cached images

//mit license. paul irish. 2010.
//webkit fix from Oren Solomianik. thx!

//callback function is passed the last image to load
//as an argument, and the collection as `this`


$.fn.imagesLoaded = function(callback){
	var elems = this.filter('img'),
	   len = elems.length;
	   
	elems.bind('load',function(){
	   if (--len <= 0){ callback.call(elems,this); }
	}).each(function(){
	  // cached images don't fire load sometimes, so we reset src.
	  if (this.complete || this.complete === undefined){
	     var src = this.src;
	     // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
	     // data uri bypasses webkit log warning (thx doug jones)
	     this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
	     this.src = src;
	  }
	});
	
	return this;
};

/*
 * Special event for image load events
 * Needed because some browsers does not trigger the event on cached images.

 * MIT License
 * Paul Irish     | @paul_irish | www.paulirish.com
 * Andree Hansson | @peolanha   | www.andreehansson.se
 * 2010.
 *
 * Usage:
 * $(images).bind('load', function (e) {
 *   // Do stuff on load
 * });
 * 
 * Note that you can bind the 'error' event on data uri images, this will trigger when
 * data uri images isn't supported.
 * 
 * Tested in:
 * FF 3+
 * IE 6-8
 * Chromium 5-6
 * Opera 9-10
 */
//(function ($) {
//	$.event.special.load = {
//		add: function (hollaback) {
//			if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
//				// Image is already complete, fire the hollaback (fixes browser issues were cached
//				// images isn't triggering the load event)
//				if ( this.complete || this.readyState === 4 ) {
//					hollaback.handler.apply(this);
//				}
//
//				// Check if data URI images is supported, fire 'error' event if not
//				else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
//					$(this).trigger('error');
//				}
//				
//				else {
//					$(this).bind('load', hollaback.handler);
//				}
//			}
//		}
//	};
//}(jQuery))

