// readyFunction
$(document).ready(function(){
	// cufon
	Cufon.replace('.headlines h1');
	Cufon.replace('.headlines h2');

	$html = jQuery('html').addClass('hasJs');

	$body = jQuery('body');
	if(swfobject.getFlashPlayerVersion().major > 9) {
		$body.addClass('hasFlash hasFlash10');
	}

	// pngFix for IE
	//$(document).pngFix();

	// default value in textSearch
	if(jQuery('#searchword') !== null){
		jQuery('#searchword').each(function(){
			var default_value=this.value;jQuery(this).focus(function(){
				if(this.value==default_value){this.value='';}
			});
			jQuery(this).blur(function(){
				if(this.value==''){this.value = default_value;}
			});
		});
	};

	// remove default ul.browsebox in indexed_search resultpage
	if(jQuery('.tx-indexedsearch') !== null){
		jQuery('form#tx_indexedsearch').attr('name','tx_indexedsearch');
		jQuery('.tx-indexedsearch-browsebox .browsebox:first').empty().remove();
	};

	//not for iphone, ipad, android
	var agent = navigator.userAgent.toLowerCase().toString();

	if(	(agent.indexOf('iphone')==-1)
	     && (agent.indexOf('ipad')==-1)
	     && (agent.indexOf('android')==-1)
	){

		// countrySelect
		var countryList = jQuery('#countrySelectListWrap');
		var countryToggle = jQuery('#countrySelectGlobal p');
		if(countryList !== null){
			countryList.css({'display':'none'});
			countryToggle.click(function(){
				if(countryList.css('display')=='none'){countryList.css({'display':'block'});}
				else{countryList.css({'display':'none'});}
			});
			jQuery('#countrySelectGlobal').click(function(e){e.stopPropagation();});
			jQuery(document).click(function(){countryList.css({'display' : 'none'});});
		};

		// stylish selects
		jQuery('#genderSelect select, #titleSelect select, #formhandler_beschriftung, #formhandler_zustand').sSelect();
		jQuery('#countrySelectbox select').sSelect({ddMaxHeight: '300px'});

		// Flashdaten
		jQuery('.fahrzeugdatenForm select').sSelect();

		// DEV!!!
		jQuery('#devSelect select').sSelect();
	}

		// language switch for homologation
	jQuery('#languageHomologation ul').click(function(){
		jQuery(this).toggleClass('active');
	});

	// defaultValues in inputs
	jQuery('.inputWrap input, .textareaWrap textarea').each(function(){
		jQuery(this).addClass('hasDefault');
		if(this.value!=''){
			jQuery(this).addClass('focused');
		};
		jQuery(this).focus(function(){
			jQuery(this).addClass('focused');
		});
		jQuery(this).blur(function(){
			if(this.value==''){
				jQuery(this).removeClass('focused');
			}
		});
	});

	// resizeable textareas
	jQuery('textarea.resizable:not(.processed)').TextAreaResizer();

	// countryHome - TeaserBox - topNews
	if(jQuery('#topNews') !== null){
		jQuery('#topNews').css({'height':'185px'});
		jQuery('#topNews li').css({'display':'none','position':'absolute'});
		initTeaser('topNews');
	};
	// countryHome - TeaserBox - topActions
	if(jQuery('#topActions') !== null){
		jQuery('#topActions').css({'height':'185px'});
		jQuery('#topActions li').css({'display':'none','position':'absolute'});
		initTeaser('topActions');
	};

	//nyroModal
	jQuery('#content a.zoom').each(function() {
		jQuery(this).nyroModal({
			endFillContent:nyroEndFillContent,
			//showBackground:nyroShowBackground,
			bgColor: '#42576C',
			windowResize: false,
			minWidth: 400,
			width: 400,
			minHeight: 300,
			height: 300,
			resizable: true,
			closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close">X</a>',
			contentLoading: '<a href="#" class="nyroModalClose">X</a>',
			galleryLinks: '<a href="#" class="nyroModalPrev"><</a><a href="#" class="nyroModalNext">></a>'
		});
	});


	//formhandler contact
	//===================
	// Initial hide
	//if(jQuery('#formhandler_contact_approach_phone:checked').length == 0) {
	//	jQuery('.contactForm .phoneWrap').hide();
	//}
	//if(jQuery('#formhandler_contact_approach_fax:checked').length == 0) {
	//	jQuery('.contactForm .faxWrap').hide();
	//}
	//if(jQuery('#formhandler_contact_approach_post:checked').length == 0) {
	//	jQuery('.contactForm .addressWrap').hide();
	//	jQuery('#formhandler_contact_required_info_post').hide();
	//}
	if(!jQuery('#formhandler_contact_submitted').length > 0) {
		jQuery('#formhandler_contact_peronal,.contactForm .printWrap,.contactForm .submitWrap').hide();
	}

	// click function
	jQuery('#formhandler_contact_approach_phone').click(function(){
		if(this.checked) {
			jQuery('.contactForm .phoneWrap').show();
			jQuery('#formhandler_contact_peronal,.contactForm .printWrap,.contactForm .submitWrap').show();
			removeRequiredEmail();
		} else {
			jQuery('.contactForm .phoneWrap').hide();
			requiredEmail();
		}
	});
	jQuery('#formhandler_contact_approach_email').click(function(){
		if(this.checked) {
			jQuery('#formhandler_contact_peronal,.contactForm .printWrap,.contactForm .submitWrap').show();
			jQuery('.contactForm .phoneWrap').hide();
		} else {
			requiredEmail();
		}
	});
	jQuery('#formhandler_contact_approach_fax').click(function(){
		if(this.checked) {
			jQuery('.contactForm .faxWrap').show();
			jQuery('#formhandler_contact_peronal,.contactForm .printWrap,.contactForm .submitWrap').show();
			removeRequiredEmail();
		} else {
			jQuery('.contactForm .faxWrap').hide();
			requiredEmail();
		}
	});
	jQuery('#formhandler_contact_approach_post').click(function(){
		if(this.checked) {
			jQuery('.contactForm .addressWrap').show();
			jQuery('#formhandler_contact_peronal,.contactForm .printWrap,.contactForm .submitWrap,#formhandler_contact_required_info_post').show();
			removeRequiredEmail();
		} else {
			jQuery('.contactForm .addressWrap,#formhandler_contact_required_info_post').hide();
			requiredEmail();
		}
	});

	//formhandler simple contact
	//==========================
	// click function
	jQuery('#formhandler_simple_contact_approach_phone').click(function(){
		if(this.checked) {
			removeRequiredEmail();
		} else {
			requiredEmail();
		}
	});
	jQuery('#formhandler_simple_contact_approach_email').click(function(){
		if(this.checked) {

		} else {
			requiredEmail();
		}
	});
	jQuery('#formhandler_simple_contact_approach_fax').click(function(){
		if(this.checked) {
			removeRequiredEmail();
		} else {
			requiredEmail();
		}
	});
	jQuery('#formhandler_simple_contact_approach_post').click(function(){
		if(this.checked) {
			removeRequiredEmail();
		} else {
			requiredEmail();
		}
	});

	// add class="hover" for ie6 pseudo-bug
	jQuery('.singleTeaser').hover(function(){
		jQuery(this).toggleClass('hover');
    });
	jQuery('.indexTeaser li').hover(function(){
		jQuery(this).toggleClass('hover');
     });

	// set the height for the singleTeaser Link
	var singleTeaserHeight = jQuery('.singleTeaser').height()+18;
	jQuery('.singleTeaser .teaserLink').css({'height':singleTeaserHeight});

	function removeRequiredEmail() {
		jQuery('#formhandler_contact_approach_email,#formhandler_simple_contact_approach_email').removeAttr('disabled');
	}

	function requiredEmail() {
		if(jQuery('#formhandler_contact_approach_phone:checked,#formhandler_simple_contact_approach_phone:checked').length == 0 &&
			jQuery('#formhandler_contact_approach_email:checked,#formhandler_simple_contact_approach_email:checked').length == 0 &&
			jQuery('#formhandler_contact_approach_fax:checked,#formhandler_simple_contact_approach_fax:checked').length == 0 &&
			jQuery('#formhandler_contact_approach_post:checked,#formhandler_simple_contact_approach_post:checked').length == 0) {
			jQuery('#formhandler_contact_approach_email,#formhandler_simple_contact_approach_email').attr('checked','checked').attr('value','email').attr('disabled','true');
		}
	}
	
	if(jQuery('.languageSelectList').length > 0){
		languageSelectList('.languageSelectList');
	}

});

