$(document).ready(function()
{
	$('a.mainLink:not(.mainClickable, .mainSelected)').live('click', function ()
	{
		$('.mainLink').removeClass('selected');
		$(this).addClass('selected');
		$('.arrow').hide();
		$(this).closest('div').prev('.arrow').show();
		$('.subItem:visible').slideUp();
		$(this).closest('div').closest('.mainItem').next('div.subItem').slideToggle();
		$('a.mainLink').css('text-decoration', 'none');
		return false;
	});
	$('a.mainLink:not(.selected)').live({mouseenter: function ()
		{
			$(this).css('text-decoration', 'underline');
		}, mouseleave: function ()
		{
			$(this).css('text-decoration', 'none');
		}
	});	
	$('.mouseover').hover(function ()
	{
		oldSrc = $(this).attr('src');
		$(this).attr('src', $(this).attr('name'));
		$(this).attr('name', oldSrc);
	}, function ()
	{
		oldSrc = $(this).attr('src');
		$(this).attr('src', $(this).attr('name'));
		$(this).attr('name', oldSrc);
	});
	$('.frontPopup').hover(function ()
	{
		$('#popupText').html($(this).children('img').attr('rel'));
	}, function ()
	{
		$('#popupText').html('');
	});
	$('input:button, input:submit').hover(function ()
	{
		$(this).css('cursor', 'pointer');
	}, function ()
	{
		$(this).css('cursor', 'default');
	});
	$('.openFF').click(function ()
	{
		$('#ffContainer').fadeIn();
		return false;
	});
	$('.closeFF').click(function ()
	{
		$('#ffContainer').fadeOut();
		return false;
	});
	$('a.mainLink:not(.selected)').live({mouseenter: function ()
		{
			$(this).css('text-decoration', 'underline');
		}, mouseleave: function ()
		{
			$(this).css('text-decoration', 'none');
		}
	});	
	$('.ffCheckbox').click(function ()
	{
		image1 = 'url("http://www.elite-traffic.de/gfx/forfragan/fillout_check_01.png")';
		image2 = 'url("http://www.elite-traffic.de/gfx/forfragan/fillout_check_02.png")'
		if($(this).css('background-image') == image1)
		{
			$('.ffCheckbox').css('background-image', image1);
			$(this).css('background-image', image2)
			if($(this).hasClass('checkDisposal'))
			{
				$('.checkPickupLeave').next('input').removeAttr('checked');
				$(this).next('input').attr('checked', 'checked');
				$('#ffBottom > :input').show();
				$('#ffBottom').css('background-image', 'url(\'http://www.elite-traffic.de/gfx/forfragan/fillout_bot_01.png\')');
			}
			else
			{
				$('.checkDisposal').next('input').removeAttr('checked');
				$(this).next('input').attr('checked', 'checked');
				$('#ffBottom > :input').show();
				$('#ffBottom').css('background-image', 'url(\'http://www.elite-traffic.de/gfx/forfragan/fillout_bot_02.png\')');
			}
		}
		else
		{
			$(this).next('input').removeAttr('checked');
			$('#ffBottom > :input').hide();
			$('#ffBottom').css('background-image', 'url(\'http://www.elite-traffic.de/gfx/forfragan/fillout_bot.png\')');
			$('.ffCheckbox').css('background-image', image1);
			$(this).css('background-image', image1)
		}
	});
	$('.ffCheckboxMore').click(function ()
	{
		image1 = 'url("http://www.elite-traffic.de/gfx/forfragan/fillout_check_01.png")';
		image2 = 'url("http://www.elite-traffic.de/gfx/forfragan/fillout_check_02.png")'
		if($(this).css('background-image') == image1)
		{
			$(this).next('input').attr('checked', 'checked');
			$('.ffCheckbox').css('background-image', image1);
			$(this).css('background-image', image2)
		}
		else
		{
			$(this).next('input').removeAttr('checked');
			$('.ffCheckbox').css('background-image', image1);
			$(this).css('background-image', image1)
		}
	});
	$('#createFF').submit(function ()
	{
		if($('#companyname').val() == '' && $('#contactperson').val() == '')
		{
			var customerAlert = true;	
		}
		if($('#phone').val() == '' && $('#email').val() == '')
		{
			var contactAlert = true;	
		}
		if(customerAlert && contactAlert)
		{
			alert('Bitte Kunde oder Ansprechpartner ausfüllen.\r\nBitte Telefon oder E-Mail ausfüllen.');
		}
		else if(customerAlert)
		{
			alert('Bitte Kunde oder Ansprechpartner ausfüllen.');
		}
		else if(contactAlert)
		{
			alert('Bitte Telefon oder E-Mail ausfüllen.');
		}
		if(customerAlert || contactAlert)
		{
			return false;
		}
	});
	$('.contactRadio').change(function ()
	{
		$('#contactContainer').slideDown();
	});
	$('#contactForm').submit(function ()
	{
		if($('#contactEmail').val() == '' || $('#contactCompany').val() == '' || $('#contactName').val() == '' || $('#contactAddress').val() == '' || $('#contactZipcode').val() == '' || $('#contactPhone').val() == '')
		{
			alert('Bitte mindestens die mit * markierten Felder ausfüllen.');
			return false;	
		}
	});
	$('#jobForm').submit(function ()
	{
		var masterError = false;
		$('.contactField').each(function ()
		{
			if($(this).val() == '')
			{
				masterError = true;
			}
		});
		if(masterError)
		{
			alert('Bitte füllen Sie alle Felder aus.');
			return false;
		}
	});
	$('#frontRotator').cycle({
		timeout : 7500,
		speed : 2500,
		delay : -1250
	});
	$('#rotator').cycle({
		timeout : 7500,
		speed : 2500,
		delay : -1250,
		pager: '#nav',
		pagerAnchorBuilder: function (idx, slide)
		{
			var busType = $(slide).attr('src').split('/');
			busType = busType[5]
			if(idx == 0)
			{
				return '<div style="float: left; width: 199px; height: 100px; background: #ffffff;"><a href="#" class="thumbLink" rel="1"><img src="http://www.elite-traffic.de/gfx/fahrzeuge/' + busType + '/00' + parseInt(parseInt(idx) + 1) + '_tn.jpg" border="0" width="189" height="90" style="margin-top: 5px; margin-left: 5px;" /></a></div>';
			}
			else
			{
				return '<div style="float: left; width: 199px; height: 100px; background: #ffffff; margin-left: 3px;"><a href="#"><img src="http://www.elite-traffic.de/gfx/fahrzeuge/' + busType + '/00' + parseInt(parseInt(idx) + 1) + '_tn.jpg" border="0" width="189" height="90" style="margin-top: 5px; margin-left: 5px;" /></a></div>';
			}
		}
	});
	$('.mapIcon').click(function ()
	{
		$('#mapInfo').html($('#stationInfo' + $(this).attr('rel')).html());
		$('.mapLink').removeClass('selected');
		$('#mapLink' + $(this).attr('rel')).addClass('selected');
		return false;	
	});
	$('.mapLink').click(function ()
	{
		$('#mapInfo').html($('#stationInfo' + $(this).attr('rel')).html());
		$('.mapLink').removeClass('selected');
		$(this).addClass('selected');
		return false;	
	});
	$('#traumzielOrder').click(function ()
	{
		$('#wunschterminContainer').hide();
		$('#traumzielContainer').show();
		return false;
	});
	$('#wunschterminOrder').click(function ()
	{
		$('#traumzielContainer').hide();
		$('#wunschterminContainer').show();
		return false;
	});
	$('.popup').qtip({ position: { target: 'mouse' }});
	$('#searchText').focus(function ()
	{
		if($(this).val() == $(this).attr('title'))
		{
			$(this).val('');	
		}
	});
	$('#searchText').blur(function ()
	{
		if($(this).val() == '')
		{
			$(this).val($(this).attr('title'));	
		}
	});
	$('#searchForm').submit(function ()
	{
		$('#searchQ').val('site:www.elite-traffic.de ' + $('#searchText').val());
		$('#searchFormQ').submit();
		return false;
	});
	$('#jobDetailsShow').click(function ()
	{
		$('#jobDetailContainer').slideDown();
	});
	$('#newsletterForm').submit(function ()
	{
		var regExpPattern = /^\s*[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})+$/;
		if(!regExpPattern.test($('#newsletterEmail').val()))
		{
			alert('Bitte geben Sie eine gültige E-Mail-Adresse an.');
			return false;
		}
	});
});
