(function($) {
	$.fn.match_heights = function() {
		this.height(this.max_height());
		
		return this;
	}
	
	$.fn.match_heights_including_padding = function() {
		var height = this.max_height_including_padding();
		
		this.each(function() {
			$(this).height(height - parseInt($(this).css('padding-top')) - parseInt($(this).css('padding-bottom')));
		});
		
		return this;
	}
	
	$.fn.max_height = function() {
		var max_height = 0;
		this.each(function() {
			if($(this).height() > max_height)
				max_height = $(this).height();
		});
		
		return max_height;
	}
	
	$.fn.max_height_including_padding = function() {
		var max_height = 0, height = 0;
		this.each(function() {
			height = $(this).height() + parseInt($(this).css('padding-top')) + parseInt($(this).css('padding-bottom'));
			if(height > max_height)
				max_height = height;
		});
		
		return max_height;
	}
	
	$.fn.get_sub_groups = function(length_of_sub_group) {
		var	total_length	= this.length,
			skip_end	= arguments[1],
			sub_groups	= [];
		
		for(var i = 0, sub_group; i < total_length / length_of_sub_group; i++) {
			sub_group = [];
			
			for(var j = 0, index; j < length_of_sub_group; j++) {
				index = (i * length_of_sub_group) + j;
				if(this[index] && !(skip_end && j == length_of_sub_group - 1))
					sub_group.push(this[index]);
			}
			
			sub_groups.push(sub_group);
		}
		
		return sub_groups;
	}
	
	$.fn.match_heights_for_sub_group = function(length_of_sub_group) {
		$(this.get_sub_groups(length_of_sub_group, arguments[1])).each(function() {
			$(this).match_heights();
		});
	}
	
	$.fn.pad_to_vertical_center = function() {
		var include_bottom = arguments[0];
		
		this.each(function() {
			var	diff		= $(this).parent().height() - $(this).height(),
				pad_top	= parseInt(diff/2);
			
			$(this).css('padding-top', pad_top + 'px');
			
			if(include_bottom)
				$(this).css('padding-bottom', (diff - pad_top) + 'px');
		});
	}
	
	$.fn.pad_to_vertical_center_for_sub_group = function(length_of_sub_group) {
		$(this.get_sub_groups(length_of_sub_group, arguments[1])).each(function() {
			$(this).pad_to_vertical_center(arguments[2]);
		});
	}
	
	$.fn.not_validate = function() {
		var errors = [];
		
		this.each(function() {
			var	input		= $(this).find('input')[0],
				label		= $(this).find('label')[0],
				value		= $(input).val(),
				title		= $(label).html(),
				validation	= 'not_blank';
			
			if(validation == 'not_blank' && (!value || value == ''))
				errors.push({ title: title, error: validation, error_text: ' should not be blank\r\n' });
		});
		
		if(errors.length > 0)
			return errors;
		
		return false;
	}
	
	$.fn.in_place_detail = function(init_options) {
		if(this.length > 0) {
			var defaults = { delay: 1000, detail_id_prefix: 'detail_for_', offset_diff_top: -5, offset_diff_left: -5 };
			var opts = $.extend(defaults, init_options);
			
			var detail_holder = $('body #detail_holder')[0];
			if(!detail_holder) $('body').append('<div id="detail_holder"></div>');
			detail_holder = $('body #detail_holder')[0];
			
			detail_holder.current_has_detail = false;
			
			$(detail_holder).hide();
			
			$(detail_holder).hover(
				function() {
				},
				function() {
					$(this).hide();
					if(detail_holder.current_has_detail) {
						detail_holder.current_has_detail.hovered = false;
					}
				}
			);
			
			this.each(function() {
				this.detail = $('#' + opts.detail_id_prefix + this.id)[0];
				
				if(this.detail) {
					$(this.detail).prepend('<div>' + $(this).html() + '</div>');
					$(this).hover(
						function() {
							var detail	= this.detail,
								_this	= this;
							_this.hovered = false;
							
							this.timeout = setTimeout(
								function() {
									var detail_holder	= $('body #detail_holder')[0],
										offset			= $(_this).offset();
									
									$(detail).addClass('on');
									$(detail_holder).html($(detail).html());
									
									$(detail_holder).show();
									
									$(detail_holder).css('top', (parseInt(offset.top) + 1 + opts.offset_diff_top) + 'px');
									$(detail_holder).css('left', (parseInt(offset.left) + opts.offset_diff_left) + 'px');
									
									detail_holder.current_has_detail = _this;
									
									detail_holder.className = detail.className;
									
									_this.hovered = true;
									
								},
								opts.delay
							);
						},
						function() {
							if(!this.hovered)
								clearTimeout(this.timeout);
						}
					);
				}
			});
		}
	}
})(jQuery);