function nyroEndResize(elts, settings) {
	var width = jQuery('#nyroModalContent').width();

	if(width < 400) {
		jQuery('#nyroModalFooter').hide();
	} else {
		jQuery('#nyroModalFooter').show();
	}
}

function showDisableFields() {
	jQuery('#formhandler_contact_approach_email:checked').removeAttr('disabled').attr('value','email');
	return true;
}

function nyroEndFillContent(elts, settings) {
	if(settings.width >= settings.minWidth) {
		var descriptionText = jQuery('.imgDesc',settings.from).html();
		if(!descriptionText){
			descriptionText = "";
		}
		var description = '<span class="description">' + descriptionText + '</span>';
		//var downloadLinkUrl = jQuery('.imgUrl').attr("href");
		var downloadLinkUrl = jQuery('.imgUrl',settings.from).attr("data-downloadURL");
		var downloadLink = '<a class="downloadLink" target="_blank" href="' + downloadLinkUrl + '">' + jsLabels.imgDownload + '</a>';
		var lightboxFooter = '<p id="nyroModalFooter">' + description + downloadLink + '</p>';
		jQuery('#nyroModalContent').append(lightboxFooter);
	}
	//jQuery('html').css({'overflow':'hidden'});
	//jQuery('#nyroModalFull').css({'overflow':'scroll'});
}

