$(function(){
	var bb_js = {
		start:function(location){
			var cache = [];
  			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);
    			}
  			}
			preLoadImages("wp-content/themes/bb/images/nav/buecher_on.png", "wp-content/themes/bb/images/nav/buecher_sel.png","wp-content/themes/bb/images/nav/start_on.png", "wp-content/themes/bb/images/nav/start_sel.png","wp-content/themes/bb/images/nav/start_on.png", "wp-content/themes/bb/images/nav/start_sel.png","wp-content/themes/bb/images/nav/events_on.png", "wp-content/themes/bb/images/nav/events_sel.png","wp-content/themes/bb/images/nav/kinderbox_on.png", "wp-content/themes/bb/images/nav/kinderbox_sel.png","wp-content/themes/bb/images/nav/kontakt_on.png", "wp-content/themes/bb/images/nav/kontakt_sel.png","wp-content/themes/bb/images/nav/ueberuns_on.png", "wp-content/themes/bb/images/nav/ueberuns_sel.png");
			
			$('#suchinput').focus(function() {
  				$(this).attr('value','');
			});
			
			$("#navigation .main_li").hover(					  
 				function(){
					$("#navigation .main_li").css("z-index",9);
					$(this).css("z-index",20);
					var i = $(this).find("img");
					var src = i.attr('src').replace("_off","_on");
  					i.attr('src', src);
 				},
				function(){
					$(this).css("z-index",20);
					var i = $(this).find("img");
					var src = i.attr('src').replace("_on","_off");
  					i.attr('src', src);
 				}
			);
			
			//tafel
			var left_h = $("#main_top_left").height();
			$("#main_top_right").css("height", left_h + "px");
			
			if ( $.browser.msie ) {
  				if( $.browser.version < 8){
					$("#main_top_right").css("height", (left_h-18) + "px");
				}
			}


			$("#main_top_right").css("min-height", "85px");
			var h = $("#main_top_right_txt").height();
			var h1 = $("#main_top_right").height();
			var pos_y = h1 - h - 8;
			$("#main_top_right_txt").css("padding-top", pos_y + "px");
			
			$(".impressum_btn").click(function(){
				var f = $(this).parent().parent().parent().parent().find("#impressum");	
				f.hasClass('hidden') ? f.removeClass('hidden') : f.addClass('hidden');
				var mh = $("#maincontent").height() - 20;
				$("#maincontent_shadow").css("height", mh + "px");
			});
			
			
			if(location.match('buecher') || location.match('suchergebnis') || location.match('events') || location.match('buchreservierung') || location.match('laeden') || location.match('team')){
				$(".preview_full_item .open").click(function(){
					if(location.match('buecher') || location.match('team')){
						var p = $(this).parent().parent().parent().find(".preview_item");
						var f = $(this).parent().parent().parent().find(".full_item");
					}
					if(location.match('buchreservierung') || location.match('suchergebnis')){
						var p = $(this).parent().parent().parent().parent().find(".preview_item");
						var f = $(this).parent().parent().parent().parent().find(".full_item");
					}
					if(location.match('events') || location.match('laeden')){
						var p = $(this).parent().parent().parent().parent().parent().find(".preview_item");
						var f = $(this).parent().parent().parent().parent().parent().find(".full_item");
					}
					p.hasClass('hidden') ? p.removeClass('hidden') : p.addClass('hidden');
					f.hasClass('hidden') ? f.removeClass('hidden') : f.addClass('hidden');
					if(location.match('events') || location.match('buecher') || location.match('laeden') || location.match('team')){
						if(p.hasClass('hidden')){
					 		var h1 = f.find(".buecher_item_left").height();
							var h2 = f.find(".buecher_item_right").height();
							h1 < h2 ? f.find(".buecher_item_left").css("min-height", (h2+18) + "px") : f.find(".buecher_item_right").css("min-height", (h1+18) + "px");	
						}else{
							f.find(".buecher_item_left").css("height", "auto");
						}
					}
					
					if(location.match('buchreservierung') || location.match('suchergebnis')){
						if(p.hasClass('hidden')){
							
							getAddText($(this).parent().parent().parent().parent().attr("id"));
							//getEbook($(this).parent().parent().parent().parent().attr("id"));
						}
					}
					
					
					var mh = $("#maincontent").height() - 20;
					$("#maincontent_shadow").css("height", mh + "px");
				});	
			}
			if(location.match('suchergebnis')){
				$(".preview_full_item .ebook").click(function(){
					getEbook($(this).parent().parent().parent().parent().attr("id"));
					//$(this).removeClass("ebook");
				});
			}

			
			//
			if(location.match('buecher') || location.match('suchergebnis') || location.match('events') || location.match('buchreservierung') || location.match('laeden') || location.match('team')){
					
					var p = $(".preview_full_item").find(".preview_item");
					var f = $(".preview_full_item").find(".full_item");

					if(location.match('events') || location.match('buecher') || location.match('laeden') || location.match('team')){
						if(p.hasClass('hidden')){
					 		var h1 = f.find(".buecher_item_left").height();
							var h2 = f.find(".buecher_item_right").height();
							h1 < h2 ? f.find(".buecher_item_left").css("min-height", (h2+18) + "px") : f.find(".buecher_item_right").css("min-height", (h2) + "px");	
						}else{
							f.find(".buecher_item_left").css("min-height", "auto");
						}
					}	
			}
			
			//
			
			if(location.match('events') || location.match('laeden') || location.match('infos')){
				$(".preview_full_item").each( 
					function(intIndex){
				$(this).find(".full_item .event_preview_img_footage img").wrap("<li>");
				$(this).find(".full_item .event_preview_img_footage li").wrapAll("<ul>");
				if($(this).find(".full_item .event_preview_img img").length > 0){
					var parentTag = $(this).find(".full_item .event_preview_img img").parent().get(0).tagName;
					if(parentTag == "A"){
						$(this).find(".full_item .event_preview_img img").unwrap();
					}
				}
				
				//
				$(this).find(".full_item .event_preview_img img").wrap("<li>");
				$(this).find(".full_item .event_preview_img li").wrapAll("<ul>");
				
				$(this).find(".preview_item .buecher_item_img_event").find("img").removeAttr('width');
				$(this).find(".preview_item .buecher_item_img_event").find("img").removeAttr('height');
				$(this).find(".full_item .buecher_item_img_event").find("img").removeAttr('width');
				$(this).find(".full_item .buecher_item_img_event").find("img").removeAttr('height');
				$(this).find(".preview_item .buecher_item_img_event").find("img").css('width','80px');
				$(this).find(".preview_item .buecher_item_img_event").find("img").removeAttr('height','auto');
				$(this).find(".full_item .buecher_item_img_event").find("img").css('width','80px');
				$(this).find(".full_item .buecher_item_img_event").find("img").removeAttr('height','auto');
				}
				);
				

				//preview-bilder
				$( ".full_item .event_preview_img ul li" ).each(
					function(intIndex){
						var img = $(this).find("img");
						img.wrap("<div>");
						$(this).append("<div class='img_border hidden'></div>");
						if(intIndex==0){
							$(this).find(".img_border").removeClass('hidden');
						}
						
					}
				);
				$( ".event_preview_img ul li" ).each(
					function(intIndex){
						$(this).click(function(){
							var l = $(this).parent().parent().parent().parent().find(".event_img");
							var src = $(this).find("img").attr('longdesc');
							l.find("img").attr('src',src);
							
							var txt = $(this).find("img").attr('alt');//.replace("</strong>","</strong><br />");
							l.find(".buecher_item_txt1").html(txt);
							
							var l = $(this).parent().find("li .img_border");
							l.addClass('hidden');
							$(this).find(".img_border").removeClass('hidden');
							
						});
					}
				);
			}
			
			if(location.match('fotogalerie')){
				$(".preview_full_item .open").click(function(){
					var p = $(this).parent().parent().parent().find(".plus_img");
					if(p.hasClass('hidden')){
						p.removeClass('hidden');
						$(this).text("{  weniger Bilder  }");
					}else{
						p.addClass('hidden');
						$(this).text("{  alle Bilder  }");
					}
					var t = $(this).parent().parent().parent().find(".galerie_fulltext");
					if(t.hasClass('hidden')){
						t.removeClass('hidden');
						$(this).text("{  weniger Bilder  }");
					}else{
						t.addClass('hidden');
						$(this).text("{  alle Bilder  }");
					}
					
					var mh = $("#maincontent").height() - 20;
					$("#maincontent_shadow").css("height", mh + "px");
				});	
			}
			
			
			//Startseite
			if(location.match('start')){
				$( ".item" ).each(
					function(intIndex){
						var t = $(this).find(".itemslist");
						t.data('num', 0);
						$(this).find(".arrow_left").click(function(){
							showItemBox(t,-1);
						});
						$(this).find(".arrow_right").click(function(){
							showItemBox(t,-2);
						});
					}
				);
			}
			
			//lightbox
			if(location.match('fotogalerie')){
				$( ".galerie_item_imgages a" ).each(
					function(intIndex){
						var txt = $(this).find("img").attr("alt");
						$(this).attr("title",txt);
					}
				);
				
				$('.galerie_item_imgages a').lightBox();
			}
			if(location.match('infos')){
				var f = $(".preview_full_item").find(".full_item");
				var h1 = f.find(".buecher_item_left").height();
				var h2 = f.find(".buecher_item_right").height();
				h1 < h2 ? f.find(".buecher_item_left").css("min-height", (h2+18) + "px") : f.find(".buecher_item_right").css("min-height", (h1+18) + "px");				
			}
			
			if(location.match('events')){
				$(".preview_full_item .signiert").click(function(){
					var f = $(this).parent().find(".bbsigniert");
					f.hasClass('hidden') ? f.removeClass('hidden') : f.addClass('hidden');
				});	
			}
			
			if(location.match('buchreservierung')){
				$(".buch_num").change(function() {
					var val = $(this).val();
					addNumToSession($(this).attr("id"), val);
  					//alert('Handler for .change() called.' + $(this).val());
				});
			}
			
			//schatten
			var rh = $("#rightcontent_content").height() + 20;
			$("#rightcontent_shadow").css("height", rh + "px");
			
			var mh = $("#maincontent").height() - 20;
			$("#maincontent_shadow").css("height", mh + "px");
		}	
	}
	bb_js.start(type);
});