function match_heights_loop(not_set, set) {
	var max_height = 0, last_max_height = 1;
	
	while(max_height != last_max_height) {
		last_max_height = max_height;
		max_height = $([set,not_set]).max_height();
		$(set).height(max_height);
	}
	
}

function common_init() {
	var init_options = arguments[0];
	
	var defaults = { in_place_detail_has_detail_class: 'has_detail' };
	init_options = $.extend(defaults, init_options);
	
	if(!init_options || !init_options.no_fancy_box) {
		$("a.fancybox").each(function() {
			var options = {
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'speedIn'		:	600, 
				'speedOut'		:	200,
				'titlePosition'	:	'inside', 
				'scrolling' : 'no',
				'overlayShow'	:	true
			}
			
			if(init_options && init_options.fancy_box_options)
				options = $.extend(options, init_options.fancy_box_options);
			
			if($(this).hasClass('player_url')) {
				var href = this.href;
				
				if(href.match(/youtube.com/i))
					href = href.replace(new RegExp("watch\\?v=", "i"), 'v/');
				else if(href.match(/youtu.be/i))
					href = href.replace(new RegExp("youtu.be", "i"), 'youtube.com/v');
				else if(href.match(/vimeo.com/i))
					href = href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1');
				
				options['href'] = href;
				options['type'] = 'swf';
				options['swf'] = {
					'wmode' : 'transparent',
					'allowfullscreen' : 'true'
				}
			}
			
			$(this).fancybox(options);
		});
	}
	
	$('._blank').each(function() { this.target = '_blank'; });
	
	if(!init_options || !init_options.no_init_slides) {
		$('.slideshow').height($('.slideshow li').max_height());
		$('.slideshow li').match_heights();
		$('.slideshow li, .slideshow_without_height_match li').css('opacity', 0);
		$('.slideshow, .slideshow_without_height_match').cycle({
			timeout: 10000
		});
	}
	
	$('#contact_form').submit(function() {
		var errors = $('div.required').not_validate();
		
		if(errors) {
			var string = 'Please amend the following fields as appropriate:\r\n\r\n';
			
			$(errors).each(function() {
				string += this.title + this.error_text;
			});
			
			alert(string);
			
			return false;	
		}
		
		return true;
	});
	
	$('.' + init_options.in_place_detail_has_detail_class).in_place_detail(init_options.in_place_detail_options);
	
}

/*
$(document).ready(function(){

	// VALIDATE ANY FORM
	$('input#commit').click(function() {
		var myAlert = '';
		
		$('.required input:not(:checkbox)').each(function() {
			var myField = $(this).attr('id');
			var myVal = $(this).val();
			var myValLength = myVal.length;
			// alert (myField + ': ' + myVal + ' (' + myValLength + ')');
			if (myValLength < 1) {
				var fieldName = $(this).attr('id').replace(/cf\_/ig,'').replace(/rf\_/ig,'').replace(/\_/ig,' ');
				myAlert = myAlert + '\r\n' + fieldName;
			}
		});
		
		$('.required input:checkbox:not(:checked)').each(function() {
			var fieldName = $(this).attr('id').replace(/cf\_/ig,'').replace(/rf\_/ig,'').replace(/\_/ig,' ');
			myAlert = myAlert + '\r\n' + fieldName;
		});
		
		if (myAlert.length > 0) {
			alert('You must fill in the following fields:' + myAlert);
			return false;
		}
	});

});*/

