//$("link[title='scriptstyle']").attr("rel","stylesheet")
//$("link[title='scriptstyle']").disabled = false;
$("head").append('<link rel="Stylesheet" type="text/css" href="/UI/Styles/script.css" />')
$(function(){
	boxFix();
    extWindows();
    expandableBox();
    expandableList();
    globalSites();
    printMe();
    popAForm();
    pollResult()
});

// boxFix() fixes heights on boxes that need to be fixed ;)

function boxFix() {
    $("div.footer div.members").height(($("div.footer").height()) + "px");
    $(".docList a[href$='.pdf']").addClass("pdf");

    equalHeight($(".siteMap > ul > li"))

    $(".quoteBubble a").each(function(){
        theLink = $(this).attr("href")
        $(this).parents(".quoteBubble").css("cursor","pointer");
        $(this).parents(".quoteBubble").click(function(){
            window.location.href = theLink;
        });
    });
}


// globalSites() showing/hiding global sites
function expandableBox() {
    $(".expandableBox .trigger").click(function(){
        $(this).parents(".expandableBox").find(".text").slideToggle();
        $(this).toggleClass("active");
    })
}
function expandableList() {
    $(".expandableList .trigger").click(function(){
        $(this).parents("li").find(".short").toggle();
        $(this).parents("li").find(".full").toggle();
        $(this).toggleClass("active");
    })
}
// globalSites() showing/hiding global sites
function globalSites() {
 
    $(".globalSites h3").click(function(){
        $(".globalSites ul").slideToggle();
        $(".globalSites h3").toggleClass("active");
    })
}


// extWindows() makes links open in a different window. use rel="external" instead of target="_blank" and rel="popup" or rel="popup|[width]|[height]" for popups

function extWindows(){
	$("a[rel=external], area[rel=external]").attr("target","_blank");
	$("a[rel^=popup], area[rel^=popup]").click(function(){
		theHref = $(this).attr("href");
		var popupWidth, popupHeight
		var relSplit = $(this).attr("rel").split("|");
		if(relSplit[1]){
			popupWidth = relSplit[1];
			popupHeight = relSplit[2];
		} else {
			popupWidth = 560;
			popupHeight = 620;
		}
		popMeUp(theHref,popupWidth,popupHeight)
		return false;
	});
}

function popMeUp(strURL, strWidth, strHeight) {
    theWin = window.open(strURL, "popupWin", "scrollbars,resizable,height=" + strHeight + ",width=" + strWidth);
    theWin.focus();
}
function printMe(){
	if(window.print) {
		$(".printMe").html('<span><img src="/UI/Images/icon.print.gif" /> Skriv ut</span>');
		$(".printMe").addClass("printButton");
		$(".printMe").click(function(){
			window.print();
		});
		$(".printMe").hover(function () {
            $(this).addClass("mjHover");
        }, function () {
            $(this).removeClass("mjHover");
        });
	}
}
// popAForm() makes forms popup in the middle of the page use rel=popform|small/medium/large
function popAForm() {
    $("a[rel*='popform']").click(function(e){
        e.preventDefault();
        scrollTop = $(window).scrollTop();
        var theRel = $(this).attr("rel")
        if((/small/).test(theRel)){
            theformClass = "popupSmall";
        } else if((/large/).test(theRel)){
            theformClass = "popupLarge";
        } else {
            theformClass = "popupMedium";
        }
        
        theHref = $(this).attr("href");
        $("body").append('<div class="popupBg"></div>');
        $("body").append('<form action="' + theHref + '" method="post" class="' + theformClass + '"><div class="popupTop"></div><div class="popupBody"></div><img src="/UI/Images/button.popup.close.gif" class="popupClose"/><div class="popupBottom"></div></form>');
        theLeft = (($(window).width() / 2) - ($("." + theformClass).width() / 2)) + "px";
        theTop = ($("." + theformClass).height()>($(window).height()-20))?(10 + scrollTop) + "px":(($(window).height() / 2) - ($("." + theformClass).height() / 2) + scrollTop) + "px";
        $("." + theformClass).css({
            display:"none",
            left:theLeft
        });
        $(".popupBody").load(theHref + " .theForm","",function(){
            theTop = ($("." + theformClass).height()>($(window).height()-20))?(10 + scrollTop) + "px":(($(window).height() / 2) - ($("." + theformClass).height() / 2) + scrollTop) + "px"
            $("." + theformClass).css({
                display:"block",
                top:theTop 
            });
        });
        $("form." + theformClass).submit(function(e){
            e.preventDefault();
	        formData = $("form").serialize();
	        $.post(theHref, formData, function(data) {
		        $(".popupBody").html($(data).find(".theForm").html())
	        });
        });
        $(".popupBg,.popupClose").click(function(){
            $(".popupBg").remove();
            $("." + theformClass).remove();
        });
        return false;
    });
}
function pollResult(){
    $(".pollResult").each(function(){
        scrollTop = $(window).scrollTop();
        $(".pollResult").before('<div class="popupBg"></div>');
        $(".pollResult").wrap('<div class="popupBody"></div>');
        $("div.popupBody").wrap('<div class="popupMedium"></div>');
        $("div.popupBody").before('<div class="popupTop"></div>');
        $("div.popupBody").after('<img src="/UI/Images/button.popup.close.gif" class="popupClose"/><div class="popupBottom"></div>');
        theLeft = (($(window).width() / 2) - ($(".popupMedium").width() / 2)) + "px";
        theTop = ($(".popupMedium").height()>($(window).height()-20))?(10 + scrollTop) + "px":(($(window).height() / 2) - ($(".popupMedium").height() / 2) + scrollTop) + "px";
        $(".popupMedium").css({
            display:"block",
            top:theTop,
            left:theLeft
        });
    
        $(".popupBg,.popupClose").click(function(){
            $(".popupBg").remove();
            $(".popupMedium").remove();
        });
    });

}

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}


var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
$(window).keydown(function(e){
	kkeys.push(e.keyCode);
		
	if ( kkeys.toString().indexOf( konami ) >= 0 ) {
		$.getScript("http://www.cornify.com/js/cornify.js",function(){
			cornify_add()
		});
	}
}, true);