function showHideDiv(target1,target2,a1,a2){
  	$(target2).hide();
  	$(target1).show();
	$(a1).removeClass("light_grey_txt");
	$(a1).addClass("green_txt");
	$(a2).removeClass("green_txt");
	$(a2).addClass("light_grey_txt");
}

var high_num = 0;
function showItem(target,num){
	if(num == -1){
		high_num--;
		if(high_num < 0){
			high_num = 	$( "#" + target + " li" ).length-1;
		}
	}else if(num == -2){
		high_num++;
		if(high_num >= $( "#" + target + " li" ).length){
			high_num = 	0;
		}
	}else{
		high_num = num;	
	}
	$( "#" + target + " li" ).each(
		function(intIndex){
			if(intIndex == high_num){
				$(this).css("display", "block");
			}else{
				$(this).css("display", "none");
			}
		}
	);
}

function showItemBox(target,num){
	var thisnum = target.data("num");
	if(num == -1){
		thisnum--;
		if(thisnum < 0){
			thisnum = 	$(target).find("li").length-1;
		}
	}else if(num == -2){
		thisnum++;
		if(thisnum >= $(target).find("li").length){
			thisnum = 	0;
		}
	}else{
		thisnum = num;	
	}
	$(target).find("li").each(
		function(intIndex){
			if(intIndex == thisnum){
				$(this).css("display", "block");
			}else{
				$(this).css("display", "none");
			}
		}
	);
	target.data("num",thisnum);
	
	var mh = $("#maincontent").height() - 20;
	$("#maincontent_shadow").css("height", mh + "px");
}


