function best_q()
{
	$('#main_background .active .normal').fadeIn();
	if ($('#main_background .enter').length != 0)
	{
		$('#main_background .enter').show();
	}
}

function blur_q()
{
	$('#main_background .active .normal').fadeOut();
	if ($('#main_background .enter').length != 0)
	{
		$('#main_background .enter').hide();
	}		
}

$(document).ready(function() {
	
	if ($('#main_background').length != 0)
	{
		$('#main_background').hover(best_q,blur_q)
		
		if ($('#main_background .background_container').length > 1)
		{
			images = $('#main_background .background_container');
			i_total_images = images.length - 1;
			
			rolling_background_timeout = window.setTimeout('change_header_image(images, i_total_images)', timer);	
		}
		
		$('#main_background .arrow_left').hover(
			function()
			{
				best_q();
				$(this).addClass('hovered_arrow');	
			}, 
			function()
			{
				$(this).removeClass('hovered_arrow');	
			}
		)
		
		$('#main_background .arrow_right').hover(
			function()
			{
				best_q();
				$(this).addClass('hovered_arrow');
			}, 
			function()
			{
				$(this).removeClass('hovered_arrow');
			}
		)
		$('.main_container .arrow_left').click(
			function()
			{
				
				row_id = $('#main_background .active').attr('id').split('background_');
				i = parseInt(row_id[1]);
				
				var next_i = $('#main_background .background_container').length - 1;			
				if (i != 0)
				{
					next_i = i - 1;
				}
				
				stopRollBackgrounds();
				$('#main_background .background_container').hide();
				$('#main_background .background_container').removeClass('active');
				$('#background_' + next_i).show().addClass('active');
			}
		)
		
		$('.main_container .arrow_right').click(
			function()
			{
				row_id = $('#main_background .active').attr('id').split('background_');
				i = parseInt(row_id[1]);
				
				var next_i = 0;
				if (i != $('#main_background .background_container').length - 1)
				{
					next_i = i+1;
				}
				
				stopRollBackgrounds();
				$('#main_background .background_container').hide();
				$('#main_background .background_container').removeClass('active');
				$('#background_' + next_i).show().addClass('active');
			}
		)
	}
	

	if ($('.submenu_position').length != 0)
	{
		var w = $('.submenu_position').prev().width() + 40;
		if ( w > $('.submenu_position').width() )
		{
			$('.submenu_position').width(w);
		}
	}
	
	if ($('#contact_form').length != 0)
	{
		
		$('#submit .reset').click(
			function()
			{
				clearForm($('#contact_form'));
			}
		)		
		$('#submit .submit').click(
			function()
			{
				if (init_contactform())
				{
					$('#contact_form').sbmit();
				}
			}
		)
		
		
		clear_inputs_hovers();
		
		$('.inputbox_container input, .inputbox_container textarea').focus(
			function() 
			{
				active_inputbox($(this).parent().parent());
			}
		);
		
		$('.inputbox_container input, .inputbox_container textarea').blur(
			function() 
			{
				clear_inputs_hovers();
			}
		);			
	}
})

var timer = 5000;
function stopRollBackgrounds()
{
	clearTimeout(rolling_background_timeout);
}

function change_header_image(images, i) 
{
	if (i == (images.length - 1) ) 
	{
//		$(images).show();
	}
	if (i != 0) 
	{
		
		next_i = i-1;
		
		$(images[next_i]).addClass('active');
		
			window.setTimeout('change_header_image(images, next_i)', timer);
			
			$(images).removeClass('active');
			$(images).removeClass('active');
			$(images[next_i]).addClass('active');
			
	} else {
		i = next_i = images.length - 1;
		
		$(images[i]).addClass('active');
		
			window.setTimeout('change_header_image(images, next_i)', timer);
			$(images).removeClass('active');
			$(images[i]).addClass('active');
			
	}
	
	
	
	
	
}

function active_inputbox(inputbox) {
	
	main_div = $(inputbox).parent();
	if (!$(main_div).is('active')) {
		$(main_div).addClass('active');
	}
}

function clear_inputs_hovers() {
	$('.active > .inputbox_container').parent().removeClass('active');
}


function clearForm(form) {
  $(':input', form).each(function() {
	var type = this.type;
	var tag = this.tagName.toLowerCase(); 
	if (type == 'text' || type == 'password' || tag == 'textarea')
	  this.value = "";
	else if (type == 'checkbox' || type == 'radio')
	  this.checked = false;
	else if (tag == 'select')
	  this.selectedIndex = -1;
  });
};

function init_contactform() {
	var accept = true;
	
	$('#contact_form div.required input, #contact_form div.required textarea').each(function () {
		if ($(this).val() == "") {
			accept = false;
			$(this).parent().parent().parent().addClass('alert');
		} else {
			$(this).parent().parent().parent().removeClass('alert');
		}	
		
		if ($(this).attr('id') == 'form_email' && !$(this).val().match(new RegExp('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$'))) {
			accept = false;
			$(this).parent().parent().parent().addClass('alert');
		} 
		
		if ($(this).attr('id') == 'form_phone' && !$(this).val().match(new RegExp('^[0-9\-\+\\s\(\)]{7,}$'))) {
			accept = false;
			$(this).parent().parent().parent().addClass('alert');
		} 
	});
	
	return accept;
}
