/**
 * @author Christian Lahrz-Beckmann
 * @since 26.04.2010
 * @changed: 18.05.2010 version 1.3
 * @changed: 13.01.2011 version 1.4
 * @changed: 16.01.2011 version 1.5
 *
 * @version 1.5
 */
            var iPad = navigator.userAgent.match(/iPad/i);
            var z = 999;
            var social_popup_maus = false;
            var social_popup_timer = null;

            var kommentarbox_m_maus = false;
            var kommentarbox_m_timer = null;

            var kommentarbox_b_maus = false;
            var kommentarbox_b_timer = null;

            checkExternalClick = function(event){
                if (jQuery(event.target).parents('.activedropdown').length === 0) {
                    jQuery('.activedropdown').removeClass('activedropdown');
                    jQuery('.options').hide();
                }
            };

            /**
             * Select-Box mit eigenem Layout überdecken und trotzdem funktionsfähig für Formulare machen
             */
            jQuery(document).ready(function(){
                jQuery(document).mousedown(checkExternalClick);
                if (jQuery.browser.safari) {
                    jQuery('#hersteller_index').css('overflow', 'inherit');
                }

                jQuery('#hersteller_suchauswahl select').each(function(){
                    if (!jQuery(this).parent().hasClass('enhanced')) {
                        targetselect = jQuery(this);
                        targetselect.hide();

                        // set our target as the parent and mark as such
                        var target = targetselect.parent();
                        target.addClass('enhanced');

                        // prep the target for our new markup
						            target.append('<dl class="dropdown"><dt><a class="dropdown_toggle" href="#"><div class="hersteller_quer_pfeil"></div></a></dt><dd><div class="options"> <div class="hersteller_select_overlay"><p><a href="">Hersteller</a></p></div> <div class="options_top"></div><div class="options_mitte"> <ul></ul> </div> <div class="options_bottom"></div></div></dd></dl>');
                        if (jQuery(target).attr('id') == 'hersteller_select_hersteller') {
                          target.find('.hersteller_select_overlay').append('<a href="" class="hersteller_select_up"><img src="/images/arrow_up.gif" alt="vorherige" /></a>');
                        }
                        target.find('.dropdown').css('zIndex', z);
                        z--;

                        // we don't want to see it yet
                        target.find('.options').hide();

                        // parse all options within the select and set indices
                        var i = 0;
                        targetselect.find('option').each(function(){
                            // add the option
                            target.find('.options ul').append('<li><a href="#"><span class="value">' + jQuery(this).text() + '</span><div class="hersteller_quer_pfeil"></div><span class="hidden index">' + i + '</span></a></li>');

                            // check to see if this is what the default should be
                            if (jQuery(this).attr('selected') == true) {
                                targetselect.parent().find('a.dropdown_toggle').append('<span></span>').find('span').text(jQuery(this).text());
                            }
                            i++;
                        });

                        //jQuery(target).find('.options_mitte ul li:first');
                        if (jQuery(target).attr('id') == 'hersteller_select_hersteller') {
                          jQuery(target).find('.options_mitte').append('<a href="" class="hersteller_select_down"><img src="/images/arrow_down.gif" alt="n&auml;chste" /></a>');
                        }

                    }
                });

				jQuery('div.options_mitte ul li').hover(function() {
					jQuery(this).css('font-weight','bold');
				}, function() {
					jQuery(this).css('font-weight','normal');
				})

				jQuery('form[name="searchform"]').bind('submit',function() {
					var s_text = jQuery(this).find('.hersteller_suche');
					if (jQuery(s_text).val() == 'Volltextsuche') {
						jQuery(s_text).attr('value','');
					}
				})

				jQuery('#hersteller_suche input.hersteller_suche').live('keypress',function(event) {
					if (event.which == '13') {
						//Enter gedrückt
						event.preventDefault();
						if (jQuery(this).val().length >= 3) {
							jQuery(this).closest('form[name="searchform"]').submit();
						} else {
							alert('Bitte geben Sie ein Suchwort mit mindestens 3 Zeichen ein!');
						}
					}
				})

				/**
				 * Verhindern das der falsche Eintrag zum Laden der Seite führt
				 * @param {Object} event
				 */
				jQuery('.hersteller_select_overlay a').live('click',function(event) {
					event.preventDefault();
					return false;
				})

				/**
				 * Schnellsuche für Herstellerliste
				 */
				//Kopie der Daten in der Liste anfertigen
				var UrsprungHerstellerListe = jQuery('#hersteller_select_hersteller div.options_mitte').clone();
				var HerstellerListe = new Array();
				jQuery(UrsprungHerstellerListe).find('a').find('span:first').each(function() {
					HerstellerListe.push(jQuery(this).text().toLowerCase());
				});
				var HerstellerListe_length = HerstellerListe.length;

				jQuery('.hersteller_select_overlay:first p').append('<input type="text" value="Schnellsuche" id="hersteller_schnellsuche" style="width:150px; font-size:12px; width:181px; height:15px; padding:1px 0 1px 3px; margin-top:4px;" />');
				jQuery('#hersteller_schnellsuche').live('click',function() {
					if (jQuery(this).val() == 'Schnellsuche') {
						jQuery(this).val('');
					}
				})
				jQuery('#hersteller_schnellsuche').live('keyup',function(event) {
					var text = jQuery(this).val().toLowerCase();
					ul_pos = 0;
					if (text != '') {
						//Suche der Hersteller
						//jQuery('#hersteller_select_hersteller div.options_mitte').find('ul li').find('a span:first:contains("'+text+'")')
						//var gefunden = jQuery('#hersteller_select_hersteller div.options_mitte').find('ul li').find('a span:first:contains("'+text+'")').parents('li');
						//var gefunden = jQuery(SucheHerstellerListe).find('a span:first:contains("'+text+'")').parents('li');

						//var gefunden = jQuery(SucheHerstellerListe).find('li').find('a span:first:contains("'+text+'")').parents('li').clone();

						var gefunden = '';
						for (var i = 0; i < HerstellerListe_length; i++) {
							var teil = HerstellerListe[i].substr(0,text.length);
							if (teil == text) {
								//Übereinstimmung gefunden
								gefunden += jQuery(UrsprungHerstellerListe).find('ul li:eq('+i+')').html();
							}
						}

						var ie_css = '';
						if (jQuery.browser.msie) {
							if (jQuery.browser.version.substr(0,1) == '6') {
								ie_css = 'height:22px';
							} else {
								ie_css = 'height:23px';
							}
						} else {
							ie_css = 'height:19px';
						}
						var liste = jQuery('#hersteller_select_hersteller div.options_mitte ul').html('<li style="'+ie_css+'">Hersteller</li>');
						jQuery(liste).append(gefunden);

					} else {
						jQuery('#hersteller_select_hersteller div.options_mitte').html(jQuery(UrsprungHerstellerListe).html());
					}
				})

				var ul_pos = 0; //Listenposition

				var setTimer = '';
				var pfeil_el = '';
				var mouse_click = false;

				/**
				 * Pfeilnavigation mit eingeschränktem View, zum Scrollen
				 */

				function selectListUp() {
					if (pfeil_el) {
						var el = jQuery(pfeil_el).parent('.hersteller_select_overlay').siblings('div.options_mitte').find('ul li:first');
						var el_h = jQuery(el).height();
						ul_pos = parseInt(ul_pos);
						if (ul_pos < 0) {
							ul_pos = parseInt(ul_pos) + el_h;
							ul_pos += 'px';
							jQuery(el).css('margin-top', ul_pos);
						}
						mouse_click = true;
					}
				}

				jQuery('.hersteller_select_up').live('click',function(event) {
					//event.preventDefault();
					clearInterval(setTimer);
					if (mouse_click == false) {
						selectListUp();
					}
				})

				jQuery('.hersteller_select_up').live('mousedown',function() {
					//Timer setzen
					pfeil_el = this;
					clearInterval(setTimer);
					mouse_click = false;
					setTimer = window.setInterval(selectListUp,200);
				})

				jQuery('.hersteller_select_up').live('hover',function() {
					var ul_height = 0; //jQuery(this).attr('ul_height');
					var el = jQuery(this).parent('.hersteller_select_overlay').siblings('div.options_mitte').find('ul');
					if (el) {
						ul_height = jQuery(el).height() + Math.abs(parseInt(ul_pos,10));
						jQuery(this).attr('ul_height',ul_height);
					}
				}, function() {
					clearInterval(setTimer);
				})

				jQuery('.hersteller_select_down').live('click',function(event) {
					//event.preventDefault();
					clearInterval(setTimer);
					if (mouse_click == false) {
						selectListDown();
					}
				})

				function selectListDown() {
					if (pfeil_el) {
						var el = jQuery(pfeil_el).parent('div.options_mitte').find('ul li:first');
						var el_h = jQuery(el).height();
						ul_pos = parseInt(ul_pos);

						var ul_height = jQuery(pfeil_el).attr('ul_height');
						if (ul_height - 228 > Math.abs(ul_pos)) {
							ul_pos = ul_pos - el_h;
							ul_pos += 'px';
							jQuery(el).css('margin-top', ul_pos);
						}
						mouse_click = true;
					}
				}

				jQuery('.hersteller_select_down').live('mousedown',function() {
					//Timer setzen
					pfeil_el = this;
					clearInterval(setTimer);
					mouse_click = false;
					setTimer = window.setInterval(selectListDown,200);
				})


				jQuery('.hersteller_select_down').live('mouseover',function() {
					var ul_height = 0;
					var el = jQuery(this).parent('div.options_mitte').find('ul');
					if (el) {
						ul_height = jQuery(el).height() + Math.abs(parseInt(ul_pos,10));
						jQuery(this).attr('ul_height',ul_height);
					}
				})

				jQuery('.hersteller_select_down').live('hover',function() {
					var ul_height = 0;
					var el = jQuery(this).parent('div.options_mitte').find('ul');
					if (el) {
						ul_height = jQuery(el).height() + Math.abs(parseInt(ul_pos,10));
						jQuery(this).attr('ul_height',ul_height);
					}
				}, function() {
					clearInterval(setTimer);
				})

				/**
				 * Aufklappen der Select Boxen
				 */

                jQuery('a.dropdown_toggle').live('click', function(){
      					jQuery('.hersteller_select_overlay p a').text(jQuery(this).parents('.dropdown').prev('select').find('option:first').text() + ' auswählen'); //Text des ersten Listenelements herausfiltern und einsetzen im Overlay
      					var theseOptions = jQuery(this).parent().parent().find('.options');
                    if (theseOptions.css('display') == 'block') {
                        jQuery('.activedropdown').removeClass('activedropdown');
                        theseOptions.hide();
                        jQuery('.hersteller_quer_pfeil').hide();
                    }
                    else {
                        theseOptions.parent().parent().addClass('activedropdown');
                        theseOptions.show();
                        jQuery('.hersteller_quer_pfeil').hide();
                    }
                    return false;
                });

                // bind to clicking a new option value
                jQuery('.options a').live('click', function(e){
                    var className = jQuery(this).attr('class');
          					if (className == 'hersteller_select_up' || className == 'hersteller_select_down') {
          						return false;
          					}
          					jQuery('.options').hide();
                    var enhanced = jQuery(this).parents('.enhanced');
                    var realselect = enhanced.find('select');

                    // set the proper index
                    realselect[0].selectedIndex = jQuery(this).find('span.index').text();

                    // update the pseudo selected element
                    enhanced.find('.dropdown_toggle').empty().append('<span></span>').find('span').text(jQuery(this).find('span.value').text());
                    jQuery('<div class="hersteller_quer_pfeil"></div>').insertAfter(enhanced.find('.dropdown_toggle span'));
					          jQuery(this).closest('form[name="searchform"]').submit();

                    return false;
                });

                /**
                 * Hersteller-Index
                 */
                var hg_elm = '';
                jQuery('.hersteller_index_elm').hover(function(){
                    jQuery(this).find('.hersteller_elm_infos').css('visibility', 'visible');
                    hg_elm = jQuery(this).find('a span:first').css('background-image');
                    if (hg_elm != 'undefined') {
                        jQuery(this).find('a span:first').css({
							'background-image':'none',
							'display':'none'
						})
						if (jQuery(this).find('.h_veranstaltungs_text').length > 0) {
							jQuery(this).find('.h_veranstaltungs_text').hide();
						}
                    }
                }, function(){
                    jQuery(this).find('.hersteller_elm_infos').css('visibility', 'hidden');
                    if (hg_elm != 'undefined') {
                        jQuery(this).find('a span:first').css({
							'background-image': hg_elm,
							'display':'inline'
							})
						if (jQuery(this).find('.h_veranstaltungs_text').length > 0) {
							jQuery(this).find('.h_veranstaltungs_text').show();
						}
                    }
                })

				/**
				 * Hersteller-Artikel
				 */
				if (jQuery('#h_info_anfordern_img').length > 0) {
					var img_anf = new Image;
					var img_zuvor = '';
					if (jQuery('#h_info_anfordern_img').hasClass('h_anfordern_produkt')) {
						img_anf.src = '/images/DreieckInfoAnfordern_1.png';
					} else {
						if (jQuery('#h_info_anfordern_img').hasClass('h_anfordern_referenz')) {
							img_anf.src = '/images/DreieckInfoAnfordern_2.png';
						} else {
							img_anf.src = '/images/DreieckInfoAnfordern_3.png';
						}
						if (jQuery('#h_info_anfordern_img').hasClass('h_anfordern_grau')) {
							img_anf.src = '/images/DreieckInfoAnfordern_Grau.png';
						}
					}
					jQuery('#h_info_anfordern_img').hover(function() {
						img_zuvor = jQuery('#h_info_anfordern_img').attr('src');
						jQuery('#h_info_anfordern_img').attr('src',img_anf.src);
						jQuery('#h_info_anfordern_img').css({
							'bottom' : '-15px',
							'right'  : '-15px'
						});
					}, function() {
						jQuery('#h_info_anfordern_img').attr('src',img_zuvor);
						jQuery('#h_info_anfordern_img').css({
							'bottom' : '0',
							'right'  : '0'
						});
					})
				}

				/**
				 * Hersteller-Artikel Formular
				 */
				if (jQuery('.hersteller_info_anfordern').length > 0) {
					jQuery('.hersteller_info_anfordern a, .h_info_link_unten a, a.h_link_info').click(function() {
						//Formular ausfahren
						jQuery('.artikel_magazin_hersteller_form').slideToggle(500);
					})
				}

				var anker = location.hash.substr(1);
				if (anker == 'form') {
					//Formular ausfahren
					jQuery('.artikel_magazin_hersteller_form').show();
					jQuery('#h_info_anfordern_img').unbind();
					jQuery('#h_info_anfordern_img').attr('src',img_anf.src);
					jQuery('#h_info_anfordern_img').css({
						'bottom' : '-15px',
						'right'  : '-15px'
					});
				}

				if (jQuery('#adresse').length > 0) {
					if (jQuery('#adresse').attr('checked')) {
						jQuery('.hersteller_adressformular').show();
					}
					jQuery('#adresse').click(function() {
						jQuery('.hersteller_adressformular').toggle();
					})
				}

        var h_dreieck = '';
        jQuery('#tabelle_hersteller_liste td.tabelle_tab2_magazin25_h').hover(function() {
          var el = jQuery(this).prev('td.tabelle_tab1_magazin_hersteller').find('a div');
          h_dreieck = jQuery(el).css('background-image');
          jQuery(el).css('background-image','none');
        }, function() {
          var el = jQuery(this).prev('td.tabelle_tab1_magazin_hersteller').find('a div');
          jQuery(el).css('background-image',h_dreieck);
          jQuery(el).css('background-repeat','no-repeat');
        });

        /**
         * Meldungsindex + Suchlisten von Meldungen
         */

         //Bestkommentierte Meldungen Box einblenden
         jQuery('.kommentar_box a.kommentar_box_bestkommentierte').hover(function() {
          clearTimeout(kommentarbox_b_timer);
          jQuery('.kommentar_box_pulldown').hide();
          jQuery('#kommentar_box_pulldown_b').show();
          jQuery('#kommentar_box_pulldown_b .kommentar_box_bestkommentiert').show();
         }, function() {
          kommentarbox_b_timer = setTimeout("kommentarbox_b_out()",500);
         });

         jQuery('#kommentar_box_pulldown_b').mouseenter(function(event) {
          kommentarbox_b_maus = true;
         });

         jQuery('#kommentar_box_pulldown_b').mouseleave(function(event) {
          jQuery('#kommentar_box_pulldown_b').hide();
          kommentarbox_b_maus = false;
         });

         //Meistgelesene Meldungen Box einblenden
         jQuery('.kommentar_box a.kommentar_box_meistgelesene').hover(function() {
          clearTimeout(kommentarbox_m_timer);
          jQuery('.kommentar_box_pulldown').hide();
          jQuery('#kommentar_box_pulldown_m').show();
          jQuery('#kommentar_box_pulldown_m .kommentar_box_meistgelesen').show();
         }, function() {
          kommentarbox_m_timer = setTimeout("kommentarbox_m_out()",500);
         });

         jQuery('#kommentar_box_pulldown_m').mouseenter(function() {
          kommentarbox_m_maus = true;
         });

         jQuery('#kommentar_box_pulldown_m').mouseleave(function() {
          jQuery('#kommentar_box_pulldown_m').hide();
          kommentarbox_m_maus = false;
         });

         /**
          * Social-Buttons in der Meldung
          * #tool_navigation_footer .senden,
          */

        jQuery('#social_buttons_short').hover(function() {
          clearTimeout(social_popup_timer);
          jQuery('#social_buttons_popup').show();
          jQuery('#social_buttons_short a span').css('opacity','1');
          jQuery('#social_buttons_short').addClass('social_buttons_short_hover');
          jQuery(this).find('a.senden').css('background-image','url(/images/send_hover.gif)');
        }, function() {
          jQuery('#social_buttons_short a span').css('opacity','0.8');
          jQuery('#social_buttons_short').removeClass('social_buttons_short_hover')
          jQuery(this).find('a.senden').css('background-image','url(/images/send.gif)');
          social_popup_timer = setTimeout("senden_button_out()",500);
        });

        jQuery('#social_buttons_short a').click(function(event) {
          event.preventDefault();
        });

        jQuery('#social_buttons_popup').mouseenter(function() {
          social_popup_maus = true;
        });

        jQuery('#social_buttons_popup').mouseleave(function() {
          social_popup_maus = false;
          jQuery('#social_buttons_popup').hide();
        });

        jQuery('#social_buttons_popup .addthis_toolbox a').hover(function() {
          jQuery(this).css('color','#0F96B3');
        }, function() {
          jQuery(this).css('color','#000');
        });

        /**
         * Aufklappen von A-Z, wenn man auf das Hintergrundbild klickt
         */
        jQuery('#wrapper').click(function(event) {
            if (jQuery(event.target).attr('id') == this.id) {
              if (jQuery('#a-z_overlay').is(':visible') == false) {
                  show_overlay('a-z_overlay','a-z_pic','a_z_activ.gif');
                  change_pic('a-z_pic','a_z_activ.gif','','');
              }
            }
        });

        /**
         * Homepage Overlays aufklappen
         */
         jQuery('#header_bottom_text1 span').hover(function(event) {
          jQuery('#spalte-1 .spalte_overlay').show();
         }, function() {
          jQuery('#spalte-1 .spalte_overlay').hide();
         })

         jQuery('#header_bottom_text2 span').hover(function(event) {
          jQuery('#spalte-2 .spalte_overlay').show();
         }, function() {
          jQuery('#spalte-2 .spalte_overlay').hide();
         })

            });

        // Ausblenden der Social-Buttons
        function senden_button_out() {
         if (social_popup_maus == false) {
          jQuery('#social_buttons_popup').hide();
         }
        }

        //Ausblenden der Meistgelesenen Meldungen
        function kommentarbox_m_out() {
         if (kommentarbox_m_maus == false) {
          jQuery('#kommentar_box_pulldown_m').hide();
         }
        }

        //Ausblenden der bestkommentierten Meldungen
        function kommentarbox_b_out() {
         if (kommentarbox_b_maus == false) {
          jQuery('#kommentar_box_pulldown_b').hide();
         }
        }

