$(function () {
    
});





/* Printing features */
function PrintContents() {
    PrintHtml('<div id="divPageContents">' + document.getElementById('divPageContents').innerHTML + '</div>', ['/x/css/Reset.css', '/x/css/PageContents.css']);
}

function PrintHtml(html, stylesheetArray) {
    if (html != '') {
        var printWindow = window.open('', 'Printvindue', '', '');
        var printDocument = '<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>';

        for (var i = 0; i < stylesheetArray.length; i++) {
            printDocument += '<link href="' + stylesheetArray[i] + '" type="text/css" rel="stylesheet" />';
        }

        printDocument += '</head><body>';
        printDocument += html;
        printDocument += '<script type="text/javascript">setTimeout("this.print(); this.close();",500);</' + 'script></body></html>';

        printWindow.document.write(printDocument);
        printWindow.document.close();
        printWindow.focus();
    }
}
/****/

/* Popup features */
var newwindow = null;

function OpenPopup(sUrl, sName, iWidth, iHeight, iTop, iLeft, iScrollbars, iStatus) {
    if ((newwindow != null) && (!newwindow.closed)) {
        newwindow.close();
    }
    newwindow = window.open(sUrl, sName, "width=" + iWidth + ", height=" + iHeight + ", top=" + iTop + ", left=" + iLeft + ", screenY=" + iTop + ", screenX=" + iLeft + ", scrollbars=" + iScrollbars + ", status=" + iStatus);
    newwindow.focus();
}

function OpenPopupInCenterScreen(sUrl, sName, iWidth, iHeight) {
    var iTop = (jQuery(window).height() - iHeight) / 2;
    var iLeft = (jQuery(window).width() - iWidth) / 2;
    OpenPopup(sUrl, sName, iWidth, iHeight, iTop, iLeft, 0, 0);
}

function OpenImagePopup(sURL, iWidth, iHeight) {
    OpenPopup("/x/function/ImageViewer.aspx?iU=" + sURL, "Billedviser", iWidth, iHeight, 100, 100, 0, 0);
}

function OpenSendLinkToFriendWindow(sUrl, sName, iWidth, iHeight, iLeft, iTop) {
    OpenPopup(sUrl, sName, iWidth, iHeight, iLeft, iTop, 0, 0);
}

function OpenCreateUserPopup(loginId) {
    OpenPopupInCenterScreen("/Popup/Login/CreateUser.aspx?loginid=" + loginId, "CreateUser", 940, 640);
}
/****/

/* Bookmark feature */
function Bookmark() {
	var title = document.title;
	var url = location.href;

	if (window.sidebar)
		window.sidebar.addPanel(title, url,"");
	else if (window.opera && window.print)
	{
		var mbm = document.createElement('a');
		mbm.setAttribute('rel','sidebar');
		mbm.setAttribute('href',url);
		mbm.setAttribute('title',title);
		mbm.click();
	}
	else if (document.all)
		window.external.AddFavorite( url, title);
}
/****/

/* Utility funtions */
function SetFocus(ControlID) {
    document.getElementById(ControlID).focus();
    document.getElementById(ControlID).value = document.getElementById(ControlID).value;
}
/****/

function SetWatermark(txt, sName)
{
	if (txt.value == '')
	{
		txt.setAttribute("class", sName);
		txt.setAttribute("className", sName);
	}
}

function RemoveWatermark(txt)
{
	txt.removeAttribute("class");
	txt.removeAttribute("className");
}

function OpenPrintCourseParticipantPrintList(CourseID, Type) {

    if ((newwindow != null) && (!newwindow.closed)) {
        newwindow.close();
    }

    newwindow = window.open("/admin/Calendar/CourseParticipantPrintList.aspx?CourseID=" + CourseID + "&Type=" + Type, "Print", "width=990, height=600, top=10, left=10, screenY=10, screenX=10, scrollbars=yes, status=no");

    newwindow.focus();
}

function OpenCalenderWindow(sURL, iWidth, iHeight) {

    if ((newwindow != null) && (!newwindow.closed)) {
        newwindow.close();
    }

    newwindow = window.open(sURL, "Kalender", "width=" + iWidth + ", height=" + iHeight + ", top=100, left=100, screenY=100, screenX=100, scrollbars=no, status=no");

    newwindow.focus();

}

