(function($){	
	
	var internal_links = new Array();
	//internal_links["http://192.168.10.100:8942/our_services"] = '#our_services';
	
	$.fn.exists = function()
	{
		return $(this).length>0;
	};
	
	
	/*--------------------------------------------------------------------------------------------
		Hero Slider
	--------------------------------------------------------------------------------------------*/
	function setup_hero_slider()
	{
		var hero = $('.hero');
		var slides = hero.find('.slides');
		
		$(window).resize(function(){
			
			slides.find('img').each(function(){
				
				// set width (height is auto)
				$(this).css({'width':$(window).width(), 'height':'auto'});
				
				
				// if height is < hero height
				if($(this).height() < parseInt(slides.css('height')))
				{
					$(this).css({'width':'auto', 'height': slides.css('height')});
				}
				
				
				// if width of image is now larger than window
				var diffx = $(this).width() - $(window).width();
				var diffy = $(this).height() - parseInt(slides.css('height'));
				
				// limit diff to only + numbers
				if(diffx < 0)
				{
					diffx = 0
				}
				else
				{
					diffx = diffx / 2;
				}
				
				// limit diff to only + numbers
				if(diffy < 0)
				{
					diffy = 0
				}
				else
				{
					diffy = diffy / 2;
				}
				
				
				// keep hero image centered
				slides.scrollLeft(diffx);
				slides.scrollTop(diffy);
				
			});
			
		}).trigger('resize');
		
		/*
		$(window).load(function(){
			
			$(window).trigger('resize');
			$(this).animate({'opacity' : '1'}, 1000);
		});
		
		*/
	}
	
	
	/*--------------------------------------------------------------------------------------------
		setup_nav_timesheet
	--------------------------------------------------------------------------------------------*/
	function setup_nav_timesheet()
	{
		var a = $('.header li.timer a');
		var div = $('.header li.timer .hover');
		var nav = $('.header .nav');
		var login = $('.header .login');
		
		
		// div is initialy open
		div.addClass('open');
		
		
		a.mouseenter(function(){
			if(div.is('.open'))
			{
				
			}
			else
			{
				div.stop().css({'opacity':'0','margin-bottom':'-35px'});
			}
			div.stop().animate({'opacity':'1', 'margin-bottom':'-40px'}, 400);
		});
		
		a.mouseleave(function(){
			div.removeClass('open').animate({'opacity':'0', 'margin-bottom':'-45px'}, 400);
		});
		
		div.mouseleave(function(){
			div.removeClass('open').animate({'opacity':'0', 'margin-bottom':'-45px'}, 400);
		});
		
		
		// Close .hover after 2 secconds
		var t = setTimeout(function(){
			div.removeClass('open').animate({'opacity':'0', 'margin-bottom':'-45px'}, 400);
		}, 2000);
		
		
		a.click(function(){
			if(nav.hasClass('open'))
			{
				nav.removeClass('open');
				nav.stop().animate({'top':'0px'}, 500);
			}
			else
			{
				nav.addClass('open');
				nav.stop().animate({'top':'50px'}, 500);
			}
			
			return false;
		});
		
		
		login.find('input#name').each(function(){
			var i = $(this);
			var default_value = i.val();
			
			i.focus(function(){
				if(i.val() == default_value)
				{
					i.val("");
				};
			});
			i.blur(function(){
				if(i.val() == "")
				{
					i.val(default_value);
				};
			});
		});
		
		login.find('input#password_text').each(function(){
			var i = $(this);
			
			i.focus(function(){
				i.hide();
				login.find('input#password').show().focus();
			});
			
		});
		
		login.find('input#password').each(function(){
			var i = $(this);
			
			i.blur(function(){
				if(i.val() == "")
				{
					i.hide();
					login.find('input#password_text').show();
				};
			});
			
			i.hide();
			
		});

	}
	
	
	/*--------------------------------------------------------------------------------------------
		setup_poll_form
	--------------------------------------------------------------------------------------------*/
	function setup_poll_form()
	{
		var div = $('form#poll');
		
		
		// radio buttons
		div.find('label.radio').click(function(){
			div.find('label.radio').removeClass('active');
			$(this).addClass('active');
			//$(this).closest('li').siblings('li').find('input[type="checkbox"]').attr('checked',false);
		});
	}
	
	
	/*--------------------------------------------------------------------------------------------
		setup_page_sliders
	--------------------------------------------------------------------------------------------*/
	function setup_page_sliders()
	{
		
		$('.page_slider').each(function(){
			
			var slider = $(this);
			var nav = slider.find('.nav');
			var mask = slider.find('.mask');
			var pages = slider.find('.pages');
			var section_id = slider.parent('.section').attr('id');
			//var page = slider.find('.page');
			
			nav.find('li a').each(function(i){
				
				var a = $(this);
				var url = a.attr('href');

				
				// create loading div
				var loading = $('<div class="loading" id="'+section_id+'_'+(i+1)+'"></div>');
				pages.append(loading);
				
				
				// add url to global var
				internal_links[url] = '#'+section_id+'_'+(i+1);
				
				
				// add nav slide to
				//a.attr('href','#'+section_id+'_'+(i+1));
				a.addClass('scroll_to');
				
				
				var temp = $('<div></div>');
				temp.load(url+' .page ', function(){
					
					// on complete
					var page = temp.find('.page');
					page.attr('id',section_id+'_'+(i+1));
					loading.replaceWith(page);
					
					// setup stuff
					
					setup_accordion();
					setup_poll_results();
					
					// resize mask to first page height
					if(i == 0)
					{
						mask.css({'height':page.height()+'px'});	
					}
				});
				
			});
			
			
			// set width of pages (in case of lots of pages)
			//pages.css({'width'	:	page.length * 960 + 'px'});
			
			
			// set active on first page
			mask.scrollLeft(0);
			nav.find('li').eq(0).addClass('active');
			
			// nav click
			/*nav.find('a').click(function(){
				
				// get index
				var i = $(this).parent('li').index();
				
				// set height
				var height = page.eq(i).height();
				
				// animate mask
				mask.animate({'height':height+'px', 'scrollLeft':(960*i)+'px'}, 500);
				
				// set active li
				nav.find('li').removeClass('active');
				nav.find('li').eq(i).addClass('active');
				return false;	
			});*/
			
		});	
	}
	
	
	/*--------------------------------------------------------------------------------------------
		Setup a.scroll_to
	--------------------------------------------------------------------------------------------*/
	function setup_scroll_to()
	{
		$('a').live('click', function(){
			
			if(internal_links[$(this).attr('href')])
			{
				//alert('s');
				// this link has now been converted into a loaded page!
				id = internal_links[$(this).attr('href')].replace('#','');
				
				var p = $('#'+id);
				var i = p.index();
				
				// slide y
				var offset = p.closest('.section').offset().top;
				$('html, body').animate({'scrollTop':offset+'px'}, 500, function(){
					window.location.hash = '#'+ p.closest('.section').attr('id');
				});
				
				// set height
				var height = p.height();
				
				// animate mask
				p.closest('.mask').animate({'height':height+'px', 'scrollLeft':(960*i)+'px'}, 500);			
				
				// set sub nav active
				if(p.closest('.section').find('.nav ul li:eq('+i+')').exists())
				{
					p.closest('.section').find('.nav ul li').removeClass('active');
					p.closest('.section').find('.nav ul li:eq('+i+')').addClass('active');
				}
				
				return false;

			}
		});
	}
	
	
	/*--------------------------------------------------------------------------------------------
		Setup Nav
	--------------------------------------------------------------------------------------------*/
	function setup_header()
	{
		var header = $('.header')
		var last_active = 'home';
		var active = '';
		var back_to_top = $('.back_to_top');
		
		
		// scroll amount
		var s = 0;
		
		// section offest - y
		var o_y = 0;
		var a_y = 0;
		var r_y = 0;
		var j_y = 0;
		var c_y = 0;
		
		
		// set values
		$(window).resize(function(){
			o_y = $('#our_services').offset().top;
			a_y = $('#about_us').offset().top;
			r_y = $('#resources').offset().top;
			j_y = $('#jobs').offset().top;
			c_y = $('#contact_us').offset().top;
		}).trigger('resize');
		
		
		// on scroll set nav li.active
		$(window).scroll(function(){
		
			s = $(window).scrollTop() + ($(window).height()/4);
			
			if(s > c_y)
			{
				active = "contact_us";
			}
			else if (s > j_y)
			{
				active = "jobs";
			}
			else if (s > r_y)
			{
				active = "resources";
			}
			else if (s > a_y)
			{
				active = "about_us";
			}
			else if (s > o_y)
			{
				active = "our_services";
			}
			else
			{
				active = "home";
			}
			
			if(active != last_active)
			{
				last_active = active;
				header.find('li').removeClass('active');
				header.find('li.'+active).addClass('active');
				
				if(active == "home")
				{
					back_to_top.stop().animate({'opacity':'0'}, 500, function(){
						$(this).css({'display':'none'});
					});
				}
				else
				{
					back_to_top.stop().css({'display':'block'}).animate({'opacity':'1'}, 500);
				}
			}
			
			
		}).trigger('resize');
		
	}
	
	
	/*--------------------------------------------------------------------------------------------
		Setup a.slide_to
	--------------------------------------------------------------------------------------------*/
	function setup_slide_to()
	{
		$('.slide_to').live('click', function(){
			
			var attr = $(this).attr('href');
			attr = "#" + attr.split('#').pop();
			
			if(internal_links[attr])
			{
				
				// this link has now been converted into a loaded page!
				id = internal_links[attr].replace('#','');
			}
			else
			{
				id = attr.replace('#','');
			}
			
			var new_hash = attr;
			
			var offset = $('#'+id).offset().top;
			$('html, body').animate({'scrollTop':offset+'px'}, 500, function(){
				window.location.hash = new_hash;
			});
			
			return false;
		});
	}
	
	
	/*--------------------------------------------------------------------------------------------
		Setup team_thumbs
	--------------------------------------------------------------------------------------------*/
	function setup_team_thumbs()
	{
		/*$('.team_thumb').each(function(){
			var div = $(this);
			
			div.mouseenter(function(){
				div.find('img.hover').animate({'opacity':'1'}, 300);
			});
			
			div.mouseleave(function(){
				div.find('img.hover').animate({'opacity':'0'}, 300);
			});
			
		});*/
		
	}
	
	
	/*--------------------------------------------------------------------------------------------
		Setup accordion
	--------------------------------------------------------------------------------------------*/
	function setup_accordion()
	{
		$('a.accordion').click(function(){
			id = $(this).attr('href').replace('#','');
			var div = $('#'+id);
			
			if(div.is(':animated'))
			{
				return false;
			}
			
			$('#'+id).animate({'height':'toggle'}, 500);
			
			return false;
		});
	}
	
	
	/*--------------------------------------------------------------------------------------------
		Setup Poll Results
	--------------------------------------------------------------------------------------------*/
	function setup_poll_results()
	{
		$('form.poll_results').each(function(){
		
			var form = $(this);
			var value_yes = form.find('input[name="value_yes"]').val();
			var value_no = form.find('input[name="value_no"]').val();
			var max_width = parseInt(form.find('.value_bar').css('max-width'));
			
			var width_yes = ((max_width / 100) * parseInt(value_yes));
			var width_no = ((max_width / 100) * parseInt(value_no));
			
			form.find('li.yes .value_bar').css({'width':width_yes + 'px'});
			form.find('li.no .value_bar').css({'width':width_no + 'px'});
			
			form.find('li.yes .value_no').html(value_yes+'%');
			form.find('li.no .value_no').html(value_no+'%');
			
		});
	}
	
	/*--------------------------------------------------------------------------------------------
	Job Adder Sub Categories
	--------------------------------------------------------------------------------------------*/
	function job_adder_sub_categories()
	{
		var index = 0;
		var category = $("select#category");
		var category_field = $(".field.category_field");
		
		var sub_category = $("select#sub_category");
		var sub_category_field = $(".field.sub_category_field");
		//alert(category.val());
		
		category_field.find('ul li a').click(function(){
			index = $(this).parent().index();
			var id = $("#category option").eq(index).val();

			//Do the AJAX call
			$.ajax({
			  url: "/ajax/ja_sub_categories/"+id,
			  success: function(data){
			  
			  	sub_category_field.find(".jqTransformSelectWrapper").remove();
			  	
			  	var select = $('<select name="sub_category" id="sub_category"><option value="">Any</option></select>');
				select.append(data);
				
				sub_category_field.append(select);
				select.jqTransSelect();
				
			  }
			});
		});
		
		category_field.find('ul li a:first').trigger('click');
		
	}
	
	/*--------------------------------------------------------------------------------------------
		Document Ready
	--------------------------------------------------------------------------------------------*/
	$(document).ready(function()
	{
		if($('body').attr('class') == 'home')
		{
			setup_slide_to();
			setup_scroll_to();
			setup_poll_form();
			setup_header();

		}
		else if($('body').attr('class') == "jobs_search"  || $('body').attr('class') == "results"  || $('body').attr('class') == "job")
		{
			$('.job_search_form form').jqTransform();
			job_adder_sub_categories();
		}
		else if($('body').attr('class') == "job")
		{
			setup_accordion();
			
			// validate fields
			$('form#apply_form').validate({
				highlight: function(element, errorClass, validClass) {
				    $(element).closest('.field').addClass('error');
				},
				unhighlight: function(element, errorClass, validClass) {
				    $(element).closest('.field').removeClass('error');
				}
			});
		}
		
		setup_hero_slider();
		setup_page_sliders();
		setup_nav_timesheet();
		
	});
	
	
	
	
	/*--------------------------------------------------------------------------------------------
		Window load
	--------------------------------------------------------------------------------------------*/
	$(window).load(function()
	{
		$(window).trigger('resize');
		//setup_team_thumbs();
		
	});
	
})(jQuery);	

