// JavaScript Document

//PRELOADER FUER BILDER

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(document).ready(function() {

jQuery.preLoadImages("/media/shop/layout/media/shop/layout/images/trans.png","/media/shop/layout/media/shop/layout/images/bg_1_1920.jpg","/media/shop/layout/media/shop/layout/images/bg_2_1920.jpg","/media/shop/layout/media/shop/layout/images/bg_3_1920.jpg","/media/shop/layout/media/shop/layout/images/bg_4_1920.jpg","/media/shop/layout/media/shop/layout/images/bg_5_1920.jpg","/media/shop/layout/media/shop/layout/images/bg_6_1920.jpg","/media/shop/layout/media/shop/layout/images/bg_7_1920.jpg","/media/shop/layout/media/shop/layout/images/bg_8_1920.jpg","/media/shop/layout/media/shop/layout/images/bg_9_1920.jpg","/media/shop/layout/media/shop/layout/images/bg_10_1920.jpg");

//HISTORY DES BROWSERS ABFANGEN MIT HILFE VON HASH

function historyCheck(){
	
	if(window.location.hash.replace('#','') != $('#main').attr('class') && window.location.hash != ''){
		$('#main').attr('class',window.location.hash.replace('#',''));
		$('#main').animate({
    opacity: 0
  }, 1500, function() {
    $('#main').load('/scripts/getAjaxData.aspx?command=FillDivContainer&ctype=2&aktionsleiste=nein&content='+window.location.hash.replace('/dcontent','').replace('/hcontent','').replace('#',''),function(){
		initialise();
		$('#main').animate({
    opacity: 1
  }, 1500);
		})
  });
		
		
		
	}
}
setInterval(historyCheck, 100);	

//Timeout setzen
aktiv=window.setTimeout("bgWechsel(10)", 35000);

//Funktionen Hauptmenue
$('#main_navigation a').mouseenter(function() {
    		       $(this).find('.inactiv').attr('src', $(this).find('.inactiv').attr('src').replace('_inactive','_active'));
			}).mouseleave(function(){
				$(this).find('.inactiv').attr('src', $(this).find('.inactiv').attr('src').replace('_active','_inactive'));
			}).click(function(){
				$('#main_navigation a').each(function(){
					$(this).find('img').attr('class','inactiv').attr('src', $(this).find('img').attr('src').replace('_active','_inactive'));      
				});
 $(this).find('img').attr('class','activ').attr('src', $(this).find('img').attr('src').replace('_inactive','_active'));
});

// Status Hauptmenue reset bei top bar
$('#top_bar a').click(function(){
               $('#main_navigation a').each(function(){
                               $(this).find('img').attr('src',$(this).find('img').attr('src').replace('_active','_inactive'));
			       $(this).find('img').attr('class','inactiv');
               });
        });

// Status Hauptmenue reset bei logo link
$('#logo a').click(function(){
               $('#main_navigation a').each(function(){
                               $(this).find('img').attr('src',$(this).find('img').attr('src').replace('_active','_inactive'));
			       $(this).find('img').attr('class','inactiv');
               });
$('img[src*=menu_home_]').attr('src',$('img[src*=menu_home_]').attr('src').replace('_inactive','_active'));
$('img[src*=menu_home_]').attr('class','activ');
        });





	initialise();
	
	if(navigator.userAgent.match(/MSIE/i) != null){
		$('.removeThis').html('<img src="http://www.kermos.de/media/shop/layout/images/sound_on.png" class="Stop" id="iePlay" style="display:inline; cursor: pointer; margin-right: 7px;"  /> | ');
		$('.playerHolder').append('<object><embed TYPE="application/x-mlayer2" src="http://www.kermos.de/music/dyn_house_of_tales_ie.mp3" autostart="true" loop="true" controller="false" width="1" height="1"></embed></object>');
	}
	
	if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/iPad/i))){
		$('#jplayer_pause').trigger('click');
	};



$('#iePlay').click(function(){
		if($(this).attr('class') == 'Stop'){
			$(this).attr('src','http://www.kermos.de/media/shop/layout/images/sound_off.png');
			$(this).attr('class','Play');
			$('object').remove();
$('body').trigger('click');
		}else {
			$(this).attr('src','http://www.kermos.de/media/shop/layout/images/sound_on.png');
			$(this).attr('class','Stop');
			$('.playerHolder').append('<object><embed TYPE="application/x-mlayer2" src="http://www.kermos.de/music/dyn_house_of_tales_ie.mp3" autostart="true" loop="true" controller="false" width="1" height="1"></embed></object>');
$('body').trigger('click');
		}
	});
});

