﻿function CheckVersion() {
    var pageversion = getQuerystring('version');
    if (pageversion == "html") {
        SetCookie('sumo', 'html', 30, '/');
    } else if (pageversion == "flash") {
        SetCookie('sumo', 'flash', 30, '/');
    }
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function RedirectAnchorToHtml() {

    var anchor = self.document.location.hash;

    if (anchor.length > 0) {
        if (anchor.toString() == "#/home") {
            window.location = "/";
        }
        else {
            anchor = anchor.substring(1, anchor.length);
            anchor = anchor + ".aspx";
            window.location = anchor;
        }
    }
}

function RedirectToFlash() {
    if (GetCookie('sumo') == "flash") {   
        if (swfobject.hasFlashPlayerVersion("9.0.28")) {
            // user has the required version of flash.
            // redirect to the homepage(flash), with the correct swfaddress (deeplinking)
            var deeplink = document.location.pathname;
            var index = deeplink.indexOf(".aspx");
            deeplink = "/#" + deeplink.substring(0, index);

            if (deeplink == "/#") {
                deeplink = "/";
            } else if (deeplink.startsWith("/#/news")) {
                deeplink = "/#/news";
            } else if (deeplink.startsWith("/#/about")) {
                deeplink = "/#/about";
            } else if (deeplink.startsWith("/#/careers")) {
                deeplink = "/#/careers";
            } else if (deeplink.startsWith("/#/services")) {
                var link = deeplink.split("/");
                if (link.length == 5) {
                    deeplink = "/" + link[1] + "/" + link[2] + "/" + link[3];
                }
            }

            if (deeplink != "/#/404") {
                window.location = deeplink;                
            }
        }
    }
}

String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

function DisplayVersionLink() {

    $(document).ready(function() {
							   
		if (GetCookie('sumo') == "flash") {
			if (swfobject.hasFlashPlayerVersion("9.0.28")) {
				// DO NOTHING: Should not happen, RedirectToFlash() will have changed the page location.
			}
			else if (swfobject.hasFlashPlayerVersion("6.0.65")) {
				//Flash plugin less than or equal to version 6.0.65 but below version 9.0.28
				var playerVersion = swfobject.getFlashPlayerVersion();
				var output = "<a href=\"/\" onclick=\"SetCookie('sumoinstaller','install', 30, '/');\" class=\"flashlink\">View flash version</a> - You have Adobe Flash Player player version " + playerVersion.major + "." + playerVersion.minor + "." + playerVersion.release + " installed. This website requires a minimum Adobe Flash Player version of 9.0.28, <a href=\"/\" onclick=\"SetCookie('sumoinstaller','install', 30, '/');\">Click here to install the latest version.</a>";
			}
			else {
				var playerVersion = swfobject.getFlashPlayerVersion();
				if (playerVersion.major == 0) {
					//No flash plugin
				    var output = "View flash version - This website requires the use of the Adobe Flash Player plugin. <a href=\"http://www.adobe.com/go/getflashplayer\" class=\"new-window\">Click here to download the latest version.</a>";
				} else {
					//Flash plugin lower than version 6.0.65
				var output = "<a href=\"http://www.adobe.com/go/getflashplayer\" class=\"new-window flashlink\">View flash version</a> - You have Adobe Flash Player player version " + playerVersion.major + "." + playerVersion.minor + "." + playerVersion.release + " installed. This website requires a minimum Adobe Flash Player version of 9.0.28, <a href=\"http://www.adobe.com/go/getflashplayer\" class=\"new-window\">Click here to download the latest version.</a>";
				}
			}
		}
		else {
		    var output = "<a href=\"javascript:void(0);\" onclick=\"SetCookie('sumo','flash', 30, '/'); RedirectToFlash();\" class=\"flashlink\">View flash version</a> - You have selected <strong>not</strong> to view the high-definition version of this website. <a href=\"javascript:void(0);\" onclick=\"SetCookie('sumo','flash', 30, '/'); RedirectToFlash();\">Click here to view the flash version.</a>";
		}
		$("#viewflash span").html(output);
		$("#viewflash").animate({height: "34px"}, 1500);
		
	});
}
