﻿function Ajax_Init() {
    var XMLHttpRequestObject = false;

    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }

    this.getData = function(dataSource, divID, special, name) {
        document.getElementById('top').style.display = 'block';
        if (XMLHttpRequestObject) {
            var obj = document.getElementById(divID);
            XMLHttpRequestObject.open("GET", dataSource);
            XMLHttpRequestObject.onreadystatechange = function() {
                if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                    obj.innerHTML = XMLHttpRequestObject.responseText;
                    if (dataSource.indexOf("?id=1&") > 0) {
                        // Insert script
                        scr = document.createElement('script');
                        scr.src = 'scripts/flashes.js';
                        obj.appendChild(scr);
                    }
                    evalScripts(obj);
                    window.scrollTo(0, 0);
                    if (special == "1") {
                        obj.innerHTML = obj.innerHTML.replace("%name%", name);
                    }
                }
                else if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status != 200)
                    alert("Error: Kunde inte ladda data!");
            }
            XMLHttpRequestObject.send(null);
        }
    }
    this.sendData = function(dataSource, name, email, senderid) {
        if (senderid == "sendButton" && (name == "" || email == "")) {
            alert("Du har inte fyllt i all nödvändig data!");
            return false;
        }
        if (XMLHttpRequestObject) {
            XMLHttpRequestObject.open("GET", dataSource + "?name=" + Url.encode(name) + "&email=" + email);
            XMLHttpRequestObject.onreadystatechange = function() {
                if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                    // display thank you
                    ShowModal('Lightbox', 'LightboxBackground', 'dataserver.aspx?id=2&type=2', '400', '400', name);
                }
                else if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status != 200)
                    alert("Error: Kunde inte spara data!");
            }
            XMLHttpRequestObject.send(null);
        }
    }
}
var window_i = "";
function ShowModal(dwindow, dbackground, datasource, width, height, name) {
    document.getElementById(dwindow).style.display = 'block';
    document.getElementById(dwindow).style.width = width + "px";
    document.getElementById(dwindow).style.height = height + "px";
    document.getElementById(dbackground).style.display = 'block';
    document.getElementById("ModalInner").innerHTML = "";
    window_i = dwindow;

    MainAjax.getData(datasource, "ModalInner", "1", name);

    OnWindowResize();
    
    if (window.attachEvent)
        window.attachEvent('onresize', OnWindowResize);
    else if (window.addEventListener)
        window.addEventListener('resize', OnWindowResize, false);
    else
        window.onresize = OnWindowResize;

    if (document.all)
        document.documentElement.onscroll = OnWindowResize;
}

function OnWindowResize() {
    var left = document.documentElement.scrollLeft;
    var top = document.documentElement.scrollTop;
    var div = document.getElementById(window_i);
    var close = document.getElementById("Close");

    div.style.left = Math.max((left + (GetWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
    div.style.top = Math.max((top + (GetWindowHeight() - div.offsetHeight) / 2), 0) + 'px';

    close.style.left = div.offsetWidth - 100 + "px";
    close.style.top = div.offsetHeight - 30 + "px";
}

function CloseModal(dwindow, dbackground) {
    document.getElementById(dwindow).style.display = 'none';
    document.getElementById(dbackground).style.display = 'none';

    if (window.detachEvent)
        window.detachEvent('onresize', OnWindowResize);
    else if (window.removeEventListener)
        window.removeEventListener('resize', OnWindowResize, false);
    else
        window.onresize = null;
}
function GetWindowWidth() {
    var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;

    return width;
}

function GetWindowHeight() {
    var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
  		0;

    return height;
}
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.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();

var scrllTmr;
var MainAjax = new Ajax_Init();
var SendAjax = new Ajax_Init();
window.onload = function() {
    MainAjax.getData('dataserver.aspx?id=1&type=1', 'Page');
    //document.getElementById('top').style.display = 'none';
    document.getElementById('Scroll').style.overflow = 'hidden';
    document.getElementById('Scrolltext').style.position = 'relative';
    document.getElementById('Scrolltext').style.display = 'block';

    cw = parseInt(document.getElementById('Scroll').offsetWidth);
    w = parseInt(document.getElementById('Scrolltext').offsetWidth);

    if (BrowserDetect.browser == "Explorer" || BrowserDetect.browser == "Opera")
        lft = cw + 10;
    else
        lft = cw + 15;
    document.getElementById('Scrolltext').style.left = lft + "px";
    scrollStep(cw, w, lft);
}
function scrollStep(cw, w, lft) {
    if (lft == w * -1) {
        if (BrowserDetect.browser == "Explorer" || BrowserDetect.browser == "Opera")
            lft = cw + 10;
        else
            lft = cw + 15;
    }
    document.getElementById('Scrolltext').style.left = lft + "px";

    if (scrllTmr)
        clearTimeout(scrllTmr);

    if (BrowserDetect.browser == "Explorer" || BrowserDetect.browser == "Opera")
        scrllTmr = setTimeout('scrollStep(cw,w,' + (lft - 1) + ')', 10);
    else
        scrllTmr = setTimeout('scrollStep(cw,w,' + (lft - 1) + ')', 15);

}
function evalScripts(el) {
    var scripts = el.getElementsByTagName("script");
    var i = 0;
    var end = scripts.length;

    for (i; i < end; i++) {
        /*alert(window.execScript);
        if (window.execScript) {
            alert(scripts[i].innerHTML);
            window.execScript(scripts[i].innerHTML);
        }*/
        eval(scripts[i].innerHTML);
    }

    scripts = null;
}
var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}