function initialise(){
	
	//FUNKTION PRESSEBEREICH

$('.submenuPresse a').click(function(){
$('.submenuPresse a').attr('class','');
$(this).attr('class','active');
});

// URHEBERRECHTSBELEHRUNG
	$('a.urheberrecht').click(function(){
		if($('#urheberBox').attr('checked') == false){
			$('a.urheberCheck').attr('href',$(this).attr('href'));
			$('#wrapperInfoboxFehlermeldung').css('display','block');
			return false
		}
	});
	
	$('#wrapperInfoboxFehlermeldung a.urheberCheck').click(function(){
		if($('#urheberBox').attr('checked') == true) {
			$('#wrapperInfoboxFehlermeldung').css('display','none');
		}else {
			$('.fehlerResponse').css('display','block');
			return false
		}
	});
	$('a.copyrightBestimmungen').click(function(){
		$('#copyrightBox').css('display','block')
		$('#copyrightBox .scroll-pane').jScrollPane();
	});
	$('#copyrightBox #errorclose').click(function(){
		$('#copyrightBox').css('display','none');		
	});



	
        //Scrollable Combobox initialisieren
 $('#ui_element').scrollablecombo();
 
 
 $('.serienbroschueren .cb_select a').click(function(){
	 $('.nameReplace').text($(this).text());
	 $('.linkReplace').attr('href','http://www.kermos.de/media/public/downloads/serienbroschueren/'+$(this).attr('href').replace('http://www.kermos.de/','')+'.pdf');
	 $('.linkReplacePageFlip').attr('href','http://issuu.com/portfoliotogo/docs/'+$(this).attr('href').replace('http://www.kermos.de/','')+'?mode=embed&layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Flight%2Flayout.xml&showFlipBtn=true');
	 $('.imageReplace').attr('src','http://www.kermos.de/media/public/downloads/serienbroschueren/'+$(this).attr('href').replace('http://www.kermos.de/','')+'.jpg');
 });
        
 $('.egkonform .cb_select a').click(function(){
	$('.nameReplace').text($(this).text());
	callContent('/dcontent/public/downloads/egerklaerung/'+$(this).attr('href').replace('http://www.kermos.de/',''),'2','.scrolltext','nochange');
})


 	//Downloads fuer EG-Konformitaetserklaerungen umschalten
/*
	$("#egdownloads").change(function () {
		$('.nameReplace').text($("select option:selected").text());
		callContent('/dcontent/public/downloads/egerklaerung/'+$("select option:selected").val(),'2','.scrolltext','nochange');
	})

	//Downloads fuer Serienbroschueren umschalten

	$(".seriendownloads").change(function () {
		$('.nameReplace').text($("select option:selected").text());
		$('.linkReplace').attr('href','/media/public/downloads/serienbroschueren/'+$("select option:selected").val()+'.pdf');
		$('.imageReplace').attr('src','/media/public/downloads/serienbroschueren/'+$("select option:selected").val()+'.jpg');
	})
*/      
	// Suchformular absenden
	//$(document).ready(function() {
	//$('#search_bt').click(function() {
//		$('#searchForm').submit();
//	});
//	
//	$('#searchForm').submit(function(){
		//if($('#search_txt').attr('value') != '' && $('#search_txt').attr('value') != 'Suchbegriff eingeben'){
//			callContent('/dcontent/public/suche','2','#main','lotus');
		//}else {
//			$('#search_txt').attr('value','Suchbegriff eingeben')
		//}
		//return false;
	//});
	//});

	//Bildwechsel Produktdetail 

	$(document).keypress(function(event) {
		if (event.keyCode == '37') {
			
			var lastImage = $('#slideThumbs img:last').attr('src').replace('/media/public/produkte/'+$('.produktname').text()+'/image_small_','').replace('.jpg','');
			var currentImage = $('#slideThumbs img.active').attr('src').replace('/media/public/produkte/'+$('.produktname').text()+'/image_small_','').replace('.jpg','');
			if(Number(currentImage) == 1){
				$('#whiteBgWrapper').attr('style','background:url(/media/public/produkte/'+$('.produktname').text()+'/image_big_'+lastImage+'.jpg) center no-repeat');
				$('#slideThumbs img').removeClass('active');
				$('#slideThumbs img:last').addClass('active');
			}else{
				$('#whiteBgWrapper').attr('style','background:url(/media/public/produkte/'+$('.produktname').text()+'/image_big_'+(parseFloat(currentImage)-1)+'.jpg) center no-repeat');
				$('#slideThumbs img').removeClass('active');
				$('#slideThumbs img:nth-child('+(parseFloat(currentImage)-1)+')').addClass('active');
			}
	   }else if (event.keyCode == '39') {
			var lastImage = $('#slideThumbs img:last').attr('src').replace('/media/public/produkte/'+$('.produktname').text()+'/image_small_','').replace('.jpg','');
			var currentImage = $('#slideThumbs img.active').attr('src').replace('/media/public/produkte/'+$('.produktname').text()+'/image_small_','').replace('.jpg','');
			if(Number(currentImage) == Number(lastImage)){
				$('#whiteBgWrapper').attr('style','background:url(/media/public/produkte/'+$('.produktname').text()+'/image_big_1.jpg) center no-repeat');
				$('#slideThumbs img').removeClass('active');
				$('#slideThumbs img:first').addClass('active');
			}else{
				$('#whiteBgWrapper').attr('style','background:url(/media/public/produkte/'+$('.produktname').text()+'/image_big_'+(parseFloat(currentImage)+1)+'.jpg) center no-repeat');
				$('#slideThumbs img').removeClass('active');
				$('#slideThumbs img:nth-child('+(parseFloat(currentImage)+1)+')').addClass('active');
			}
   		}
	});

	$('#slideThumbs img').click(function(){
		$('#slideThumbs img').removeClass('active');
		$(this).addClass('active');
		var mySource = $(this).attr('src');
		$('#whiteBgWrapper').animate({
    opacity: 0
  }, 1000, function() {
   	$('#whiteBgWrapper').attr('style','background:url('+mySource.replace('small','big')+') center no-repeat');
  }).animate({
    opacity: 1
  }, 1000);
		
	});

	$('#slideNavigation .left').click(function(){
		var lastImage = $('#slideThumbs img:last').attr('src').replace('/media/public/produkte/'+$('.produktname').text()+'/image_small_','').replace('.jpg','').replace('http://www.kermos.de','');
		var currentImage = $('#slideThumbs img.active').attr('src').replace('/media/public/produkte/'+$('.produktname').text()+'/image_small_','').replace('.jpg','').replace('http://www.kermos.de','');
		if(parseFloat(currentImage) == 1){
			
		$('#whiteBgWrapper').animate({
    opacity: 0
  }, 1000, function() {
$('#whiteBgWrapper').attr('style','background:url(/media/public/produkte/'+$('.produktname').text()+'/image_big_'+lastImage+'.jpg) center no-repeat');
  }).animate({
    opacity: 1
  }, 1000);
			
			
			$('#slideThumbs img').removeClass('active');
			$('#slideThumbs img:last').addClass('active');
		}else{
			
			$('#whiteBgWrapper').animate({
    opacity: 0
  }, 1000, function() {
			$('#whiteBgWrapper').attr('style','background:url(/media/public/produkte/'+$('.produktname').text()+'/image_big_'+(parseFloat(currentImage)-1)+'.jpg) center no-repeat');
  }).animate({
    opacity: 1
  }, 1000);
			
			
			$('#slideThumbs img').removeClass('active');
			$('#slideThumbs img:nth-child('+(parseFloat(currentImage)-1)+')').addClass('active');
		}
	});

	$('#slideNavigation .right').click(function(){
		var lastImage = $('#slideThumbs img:last').attr('src').replace('/media/public/produkte/'+$('.produktname').text()+'/image_small_','').replace('.jpg','').replace('http://www.kermos.de','');
		var currentImage = $('#slideThumbs img.active').attr('src').replace('/media/public/produkte/'+$('.produktname').text()+'/image_small_','').replace('.jpg','').replace('http://www.kermos.de','');
		if(parseFloat(currentImage) == parseFloat(lastImage)){
			
			$('#whiteBgWrapper').animate({
    opacity: 0
  }, 1000, function() {
			$('#whiteBgWrapper').attr('style','background:url(/media/public/produkte/'+$('.produktname').text()+'/image_big_1.jpg) center no-repeat');
  }).animate({
    opacity: 1
  }, 1000);
			
			$('#slideThumbs img').removeClass('active');
			$('#slideThumbs img:first').addClass('active');
		}else{
			
			$('#whiteBgWrapper').animate({
    opacity: 0
  }, 1000, function() {
			$('#whiteBgWrapper').attr('style','background:url(/media/public/produkte/'+$('.produktname').text()+'/image_big_'+(parseFloat(currentImage)+1)+'.jpg) center no-repeat');
  }).animate({
    opacity: 1
  }, 1000);
			
			$('#slideThumbs img').removeClass('active');
			$('#slideThumbs img:nth-child('+(parseFloat(currentImage)+1)+')').addClass('active');
		}
	});

	//Media-Player initialisieren
if(navigator.userAgent.match(/MSIE/i) == null){
	$("#jquery_jplayer").jPlayer({
		ready: function () {
			this.element.jPlayer("setFile", "http://www.kermos.de/music/dyn_house_of_tales.mp3" , "http://www.kermos.de/music/dyn_house_of_tales.ogg").jPlayer("play");
		},
		volume: 80,
		swfPath: "inc",
		oggSupport: true,
		preload: 'auto'
	})
	.jPlayer("onSoundComplete", function() {
		this.element.jPlayer("play");
	});
}else {
$('#jplayer_play').remove();
$('#jplayer_pause').remove();
$('#sound').remove();

//ACHTUNG: NEU!!!


//		$('.removeThis').html('<a class="Stop" id="iePlay" href="#play"><img src="http://www.kermos.de/media/shop/layout/images/sound_on.png" /></a> | ');



	
};

	//Javascript-Scrollleisten einbauen inkl. Apple-Weiche (Content wird durch zwei Finger scrollbar, Scrollleiste nicht sichtbar)

	if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/iPad/i))){
		$('.scroll-pane').css('overflow','auto');
		$('body').css('background-position','top left');
	} else {
		$('.scroll-pane').jScrollPane();
	}

	//Ermitteln der Abstaende zwischen Teaser und Content je nach Monitoraufloesung

	var meineHoehe = $(window).height()-204;
	$('#startseite').css('height',meineHoehe)
	$('body').css('overflow','hidden');
	rT();

	//Funktionen fuer die Animation der Balken unter /public (Einstiegsseite)

