/* MODAL------------------------------------------------*/
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: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    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();


function ShowTocModal(cod) {
    switch (cod) {
        case 1:
            GenerateModal("Link01");
            break;
        case 2:
            GenerateModal("Link02");
            break;
        case 3:
            GenerateModal("Link03");
            break;
        default:
            break;
    }
}
function GenerateModal(IdName) {
    document.getElementById(IdName).style.display = 'block';
    CenterDialog(IdName);
    ShowMask("ModalBgLayer", 'block');
}
function closeGeneratedModal(IdName, AnIframe) {
    if (AnIframe == 'IsAnIFrame') {
        parent.document.getElementById('ModalBgLayer').style.display = 'none'
        parent.document.getElementById(IdName).style.display = 'none';
    } else {
        document.getElementById(IdName).style.display = 'none';
        CenterDialog(IdName);
        ShowMask("ModalBgLayer", 'none');
    }
}
function ReadMessege(ttl, msg) {
    $('#MsgTitle').html(ttl);
    $('#MsgContent').html(msg);
    GenerateModal("MsgsModalWindow");
}
function CenterDialog(dialogId) {
    var dialog = document.getElementById(dialogId);
    var PageDimensions = GetPageSize();
    dialog.style.left = PageDimensions[2] / 2 - dialog.offsetWidth / 2 + 'px';
    if ((BrowserDetect.browser == 'Explorer' && BrowserDetect.version > 6) || BrowserDetect.browser != 'Explorer') {
        dialog.style.top = PageDimensions[3] / 2 - dialog.offsetHeight / 2 + 'px';
    }
}

// the full window dimensions array's function
function GetPageSize() {

    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        windowWidth = self.innerWidth - 17; //remove the scroll bar width
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}

//faster & shorter function that get only the window width
//to save CPU usage waste & improve performances  
//         
function GetWindowWidth() {
    var xScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
    }
    var windowWidth;
    if (self.innerHeight) {	// all except Explorer
        windowWidth = self.innerWidth;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
    }
    var getPageWidth;
    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        getPageWidth = windowWidth;
    } else {
        getPageWidth = xScroll;
    }
    return getPageWidth;
}

// This function shows a transparent mask with the window dimensions
function ShowMask(el, status) {

    var PageDimensions = GetPageSize();
    document.getElementById(el).style.height = PageDimensions[1] + 'px';
    document.getElementById(el).style.width = PageDimensions[0] + 'px';
    document.getElementById(el).style.display = status; //opens a transparent gray mask under the div.
}

/*
place dialog in the center of the screen;
TO USE THIS FUNCTION- PLEASE SET THE DIALOG POSITION TO FIXED for !IE6 !!!!!
Please call js/CFramework/browserDetection.js before calling this file, in order to receieve 
the Browser Object
*/
function CenterDialog(dialogId) {
    var dialog = document.getElementById(dialogId);
    var PageDimensions = GetPageSize();
    dialog.style.left = PageDimensions[2] / 2 - dialog.offsetWidth / 2 + 'px';
    if ((BrowserDetect.browser == 'Explorer' && BrowserDetect.version > 6) || BrowserDetect.browser != 'Explorer') {
        dialog.style.top = PageDimensions[3] / 2 - dialog.offsetHeight / 2 + 'px';
    }
}

function ResizeWindow(el, contentWidth) {
    var divPagamento = document.getElementById(el);
    var windowWidth = GetWindowWidth();
    if (windowWidth <= contentWidth) {
        divPagamento.style.width = contentWidth + 'px';
    } else {
        divPagamento.style.width = 100 + '%';
    }
}