// Funktion welche beim Submit alle im Eltern Element vorhanden Inputs mit 
// der Klassen ".input-fill-by-title" kontrolliert
var inputByTitleSubmit = function(parentElement){
    var inputs = parentElement.find('.input-fill-by-title');
    inputs.each(function(index, element){
        var input = $(element);
        var title = input.attr('title');
        var value = input.val();
        
        if (title == value) {
            input.val('');
		};
	});
};

// Funktion welche beim Reset alle im Eltern Element vorhanden Inputs mit
// der Klassen ".input-fill-by-title" kontrolliert
var inputByTitleReset = function(parentElement){
    var inputs = parentElement.find('.input-fill-by-title');
    inputs.each(function(index, element){
        var input = $(element);
        var title = input.attr('title');
        var value = input.val();

        if (value == '') {
            input.val(title);
                };
        });
};



$(document).ready(function(){
	

	/*---------------------------------------------------------------------------------------------------- */
    /* SITECHIPS PRUEFUNGEN */
	var scCB = (location.href.match('/redaktion/') != null);
	var scMSIE = ($.browser.msie == true) ? true : false ;
	var scMOZILLA = ($.browser.mozilla == true) ? true : false ;
	var scOPERA = ($.browser.opera == true) ? true : false ;
	var scWEBKIT = ($.browser.webkit == true) ? true : false ;
	var scCHROME = (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) ? true : false ;
	
	/*---------------------------------------------------------------------------------------------------- 
     * SCHRIFTGROESSE ANPASSEN UND FUER FOLGESEITEN MERKEN
     * benoetigt: http://plugins.jquery.com/project/cookie
     */
    var defaultSize = 100;
    var currentSize = 100;
    
	/*---------------------------------------------------------------------------------------------------- */
    /* setzen der Schriftgroesse auf #main */
    var setFontSize = function(size){
        if (size <= 150) {
            if (size >= 90) {
                currentSize = size;
                $.cookie('LBV_FONTSIZE', size);
                $('#main').css('font-size', size + '%');
            };
		};
	};
    
	/*---------------------------------------------------------------------------------------------------- */
    /* ermitteln Schriftgroesse fuer Seiten refresh und setzen des Cookies */
    var getFontSize = function(){
        var cookieSize = $.cookie('LBV_FONTSIZE');
        if (cookieSize == null) {
            $.cookie('LBV_FONTSIZE', defaultSize);
            setFontSize(defaultSize);
        }
		else {
            setFontSize(cookieSize);
        };
    };
    getFontSize();
    
    /*---------------------------------------------------------------------------------------------------- */
    /* Schriftgroesse + */
    $('.font_big').click(function(e){
        e.preventDefault();
        var newSize = parseInt(currentSize) + 10;
        setFontSize(newSize);
    });
    
	/*---------------------------------------------------------------------------------------------------- */
    /* Schriftgroesse - */
    $('.font_small').click(function(e){
        e.preventDefault();
        var newSize = parseInt(currentSize) - 10;
        setFontSize(newSize);
    });
    
	/*---------------------------------------------------------------------------------------------------- */
    /* Schriftgroesse reset */
    $('.font_normal').click(function(e){
        e.preventDefault();
        currentSize = defaultSize;
        setFontSize(currentSize);
    });
	
	/*---------------------------------------------------------------------------------------------------- */
	/* AKKORDEONELEMENTE EIN- AUSBLENDEN */
	$('.toggel-content').hide();
	$('.btn_toggel').click(function() {
		$('.btn_toggel').removeClass('active');
		$('.toggel-content').slideUp("fast");
		if($(this).next().is(':hidden'))
		{
			$(this).addClass('active');
			$(this).next().slideDown("fast");
		}
	});

	/* Akkordeon im Backend permanent anzeigen */
	if(scCB){
		$('.toggel-content').show();
	};
	
	/*---------------------------------------------------------------------------------------------------- */
	/* SUCHHINWEISE EIN- AUSBLENDEN */
	$('.eventteaser').hide();
	$(".eventname").click(function() {
		$('.eventname').removeClass('active');
		$('.eventteaser').slideUp("fast");
		if($(this).next().is(':hidden'))
		{
			$(this).addClass('active');
			$(this).next().slideDown("fast");
		}
	});

	/*---------------------------------------------------------------------------------------------------- */
	/* STARTSEITENNAVI EIN- AUSBLENDEN */
	$('.start_chapter_frame_1').hide();
	$('.start_chapter_frame_2').hide();
	$('.start_chapter_frame_3').hide();
	$('.start_chapter_frame_4').hide();

	$('a.start_content_1').mouseover(function() {
		$('.start_chapter_frame_1').show();
	});
	
	$('a.start_content_1').mouseout(function() {
		$('.start_chapter_frame_1').hide();
	});
	
	$('a.start_content_1').click(function() {
		$('.start_chapter_frame_1').toggle();
	});

	$('.start_chapter_frame_1').mouseover(function() {
		$('.start_chapter_frame_1').show();
		$('a.start_content_1').addClass('active');
	});

	$('.start_chapter_frame_1').mouseout(function() {
		$('.start_chapter_frame_1').hide();
		$('a.start_content_1').removeClass('active');
	});

	$('a.start_content_2').mouseover(function() {
		$('.start_chapter_frame_2').show();
	});
	
	$('a.start_content_2').mouseout(function() {
		$('.start_chapter_frame_2').hide();
	});
	
	$('a.start_content_2').click(function() {
		$('.start_chapter_frame_2').toggle();
	});

	$('.start_chapter_frame_2').mouseover(function() {
		$('.start_chapter_frame_2').show();
		$('a.start_content_2').addClass('active');
	});

	$('.start_chapter_frame_2').mouseout(function() {
		$('.start_chapter_frame_2').hide();
		$('a.start_content_2').removeClass('active');
	});

	$('a.start_content_3').mouseover(function() {
		$('.start_chapter_frame_3').show();
	});
	
	$('a.start_content_3').mouseout(function() {
		$('.start_chapter_frame_3').hide();
	});
	
	$('a.start_content_3').click(function() {
		$('.start_chapter_frame_3').toggle();
	});

	$('.start_chapter_frame_3').mouseover(function() {
		$('.start_chapter_frame_3').show();
		$('a.start_content_3').addClass('active');
	});

	$('.start_chapter_frame_3').mouseout(function() {
		$('.start_chapter_frame_3').hide();
		$('a.start_content_3').removeClass('active');
	});

	$('a.start_content_4').mouseover(function() {
		$('.start_chapter_frame_4').show();
	});
	
	$('a.start_content_4').mouseout(function() {
		$('.start_chapter_frame_4').hide();
	});
	
	$('a.start_content_4').click(function() {
		$('.start_chapter_frame_4').toggle();
	});

	$('.start_chapter_frame_4').mouseover(function() {
		$('.start_chapter_frame_4').show();
		$('a.start_content_4').addClass('active');
	});

	$('.start_chapter_frame_4').mouseout(function() {
		$('.start_chapter_frame_4').hide();
		$('a.start_content_4').removeClass('active');
	});
	/*---------------------------------------------------------------------------------------------------- */
	/* ACCORDIONS */
 	var accordions = $('.accordion_container');
	
	if (accordions.length > 0 && scCB == false) {
		accordions.find('.accordion_content').hide();
	
		$(".accordion-open").click(function(){
			var elm = $(this)
			var parent = elm.parent();
			var content = parent.find('.accordion_content');
			var details = parent.find('.accoordian_details');
			
			accordions.find('.accoordian_details').find('span').html('Öffnen');
			accordions.find('.accoordian_details').parent().removeClass('open');
			
			if(content.is(':hidden')) {
				if (!scMSIE) {
					accordions.find('.accordion_content').slideUp();
				} else {
					accordions.find('.accordion_content').hide();
				};
				
				details.parent().addClass('open');
				details.find('span').html('Schließen');
				
				if (!scMSIE) {
					content.slideDown();
				} else {
					content.show();
				};
			} else {
				if (!scMSIE) {
					accordions.find('.accordion_content').slideUp();
				} else {
					accordions.find('.accordion_content').hide();
				};
			};
		});
	};


	/*---------------------------------------------------------------------------------------------------- */
	/* FORMULAR BEHANDLUNG */
	
	$('.contact_container_form').attr('style','display:none');
	/*---------------------------------------------------------------------------------------------------- */
	/* Automatisch Inputs Values mit Titel fuellen */
	var inputsByTitle = $('.input-fill-by-title');
	if (inputsByTitle.length > 0) {
		
		// Schreiben der Titel ins Value  <input value="[title]"/>
		inputsByTitle.each(function(index, value){
			$(value).val($(value).attr('title'));
		});
		
		// wenn Feld ausgew�hlt
		inputsByTitle.focusin(function(){
            if ($(this).val() == $(this).attr('title')) {
                $(this).val('');
            };
		});
		
		inputsByTitle.focusout(function(){
			var input = $(this);
			if(input.val() == '') {
				input.val(input.attr('title'));
			};
		});		
	};
	/* Formular im Backend permanent anzeigen */
	if(scCB){
		$('.contact_container_form').attr('style','display:block');
	};
	
	/*---------------------------------------------------------------------------------------------------- */
	/* input mit Klasse "submit" in ein button mit Klassen "btn" umwandeln */
	var input_submits = $('input.submit');
	if (input_submits.length > 0) {
		input_submits.each(function(index, value){
			var input = $(value);
			input.after('<!-- input transformed by wiro.js -->');
			input.after('<button type="' + input.attr('type') + '" class="' + input.attr('class') + ' btn"><span><span>' + input.val() + '</span></span></button>');
			input.remove();
		});
	};

	/*---------------------------------------------------------------------------------------------------- */
	/* ANSPRECHPARTNER FORMULAR */
	if ($('.open-contact-form').length > 0) {
	
		/* EVENT - Oeffnen des Ansprechpartner Formulars */
		$('.open-contact-form').click(function(e){
			e.preventDefault();
			
			var siderow = $(this).parent();
			siderow.addClass('active');
			
			var form_container = $(this).parent().next();
			
			if (form_container.is(':hidden')) {
				if (!scMSIE) {
					form_container.slideDown();
				}
				else {
					form_container.show();
				};
							}
			else {
				if (!scMSIE) {
					form_container.slideUp(function(){
						siderow.removeClass('active');
					});
				}
				else {
					form_container.hide();
					siderow.removeClass('active');
				};
			};
		});
		
		/* EVENT - Schliessen des Ansprechpartner Formulars */
		$('.close-contact-form').click(function(e){
			e.preventDefault();
			$(this).parent().parent().prev().find('.open-contact-form').trigger('click');
		});
		
		/* EVENT - Senden des Ansprechpartner Formulars */
		$('.contact_form').submit(function(e){
			var form = $(this);
			form.append('<input type="hidden" name="module" value="contact"/>');
			inputByTitleSubmit(form);		// Felder leeren
			form.find('.error').val('');	// Felder die Kennzeichen fehlerhaft besitzen leeren
		});
		
		/* Ansprechpartner Formular - Pruefen ob Fehler vorhanden sind */
		var contact_form_errors = $('.contact_container_form').find('.errors');
		if (contact_form_errors.length > 0) {
			contact_form_errors.each(function(index, element){
			
				var errors = $(this).find('li');
				
				errors.each(function(index, element){
					var err_element = $(element)
					var form_element = err_element.parent().parent().parent().find('[name=' + $(element).attr('rel') + ']');
					form_element.addClass('error');
					form_element.val(err_element.html());
				});
				
				$(this).parent().parent().prev().find('.open-contact-form').trigger('click');
			});
			
			/* EVENT - Fehlerhafte Ansprechpartner Eingabefelder werden bei Fokus resetet. */
			$('.contact_form .error').focus(function(e){
				$(this).removeClass('error');
				$(this).val('');
			});
		};
		
		/* Ansprechpartner Formular - Pruefen ob Unbekante Fehler vorhanden sind */
		var contact_form_errors = $('.contact_container_form').find('.unkown_errors');
		if (contact_form_errors.length > 0) {
			contact_form_errors.each(function(index, element){
				$(this).parent().parent().prev().find('.open-contact-form').trigger('click');
				
				var form = $(this).parent();
				
				form.find('select').attr('disabled','disabled').addClass('disabled');
				form.find('input').attr('disabled','disabled').addClass('disabled');
				form.find('textarea').attr('disabled','disabled').addClass('disabled');
				form.find('button').attr('disabled','disabled').addClass('disabled');
			});
		};
	};
	
	/*---------------------------------------------------------------------------------------------------- */
	/* TABELLE VERANSTALTUNGEN UM KLASSEN ERWEITERN */
	$('table.date_list tbody tr:odd').addClass('odd');
	$('table.date_list tbody tr:even').addClass('even');
	
    /*---------------------------------------------------------------------------------------------------- */
	/* IFrame via Fancybox laden */
    $(".load-media-iframe").live('click', function(e){
		e.preventDefault();

		if (true) {
			var elm = $(this);

			$.fancybox({
				'transitionIn': 'fadeIn',
				'transitionOut': 'fadeIn',
				'href': elm.attr('href'),
				'type': 'iframe',
				'width': 930,
				'height': 500,
				'autoDimensions': false,
				'titleShow': false
			});
		} else {
			alert('Diese Funktion ist im Vorschaumodus deaktiviert!');
		};
    });
	
	if(!scCB){
		if ($('#gallery-special img').length>0){
		 		$('#gallery-special').qb_gallery({backgroundFrameClass:'.gallery_imageframe',showhideobjekt :"img"}); 
  				window.parent.$('#fancybox-frame').css('overflow','hidden');
		} else {
				window.parent.$('#fancybox-frame').css('overflow','auto');
		}
	} else {
		$('.qbepBlock').find(".images img").css('display','block');
		$('.qbepBlock').find(".images img").css('position','relative');
		$('.qbepBlock').find(".gallery_imageframe").remove();
		$('.qbepBlock').find(".pagination").remove();
		$('.qbepBlock').find(".image_menu").remove();
	}
	
	/* Akkordeon im Backend permanent anzeigen */
	if(scCB){
		$('.slide-area').show();
	};
	
	
	/*---------------------------------------------------------------------------------------------------- */
	/* STADTRUNDGANG IMAGEMAP */

	dside=new Array();
	dside['st_innenstadt'] = '1621';
	dside['st_stgertrud'] = '1037';
	dside['st_kuecknitz'] = '1023';
	dside['st_buntekuh'] = '756';
	dside['st_moisling'] = '1601';
	dside['st_stjuergen'] = '1044';
	dside['st_stlorenz'] = '1051';
	dside['st_travemuende'] = '1058';
	dside['st_nordwestmecklenburg'] = '1030';

	$('.districts map area').mouseover(function(){
		$('.districts .districts-hover').attr('class','districts-hover');
		$('.districts .districts-hover').addClass('h-'+$(this).attr('class'));
		var thislink=this;
		$('.districts-nav a').each(function() {
			if ($(thislink).attr('href').indexOf($(this).attr('rel'))!=-1){
				$('.districts-nav a').attr('class','');
				$('.districts-nav li.current a').addClass('active');
				$(this).attr('class','active');
			}
		})
	})
	$('.districts map area').click(function(){
		$('.districts .districts-hover').addClass('h-'+$(this).attr('class'));
		var thislink=this;
		$('.districts-nav a').each(function() {
			if ($(thislink).attr('href').indexOf($(this).attr('rel'))!=-1)
				document.location.href=$(this).attr('href');
		})
	})
	$('.districts map').mouseout(function(){
		$('.districts .districts-hover').attr('class','districts-hover');
		$('.districts map area').each(function() {
			if ($(this).attr('href').indexOf($('.districts-nav li.current a').attr('rel'))!=-1)
				$('.districts .districts-hover').addClass('h-'+$(this).attr('class'));
		})
		$('.districts-nav li a').attr('class','');
		$('.districts-nav li.current a').addClass('active');
	})
	
	$('.districts-nav a').mouseover(function(){
		$('.districts .districts-hover').attr('class','districts-hover');
		var thislink=this;
		$('.districts map area').each(function() {
			if ($(this).attr('href').indexOf($(thislink).attr('rel'))!=-1)
				$('.districts .districts-hover').addClass('h-'+$(this).attr('class'));
		})
	})
	$('.districts-nav').mouseout(function(){
		$('.districts .districts-hover').attr('class','districts-hover');
		$('.districts map area').each(function() {
			if ($(this).attr('href').indexOf($('.districts-nav li.current a').attr('rel'))!=-1)
				$('.districts .districts-hover').addClass('h-'+$(this).attr('class'));
		})
	})
	$('.districts-nav a').each(function() {
		if (document.location.href.indexOf('.'+dside[$(this).attr('rel')]+'.')!=-1 || 
			document.location.href.indexOf('id='+dside[$(this).attr('rel')])!=-1 ) {
			$(this).parent().addClass('current');
			$(this).addClass('active');
		}
	})
	$('.districts map area').each(function() {
		if ($(this).attr('href').indexOf($('.districts-nav li.current a').attr('rel'))!=-1)
			$('.districts .districts-hover').addClass('h-'+$(this).attr('class'));
	})	


});
