//xpngFix();
var changer = 0;
function changenavi (obj_id)
	{	
					if(changer == obj_id) {
						changer = obj_id;
					}
					else if(changer != obj_id && changer != '0') {
						document.getElementById(obj_id).style.display = '';
						changer = obj_id;
					}
					else {
						document.getElementById(obj_id).style.display = '';
						changer = obj_id;
					}
					/*if(document.getElementById(obj_id).style.display == '')
					{
						document.getElementById(obj_id).style.display = 'none';     
					} else {
						document.getElementById(obj_id).style.display = '';
					}*/		                                                                                                                          
	};
	
(function ($){
$.noConflict();
jQuery(document).ready(function(){
	
	jQuery('.header .lang').change(function(){
		location.href = jQuery(this).val();
	});
	
	jQuery('#s3').cycle({ 
    fx:    'fade',
    speed:  2500 
 });
 
 jQuery('#s4').cycle({ 
    fx:    'fade',
    speed:  1500,
    timeout: 5000 
 });
 
	//$('.subnavi-dropdown').addClass("selected");
	//Dropdown navi
	
	
	$('.navi-top2 .navi-container a').hover(function() {
//		if(typeof newDropdown != 'undefined') hideTimeout = setTimeout("newDropdown.slideUp(200)", 500);
		newDropdown = $('.dropdown-'+ $(this).attr('rel'));
		showTimeout = setTimeout("newDropdown.slideDown(100)", 100);
		//document.getElementById($(this).attr('rel')).style.display = '';
	}, function() {
		oldDropdown = newDropdown;
		hideTimeout = setTimeout("oldDropdown.slideUp(200)", 100);
	});

	
	$('.subnavi-dropdown').hover(function() {
		if(typeof oldDropdown == 'undefined') {
			//document.getElementById($(this).attr('id')).style.display = 'none'; 
			return;
		}
		clearTimeout(hideTimeout);
		if(oldDropdown != newDropdown) {
			
			
			clearTimeout(hideTimeout);
			
			
//			clearTimeout(hideTimeout);
		}
	}, function() {
		document.getElementById($(this).attr('id')).style.display = 'none'; 
		hideTimeout = setTimeout("oldDropdown.slideUp(10)", 50);
	});
	


	//Calculator
	var $toLang = $('#target-lang'),
		$fromLang = $('#source-lang')
	;
	$fromLang.change(function(e){
		var selectedLang = e.target.value;
		//reset right value if the same lang left was selected
		if (selectedLang == $toLang.val())
			$toLang.get(0).selectedIndex = 0;

		$toLang.removeAttr('disabled').children().removeAttr('disabled').
		filter('[value="'+selectedLang+'"]').attr('disabled','disabled');
	});
	
	
	var $oblFieldsCalc = $('#calculate').children('*[rel!=""]').xTooltip({form: 'error'});
	
	//calculate the price
	$('#calculate').submit(function(){
		
		var $form = $(this);
		$oblFieldsCalc.each(function(i){
			if ( !this.value )
			{
				$(this).xTooltip('show');
				return false;
			} else if (i>= $oblFieldsCalc.length-1)
				calculate();
		});

		function calculate ()
		{
			var textLength = $form.children('textarea').val().length,
				langToData = getLangData($toLang),
				langFromData = getLangData($fromLang),
				fromLang = $fromLang.val(),
				toLang = $toLang.val(),
				textContent = $form.children('textarea').val()
				
			;
			if (toLang == 'de')
				langToData = langFromData;

			if ( fromLang == 'de' || toLang == 'de' )
				langPercent = 100;
			else
				langPercent = 100*100/langFromData[0];

			var	
				price = textLength*langToData[0]/langPercent*langToData[1]/50,
				priceAuth = textLength*langToData[0]/langPercent*langToData[2]/50,
				rabatt = parseFloat( $form.children('input[name="rabatt"]').val().replace(/,/,'.') ),
				price_name = $form.children('input[name="price_name"]').val()
			;
			
			if ((price-price*rabatt/100) < 80){
				var memprice = 80;
				var mempriceAuth = 86;
			}
			else{
				var memprice = price-price*rabatt/100;
				var mempriceAuth = priceAuth-priceAuth*rabatt/100;
			}
			$form.slideUp(300).next().slideDown(300).find('.price').text(round(memprice,2)+' Euro ('+ price_name +')');
			$form.next().find('.price-total').text(round(memprice,2)+' Euro ('+ price_name +')');
			$form.next().find('.price-total-with-auth').text(round(mempriceAuth,2)+' Euro ('+ price_name +')');
			$form.next().find('.weitertext').val(textContent);
			$form.next().find('.Ausgangssprache').val(fromLang);
			$form.next().find('.Zielsprache').val(toLang);
			$form.next().find('#Gesamtsumme').val(round(memprice,2)+' Euro ('+ price_name +')');
			$form.next().find('#Gesamtsumme_mit_beglaubigung').val(round(mempriceAuth,2)+' Euro ('+ price_name +')');
			
			function round (x, n) 
			{
			  if (n < 1 || n > 14) return false;
			  var e = Math.pow(10, n);
			  var k = (Math.round(x * e) / e).toString();
			  if (k.indexOf('.') == -1) k += '.';
			  k += e.toString().substring(1);
			  return k.substring(0, k.indexOf('.') + n+1);
			};
			
			function getLangData ($elem)
			{
				return $.map($elem.children('[value="'+$elem.val()+'"]').attr('rel').split(':'), function(n){
					 return parseFloat(n.replace(/,/,'.'));
				});
			};
		};


		return false;
	});


	
	$('.calculator #edit').click(function(){
		$('.calculator .bill').slideUp(300);
		$('.calculator form').slideDown(300);
		return false;
	});
	

	$oblFieldsRequest = $('#request').children('*[rel!=""]').xTooltip({form: 'error'});
	$('#request').submit(function(){
		var ret = true;
		$oblFieldsRequest.each(function(i){
			if (!this.value || (this.name == 'E-Mail' && !this.value.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) || (this.name == 'agbcheck' && !this.checked) )
			{
				$(this).xTooltip('show');
				ret = false;
				return false;
			};
		});
		return ret;
	
	});


	//Request form
	var $oblFields2 = $('#price-quotation').children('*[rel!=""]').xTooltip({form: 'error'});
	$('#price-quotation').submit(function(){
		var ret = true;
		$oblFields2.each(function(i){
			if (!this.value || (this.name == 'E-Mail' && !this.value.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) )
			{
				$(this).xTooltip('show');
				ret = false;
				return false;
			} 
		});
		return ret;
	
	});
	
	$('#tester').click(function(){
		$('#downloads').append('<label>'+$('#download_text').val()+':</label><input type="file" class="file" name="Dateiupload[]"/><br />');
	});

});



})(jQuery);