﻿///<reference path="../jquery.ui/jquery-1.3.2-vsdoc.js">

// apply inline-box only for mozilla
if (jQuery.browser.mozilla) {
	// do when DOM is ready
	$(function() {
		// search form, hide it, search labels to modify, filter classes nocmx and error
	$('validateform').hide().find('p>label:not(.nocmx):not(.error)').each(function() {
			var $this = $(this);
			var labelContent = $this.html();
			var labelWidth = document.defaultView.getComputedStyle(this, '').getPropertyValue('width');
			// create block element with width of label
			var labelSpan = $("<span>")
				.css("display", "block")
				.width(labelWidth)
				.html(labelContent);
			// change display to mozilla specific inline-box
			$this.css("display", "-moz-inline-box")
			// remove children
				.empty()
			// add span element
				.append(labelSpan);
			// show form again
		}).end().show();
	});
};

String.prototype.format = function() {
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; });
}

function Popup(url, wwidth, wheight) 
{
 var width  = wwidth;
 var height = wheight;
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=yes';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'windowname5', params);
 if (window.focus) {newwin.focus()}
 return false;
}

function HideBannerContent() {
$('#bannerArea').css('display', 'none')
}

function ShowBannerContent() {
	$('#bannerArea').css('display', 'inline')
}

$(function() {
    if (__LOGGEDIN) {
        var lastRequest = new Date();
        var interval = setInterval(function() {
            var minutes = (new Date() - lastRequest) / 60000;
            var diff = Math.ceil(__TIMEOUT - minutes);

            if (diff <= 0) {
                document.location = __URL_INDEX + "?logout=1&returnUrl=" + document.URL;
            }
            else if (diff <= 5) {
                if ($('#__timeOutMessageBox').length == 0)
                    createTimeOutBox();
                $('#__timeOutInMinutes').html(diff);
            }
        }, 60000);
    }
});

function createTimeOutBox() {
    var bg = $("<div class=\"disableScreen\"></div>");
    bg.appendTo("body");
    bg.css("display", "block");
    bg.fadeTo("fast", 0.3);

    var box = $(
                    "<div id=\"__timeOutMessageBox\">" +
                        "Sessioon aegub <span id=\"__timeOutInMinutes\"></span> minuti pärast" +
                        "<div class=\"buttonCont;\">" +
                            "<a href=\"javascript:void(0);\" class=\"button\" onclick=\"document.location=document.URL\">Jätka</a>" +
                            "<a href=\"javascript:void(0);\" class=\"button\" onclick=\"document.location=__URL_INDEX+'?logout=1&returnUrl='+document.URL\">Logi välja</a>" +
                    "</div></div>");
    box.appendTo("body");

    box.css({
        marginLeft: '-' + (box.outerWidth() / 2) + 'px',
        marginTop: '-' + (box.outerHeight() / 2) + 'px'
    });
}

function closeTimeOutBox() {
    $('.disableScreen').remove();
    $('#__timeOutMessageBox').remove();
}