$(document).ready(function() {
	loadCompetenceAccordion();
	$('.quickstart').click(function() {
		$('#quickstart').toggleClass('active');
	});
	//setMenuHover();
	initAnsprechpartner();
	clearInputOnClick();
	enableAnchorFormSubmit();
	initPeoleBoard();
	initModalView();
	initHeaderTeaser();
	initDownload();
	setBlurImage();
	
	makeSameColHeight();
	
	/* merged code from cal.js - originally also in documend ready - more code see above */
	
	initSlideTo();
	
	initNiederlassungen();
	
	$(".menu_quickstart .menuparent > a").attr("href", "JavaScript:void(0);");
	$(".menu_quickstart .menuparent > a").attr("style", "color:#000;cursor:default;");
	
});


function makeSameColHeight() {
	var left = $('.realcontent .left');
	var right = $('.realcontent .right');
	
	left.height('auto');
	right.height('auto');
	
	left.find('.csc-default-wrapper:last-child .csc-default-shadow-content').height('auto');
	right.find('.csc-default-wrapper:last-child .csc-default-shadow-content').height('auto');
	
	var smallerCol;
	if(left.height() < right.height()) {
		smallerCol = left;
	} else {
		smallerCol = right;
	}
	
	var difference = Math.abs(left.height() -right.height());
	var elementToExpand = smallerCol.find('.csc-default-wrapper:last-child .csc-default-shadow-content');
	var expandedHeight = elementToExpand.height() + difference;
	
	elementToExpand.height(expandedHeight+"px");
	
	// optional insert new blank box
	//var box = $('<div class="csc-default-wrapper"><div class="csc-default-shadow-top"></div><div class="csc-default-shadow-content"></div><div class="csc-default-shadow-bottom"></div></div>');
	//box.find('.csc-default-shadow-content').height("40px");
	//right.append(box);	
}

function setMenuHover() {
	$('.menu_main li').hover(
		function() {
			$('.menu_main li').removeClass('hover');
			$(this).addClass('hover');
		},
		function() {
			$(this).delay(200).removeClass('hover');
		}
	);
	$('.menu_main li ul').hover(
		function() {
			$(this).parent().addClass('hover');
		},
		function() {
			$(this).delay(200).removeClass('hover');
		}
	);
}

function initHeaderTeaser() {
	var teaser_content = $('.header_teaser_left');
	var height_hidden = teaser_content.height();
	// ohne if erzeugt jquery fehler "a is undefined"
	if(teaser_content[0]) {
		$.data(teaser_content[0], "curHeight", height_hidden);
	}
	
	var btn_show = $('.btn_show_teaser');
	btn_show.click(function() {
		toggleHeaderTeaser();
	});
	var bg_default = $('.bg_header_teaser');
}

var isVisible = false;

function toggleHeaderTeaser() {
	var img_bg = $('.head .bg_header_teaser');
	var img_active = $('.head .bg_header_teaser_active');
	var teaser_text = $('.head .bodytext');
	var teaser_content = $('.header_teaser_left');
	
	var height_visible = "280px";
	var height_hidden = $.data(teaser_content[0], "curHeight");
		
	img_bg.fadeToggle(200);
	img_active.fadeToggle(200);
	teaser_text.fadeToggle(200);
	
	if(!isVisible) {
		teaser_content.animate(
			{ height: height_visible},
			200
		);
		isVisible = true;
	} else {
		teaser_content.animate(
			{ height: height_hidden},
			200
		);
		isVisible = false;
	}
}

function clearInputOnClick() {
	$('input[type="text"].clear_form_on_click').each(function() {
		var initText = $(this).val();
		$.data(this,"initText", initText);
	});
	$('input[type="text"].clear_form_on_click').focus(function() {
		var currText = $(this).val();
		var initText = $.data(this,"initText", initText);
		if(currText == initText) {
			$(this).val("");
		}
	});
	$('input[type="text"].clear_form_on_click').focusout(function(){
		var currText = $(this).val();
		var initText = $.data(this,"initText", initText);
		if(currText == "") {
			$(this).val(initText);
		}
	}); 
}


function enableAnchorFormSubmit() {
	$('.submit').click(function(){
		$(this).parents("form").submit();
	});
}