function LinkToInternalSite(menufield, externalfield) {
    var externalsite = prompt("Hvilken side skal der henføres til?", "http://");
    document.getElementById("startpageShow").innerHTML = externalsite;
    document.getElementById(externalfield).value = externalsite;
    document.getElementById(menufield).value = "";
}

function Decrypt(input) {
    var output = "";

    for (var i = input.length - 1; i >= 0; i--) {
        output = output + input.substring(i, i + 1);
    }

    document.write("<a href='mailto:" + output + "'>" + output + "</a>");
}

function FlashObject(sPath, iWidth, iHeight) {
    var sHTML = "<!--[if !IE]> -->" +
				"<object type='application/x-shockwave-flash' data='" + sPath + "' width='" + iWidth + "' height='" + iHeight + "' scale='noscale' salign='lt'>" +
				"<!-- <![endif]-->" +
				"<!--[if IE]>" +
				"<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='" + iWidth + "' height='" + iHeight + "'>" +
				"<param name='movie' value='" + sPath + "' />" +
				"<!--><!---->" +
				"<param name='loop' value='true' />" +
				"<param name='menu' value='false' />" +
				"<param name='scale' value='noscale' />" +
				"<param name='salign' value='lt' />" +
                "<param name='WMODE' value='opaque' />" +
				"</object>" +
				"<!-- <![endif]-->";

    document.write(sHTML);
}

/*
* This makes alt tags, popup on <map> elements
* Place the following script under the map: 
* <script type="text/javascript">ActivateMap();</script>
* 
* Place the following div around the image:
* <div id="map-overlay"></div>
* 
*/

function ActivateMap(className) {
    $("map area").each(function () {
        var overlay = $('#map-overlay');
        var area = $(this), alt = area.attr('alt');
        var left = area.attr("coords").split(',')[0];
        var top = area.attr("coords").split(',')[1];

        area.hover(function () {
            overlay.show();
            overlay.css("left", parseInt(left));
            overlay.css("top", parseInt(top - (overlay.height() + 5)));
            overlay.html(alt);
        }, function () {
            overlay.hide();
        });
    });
}

/*
* $(element).cmsWindow(); This can be used on any element, and it opens the element on top of a transparent black background
*
* Css Classes
* cmsWindow-onenter:      Can be placed on all controls, which created a onkeydown event checking for the enter key and pressing the one controle with cmsWindow-onenterrecv.
* cmsWindow-onenterrecv:  Can be placed on the one control, that all cmsWindow-onenter is referringing to. This is normally the submit button in the form.
* cmsWindow-focus:        Can be placed on one control, that will have focus once the modal is shown.
* cmsWindow-close:        Can be placed on multiple controls, this will close the modal when clicked. 
* If cmsWindow-close doesn't exsist in the modal, the "blackout" area will clickable for closing
*/

(function ($) {
    $.fn.extend({
        cmsWindow: function () {
            return this.each(function () {
                            
                var element = $(this);

                element.show();

                var width = (element.width() / 2) * -1;
                var height = (element.height() / 2) * -1;
                
                element.css("position", "absolute").css("left", "50%").css("top", "50%").css("margin-left", width + "px").css("margin-top", height + "px");

                var blackout = $("<div>");
                
                blackout.attr("class", "cmsWindow-blackout");

                var onEnterReceiver = element.find(".cmsWindow-onenterrecv");

                if (onEnterReceiver.length == 1)
                    element.find(".cmsWindow-onenter").attr("onkeydown", "if(event.keyCode==13){document.getElementById('" + onEnterReceiver.attr("id") + "').click(); return false;}");

                var contentelement = element.find(".cmsWindow-content");
                contentelement.css("height", element.height() - 67);

                var focuselement = element.find(".cmsWindow-focus");
                if (focuselement.length == 1) { focuselement.focus(); }

                var closelement = element.find(".cmsWindow-close");

                if (closelement.length == 0)
                    closelement = blackout;

                closelement.click(function() { element.hide(); blackout.hide(); });

                $("body").prepend(blackout);

            });
        }
    });
})(jQuery);


