(function($) {
  $.fn.equalizeCols = function(children){
    var child = Array(0);
    if (children) child = children.split(",");
    var maxH = 0;
    this.each(
      function(i)
      {
        if (this.offsetHeight>maxH) maxH = this.offsetHeight;
      }
    ).css("height", "auto").each(
      function(i)
      {
        var gap = maxH-this.offsetHeight;
        if (gap > 0)
        {
          t = document.createElement('div');
          $(t).attr("class","fill").css("height",gap+"px");
          if (child.length > i)
          {
            $(this).find(child[i]).children(':last-child').after(t);
          }
          else
          {
            $(this).children(':last-child').after(t);
          }
        }
      }
    );

  }
})(jQuery);

/*******************************************************************************

 CSS on Sails Framework
 Title: maidalaser.com
 Author: XHTMLized (http://www.xhtmlized.com/)
 Date: July 2011

 *******************************************************************************/
$( function() {
	
	$("img[rel]").overlay({mask: '#000'});
		
	// Contact Form Validator
	$("#contact-main").validator({
		position: 'top left',
		offset: [-5, 0],
		message: '<div></div>' // em element is the arrow
	});
	
	$("#cform").validator({
		position: 'top right',
		offset: [20, 0]
	});

	// Personalize My Message link
	var msgLink = $('#personalizeMsg');
	var msgArea = $('#contact_comment');
	//msgArea.hide();
	msgLink.click( function(e) {
		e.preventDefault();
		msgArea.slideDown(400);
	})
	/*
	//Hides the slickbox as soon as the DOM is ready
	//(a little sooner than page load)
	$('.slickbox').hide();

	//Shows the slickbox on clicking the noted link
	$('a.slick-down').click( function() {
	$('.slickbox').slideToggle('slow');
	return false;
	});
	//Hides the slickbox on clicking the noted link
	$('a.slick-up').click( function() {
	$('.slickbox').slideUp('slow');
	return false;
	});

	*/

	// fix placeholder field with jquery https://gist.github.com/379601 http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
	$('[placeholder]').focus( function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		}
	}).blur( function() {
		var input = $(this);
		if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		}
	}).blur().parents('form').submit( function() {
		$(this).find('[placeholder]').each( function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
			}
		})
	});
});
//
// Note: This file depends on the jQuery library.
//

