﻿// Fixes a bug in the ASP.NET AJAX framework for Safari and Chrome. The ASP.NET
// AJAX framework contains a fix that allows it to work with Safari 2. However,
// Safari 3+ and Chrome contain a new JavaScript engine that allows it to
// correctly render pages. The ASP.NET AJAX framework does not correct for that,
// so this fix is needed to identify these modern browsers correctly.
Sys.Browser.WebKit = {};
if (navigator.userAgent.indexOf('WebKit/') > -1) {
    Sys.Browser.agent = Sys.Browser.WebKit;
    Sys.Browser.version = parseFloat(navigator.userAgent.match(/WebKit\/(\d+(\.\d+)?)/)[1]);
    Sys.Browser.name = 'WebKit';
}

function ToggleEditorDisplay( divClientID, imgClientUrl, expandImageUrl, minimizeImageUrl ) 
{
    var el = document.getElementById(divClientID);
    var today = new Date();
    var expiry = new Date(today.valueOf() + (20 * 60000)); // plus 20 minutes
    
    if( el.style.display=='none' ) 
    {
        el.style.display='';
        document.images[imgClientUrl].src= minimizeImageUrl;
        // Keep editor expansion state to restore it between postbacks.
        document.cookie = divClientID + "=block; expires=" + expiry.toGMTString() + "; path=/";
    }
    else 
    {
        el.style.display='none';
        document.images[imgClientUrl].src= expandImageUrl;
        document.cookie = divClientID + "=none; expires=" + expiry.toGMTString() + "; path=/";
    }
}

function DisplayModalDialog(url, opts, name, callback)
{
    var winName = (name) ? name : "ModalDialog";
    var retVal;
    
    // If browser is IE<6 or Safari do not use modal dialog. Safari 4 claims 
    // to support .showModalDialog but then crashes when you close the dialog
    // either by calling close() or by clicking on the close (x) button of the dialog.
	if ((window.showModalDialog) &&
	    (!((BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 6) || 
	    BrowserDetect.browser == "Safari")))
	{
		retVal=window.showModalDialog(url, winName, "dialogWidth:600px;dialogHeight:600px;status:no");
		if(callback)
		{
		    callback(retVal);
		}
	}
	else
	{
	    if( opts == null || opts == "undefined" )
	    {
		    opts = "width=600,height=600,resizable=no,status=no,scrollbars=yes,modal=yes,dialog=yes" ;
	    }		    

	    var hwnd = window.open( url, winName, opts ) ;
	    if( (document.window != null ) && (!hwnd.opener) )
	    {
		    hwnd.opener = document.window ;
	    }	
	    hwnd.moveTo( 100, 80 ) ; 	    
	}    
}

function AddWebPartDialogCallback(uniqueId, result)
{
    if(result == null)
    {
        return;
    }    
    else
    {
        __doPostBack(uniqueId,result);
    }
}

function DoCatalogPostBack(postBackReference, returnValue) 
{
    eval(postBackReference.replace("[[WEBPART]]", returnValue));
}

// Adds a function to window.onload event preserving
// already attached function if any.
function AddOnload(funcToAdd)
{
    if (window.onload)
    {
      var currentfunc = window.onload; 
      window.onload = function()
      {
        currentfunc();
        funcToAdd();
      }
    }
    else
    {
      window.onload = funcToAdd;
    }
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

var authTimerId;
// 
function MonitorAuthTimeout()
{ 
     var w = document.getElementById('warning');
     var wc = document.getElementById('warning-content');
     var ttlm; // time to live in minutes
     
     if (w != null && authWarning != null && authTtl != null)
     {
         if (authTtl > 0)
         { 
            authTtl-=65;        
            if (authTtl <= 360)
            {
                ttlm = Math.round(authTtl / 60);
                wc.innerHTML = authWarning.replace("[0]", ttlm);
                w.style.visibility = "visible";
            }
            authTimerId = setTimeout("MonitorAuthTimeout()",65000);
         } 
         if (authTtl <= 0)
         { 
            wc.innerHTML = authExpired;
            w.style.visibility = "visible";
         } 
     }      
} 

function StopMonitoringAuthTimeout()
{
    clearTimeout(authTimerId);
}

function CloseWarning()
{
    var w = document.getElementById('warning');
    if (w)
    {
        w.style.visibility = "hidden";
    }    
}

function CloseAuthWarning(stopMonitoring)
{
    CloseWarning();
    if (stopMonitoring)
    {
        StopMonitoringAuthTimeout();
    }
}

function LaunchLoginDialog()
{
    CloseAuthWarning(true);
    DisplayModalDialog('/_infrastructure/LoginDialog.aspx');
}

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
function InitializeRequest(sender, args) {
    if (prm.get_isInAsyncPostBack()) {
        args.set_cancel(true);
    }
}