if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/iPad/i))){
		$('#startseite div').css('margin-top','0px');
	}

	$('#startseite div').mouseover(function() {
		if($(this).css('margin-top') == '260px' || $(this).css('margin-top') == '90px'){
			$(this).stop();
			$(this).animate({
    			'margin-top': '0'
  			}, 1000, 'swing');
		}
	});
	$('#startseite div').mouseleave(function() {
		if($(this).attr('class') == 'spalte_klein'){
			$(this).stop();
			$(this).animate({
    			'margin-top': '260px'
 			}, 500, 'swing');
		}else {
			$(this).stop();
			$(this).animate({
   				'margin-top': '90px'
 			}, 500, 'swing');
		}
	});
	
//Submit-Handler fuer Kontakt-Formular inkl. Validierung
	status = 'notSend';
	$('#kontakt').submit(function() {
		if(status == 'send'){
			$('#wrapperInfoboxSendmeldung').css('display','block');	
		}else {
		      	if($('#vorname').attr('value') != '' && $('#nachname').attr('value') != '' && $('#plz').attr('value') != '' && $('#email').attr('value').match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)  && $('#mitteilung').attr('value') != '') {
		//	alert('Alle Felder und Mailadresse sind valide.');
	
			}else {
		//	alert('Bitte Pflichtfelder ausfuellen und eine valide Mailadresse nutzen.');
			$('#wrapperInfoboxFehlermeldung').css('display','block');
			return false;
			}
			$('#meldung').load('http://www.kermos.de/scripts/formprocessor.aspx', { hid_mailsub: 'Neue Kontaktnachricht von KERMOS.de', Vorname: $('#vorname').val(), Nachname: $('#nachname').val(), EMail: $('#email').val(), Telefon: $('#telefon').val(), Mitteilung: $('#mitteilung').val(), plz: $('#plz').val(), Ort: $('#ort').val(), Strasse: $('#strasse').val(), Hausnummer: $('#hausnummer').val(), requestmode: 'ajax'   }, function() {
		      		$('#meldung').text('Vielen Dank fuer Ihre Anfrage!');
				$('#wrapperInfoboxErfolgsmeldung').css('display','block');
				status = 'send';
				//alert(status);
			});
		}
		return false;
	});	
	
	$('#wrapperInfoboxFehlermeldung #errorclose').click(function(){
		$('#wrapperInfoboxFehlermeldung').css('display','none');		
	});
	$('#wrapperInfoboxErfolgsmeldung #errorclose').click(function(){
		$('#wrapperInfoboxErfolgsmeldung').css('display','none');		
	});
	$('#wrapperInfoboxSendmeldung #errorclose').click(function(){
		$('#wrapperInfoboxSendmeldung').css('display','none');		
	});
	$('input').change(function(){
		status = 'notSend';
	});

	
}