// Module pattern:
// http://yuiblog.com/blog/2007/06/12/module-pattern/
var FORMALIZE = (function($, window, document, undefined) {
	// Private constants.
	var PLACEHOLDER_SUPPORTED = 'placeholder' in document.createElement('input');
	var AUTOFOCUS_SUPPORTED = 'autofocus' in document.createElement('input');
	var WEBKIT = 'webkitAppearance' in document.createElement('select').style;
	var IE6 = !!($.browser.msie && parseInt($.browser.version, 10) === 6);
	var IE7 = !!($.browser.msie && parseInt($.browser.version, 10) === 7);

	// Expose innards of FORMALIZE.
	return {
		// FORMALIZE.go
		go: function() {
			for (var i in FORMALIZE.init) {
				FORMALIZE.init[i]();
			}
		},
		// FORMALIZE.init
		init: {
			// FORMALIZE.init.detect_webkit
			detect_webkit: function() {
				if (!WEBKIT) {
					return;
				}

				// Tweaks for Safari + Chrome.
				$('html').addClass('is_webkit');
			},
			// FORMALIZE.init.full_input_size
			full_input_size: function() {
				if (!IE7 || !$('textarea, input.input_full').length) {
					return;
				}

				// This fixes width: 100% on <textarea> and class="input_full".
				// It ensures that form elements don't go wider than container.
				$('textarea, input.input_full').wrap('<span class="input_full_wrap"></span>');
			},
			// FORMALIZE.init.ie6_skin_inputs
			ie6_skin_inputs: function() {
				// Test for Internet Explorer 6.
				if (!IE6 || !$('input, select, textarea').length) {
					// Exit if the browser is not IE6,
					// or if no form elements exist.
					return;
				}

				// For <input type="submit" />, etc.
				var button_regex = /button|submit|reset/;

				// For <input type="text" />, etc.
				var type_regex = /date|datetime|datetime-local|email|month|number|password|range|search|tel|text|time|url|week/;

				$('input').each( function() {
					var el = $(this);

					// Is it a button?
					if (this.getAttribute('type').match(button_regex)) {
						el.addClass('ie6_button');

						/* Is it disabled? */
						if (this.disabled) {
							el.addClass('ie6_button_disabled');
						}
					}
					// Or is it a textual input?
					else if (this.getAttribute('type').match(type_regex)) {
						el.addClass('ie6_input');

						/* Is it disabled? */
						if (this.disabled) {
							el.addClass('ie6_input_disabled');
						}
					}
				});
				$('textarea, select').each( function() {
					/* Is it disabled? */
					if (this.disabled) {
						$(this).addClass('ie6_input_disabled');
					}
				});
			},
			// FORMALIZE.init.autofocus
			autofocus: function() {
				if (AUTOFOCUS_SUPPORTED || !$(':input[autofocus]').length) {
					return;
				}

				$(':input[autofocus]:visible:first').focus();
			},
			// FORMALIZE.init.placeholder
			placeholder: function() {
				if (PLACEHOLDER_SUPPORTED || !$(':input[placeholder]').length) {
					// Exit if placeholder is supported natively,
					// or if page does not have any placeholder.
					return;
				}

				FORMALIZE.misc.add_placeholder();

				$(':input[placeholder]').each( function() {
					var el = $(this);
					var text = el.attr('placeholder');

					el.focus( function() {
						if (el.val() === text) {
							el.val('').removeClass('placeholder_text');
						}
					}).blur( function() {
						FORMALIZE.misc.add_placeholder();
					});
					// Prevent <form> from accidentally
					// submitting the placeholder text.
					el.closest('form').submit( function() {
						if (el.val() === text) {
							el.val('').removeClass('placeholder_text');
						}
					}).bind('reset', function() {
						setTimeout(FORMALIZE.misc.add_placeholder, 50);
					});
				});
			}
		},
		// FORMALIZE.misc
		misc: {
			// FORMALIZE.misc.add_placeholder
			add_placeholder: function() {
				if (PLACEHOLDER_SUPPORTED || !$(':input[placeholder]').length) {
					// Exit if placeholder is supported natively,
					// or if page does not have any placeholder.
					return;
				}

				$(':input[placeholder]').each( function() {
					var el = $(this);
					var text = el.attr('placeholder');

					if (!el.val() || el.val() === text) {
						el.val(text).addClass('placeholder_text');
					}
				});
			}
		}
	};
	// Alias jQuery, window, document.
})(jQuery, this, this.document);
// Automatically calls all functions in FORMALIZE.init
jQuery(document).ready( function() {
	FORMALIZE.go();

	//
	$('body').removeClass('no-js');

	//
//	$('input, textarea').placeholder();
/*
	//
	$('.slideshow-wrapper .slideshow .slides').cycle({
		fx: 'fade',
		pause:  1,
		timeout: 0,
		pager: '.nav',
		before: onBefore,
		after: onAfter
	});
	$(".slideshow-wrapper").hover( function () {
		$('.slideshow-wrapper .slideshow .slides').cycle('pause');
	}, function () {
		$('.slideshow-wrapper .slideshow .slides').cycle('resume');
	}
	);
	$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
		$(pager).find('li').removeClass('activeLI')
		.filter('li:eq('+currSlideIndex+')').addClass('activeLI');
	};
	function onBefore () {
		$(".slideshow-wrapper .slideshow .description div").fadeOut();
	}

	function onAfter(curr,next,opts) {
		var description = (opts.currSlide +1);
		$(".slideshow-wrapper .slideshow .description div:nth-child("+description+")").fadeIn();
		//var caption1 = (opts.currSlide +1) + '/' + opts.slideCount;
		//$('#caption1').html(caption1);
	}
*/
	//
	$(".nav").tabs(".slideshow-wrapper .slideshow .slides img", {

		// enable "cross-fading" effect
		effect: 'fade',
		fadeOutSpeed: "slow",
		rotate: true,
		onBeforeClick: function(event, tabIndex) {
			var description = (tabIndex +1);
			$(".slideshow-wrapper .slideshow .description div").hide();
			$(".slideshow-wrapper .slideshow .description div:nth-child("+description+")").fadeIn();
		}
	}).slideshow(
		{
			autoplay: true,
			interval: 4000
		}
	);

	//
	$(".scrollable").scrollable({ circular: true, vertical: true, mousewheel: true }).autoscroll({ autoplay: true, interval: 3000 });

	if (($.browser.msie) && ($.browser.version <= "9.0")) {

		// CSS3PIE
		var CSS3PIE_selectors = [
		'#sidebar nav',
		'.footer-wrapper',
		'#content .img-right',
		'#sidebar .blog',
		'#sidebar .blog',
		'.slideshow-wrapper'
		];
		$( CSS3PIE_selectors.join(',') ).each( function() {
			PIE.attach(this);
		});
	}

	if (($.browser.msie) && ($.browser.version == "6.0")) {

		// CSS3PIE
		var CSS3PIE_selectors = [
		'#sidebar nav ul li.active a',
		'#footer ul',
		'.contact-wrapper .contact article',
		'.contact-wrapper .contact',
		'.contact-wrapper',
		'#content .img-right span',
		'.featured article .more',
		'.featured article ul li',
		'.slideshow-wrapper .slideshow .description div a',
		'.slideshow-wrapper .slideshow .mask2',
		'.slideshow-wrapper .slideshow .mask',
		'.slideshow-wrapper2',
		'#header .maidalaser'
		];
		$( CSS3PIE_selectors.join(',') ).each( function() {
			PIE.attach(this);
		});
	}

});

jQuery(window).load( function() {

	$(".featured article .content").equalizeCols();
	$(".contact-wrapper .contact article").equalizeCols();

});
