// JavaScript Document

/*  ListArticles  */
$(document).ready( function() {
    
    /* hide "Standard" and "Online" from issuelist */
    $('div.IssueList ul.year').find(':contains("Online")').hide();


	$('.listarticle').each( function() {
		var listNum = 6;
		var pageNum = 0;
		$(this).find('li').mouseover( function() {
			$(this).addClass('listOver');					 
		});
		$(this).find('li').mouseout( function() {
			$(this).removeClass('listOver');			
		});
		
		var listCant = $(this).find('li').length;
		$(this).find('span span.nrArt').html(listCant);
		if (listCant%listNum == 0) {
			pageCant = parseInt(listCant/listNum);
		}
		else {
			pageCant = parseInt(listCant/listNum)+1;
		}
		$(this).find('li').each( function(i) {
			$(this).addClass('list'+(parseInt(i)+1));
		});
		for ( var k=0; k<=pageCant; k++) {
			for ( var j=(k*listNum)+1; j<=(k*listNum)+listNum; j++) {
				$(this).find('.list'+j).addClass('page'+(parseInt(k)+1));
			}
			if (k>=2) {
				$(this).find('.pager').append('<span class="divisor">|</span><a id="pagNr'+k+'" class="pag">&nbsp;'+k+'&nbsp;</a>');
			}
		}
		
		$(this).find('.pager a').click( function() {
			var clickedPag = $(this).attr('id').substr(5,10);
			if ($(this).hasClass('pagAct')) {
			}	
			else {
				$(this).parent().find('a.pagAct').removeClass('pagAct');
				$(this).addClass('pagAct');
				$(this).parents('.listarticle').find('li').css({
					display: 'none'
				});
				$(this).parents('.listarticle').find('li.page'+clickedPag).css({
					display: 'block'
				});
				
			}
		});
	
	});
	kontaktLabels();
	fckManipulate();
	eventJS();
    //formular();
	
	/*  Footer Bottom  */ /*Leave me at the end*/
	footerbottom();
	$(window).resize( function() {
		footerbottom();				
	});
});

/*Navigation*/
$(function(){
        $('.naviHauptnavigation').superfish({
                delay: 500,
                autoArrows: false,
                dropShadows: false
        });
});

function footerbottom() {
	if($('#cmsEditor').length == 0)
	{
		var heightPage = $('#page').attr('clientHeight');
		var heightContent = $('#content').attr('clientHeight');
		var heightHtml = $('html').attr('clientHeight');
		if ( heightPage < heightHtml) {
			$('#page').attr('style','min-height:'+(heightHtml-(heightPage-heightContent+1))+'px');
		}
		else {
			$('#content').removeAttr('style');
			var heightPage = $('#page').attr('clientHeight');
			var heightContent = $('#content').attr('clientHeight');
			var heightHtml = $('html').attr('clientHeight');
			if ( heightPage < heightHtml) {
				$('#content').attr('style','min-height:'+(heightHtml-(heightPage-heightContent+1))+'px');
			}
		}
	}
	else
	{
		var heightPage = $('#page').attr('clientHeight');
		var heightContent = $('#content').attr('clientHeight');
		var heightHtml = ($('html').attr('clientHeight')-70);
		if ( heightPage < heightHtml) {
			$('#content').attr('style','min-height:'+(heightHtml-(heightPage-heightContent+1))+'px');
		}
		else {
			$('#content').removeAttr('style');
			var heightPage = $('#page').attr('clientHeight');
			var heightContent = $('#content').attr('clientHeight');
			var heightHtml = ($('html').attr('clientHeight')-70);
			if ( heightPage < heightHtml) {
				$('#content').attr('style','min-height:'+(heightHtml-(heightPage-heightContent+1))+'px');
			}
		}
	}
}
/****browse button style****/
(function($) {
    
    $.fn.filestyle = function(options) {
                
        /* TODO: This should not override CSS. */
        var settings = {
            width : 250
        };
                
        if(options) {
            $.extend(settings, options);
        };
                        
        return this.each(function() {
            
            var self = this;
            var wrapper = $("<div>")
                            .css({
                                "width": settings.imagewidth + "px",
                                "height": settings.imageheight + "px",
                                "background": "url(" + settings.image + ") 0 0 no-repeat",
                                "background-position": "right",
                                "display": "inline",
                                "position": "absolute",
                                "overflow": "hidden"
                            });
                            
            var filename = $('<input class="file">')
                             .addClass($(self).attr("class"))
                             .css({
                                 "display": "inline",
                                 "width": settings.width + "px"
                             });

            $(self).before(filename);
            $(self).wrap(wrapper);

            $(self).css({
                        "position": "relative",
                        "height": settings.imageheight + "px",
                        "width": settings.width + "px",
                        "display": "inline",
                        "cursor": "pointer",
                        "opacity": "0.0"
                    });

            if ($.browser.mozilla) {
                if (/Win/.test(navigator.platform)) {
                    $(self).css("margin-left", "-142px");                    
                } else {
                    $(self).css("margin-left", "-168px");                    
                };
            } else {
                $(self).css("margin-left", settings.imagewidth - settings.width + "px");                
            };

            $(self).bind("change", function() {
                filename.val($(self).val());
            });
      
        });
        

    };
	
    
})(jQuery);