function setNavi(){
	$( "#highlight_navi li" ).each(
		function(intIndex){
			if(intIndex == high_num){
				var i = $(this).find("img");
				var src = i.attr('src').replace("_off","_on");
  				i.attr('src', src);
			}else{
				var i = $(this).find("img");
				var src = i.attr('src').replace("_on","_off");
  				i.attr('src', src);
			}
		}
	);
}
function showFullItem(full,preview){
	if($("#" + full).css("display") == "none"){
		$("#" + full).show();
	}else{
		$("#" + full).hide();
	}
	if($("#" + preview).css("display") == "none"){
		$("#" + preview).show();
	}else{
		$("#" + preview).hide();
	}
}

function setShadow(){
	var rh = $("#rightcontent_content").height() + 20;
	$("#rightcontent_shadow").css("height", rh + "px");
}


function eingegeben()
{

 if(document.kontaktform.meinmail.value == "") {
   alert("Bitte gib Deine Email-Adresse ein.");
   document.kontaktform.meinmail.focus();
   return false;
  }
 if(document.kontaktform.meinmail.value.indexOf('@') == -1) {
   alert("Bitte gib eine gueltige Email-Adresse ein.");
   document.kontaktform.meinmail.focus();
   return false;
  }
  if (document.kontaktform.zustimmen.checked != true ){ 
	  alert ( "Bitte bestaetige, dass Du die Informationen zum Datenschutz zur Kenntnis genommen hast" );
	  document.kontaktform.zustimmen.focus();
	   return false;
  }
}
function eingegebenCheck(mname,mmail,mdaten)
{
 if(document.getElementById(mmail).value == "") {
   alert("Bitte gib Deine Email-Adresse ein.");
   document.getElementById(mmail).focus();
   return false;
  }
 if(document.getElementById(mmail).value.indexOf('@') == -1) {
   alert("Bitte gib eine gueltige Email-Adresse ein.");
   document.getElementById(mmail).focus();
   return false;
  }
  if (document.getElementById(mdaten).checked != true ){ 
	  alert ( "Bitte bestaetige, dass Du die Informationen zum Datenschutz zur Kenntnis genommen hast" );
	  document.getElementById(mdaten).focus();
	   return false;
  } 
}