function initPeoleBoard() {
	var showButtons = $('.ppboard_links a');
	showButtons.click(function() {
		var personId = $(this).attr("id");
		var personSelector = "#user" + personId;
		togglePerson(personSelector);
	});
	
	var closeButtons = $('.pp_closebutton a');
	closeButtons.click(function() {
		var personId = $(this).parents('.single_person').attr('id');
		var personSelector = "#"+personId;
		togglePerson(personSelector);
	});
	
	$(".ppboard_links a").hover(
  		function () {
    		$(this).find("img").attr("src", "fileadmin/templates/avw/images/ppboard_pfeil_rollover.png");
  		},
  		function () {
    		$(this).find("img").attr("src", "fileadmin/templates/avw/images/ppboard_pfeil.png");
  		}
	);
}

function togglePerson(personSelector) {
	var person = $(personSelector);
	var unblurredImage = $(".pb_big_image.unblurred");
	var blurredImage = $(".pb_big_image.blurred");
	var linkButtons = $('.ppboard_links');
	var pbLink = $('.ppboard_image');
	
	pbLink.toggle();
	linkButtons.toggle();
	unblurredImage.toggle();
	blurredImage.toggle();
	person.toggle();
	
	$(".headlines_wrapper").toggle();
	
}

function initModalView() {
	// alle links href entfernen und dafür modal view öffnen
	var modalLinks = $('.internal-link-button-modal');
	modalLinks.each(function() {
		var hrefAttr = $(this).attr("href");
		$(this).attr("href",'javascript:void(0);');
		$.data(this,"modalLink", hrefAttr);
	});
	
	modalLinks.click(function() {
		var url = $.data(this,"modalLink");
		toggleViewModal();
		showLoading();
		openInModalView(url);
	});
	
	$('#modal_view_close').click(function() {
		toggleViewModal();
	});
	
}

function toggleViewModal() {
	$('#modal_view').toggle();
}

function showLoading() {
	var loadingWheel = $('<img style="display:block; margin:0 auto;" src="fileadmin/templates/avw/images/loading_wheel.gif" alt="laoding"/>');
	$('#modal_view_content').html(loadingWheel);
}


function openInModalView(url) {
	$('#modal_view_content').load(url+' #content_raw', function() {
		makeLinksToModalLinks();
		modalViewLoaded();
	});
}

function makeLinksToModalLinks() {
	var modalLinks = $('#modal_view_content .modal_link a');
	modalLinks.each(function() {
		var hrefAttr = $(this).attr("href");
		$(this).attr("href",'javascript:void(0);');
		$.data(this,"modalLink", hrefAttr);
	});
	
	modalLinks.click(function() {
		var url = $.data(this,"modalLink");
		showLoading();
		openInModalView(url);
	});
}



$(function() {

	if($(".csc-firstHeader").text() == "Kalender") {
		$(".csc-default-wrapper").hide();
	
		toggleViewModal();
		showLoading();
		openInModalView("/?id=87");
	}
	
	
});



/*Ansprechpartner */

function initAnsprechpartner() {
	$('#ansprechpartnerForm').change(function() {
		var el = $(".tx-avwansprechpartner-pi1 .opt:selected").parent().attr("name");
		$('.'+el).attr('name', 'tx_wtdirectory_pi1[show]');
		document.forms['ansprechpartnerForm'].submit( );
	});
}

/*Downloadbereich nicht modal */
function initDownload(){
	$("div.cat_label").click(function () {
			// reset dass cols gleich hoch sind
			// hier notwendig wegen der slidefunktion
			// kontent wird laenger und kuerzer
			var left = $('.realcontent .left');
			var right = $('.realcontent .right');
			
			left.height('auto');
			right.height('auto');
			
			left.find('.csc-default-wrapper:last-child .csc-default-shadow-content').height('auto');
			right.find('.csc-default-wrapper:last-child .csc-default-shadow-content').height('auto');
			
			
    	if ($(this).parent().parent().children(".tx_abdownloads_treeView_downloads").is(":hidden")) {
      		$(this).parent().parent().children(".tx_abdownloads_treeView_downloads").slideDown("fast",function() {  });
    	} else {
      		$(this).parent().parent().children(".tx_abdownloads_treeView_downloads").slideUp("fast",function() {  });
    	}
  	});
  $('.tx_abdownloads_treeView_downloads:odd').addClass('odd');
}


