jQuery(document).ready(function() {
	var agent=navigator.userAgent.toLowerCase();
	var is_iphone = ((agent.indexOf('iphone')!=-1));
	if(jQuery('#allgemeinesMenu li.first a').text() == 'english')
		var lang = 0;
	else
		var lang = 1;
	if(is_iphone && (window.location.pathname == '' || window.location.pathname == '/')){
		if(lang == 0){
			if(confirm("Möchten Sie die spezielle Moormann-iPhone Website ansehen?") == true)	
				window.location.href = "/iphone/";
				
		}
		if(lang == 1){
			if(confirm("Do you want to visit the special Moormann-Iphone Website?") == true)	
				window.location.href = "/en/iphone/";
		}
	}
	
	$.ajaxSetup({
		cache: true
	});

	// target blank externe Links und berge
	jQuery("a[href^='http']").attr('target','_blank');
	jQuery("a[href^='berge/'],a[href^='en/berge/']").attr('target','_blank');


	
	jQuery(document).dblclick(function(){
		jQuery('body').toggleClass('');
	});
	
	//noshade attribut auf hr für IE
	jQuery('hr').attr('noshade','noshade');
	
	// automatische click und hover-Funktionen
	jQuery('.clickIt > a').toggle(function(){
		var target = jQuery(this).attr('rel');
		jQuery('#'+target).show('fast');
		
	},function(){
		var target = jQuery(this).attr('rel');
		jQuery('#'+target).hide('fast');
	});

	jQuery('.hoverIt > a, .clickIt > a').attr('rel',function(arr){
		var relText = jQuery(this).text().toLowerCase().replace(' ', '');
		if(relText == 'search')
			relText = 'suche';
		return relText;
		}
	);
	jQuery('.hoverIt > a').append("*");


	jQuery('.hoverIt > a').hover(function(){
		var target = jQuery(this).attr('rel');
		jQuery('#'+target).show();
		
	},function(){
		var target = jQuery(this).attr('rel');
		if(jQuery(this).parent().parent('#moormannBottomMenu').length > 0)
			window.setTimeout(function(){jQuery('#'+target).hide('fast');},1500);
		else
			jQuery('#'+target).hide('fast');
	});
	
	// input-Felder leeren/fuellen
	swapValue = [];
	jQuery("#sword").each(function(i){
	   swapValue[i] = jQuery(this).val();
	   jQuery(this).focus(function(){
	      if (jQuery(this).val() == swapValue[i]) {
	         jQuery(this).val("");
	      }
	   }).blur(function(){
	      if (jQuery.trim(jQuery(this).val()) == "") {
	         jQuery(this).val(swapValue[i]);
	      }
	   });
	});
		
	//Bild-Vergroesserung bei resize -> body statt window?
	var startWindowHeight = jQuery(window).height();
	var startBodyHeight = jQuery('body').outerHeight()+20;
	if(startBodyHeight > startWindowHeight) {
		jQuery('#moebelSeitenGrossBild span.csc-textpic-caption, #moebelSeitenGrossBild .csc-textpic-clear, #moebelSeitenGrossBild a').remove();
		jQuery('#moebelSeitenGrossBild img,').css({'height':startBodyHeight+'px'});
		jQuery('#moebelSeitenGrossBild, #moebelSeitenGrossBild .csc-textpic, #moebelSeitenGrossBild .csc-textpic-image').css({'height':startBodyHeight+'px'});
		 
		jQuery('#moebelSeitenGrossBild img').removeAttr('height');
		jQuery('#moebelSeitenGrossBild img').removeAttr('width');
	} else {
		jQuery('#moebelSeitenGrossBild span.csc-textpic-caption, #moebelSeitenGrossBild .csc-textpic-clear, #moebelSeitenGrossBild a').remove();
		jQuery('#moebelSeitenGrossBild img,').css({'height':startWindowHeight+'px'});
		jQuery('#moebelSeitenGrossBild, #moebelSeitenGrossBild .csc-textpic, #moebelSeitenGrossBild .csc-textpic-image').css({'height':startWindowHeight+'px'});
		 
		jQuery('#moebelSeitenGrossBild img').removeAttr('height');
		jQuery('#moebelSeitenGrossBild img').removeAttr('width');
	}
	
	jQuery(window).bind("resize", resizeWindow);
	function resizeWindow(e) {
		var newWindowHeight = jQuery(window).height();
		if(startBodyHeight < newWindowHeight){
		    jQuery('#moebelSeitenGrossBild img').css({'height':newWindowHeight+'px'});
			jQuery('#moebelSeitenGrossBild, #moebelSeitenGrossBild .csc-textpic, #moebelSeitenGrossBild .csc-textpic-image').css({'height':newWindowHeight+'px'});
		} 
		
	}
 	
	/* Letzte News nicht umbrechen! */
	jQuery('.news-latest-item:eq(2)').css({'margin-right':'0px'});
	jQuery('.bildergalerie li:last').css({'margin-right':'0px'});

	/* Galerie */
	var activeLi = 0;
	jQuery('ul.bildergalerie li').click(function(){
		var href = jQuery(this).children('a').attr('href');
		jQuery('#bild').attr('src',href);
		activeLi = jQuery('ul.bildergalerie li').index(this);
		makeHrefBilderGalerie();
		return false;
	});
	jQuery('#bild').removeAttr('height');
	jQuery('#bild').removeAttr('width');
	jQuery('#bild').wrap('<a id="bildLink" href=""></a>');

	jQuery('#bildLink').click(function(){
		var href = jQuery(this).attr('href');
		jQuery('#bild').attr('src',href);
		activeLi = activeLi+1;
		makeHrefBilderGalerie();
		return false;
	});
	

	/* Wegbeschreibung */


	var makeHrefBilderGalerie = function(){
		if(jQuery('a.imagenr_'+activeLi).length <= 0){
			activeLi = 0;
		} 
		jQuery('#bild').attr('src',jQuery('a.imagenr_'+activeLi).attr('href'));
	}
	
	makeHrefBilderGalerie();

	var makeHrefBilderGaleriePrevious = function(id){
		if(jQuery('a.imagenr_'+activeLi).length <= 0){
			activeLi = 0;
		} 
		jQuery(id).attr('src',jQuery('a.imagenr_'+activeLi).attr('href'));
	}

	var makeHrefBilderGalerieNext = function(id){
		if(jQuery('a.imagenr_'+activeLi).length <= 0){
			activeLi = 0;
		} 
		jQuery(id).attr('src',jQuery('a.imagenr_'+activeLi).attr('href'));
	}

	//Pfeile links/rechts
//	$('body#wegbeschreibung #bildLink').after('<br /><span id="previousImage"><a href="">&lt;</a></span><span id="nextImage"><a href="">&gt;</a></span>');


	
	//Suche und hoverIts sichtbar machen
	
	//Menue unten Linie
	var gesamtbreite = 0;
	//moormannBottomMenu ausblenden, wenn keine Inhalte
	if(jQuery('#moormannBottomMenu')){
		jQuery('#moormannBottomMenu li').each(function(){
			gesamtbreite = gesamtbreite + jQuery(this).width();
		});
		if(gesamtbreite > 475){
			//jQuery('#moormannBottomMenu').css({'border-bottom':'1px dashed #5F5F5F','margin-bottom':'16px'});
			//jQuery('#moormannBottomMenu.volleBreite').css({'border-bottom':'1px dashed #5F5F5F','margin-bottom':'38px'});
			//jQuery('#moormannBottomMenu.volleBreite').parent('#footer').css({'background-image':'url(/fileadmin/templates/img/bottommenubg.gif)',backgroundPositionTop:'20px 0',backgroundRepeat:'repeat-x'});
		}
		jQuery('#moormannBottomMenu:not(:has(*))').remove();
		
	}
	//Bilderliste hover (Material)
	jQuery('.bilderliste.material .csc-textpic-image a').click(function(){return false;})
	var swapImg;
	jQuery('.bilderliste.material .csc-textpic-image a').hover(function(){
		swapImg = jQuery(this).children('img').attr('src');
		jQuery(this).children('img').attr('src',jQuery(this).attr('href'));
		jQuery(this).attr('href',swapImg);
	},function(){
		swapImg = jQuery(this).children('img').attr('src');
		jQuery(this).children('img').attr('src',jQuery(this).attr('href'));
		jQuery(this).attr('href',swapImg);
	});
	
	jQuery('.moebelliste .csc-textpic-image').hover(function(){
		//Hier kann ein Link sein, muss aber nicht
		jQuery(this).children('img').css({opacity:'0.2'});
		jQuery(this).children('a').children('img').css({opacity:'0.2'});
		jQuery(this).children('.csc-textpic-caption').show();
	},function(){
		jQuery(this).children('img').css({opacity:'1.0'});
		jQuery(this).children('a').children('img').css({opacity:'1.0'});
		jQuery(this).children('.csc-textpic-caption').hide();
	});
	jQuery('.csc-textpic-caption').click(function(){
		var locationHref = jQuery(this).siblings('a').attr('href');
		if(locationHref)
			window.location.href = locationHref;
		else
			return false;
	});
	
	
	//News-Links Click
	jQuery(".meldung .links a[href^='moormann/infos/'], .meldung .links a[href^='en/moormann/infos/']").click(function(){
			var target = $(this).parents('.meldung').children('.newsload');
			$(target).load($(this).attr('href'),function(){
				$(this).siblings('.close').remove();
				$(target).after('<div class="close"><a class="close" href="javascript:;" title="schliessen/close">x</a></div>');
				$('a.close').bind('click',function(){
					$(this).parent().siblings('.newsload').html('');
//					$(this).parent().siblings('.newsload').animate({'height':'0px'},'3000');
//					$(this).parent().siblings('.newsload').css({'min-height':'0px'});
					$(this).parent('.close').remove();
				});
				var height = $(target).innerHeight();
//				alert($(target).children('img').height());
//				$(target).animate({'height':height+'px'},'3000');
//				$(target).css({'min-height':'200px'});
			});
			return false;
	});

	jQuery('a[href*=#]').click(function() { //faehrt sanft zu allen inline-links
		if (window.location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && window.location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target	|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				jQuery('html,body').animate({scrollTop: targetOffset}, 700);
				return false;
			}
		}
	});
	
	//Konfigurator gleich zum Inhalt springen
	if(jQuery('body#konfigurator').length > 0 ){
		jQuery(window).scrollTop(230);
	}
	
	jQuery(window).scroll(function(){ // Windows Scroll-Event
          if(jQuery(window).scrollTop() <= 500){
            jQuery('#topScroll a').hide() // ToTop-Link ausblenden wenn die Scroll-Position 0 ist
          } else{
            jQuery('#topScroll a').show() // ToTop-Link einblenden wenn die Scroll-Position nicht 0 ist
          }
    });
	
	/*News - letzten HR ausblenden*/
	$('.news-list-container .meldung:last hr.clearer, .eintrag:last hr.clearer').remove();

	//Korea: Marktplatz ausblenden
	
	if (google.loader.ClientLocation && google.loader.ClientLocation.address.country_code == "KR") {
			jQuery('li.marktplatz').hide();
	}
	/*if(navigator.geolocation) { 
		navigator.geolocation.getCurrentPosition(function(position) { 
	    	var country = position.address.countryCode;
	    });
	    if(country == 'DE'){
			jQuery('li.marktplatz').hide();	    
	    }
	}*/
	/* Preis einsetzen */
	if(jQuery('span.preis').length > 0){
		var loc;
		if (google.loader.ClientLocation) {
      		  loc = google.loader.ClientLocation.address.country_code;
    	} else if (navigator.geolocation) {  
				navigator.geolocation.getCurrentPosition(function(position){
					loc = position.address.country_code;
			});
		}
	}

	jQuery('span.preis').each(
		function(index){
			var pl = '';
			var pl_index = 6;
			var scope = jQuery(this).attr('name');
			jQuery(this).html('Preis wird berechnet...');
		    var $span = jQuery(this);
			if(loc){
			    jQuery.getJSON("index.php?eID=getprice&L="+lang+"&countrycode="+loc+"&scope="+scope,
				    function(data){
					      		if(jQuery('span#preis').text().length == 0){
							      		jQuery('span#preis').append(data.items[0].index).show();
							    }
							    $span.text(data.items[0].preis);

    			});
			 } else {
			    jQuery.getJSON("index.php?eID=getprice&L="+lang+"&countrycode=none&scope="+scope,
				    function(data){
					      		if(jQuery('span#preis').text().length == 0){
							      		jQuery('span#preis').append(data.items[0].index).show();
							    }
							    $span.text(data.items[0].preis);

    			});
			 }

	});

});