function clearForm(oForm)
{
    var elements = oForm.elements;
    oForm.reset();
    for(i=0; i<elements.length; i++) 
    {
        field_type = elements[i].type.toLowerCase();
        switch(field_type) 
        {
            case "text":
            case "password":
            case "textarea":
            case "hidden":
                elements[i].value = "";
            break;

            case "radio":
            case "checkbox":
                if (elements[i].checked)
                {
                    elements[i].checked = false;
                }
            break;

            case "select-one":
            case "select-multi":
                elements[i].selectedIndex = -1;
            break;

            default:
            break;
        }
    }
}

// Backend - Kontakt-Kategorie

function kontaktLabels() {
	var option = new Array();
	$('#article_mainCategory option').each(function() {
		if($(this).val()=='') {
			value = '0';
		} else {
			value = $(this).val();
		}
		option[value] = $(this).html().indexOf('Team');
	})
	value = $("#article_mainCategory").val();
	if(option[value]>-1) {
		$("label[for=article_title]").text("Name; Position");
		$("label[for=article_subtitle]").text("Kürzel");
		$("label[for=article_topline]").text("Gruppe");
		$("label[for=article_brief]").text("E-Mail-Adresse; (Telefonnummer)");
	}
	$("#article_mainCategory").change(function () {
		value = $("#article_mainCategory").val();
		if(option[value]!=-1) {
			$("label[for=article_title]").text("Name; Position");
			$("label[for=article_subtitle]").text("Kürzel");
			$("label[for=article_topline]").text("Gruppe");
			$("label[for=article_brief]").text("E-Mail-Adresse; (Telefonnummer)");
		} else {
			$("label[for=article_title]").text("Titel");
			$("label[for=article_subtitle]").text("Untertitel");
			$("label[for=article_topline]").text("Überschrift");
			$("label[for=article_brief]").text("Kurzbeschreibung");
		}
	})
	
}

// Bild mit unterschrift anzeigen

function fckManipulate() {
	$('#page iframe').contents().find('#btnOk.Button').click(function() {
	})
}

// Metas
function addAdditionalMetas() {
	var name;
	var content;
	$("body").find("input[tag=meta]").each(function(i) {
		name = $(this).attr("name");
		content = $(this).attr("content");
		if($('head meta[name='+name+']').length) {
			if ($('head meta[name="'+name+'"]').attr("content") == content) {
			} else {
				content = $('head meta[name="'+name+'"]').attr("content") + " " + content;
			}
			$('head meta[name="'+name+'"]').attr("content", content);
		} else {
			$("head title").before('<meta content="'+content+'" name="'+name+'" />');
		}
		$(this).remove();
	})
}

// BreadCrump - Manipulation
function manipulateBC() {
	if($('#outer').hasClass('searching')) {
		var text = $('#breadcrumb a:last').text();
		if (text!='Startseite') {
			$('#breadcrumb a:last').before('<strong>Suchergebnis</strong>');
			$('#breadcrumb a:last').remove();
		}
	}
}

// Textausschnitt

function cuttingText(div, template) {
	var template = template;
	var div = div;
	
	if(template=='article') {
			$('div.' + div).each(function() {
				var title = $(this).find('h1').html();
				var description = $(this).find('h4').html();
				var linking = $(this).find('a').attr('href');
                
				if (description == '' || description == null) {
					description = ' ';
				}
				if (title == '' || title == null) {
					title = ' ';
				}
				
				if ($(this).parent().find('img').length > 0) {
					$(this).parent().find('img').after('<h4><a href="'+linking+'">'+title+'</a></h4><p>'+description+'</p>');
				} else {
					if (div!='tmpTeaser') {
					description = '<p>' + description + '</p>';
					}
					$(this).parent().prepend('<h4><a href="'+linking+'">'+title+'</a></h4>'+description);
                    
				}
				if(linking!='' && linking!=null) {
					$(this).parent().find('p a').attr('href', linking)
					$(this).find('a.link').remove();
				}
				$(this).remove();
			})
	}
	if(template=='startseite') {
		if(div=='newsContent') {
			$('div.' + div).each(function() {
				var h1 = $(this).find('h1').html();
				var h4 = $(this).find('h4').html();
				$(this).find('p').remove;
				$(this).prepend('<p><strong>'+h1+'</strong></p><p>'+h4+'</p>');
				if ($(this).find('p').length>2) {
					var i = 0;
					$(this).find('p').each(function() {
						i++;
						if(i>2) {
							$(this).remove();
						}
					})
				}
				$(this).find('h1, h4').remove();
				$(this).addClass('article').removeClass('newsContent');
			})
		} else {
			
		}
	}
}