function setFontSize(size) {
	var fontSize = size + "px";
	var lineHeight = (size * 1.4) +"px";
 	$('.csc-default p').css("font-size",fontSize).css("line-height",lineHeight);
}


function setBlurImage() {
	if($('.head').children().length == 0) {
		$('.head').height("85px");
		$('#content').css("background","url('./fileadmin/templates/avw/images/bg_head_teaser.jpg') 0 49px no-repeat");
	}
//	./fileadmin/templates/avw/images/bg_head_teaser.jpg
}



/* ======================================================== */
/* ========= MERGED SKRIPT - original cal.js ============== */
/* ======================================================== */

function initSlideTo() {
	$('.internal-link-button-modal').slideto({
		target : '#modal_view',
		speed  : 500
	});
}


function modalViewLoaded(){
	$("div.cat_label").click(function () {
  	
    	if ($(this).parent().parent().children(".tx_abdownloads_treeView_downloads").is(":hidden")) {
      		$(this).parent().parent().children(".tx_abdownloads_treeView_downloads").show("slow");
    	} else {
      		$(this).parent().parent().children(".tx_abdownloads_treeView_downloads").slideUp();
    	}
  	});
	
}


var cal_actualOverlay = null;
function cal_showDetails(td) {
	var infobox = $($(td)[0]).children()[1];
	
	cal_actualOverlay = infobox;
	
	$(document).bind('mouseup.cal_overlay', cal_hideDetails);

	imgwrapper = $($(infobox).children()[1]).children()[3];
	img = $($($($($(imgwrapper).children()[0]).children()[0]).children()[0]).children()[0]).children()[0];
	$(imgwrapper).html(img);


	$(infobox).css("margin-top", "-" + ($(infobox).height()/2)-($(td).height())-10 + "px");


	$(infobox).show();
}

function cal_hideDetails(e) {
	$(cal_actualOverlay).hide();
	$(document).unbind('mouseup.cal_overlay');
}

function loadCompetenceAccordion() {
	textlength = 150;
	
	if($(".csc-firstHeader").text() != "Unsere Kompetenzen") {
		return false;
	}
	
	competencies = $("p.bodytext");
	for(i=0; i< competencies.length; i++) {
		all = $(competencies[i]).html();
		if(all.length > 10) {
			anleser = all.substr(0, textlength);
			rest =  all.substr(textlength, all.length - textlength);
			
			offset = rest.indexOf(".") + 1;
			
			anleser = all.substr(0, textlength + offset);
			rest =  all.substr(textlength + offset, all.length - textlength + offset);
			
			$(competencies[i]).html(anleser + " <a href='JavaScript:showMoreInfo(" + i + ");' id='moreinfo_link_" + i + "' style='color: #4C8F00; font-weight: bold; text-decoration: underline; font-style: italic;'>mehr ...</a><span id='moreinfo_" + i + "' style='display: none'>" + rest + "</span>");
		}
	}
}

function showMoreInfo(no) {
	$("#moreinfo_" + no).show();
	$("#moreinfo_link_" + no).hide();
}





function initNiederlassungen() {
	
	$(".citys a, .map_coordinates a").click(function(e) {
			var rel = $(this).attr("rel");
			var contactId = $('.citys > a[rel="' + rel + '"]')[0].id;
			var url = "/kontakt/ansprechpartner/?plz=&tx_wtdirectory_pi1[show]=";
	
			$('#contact_detail').load(url + contactId + ' .tx-wtdirectory-pi1 .clearfix', function() {
				// wichtig, da cols gleich hoch sein sollen
				makeSameColHeight();
			});
			
	});
	
	$(".map_coordinates a, .citys a").slideto({
				target : '.left',
				speed  : 500
			});
	

	
	$(".map_coordinates a, .citys a").hover(function(e) { 
			var rel = $(this).attr("rel");
			$('a[rel="' + rel + '"]').toggleClass("activeMapItem");
	});
}