function initTeaser(targetID) {

	var link;
	var naviHTML;
	var cnt = 0;

	jQuery('#' + targetID + ' ul li').each( function() {
		if(cnt == 0)
			jQuery(this).addClass("active").css({'display':'block'});
		cnt++;
	});
	naviHTML = '<div id="maxCount_' + cnt + '" class="navList">';
	naviHTML += '<p>1 ' + jsLabels.jsLabel_newsTeaser_to + ' ' + cnt + '</p>';
	naviHTML += '</div>';
	if (jQuery('#' + targetID + ' ul li').length > 1) {
		jQuery('#' + targetID).append(naviHTML);
	}
	for(var i=1; i<=cnt; i++) {

		link = jQuery('<a id="' + targetID + 'Navi_' + i + '"><!-- empty --></a>');
		if(i == 1)
			link.addClass('active');
		link.click(function() {
			var clickTarget = $(this);
			var id = clickTarget.attr('id').split('_')[1];
			var cnt = 1
			jQuery('#' + targetID + ' li').each( function() {
				if(cnt == id)
					jQuery(this).addClass("active").css({'display':'block'});
				else
					jQuery(this).removeClass("active").css({'display':'none'});
				cnt++;
			});
			var cnt = 1
			jQuery(clickTarget.parent().children('a')).each( function() {
				if(cnt == id)
					jQuery(this).addClass("active");
				else
					jQuery(this).removeClass("active");
				cnt++;
			});
			clickTarget.parent().children('p').html(id + ' ' + jsLabels.jsLabel_newsTeaser_to + ' ' + clickTarget.parent().attr('id').split('_')[1]);
		});
		jQuery('#' + targetID + ' .navList').append(link);
	}
}

// languageSelectList
function languageSelectList(obj) {
	var $linkList = jQuery(obj),
		$linkListUL = $linkList.find('ul');
		$activeLink = $linkListUL.find('.active a');
	$linkList.click(function(e) { e.stopPropagation(); });
	$activeLink.click(function(e) { e.preventDefault(); });
	$linkList.click(function() {
		$linkList.toggleClass('linkListActive');
	});
	jQuery(document).click(function() {
		$linkList.removeClass('linkListActive');
	});
}


/**
 * Diese Funktion kann fuer console.log Anweisungen verwendet werden. Damit werden dann keine Fehler erzeugt, wenn console nicht vorhanden ist
 */

function tryLog(message) {
	try {console.log(message);}
	catch (e) {}
}
