﻿/**
* Adds the given function as an onload-event 
* credits to Simon Willison
* http://simonwillison.net/2004/May/26/addLoadEvent/
*/
function addLoadEvent(func){	
	var oldonload=window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}
	else{
		window.onload=function(){
			if (oldonload) {
        		oldonload();
     		 }
			func();
		}
	}
}

//To open links with rel=external in new window. See method Render in /templates/MasterPages/MasterPage.master.cs
addLoadEvent(externalLinks);
function externalLinks() {
 	if (!document.getElementsByTagName) return;
 	var anchors = document.links;
 	for (var i=0; i<anchors.length; i++) 
	{
   		var anchor = anchors[i]; 
   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
     		anchor.target = "_blank"; 
 	}	 
}

function redirect(url)
{ window.location = url; }

function goback()
{ window.history.back(); }

$(function() {
    // Startpage calendar/news 
    $('#aktuelltH2').click(function() {

        $('#aktuelltHeadingDiv').removeClass('aktuelltGrayed').addClass('aktuelltMarked');
        $('#kalenderHeadingDiv').removeClass('kalenderMarked').addClass('kalenderGrayed');

        $('#puff475x240Right').toggle(true);
        $('#puff475x240RightKalender').toggle(false);
    });
    $('#kalenderH2').click(function() {

        $('#aktuelltHeadingDiv').removeClass('aktuelltMarked').addClass('aktuelltGrayed');
        $('#kalenderHeadingDiv').removeClass('kalenderGrayed').addClass('kalenderMarked');
        $('#puff475x240RightKalender').toggle(true);
        $('#puff475x240Right').toggle(false);
    });

    //contentFooter
    $('#contentFooterShareLi').click(function() {
        $('#contentFooterShareDiv').toggle("fast");
    });

    //calendar
    $('.addMoreText').click(function() {
        $(this).parent('.calendarshort').toggle(false);
        $(this).parent().siblings('.calendarfull').toggle(true);
    });

    $('.closeMoreText').click(function() {
        $(this).parent('.calendarfull').toggle(false);
        $(this).parent().siblings('.calendarshort').toggle(true);
    });

    if ($("#f-categories").length) {
		$(".f-category-content").hide();
		$(".f-category-header").removeClass("open");
		
		
		if ($(".f-category").length) {
			// Restore subject state from cookie
			var cookiePrefix = "forumState_";
			
			$(".f-category-header .f-category-header-subject").each(function() {
				var cookie = getCookie(cookiePrefix + $(this).closest(".f-category-header").attr("id"));

				if (cookie != null && cookie.indexOf("open") > -1 && $(this).closest(".f-category-header").attr("class").indexOf("open") == -1) {
					$(this).closest(".f-category-header").addClass("open");
					$(this).parent().parent().find(".f-category-content").slideDown(400);
				}
			});
		}
					
		$(".f-category-header .f-category-header-subject").click(function() {
			if ($(this).parent().parent().find(".f-category-content").css("display") == "block") {
				$(this).parent().removeClass("open")
				$(this).parent().parent().find(".f-category-content").slideUp(400);
			} else {
				$(this).parent().addClass("open")
				$(this).parent().parent().find(".f-category-content").slideDown(400);
			}
			persint_SaveForumStateAsCookie();			
			return false;
		});
    }

    //Search. Search field top
    var clearMePrevious = '';

    // clear input on focus
    $(".txtTopSearch").focus(function() {
        clearMePrevious = $(this).val();
        $(this).val('');
    });

    // if field is empty afterward, add text again
    $('.txtTopSearch').blur(function() {
        if ($(this).val() == '') {
            $(this).val(clearMePrevious);
        }
    });
});


// faq 
$(function() {
    if ($(".faq").length) {
        $(".faq dd").hide();

        $(".faq dt").hover(
			function() {
			    $(this).addClass("hover");
			},
			function() {
			    $(this).removeClass("hover");
			}
		);

        $(".faq dt").toggle(
			function() {
			    $(this).next("dd").slideDown(300);
			    $(this).addClass("open");
			},
			function() {
			    $(this).next("dd").slideUp(300);
			    $(this).removeClass("open");
			}
		);
    }
});

// captcha 
$(function() {
    if ($("#captchahelptext").length) {
        $("#captchahelptext").hide();
        $("#captcha-help").click(function() {
            if ($("#captchahelptext").css("display") == "none") {
                $("#captchahelptext").slideDown(300);
                return false;
            } else {
                $("#captchahelptext").slideUp(300);
                return false;
            }
        });
    }
});

// Search options
$(function() {
    if ($("#ess-optionswrap").length) {
        $("#ess-optionswrap").hide();

        $("#advancedSearchLink").toggle(
			function() {
			    $("#advancedSearchLink").addClass("advancedSearchLinkOpen");
			    $("#ess-optionswrap").slideDown(300);
			},
			function() {
			    $("#advancedSearchLink").removeClass("advancedSearchLinkOpen");
			    $("#ess-optionswrap").slideUp(300);
			}
		);
    }
});


//Forum setings
$(function() {
    if ($(".f-category").length) {
		// Restore subject state from cookie
		var cookiePrefix = "forumState_";
			
		$(".f-category-header span").each(function() {
			var cookie = getCookie(cookiePrefix + $(this).closest(".f-category-header").attr("id"));
			if (cookie != null && cookie.indexOf("open") > -1 && $(this).closest(".f-category-header").attr("class").indexOf("open") == -1) {
				$(this).closest(".f-category-header").addClass("open");
				$(this).parent().parent().find(".f-category-content").slideDown(300);
			}				
		});
	}
});

function setCookie( name, value, expire, path, domain, secure ) {
    var today = new Date();
    // if expire variable  is set, set the expire time correctly (in days instead of ms)
    if (expire) expire = expire * 1000 * 60 * 60 * 24;
    
    var expireDate = new Date( today.getTime() + expire);
    
    document.cookie = name + "=" + escape(value) + 
        ((expire)?";expires=" + expireDate.toGMTString() : "") +
        ((path)?";path=" + path : "") +
        ((domain)?";domain=" + domain : "") +
        ((secure)?";secure=" + secure : "");
}

function persint_SaveForumStateAsCookie() {	
	var cookiePrefix = "forumState_";
	$(".f-category-header").each(function() {
		setCookie(  cookiePrefix + $(this).closest(".f-category-header").attr("id"), // name
					$(this).closest(".f-category-header").attr("class"), // value
					365, // expire (in days)
					"/" // path
		);
	});
}
		
function getCookie( name ) {
    if (!document.cookie) return null;
    if (document.cookie.indexOf(name) == -1) return null;
    var allCookies = document.cookie.split(';');
    var tempCookie;
   
    for (var i=0;i < allCookies.length;i++) {
       tempCookie = allCookies[i].split("=");
       
       if (tempCookie[0].replace(/^\s+|\s+$/g, "") == name.replace(/^\s+|\s+$/g, ""))
           return (tempCookie[1] == null)?null:unescape(tempCookie[1]);            
   }
}
