$(document).ready(function() {
	
	function isValidEmailAddress(emailAddress) 
	{
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		
		return pattern.test(emailAddress);
	}
	
	$('html').removeClass('no-js');

	// You need to specify the size of your background image here (could be done automatically by some PHP code)
	var nFullscreenrOptions = {width:1280, height:1175, bgID: '#background-body'};
	// This will activate the full screen background!
	jQuery.fn.nFullscreenr(nFullscreenrOptions);
	
	$(".map").colorbox({iframe:true, innerWidth:575, innerHeight:450});
	
	$("#history-link").colorbox({inline:true, href:"#photos", title:"", width: 650});
	
	$('#photos').galleryView({panel_width:600,panel_height:386,overlay_height:70,gallery_width:600,frame_width:90,frame_height:90, nav_theme:"light", transition_speed:100, transition_interval:10000000, fade_panels:false, pause_on_hover:true});

  
  $('.person-thumb').click(function()
	{
		var parent = $(this).parent('.contact');
		
		var targetImage = parent.find('img.person-image');
		var targetPath = targetImage.attr('src');
		
		if(targetPath != null)
		{	
			$.fn.colorbox({href:targetPath});
		}
	})
	
	/*--------------------------------------------------
		Description
	--------------------------------------------------*/
	$('.navigation_list').hover(
		function () { $(this).addClass('over'); }, 
		function () { $(this).removeClass('over'); }
	);
	
	/*--------------------------------------------------
		Description
	--------------------------------------------------*/
	$('.acessability-list').hover(
		function()  { $(this).siblings(' .navigation_list').addClass('over'); }, 
		function() { $(this).siblings(' .navigation_list').removeClass('over'); }
	);
	
	/*--------------------------------------------------
		Description
	--------------------------------------------------*/
	$('#contact-form').submit(function()
	{
		var validation_success = true;
		
		var inputs = $('form#contact-form').find('.required');
		
		jQuery.each(inputs, function()
		{
			var id = $(this).attr('id');
			var value = $(this).val();
			
			if(value == '' || value == 'undefined' || value.length < 1)
			{
				validation_success = false;
				
				$('#'+id).focus();
				$('#'+id).css('background-color', '#e3f0f7');
			}
			else
			{	
				if(id == 'email' && !isValidEmailAddress(value))
				{
					validation_success = false;

					$('#'+id).focus();
					$('#'+id).css('background-color', '#e3f0f7');
				}
				else
				{
					$('#'+id).css('background-color', '#ffffff');
				}
				
			}
		});
		
		if(!validation_success)
		{
			return false;
		}
		
	});
	
	$('.capa_search_plane').click(function(){
		var tmp = $(this).children(".value").html();
		var text = $(this).children(".text").html();
		
		var urls = ({
			plane_search : $('#capa_plane_search').val()
		});
		
		$("#hiddenPlane").val($(this).children(".value").html());
		
		$.post(urls.plane_search, { plane: $(this).children(".value").html(), query_val: $('#cl-query_val').val()}, function(data){
			$("#capalist_data").html(data);
			$("#plane_searched").html(text);
		});

		return false;
	});

	$('#cl-query_val').keyup(function(){
		
		var urls = ({
			part_search : $('#capa_part_search').val()
		});
		
		$.post(urls.part_search, { query_val: $(this).val(), plane: $("#hiddenPlane").val()}, function(data){
			$("#capalist_data").html(data);
		});
	});
	
	$('#ajax_load')
	.hide()  // hide it initially
	.ajaxStart(function() {
		$(this).show();
	})
	.ajaxStop(function() {
		$(this).hide();
	});
	
	/*--------------------------------- Surpluslist function --------------------------------*/
	$('.sortable').css('cursor', "pointer");
	$('.sortable').click(function(){
		var surplusUrl = $('#surplusUrl').val();
		$(".sortable").parent().removeClass("sorted");
		$(this).parent().addClass("sorted");
		if($(this).hasClass("desc")) {
			var dir = "asc";
			$(this).addClass("asc");
			$(this).removeClass("desc");
			$(this).parent().addClass("asc");

		} else {
			var dir = "desc";
			$(this).addClass("desc");
			$(this).removeClass("asc");
			$(this).parent().addClass("desc");
		}
		$.post(surplusUrl, { by: $(this).attr('id'), dir: dir}, function(data){
			$("#surplus_data").html(data);
		});
		
		return false;
	});
	
});

/**
 * Set height of two elements equal
 */
function fixHeights(first, second)
{
	if($(first).height() > $(second).height())
		$(second).height($(first).height());
	else if($(first).height() < $(second).height())
		$(first).height($(second).height());
}