// Bildbox mit Unterschrift

function imageLabelBox() {
	$('div.Article p img.unterschrift').each(function() {
		var imageWidth = $(this).width();
		if (imageWidth>=400){
			imageWidth=400;
		} 
		$(this).wrap('<div class="box'+imageWidth+'"><p></p></div>"');
		var text = $(this).attr('alt');
		$(this).parent().after('<p>'+text+'</p>');
	})
}
function imageBigPosition() {
	var bigImg = $('div.Article div.teaserImageArticle').html();
	$('div.Article div.teaserImageArticle').remove();
	if(bigImg!=null && bigImg!='null' && bigImg!='') {
		$('div.Article div.teaser:first').before('<div class="teaserImageArticle">'+bigImg+'</div>');
	}
}
function eventJS() {
	if ($('#event_datelocation').hasClass('datepicker')) {
		$('#event_datelocation').removeClass('datepicker hasDatepicker');
	}
	
	$('a.eventMore').click(function() {
		if($(this).parent().find('.moreContainer').hasClass('showEvent')) {
			$(this).parent().find('.moreContainer').hide();
			$(this).parent().find('.moreContainer').removeClass('showEvent');
			$(this).text('mehr');
		} else {
			$(this).parent().find('.moreContainer').show();
			$(this).parent().find('.moreContainer').addClass('showEvent');
			$(this).text('ausblenden');
		}
		return false;
	})
	
}

function formular(a, b) {
	$('a[title="Reload image"] img.captcha').parent().addClass('captchaLink');
	$('.Component.Article .newsContent').each(function() {
		var oldContent = $(this).html();
		var h1 = '<h2>'+$(this).find('h1').html()+'</h2>';
		if(h1=='<h2>null</h2>'){h1=' '};
		$(this).find('h1').remove();
		var h2 = '<h3>'+$(this).find('h2').html()+'</h3>';
		if(h2=='<h3>null</h3>'){h2=' '};
		$(this).find('h2').remove();
		var h3 = '<h4>'+$(this).find('h3').html()+'</h4>';
		if(h3=='<h4>null</h4>'){h3=' '};
		$(this).find('h3').remove();
		var h4 = '<p>'+$(this).find('h4').html()+'</p>';
		if(h4=='<p>null</p>'){h4=' '};
		$(this).find('h4').remove();
		var newContent = '<div class="teaser">'+h1+h2+h3+h4+'</div>';
		
		$(this).parent().find('.newsContent').before(newContent);
	})
	titel = a;
	datum = b;
	if(typeof(titel)!='undefined'){
		$('.FormBuilderAm #Anmeldung_Seminare_titel_seminar').val(titel);
	}
	if(typeof(datum)!='undefined'){
		$('.FormBuilderAm #Anmeldung_Seminare_date_seminar').val(datum);
	}
	return false;
}

function medienhausAnfahrt() {
	var address ="Rechneigrabenstra&#223;e 10 D-60311 Frankfurt";
	var map = null;

	if(GBrowserIsCompatible()){
		map = new GMap2(document.getElementById("map_canvas"));
		map.setMapType(G_NORMAL_MAP);
		map.setCenter(new GLatLng(50.11207484, 8.69167686), 16);
		map.addControl(new GMenuMapTypeControl());
		map.addControl(new GSmallZoomControl3D());
		map.enableScrollWheelZoom();
		
		var marker = new GMarker(new GLatLng(50.11132488, 8.69167686));
		var html = "Gemeinn&#252;tzige MEDIENHAUS GmbH<br \/>Rechneigrabenstra&#223;e 10, 60311 Frankfurt<br><br><a href=\"http://maps.google.de/maps?f=q&source=s_q&hl=de&q=Rechneigrabenstra%C3%9Fe+10,+Innenstadt+60311+Frankfurt+am+Main,+Frankfurt+am+Main,+Hessen&sll=51.151786,10.415039&sspn=15.571663,34.628906&ie=UTF8&cd=1&geocode=FXOj_AId4p-EAA&split=0&ll=50.11132488,8.69167686&spn=0.015522,0.033817&z=15&iwloc=A&iwstate1=dir\" target=\"_blank\">Anfahrt berechnen<\/a>";
		
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml(html);
		});
		
		map.addOverlay(marker);
		marker.openInfoWindowHtml(html);
	}
}

function checkAll() {
	$('a.checkAll').click(function() {
		var text = $(this).text();
		if(text=='Alle') {
			$(this).parent().find('input[type="checkbox"]').each(function() {
				$(this).attr('checked', true);
				$(this).parent().find('a.checkAll').text('Keine');
			})
		} else {
			$(this).parent().find('input[type="checkbox"]').each(function() {
				$(this).attr('checked', false);
				$(this).parent().find('a.checkAll').text('Alle');
			})
		}
		return false;
	})
}