function rT(){
	var a = Math.round(($(window).height()-589)/2)
	if(a>230){a=230}else if(a<2){a=2} 
	$('#teaser').css('margin','0 0 '+a+'px 0');
if(navigator.userAgent.match(/msie/i)){
$('#startseite').css(($(window).height()-138)+'height','px');

}
}
$(window).resize(function(){rT()}); 



//AJAX-Request fuer den Content inkl. Wechsel des Hintergrundbildes
function callContent(url,cType,target,backgroundImage,transition,fader){
if(cType == '2'){
	var speed = 1200;
window.location.hash = url;
	$('#main').attr('class',url);
}
else {
	var speed = 0;
	}
  // Hintergrundbild austauschen		
		if(backgroundImage!='nochange'){
			//$('body').css('background-image','url("/media/shop/layout/images/bg_'+backgroundImage+'_1920.jpg")');
			$('body').css('background','#000');
			
	$('#background').animate({
    opacity: 0
  }, speed, function() {
    $(this).css('background-image','url("/media/shop/layout/images/bg_'+backgroundImage+'_1920.jpg")').animate({
    opacity: 1
  }, speed)
  });
		}
  
  
  
  
  
  if(cType == '2' && fader == true){
	  
	$('.fadeElement ').animate({
		opacity:0
	},0);
	$('#faderDIV').css('display','block');
 $('.fadeElement').animate({
    opacity:1
  }, speed, function() { 

	$(target).load('/scripts/getAjaxData.aspx?command=FillDivContainer&ctype='+cType+'&aktionsleiste=nein&content='+url.replace('/dcontent','').replace('/hcontent',''), '', function(){
		if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/iPad/i))){
			$('.scroll-pane').css('overflow','auto');
		}else {
			$('.scroll-pane').jScrollPane();
		}

		
		initialise();
	$('.fadeElement ').animate({
    opacity: 0
  }, speed,function(){
	$('#faderDIV').css('display','none'); 
  });
  
  
		//Timeout f?r Hintergrundbildwechsel nach 35 Sekunden
                window.clearTimeout(aktiv);
                aktiv=window.setTimeout("bgWechsel(10)", 35000);	
				
});
});  
  }else if(cType == '2' && fader == undefined){
	
 $(target).animate({
    opacity: 0
  }, speed, function() { 

	$(target).load('/scripts/getAjaxData.aspx?command=FillDivContainer&ctype='+cType+'&aktionsleiste=nein&content='+url.replace('/dcontent','').replace('/hcontent',''), '', function(){
		if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/iPad/i))){
			$('.scroll-pane').css('overflow','auto');
		}else {
			$('.scroll-pane').jScrollPane();
		}

		
		initialise();
	$(target).animate({
    opacity: 1
  }, speed);
		//Timeout f?r Hintergrundbildwechsel nach 35 Sekunden
                window.clearTimeout(aktiv);
                aktiv=window.setTimeout("bgWechsel(10)", 35000);	
});
});  
  }else {
	
		$(target).load('/scripts/getAjaxData.aspx?command=FillDivContainer&ctype='+cType+'&aktionsleiste=nein&content='+url.replace('/dcontent','').replace('/hcontent',''), '', function(){
if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/iPad/i))){
			$('.scroll-pane').css('overflow','auto');
		}else {
			$('.scroll-pane').jScrollPane();
		}
		initialise();
		}); 
  }

  
  
  
  
  
  
  
  
}

//Suchfunktion initialisieren
	function DoSearch() {
			if($('#search_txt').attr('value') != '' && $('#search_txt').attr('value') != 'Suchbegriff eingeben'){
			callContent('/dcontent/public/suche','2','#main','10');
		}else {
			$('#search_txt').attr('value','Suchbegriff eingeben')
		}
	}
	
	function CheckEnter(myfield,e, functionname)
	{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

		if (keycode == 13)
			 {
			 DoSearch();
			 return false;
			 }
		else
			 return true;
	}


// Hintergrundbilder nach 35 Sek wechseln
function bgWechsel(anzahlBilder) {
	var myImage = $('#background').css('background-image').split('_');
	myImage = myImage[1].replace('_1920.jpg")','');
	if(myImage < anzahlBilder) {
		myImage++;
//alert(myImage);
	} else {
		myImage = 1;
	}
	$('body').css('background','#000');
	
	
	$('#background').animate({
    opacity: 0
  }, 1000, function() {
    $(this).css('background-image','url("/media/shop/layout/images/bg_'+myImage+'_1920.jpg")').animate({
    opacity: 1
  }, 1000)
  });
	aktiv=window.setTimeout("bgWechsel(10)", 35000);
}
