// Creating Global Namespace for EBM
// requires jQuery 1.3.2.

// Creating namespaces
var EBM = {}

$.browser.msie6 = $.browser.msie && ($.browser.version && ($.browser.version < 7) || /6.0/.test(navigator.userAgent)); 
$.browser.msie7 = $.browser.msie && ($.browser.version && ($.browser.version < 8 && $.browser.version > 6) || /7.0/.test(navigator.userAgent)); 

// Starting point
$(document).ready( function() {
	$("noscript").css("display", "none");
	EBM.inputSearchReplace();
	var folgeseitenImage = $("body").attr('class');
	//$('#backgroundImage').append('<img src="/fileadmin/template/main/ebm09/img/emotion-'+folgeseitenImage+'.jpg" width="1024" height="658" alt="" border="0" class="bgmaximage" id="bgmaximageId0" />');
	$('#backgroundImage').append('<img src="/fileadmin/template/main/ebm09/img/lernwelt/emotion-'+folgeseitenImage+'.jpg" width="1024" height="658" alt="" border="0" class="bgmaximage" id="bgmaximageId0" />');
	
	EBM.styleChange.changeIt();
	EBM.setTransparent();
	EBM.doNewsLinking();
	EBM.subNavRollover();
	EBM.doResizeAll();
	EBM.styleChange.initInterval();
	EBM.removeEmptyRow();

	$(".jm-gallery-centered-links a").click(function(){	this.target = "_blank";	});
	
	

$("#gesuchebmenergiefonds").submit(function()
{
	if (!isCheckedByClass("projekt-auswahl"))
	{
		alert ("Bitte wählen Sie mindestens 1 Projekt aus");
		return false;
	}				
	else
	{
		return true; //submit the form
	}				
});
function isCheckedByClass(id)
{
	var checked = $("input[@class^="+id+"]:checked").length;
	if (checked == 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}
		
});

//show/hide default search text
EBM.inputSearchReplace = function() {
 	var z = jQuery("#inpsearch").attr("value");
	var inpsearchtext = jQuery("#inpsearch").attr("value");
	jQuery("#inpsearch").focus(function() {
		if (inpsearchtext == z) {
			jQuery(this).attr('value', '');
		} else {
			jQuery(this).attr('value', inpsearchtext);
		}
		jQuery(this).attr("value","");
	});
	jQuery("#inpsearch").blur(function() {
		jQuery(this).attr("value", inpsearchtext);
	});
	jQuery("#inpsearch").keyup(function() {
		inpsearchtext = jQuery(this).attr('value');
	});

	//show/hide default text on search result
	jQuery("#mainsearch").focus(function() {
		jQuery(this).attr("value", "");
		jQuery(this).addClass("highlighttext");
	});
	jQuery("#mainsearch").blur(function() {
		jQuery(this).removeClass("highlighttext");
	});
	
	// external link with icons
	$("#metaNav ul li a[href^='http://']").addClass("external");
}

EBM.doResizeAll = function() {
	var bgImgDiv = $("#backgroundImage");
	var bgImage = $(".bgmaximage");
	var origPicWidth = '1024';
	var origPicHeight ='658';
	var resizeSize = 300;
	var actDocHeight = $(document).height();
	var actDocWidth = $(document).width();
	
	// find ratio
	var bgPicRatio = origPicWidth/origPicHeight;
	bgPicRatio = bgPicRatio.toFixed(3);
	//console.log(ratio);
	
	bgImgDiv.height(resizeSize);
	bgImgDiv.width(resizeSize);
	if (!$.browser.msie6) {
		$("#wrapper, #contentWrapper, #panelRight").height(resizeSize+50);	/*+50 wurde dazugezählt weil IE einen komischen rand unten macht*/
	}else{
		$("#wrapper, #panelRight").height(resizeSize);
	}

	bgImage.height(actDocHeight);
	bgImage.width(actDocHeight*bgPicRatio);
	//console.log(bgImage.width()+"****"+bgImgDiv.width());

	window.setTimeout(function(){
		var actDocHeight = $(document).height();
		var actDocWidth = $(document).width();
		
		if($.browser.msie && !$.browser.msie7){
			actDocHeight = actDocHeight-20
			actDocWidth = actDocWidth-20
		}
		bgImgDiv.height(actDocHeight);
		bgImgDiv.width(actDocWidth);
		
		$("#contentWrapper, #panelRight").height(actDocHeight-178);
		
		$("#home #contentWrapper, #home #panelRight").height(actDocHeight-147);
		
		
		if (bgImage.width() < bgImgDiv.width()) { 
			//wenn bild schmaler als div dann nehme die breite als referenz
			bgImage.width(actDocWidth);
			bgImage.height(actDocWidth/bgPicRatio);
		
		}else if  (bgImage.height() < bgImgDiv.height()){
			bgImage.height(actDocHeight);
			bgImage.width(actDocHeight*bgPicRatio);
			//console.log(actDocWidth+"***"+actDocWidth/bgPicRatio)	
		}
		
	}, 0);	
}

function doSomethingOnResize() {
    EBM.doResizeAll();
};

function changeLogin() {
if($("#loginabo").css("display") != "none") {
	$("#loginabo").css("display", "none");
	$("#loginwebmail").css("display", "block");
}else{
	$("#loginabo").css("display", "block");
	$("#loginwebmail").css("display", "none");
}
}
var resizeTimer = null;
$(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(doSomethingOnResize, 50);
});
// ie6 }

