$(function(){

		//Klasse für die 3. Naviebene - Workaround, da bei abgeschlatetem JS diese Navi nicht als tabs angezeit wird
		if ($(".tab").get(0) != null)
		{
			var ele = $(".tab").get(0);
			var class_name = ele.className;
			if (class_name == "subsubsub tab")
			{
				$(".tab").each(function(){
										     $(this).removeClass("subsubsub tab");
						     		     $(this).addClass("subsubsub_tab");
						           });
			}
		}

		//verschiebe Inhaltsbereich, wenn Navi als Tabs angezeigt werden
		var display = $(".active > .subsub_tab").css("display");
		var display2 = $(".active > .subsubsub_tab").css("display");

		if(display == "block") {
			$("#maincontent").css({position:'relative',top:'3em'});
		}

		if(display2 == "block") {
			$("#maincontent").css({position:'relative',top:'3em'});
		}

		var offset = $(".active > .subsubsub_tab").offset().top;
		if (offset != "")
		{
			var offset = eval(offset - 2*offset + 133);
			$(".subsubsub_tab").css("top", offset);
		}

		//facebox addon for images
		$("#paragraph_img a > img").each(function(){
                                       var $a = $(this).parent();
                                       // check for flash video contents
                                       if ($a.attr('href').indexOf('video') == -1) {
                                         $a.click(function(){
                                         	          	$(document).unbind('reveal.facebox').bind('reveal.facebox',
							                                          function(){
							                                          	var org_popup_content_height = $("#popup_content").height();
							                                          	var org_popup_content_img_height = $("#popup_content_img").height();
																													var org_facebox_height = $("#facebox").height();
																													facebox_resize();
																												});
			                                                var attachment_url = $(this).attr('href');
			                                                $.facebox({ajax:attachment_url});
			                                                return false;                                  
                                                  });
		                                   }
                                       else {
                                         $a.click(function(){
                                                    var flv_url = $(this).attr('href') + '.flv';
                                                    $(document).bind('reveal.facebox',
                                                                     function(){
                                                                 				var org_popup_content_height = $("#popup_content").height();
                                                                 				var org_popup_content_img_height = $("#popup_content_img").height();
																																				var org_facebox_height = $("#facebox").height();
																																				facebox_resize();
                                                                       $(document).unbind('reveal.facebox');

                                                                       var flash = {
                                                                         baseurl: 'http://videos.brandmaker.com/',
                                                                         flvplayer: 'player_flv_multi.swf',
                                                                         configxml: 'playerconfig_mapsTutorials.xml',
                                                                         width: 800,
                                                                         height: 600
                                                                       };

                                                                       swfobject.embedSWF(flash.baseurl+flash.flvplayer,
                                                                                          "video_content", flash.width, flash.height, "9.0.0", "expressInstall.swf",
                                                                                          {
                                                                                            flv:flv_url,
                                                                                            width:flash.width,height:flash.height,
                                                                                            configxml:flash.baseurl+flash.configxml
                                                                                          },
                                                                                          {
                                                                                            movie:flash.baseurl+flash.flvplayer,
                                                                                            allowFullScreen:'true'
                                                                                          });
                                                                     });
                                                    $.facebox('<div id="video_content" style="width:800px; height:600px;"></div>');
                                                    return false;
                                                  });
                                       }
				                             });

		// FF Bugfix
		$("#news .box_content .img_large img").each(function() {
				      var img_height = $(this).css("height");
				      $(this).parent().css("height", img_height);
		        });

		$("#news .box_content .img_large a img").each(function() {
				      var img_height = $(this).css("height");
				      $(this).parent().parent().css("height", img_height);
				    });

        $("#subcontent .box_content div a img").each(function(id,el) {
                        var img_height = $(this).css("height").replace("px","");
                        var img_padding_top = el.style.marginTop.replace("px","");
                        var img_padding_bottom = el.style.marginBottom.replace("px","");            
                        img_height = parseInt(img_height) + parseInt(img_padding_bottom) + parseInt(img_padding_top);
                        $(this).parent().parent().css("height", img_height);
                    });

		$("#subcontent .box_content .img_large img").each(function(id,el) {
                        var img_height = $(this).css("height").replace("px","");
                        var img_padding_top = el.style.marginTop.replace("px","");
                        var img_padding_bottom = el.style.marginBottom.replace("px",""); 
                        if (img_padding_bottom != "" && img_padding_top != "") {          
                            img_height = parseInt(img_height) + parseInt(img_padding_bottom) + parseInt(img_padding_top);
                        }
                        $(this).parent().parent().css("height", img_height+"px");
				    });

		// replace image popups by using facebox
		 $("a").not("#paragraph_img a").each(function() {
              // 3 different kinds of OSIRIS popup magic
		          if($(this).attr("href")) {
		            if($(this).attr("href").indexOf('standard_attach') != -1) {
		              $(this).click(function(){
                                                    var attachment_url = $(this).attr('href');
                                                    $.facebox({ajax:attachment_url});
                                                    return false;
                                                  });
		            }
                // image popup galery handlers
		            else if($(this).attr("href").indexOf('attach=1') != -1 || $(this).attr("href").indexOf('attachments') != -1) {
		              $(this).click(function(){
                                  $(document).unbind('reveal.facebox').bind('reveal.facebox',
                                          function(){
                                          	var org_popup_content_height = $("#popup_content").height();
                                          	var org_popup_content_img_height = $("#popup_content_img").height();	
																						var org_facebox_height = $("#facebox").height();
																						facebox_resize();
                                            $('#popup_head a').not('#up').not('#down').click(function(){
                                                       var show_img = $(this).attr('href');
                                                       var img_nr = show_img.match(/img_\d+/);
           
                                                       $('#popup_head a').removeClass("selected");
                                                       $(this).addClass("selected");
                                                       
                                                       $('#popup_content_img div').addClass('hide').removeClass('show');
                                                       $('#'+img_nr).removeClass('hide').addClass('show');
                                                       return false;
                                                     });
                                            $('#popup_head #up').unbind("click").click(function(){
                                            														if ($('#popup_content_img div.show').next('div').length != 0)
                                            														{
                                                                         $('#popup_content_img div.show').removeClass('show').addClass('hide').next('div').addClass('show').removeClass('hide');
																																         $('#popup_head .selected').removeClass("selected").next("a").addClass("selected");                                                                           
                                                                        }
                                                                         return false;                                                                        
                                                                       });
                                            $('#popup_head #down').unbind("click").click(function(){
                                            														if ($('#popup_content_img div.show').prev('div').length != 0)
                                            														{                                            	
                                                                           $('#popup_content_img div.show').removeClass('show').addClass('hide').prev('div').addClass('show').removeClass('hide');
																																	         $('#popup_head .selected').removeClass("selected").prev("a").addClass("selected");                                                                              
                                                                         }
                                                                         return false;
                                                                         });

                                          });

                                  var attachment_url = $(this).attr('href');
                                  $.facebox({ajax:attachment_url});
                                  return false;
                                });
		            }
		          }
		        });

	// overlay images for video links and image links
		$("#paragraph_img table a").each(function(){
		    var link = $(this).attr("href");
		    if(link != null && link.indexOf('video') != -1) {
		      	$(this).parent().removeClass();
		       	$(this).parent().addClass("video");
		}
		else if(link != null && link.indexOf('video') == -1) 
		{
		       	$(this).parent().removeClass();
		       	$(this).parent().addClass("lupe");
					    	}
	 	})
		 
		 //toggle overlay images
		 $("#paragraph_img img, .paragraph_switch img, .paragraph_switch_klein img").hover(function(){
			  	      $(this).prev().addClass("hover");
			    	},
			    	function() {
			      	  $(this).prev().removeClass("hover");
			    	}
			);
								
			
			//Schlagschatten der Webseite an Fenstergröße anpassen
			if($("#content").height() <= $("#maincontent").height() || $("#content").height() <= $(window).height()){
			      var maincontent_height = $("#maincontent").height();
			      var content_height = $("#content").height();
			      var win_height = $(window).height()
			 
			        if (maincontent_height > content_height)
			        {
			             var doc_height = $("#content").height() + maincontent_height;
			             $("#content").css('height',doc_height);
			        }
			       
			        if ($("#content").height() >= win_height)
				{
			            var doc_height = $("#content").height() + maincontent_height;
			            $("#content").css('height',doc_height);
				}
				else
				{
				    $("html").css('height','100%');
				    $(".BrandMaker").css('height','100%');
			   }
			}
			
			var org_popup_content_height = $("#popup_content").height();
			var org_popup_content_img_height = $("#popup_content_img").height();
			var org_facebox_height = $("#facebox").height();
			facebox_resize();
			
			function facebox_resize() {	
				$(window).unbind("resize").resize(function(){
						 	
							var win_height = ($(window).height());
					    var facebox_height = ($("#facebox").height());
					    
					    if ($("#popup_content").length != 0)
					    {
					    	var popup_content_height = $("#popup_content").height();
					    }
					    else if($("#popup_content_img").length != 0)
					    {
					    	var popup_content_height = $("#popup_content_img").height();				    	
					    }
				
					    if((win_height < facebox_height) || ((win_height > facebox_height) && (win_height < org_facebox_height)))
					    {
					        if ($("#facebox").length != 0 && ($("#popup_content").length != 0 || $("#popup_content_img").length != 0))
					        {
					            var facebox_content_headerfooter = (facebox_height - popup_content_height);
					            var new_facebox_content_height = win_height - facebox_content_headerfooter;
					            $("#popup_content").css("height",new_facebox_content_height);
					            $("#popup_content_img").css("height",new_facebox_content_height);				            
					        }
					    }
					    else
					    {
					        if ($("#facebox").length != 0 && $(("#popup_content").length != 0 || $("#popup_content_img").length != 0))
					        {
					            $("#popup_content").css("height",org_popup_content_height);
					            $("#popup_content_img").css("height",org_popup_content_img_height);				            
					        }        
					    }
					    
					  	if (($(window).height()-$('#facebox').height()) / 2+getPageScroll()[1] < 0)
					  	{
					  		var top_pos = 0;
					  	}
					  	else
					  	{
					  		var top_pos = ($(window).height()-$('#facebox').height()) / 2+getPageScroll()[1];
					  	}  	
							$('#facebox').animate({
								top:	top_pos,//getPageScroll()[1] + (getPageHeight() / 10),
								left:	($(document).width()-$('#facebox').width()) / 2
							},50);
												    
				return false;	 
				
				});
			}

		  // getPageScroll() by quirksmode.com
		  function getPageScroll() {
		    var xScroll, yScroll;
		    if (self.pageYOffset) {
		      yScroll = self.pageYOffset;
		      xScroll = self.pageXOffset;
		    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		      yScroll = document.documentElement.scrollTop;
		      xScroll = document.documentElement.scrollLeft;
		    } else if (document.body) {// all other Explorers
		      yScroll = document.body.scrollTop;
		      xScroll = document.body.scrollLeft;
		    }
		    return new Array(xScroll,yScroll)
		  }

    /* fetch image inside tile
     * and add a border left style
     *
     * depends on layout defined in "paragraph_kachel_klein.htm"
     */
    $('[id^=kachel] .item_klein img').css('border-right','1px solid #CDCDCD')					
});
