(function($){
	
	
	$.fn.exists = function()
	{
		return $(this).length>0;
	};
	
	
	/*--------------------------------------------------------------------------------------------
		setup_forms
	--------------------------------------------------------------------------------------------*/
	function setup_forms()
	{
		$('form').each(function(){
		
			var form = $(this);
			
			form.validate({
				highlight: function(element, errorClass, validClass) {
				    $(element).closest('.field').addClass('error');
				},
				unhighlight: function(element, errorClass, validClass) {
				    $(element).closest('.field').removeClass('error');
				}
			});
		});
	}
	
	
	/*--------------------------------------------------------------------------------------------
		setup_slider
	--------------------------------------------------------------------------------------------*/
	$.fn.setup_slider = function()
	{
		var div = $(this);
		var li_width = 940;
		var i = 0;
		var gallery = div.find('.gallery');
		var slides = div.find('.gallery ul li');
		var auto = true;
		var timer;
		var delay = 4000;
		
		gallery.css({'scrollLeft' : 0});
		
		div.find('.link-next').click(function(){
			
			auto = false;
			
			if(slides.eq(i+1).exists())
			{
				i++;

				gallery.children('ul').animate({'left' : - li_width*i}, 500);
			}
			
			return false;
			
		});
		
		
		div.find('.link-prev').click(function(){
			
			auto = false;
			
			if(i > 0 && slides.eq(i-1).exists())
			{
				i--;
				
				gallery.children('ul').animate({'left' : - li_width*i}, 500);
			}
			
			return false;
			
		});
		
		// auto slide
		function slide_next(new_delay)
		{
			if(!auto) return false;
			
			new_delay = (new_delay != null) ? new_delay : delay;
			
			clearTimeout(timer);
			
			timer = setTimeout(function(){
				if(slides.eq(i+1).exists())
				{
					i++;
				}
				else
				{
					i = 0;
				}
					
				gallery.children('ul').animate({'left' : - li_width*i}, 500);
				
				slide_next();
				
			}, new_delay);
		}
		slide_next();
		
		
		// hover pause
		var timer_start = 0;
		div.hover(function(){
			
			// stop auto
			clearTimeout(timer);
			
			// start timer
			var newDate = new Date();
			timer_start = newDate.getTime();
			
		}, function(){
			
			var newDate = new Date();
			var hover_length = newDate.getTime() - timer_start;
			var new_delay = delay - hover_length;
			
			if(new_delay < 0)
			{
				new_delay = 0;
			}
			
			slide_next(new_delay);
			
		});
		
	};
	
	
	/*--------------------------------------------------------------------------------------------
		setup_people_slider
	--------------------------------------------------------------------------------------------*/
	$.fn.setup_people_slider = function()
	{
		var div = $(this);
		var gallery = div.find('.gallery-inner');
		var li_width = div.find('.gallery-inner ul li:first-child').outerWidth();
		var i = 0;
		var i_max = div.find('.gallery-inner ul li').length - 4;
		
		
		div.find('.link-next').click(function(){
			
			if(i < i_max)
			{
				i++;
				
				gallery.animate({'scrollLeft' : li_width*i}, 500);
			}
			
			return false;
			
		});
		
		
		div.find('.link-prev').click(function(){

			if(i > 0)
			{
				i--;
				
				gallery.animate({'scrollLeft' : li_width*i}, 500);
			}
			
			return false;
			
		});
	};
	
	
	/*--------------------------------------------------------------------------------------------
		setup_how_we_do_it_slider
	--------------------------------------------------------------------------------------------*/
	$.fn.setup_how_we_do_it_slider = function()
	{
		var div = $(this);
		var li_width = div.width();
		var i = 0;
		var gallery = div.find('.gallery-inner');
		var slides = div.find('.gallery-inner ul li');
		var nav = div.find('.nav-inner ul li');
		
		div.find('.link-next').click(function(){
			
			if(slides.eq(i+1).exists())
			{
				i++;
				
				gallery.animate({'scrollLeft' : li_width*i}, 500);
				
				nav.removeClass('active');
				nav.eq(i).addClass('active');
			}
			
			return false;
			
		});
		
		
		div.find('.link-prev').click(function(){

			if(i > 0 && slides.eq(i-1).exists())
			{
				i--;
				
				gallery.animate({'scrollLeft' : li_width*i}, 500);
				
				nav.removeClass('active');
				nav.eq(i).addClass('active');
			}
			
			return false;
			
		});
		
		nav.click(function(){
			
			i = $(this).index();
			gallery.animate({'scrollLeft' : li_width*i}, 500);
			
			nav.removeClass('active');
			nav.eq(i).addClass('active');
				
			return false;
		});
	};
	
	/*--------------------------------------------------------------------------------------------
		setup_slider
	--------------------------------------------------------------------------------------------*/
	$.fn.setup_awards_slider = function()
	{
		var div = $(this);
		var parent = $(this).parent();
		
		parent.css({
			position: "relative"
		});
		
		//position the images
		div.find('img').each(function(){
			$(this).css({
				//position: "absolute",
				top: 0,
				left: 350
			})
		});
		
		//Hide all but the first image
		div.find('img').hide().eq(0).show();
		div.find('img').eq(0).addClass("active_image")
		
		//When a link is click do the fade in and out
		$('.awards-ul li a').click(function(){
			
			$('.awards-ul li.active').removeClass("active");
			$(this).parent().addClass("active");
			
			el = $(this).parent().index();
			
			div.find('.active_image').fadeOut(function(){
				div.find('.active_image').removeClass("active_image")
				div.find('img').eq(el).fadeIn().addClass("active_image");
			});	
					
			return false;
		});
		
	};
	
	
	
	/*--------------------------------------------------------------------------------------------
		Document Ready
	--------------------------------------------------------------------------------------------*/
	
	
	$(document).ready(function()
	{
		setup_forms();
		
		var contact_button = $('#toggle_contact_mask');
		contact_button.click(function(){
			
			
			if(contact_button.parent().hasClass('active'))
			{
				contact_button.parent().removeClass('active');
			}
			else
			{
				contact_button.parent().addClass('active');
			}
			
			$('#contact_mask').animate({'height' : 'toggle'}, 500);
			
			return false;
			
		});
		
		$('#scroll_to_top').click(function(){
			
			$('body, html').animate({'scrollTop' : 0}, 1000);
			
			return false;
		});
		
		$('#c_submit_a').click(function(){
			$('#c_submit').trigger('click');
			return false;
		});
	});
	
	
	/*--------------------------------------------------------------------------------------------
		Window load
	--------------------------------------------------------------------------------------------*/
	$(window).load(function()
	{

	});

	
	
})(jQuery);