function getAddText(intID){
$.ajax({
   type: "POST",
   url: "wp-content/themes/bb/_zusatztext.php",
   data: "id="+intID,
   success: function(strResponse){
     $("#"+intID).find(".full_item .text2 span").html(strResponse);
   }
 });
}
function getEbook(intID){
$.ajax({
   type: "POST",
   url: "wp-content/themes/bb/_ebook.php",
   data: "id="+intID,
   success: function(strResponse){
		if(strResponse=="has_result"){
			var ebook_link = "<a href=\"http://www.libreka.de/retailerlink?retid=5234922&ISBN=" + intID + "\" target=\"_blank\" class=\"arial_10_bold caps grey_txt\">{ e-Book jetzt bei unserem Partner libreka! bestellen }</a>"
     		$("#"+intID).find(".full_item .text3 span").html(ebook_link);
		}else{
			var ebook_link = "momentan leider nicht als e-Book erh&auml;ltlich";
			$("#"+intID).find(".full_item .text3 span").html(ebook_link);
		}
   }
 });
}

function addNumToSession(intID,numval){
$.ajax({
   type: "POST",
   url: "wp-content/themes/bb/_anzahl.php",
   data: "id="+intID+"&num="+numval,
   success: function(strResponse){
     //
   }
 });
}