EBM.removeEmptyRow = function() {
	$("table#FondsformProject tr").each(function() {
      if($(this).html().contains("<td><p></p></td>")) {
		$(this).addClass("hidden");
	  };
    });
}

EBM.doNewsLinking = function() {
	//count news li elements
	var countNews = $(".newsbox > li").size();
	var heightNews = 94;
	newsWrapperHeight = countNews*heightNews+129
	$("#contentWrapper").css("max-height", 'none');
	$("#contentWrapper").css("min-height", newsWrapperHeight);
	
	$(".newsbox li").css("opacity", 0.87);
	$(".newsbox li").css("cursor", "pointer");
	$(".newsbox li.nrm").click(function () { window.location =  $(this).find("a").attr("href")});
	$(".newsbox li").hover(
		function(){ $(this).animate( { backgroundColor:"#000000",opacity: "0.87",opacity: "0.87",color: "#fff" } , 200)
					$(this).find("a").animate( {color: "#fff" } , 180)},
		function(){ $(this).animate( { backgroundColor:"#ffffff",opacity: "0.87",opacity: "0.87",color: "#000" } , 300)
					$(this).find("a").animate( {color: "#000" } , 180)}
	);
}

// Setzt den Hintergrund der Elemente transparent
// 80%
EBM.setTransparent = function() {
	$("#logo,#bereich,#folg1e #panelRight,#breadCrumb").addClass("transparent87"); //#subNav,#home #quicklinks, #mainNav ul ul,
	//$("#bereichNav").addClass("transparent50");
	$("#bereichNav ul li ul").addClass("transparent95");
}

EBM.subNavRollover = function() {
	$("#mainNav ul li").hover(
		function(){ 
			$(this).addClass("rollover");},
		function(){ 
			$(this).removeClass("rollover");}
	);
}

EBM.styleChange = {
	props:  {
		IntervalTime: 11000,
		classArray: ['werkstatt','information','fuehrungen'],
		classInt: -1,
		bgImageArray: {
			werkstatt: '/fileadmin/template/main/ebm09/img/lernwelt/emotion-werkstatt.jpg',
			information: '/fileadmin/template/main/ebm09/img/lernwelt/emotion-information.jpg',
			fuehrungen: '/fileadmin/template/main/ebm09/img/lernwelt/emotion-fuehrungen.jpg'
		},
		preLoadArray: {}
	},
	initInterval: function() {
		// Start Interval
		EBM.styleChange.colorInterval = window.setInterval('EBM.styleChange.changeIt()', EBM.styleChange.props.IntervalTime);
		
		// Preload Images
		for(var i in EBM.styleChange.props.bgImageArray) {
			var preLoadImg = new Image();
			preLoadImg.src = EBM.styleChange.props.bgImageArray[i];
			EBM.styleChange.props.preLoadArray[i] = preLoadImg;
		}
		// BereichNavi Rollover
		$("#bereichNav ul li.werkstatt, #bereichNav ul li.information, #bereichNav ul li.fuehrungen").hover(
			function(){
				// Clear Interval
				window.clearInterval(EBM.styleChange.colorInterval);
				EBM.styleChange.changeIt($(this).attr('class'));
				$(this).addClass("rollover");
				$(this).children("h3").addClass("rollover"); },
			function(){ 
				// Start Interval
				EBM.styleChange.colorInterval = window.setInterval('EBM.styleChange.changeIt()', EBM.styleChange.props.IntervalTime);
				$(this).removeClass("rollover");
				$(this).children("h3").removeClass("rollover"); }
		);
	},
	changeIt: function(newClass) {
		var aktBgmaximageId = EBM.styleChange.props.classInt;
		EBM.styleChange.props.classInt = EBM.styleChange.props.classInt==2?0:EBM.styleChange.props.classInt+1; // Set Class-Integer +1 or 0
		var changeClass = (newClass=='' || newClass==undefined)?EBM.styleChange.props.classArray[EBM.styleChange.props.classInt]:newClass; // Define changeClass to next Class or "newClass"
		var emotionImgSrc = EBM.styleChange.props.bgImageArray[changeClass]; 
		$('body#home').removeClass($(this).attr('class'));
		$('body#home').addClass(changeClass);
		
		//console.log();
		var actImageInlinestyle = $('#home img#bgmaximageId'+aktBgmaximageId).attr("style");
		$('#home img#bgmaximageId'+aktBgmaximageId).attr("style", actImageInlinestyle+";z-index: 10;");
		$('#home #backgroundImage').append("<img class='bgmaximage' id='bgmaximageId"+EBM.styleChange.props.classInt+"' src='"+emotionImgSrc+"' style='z-index: 9;"+actImageInlinestyle+"'>");
		$('#home img#bgmaximageId'+aktBgmaximageId).fadeOut("slow",function () {$(this).remove();});
	}
}

