﻿var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;
var closetab;
var bodyWidth = 0;
var modalWidth = 0, modalHeight = 0;
//var currentBanner = 1;

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    closetab = $(this).addClass("hover");
    ddmenuitem = $(this).find('ul').css('visibility', 'visible'); //$(this).find('ul').css('visibility', 'visible');
}

function jsddm_close() {
    if (ddmenuitem) {
        ddmenuitem.css('visibility', 'hidden');
        closetab.removeClass('hover');              
    }
}

function jsddm_timer() {   
    closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    } 
}

function ClearSalesMenu() {
	$(".sales-menu div").each(function() {
		$(this).removeClass();
	});
	
	$("#contactbottom span").each(function() {
		$(this).css("display", "none");
	});
}

function HomepageAdSpotlight()
{
    var axel = Math.random() + "";
    var a = axel * 18739124922456;
    document.write('<img src="http://ad.doubleclick.net/activity;src=1064040;type=citri052;cat=softl405;ord=' + a + '?" width="1" height="1" alt="" style="display: none;" />'); 
}


function CitrixNetscalerAdSpotlight()
{
    var axel = Math.random() + ""; 
    var a = axel * 17620988711228; 
    document.write('<img src="http://ad.doubleclick.net/activity;src=1064040;type=citri052;cat=softl749;ord=' + a + '?" width="1" height="1" alt=""/>');
}


// All hover images with on and off states
function ImageHover(name, ext, state) {    
    if (state == "on")
        $("#" + name).attr("src", ("/images/right-column/" + name + "_on." + ext));
    else
        $("#" + name).attr("src", ("/images/right-column/" + name + "_off." + ext));
}

function homeImageHover(name, ext, state) {
    if (state == "on")
        $("#" + name).attr("src", ("/images/homepage/" + name + "_on." + ext));
    else
        $("#" + name).attr("src", ("/images/homepage/" + name + "_off." + ext));
}

function openWin(path) {
    window.open(path, "softlayer", "width=900,height=600");
}

function adjustModal() {
	var style = "width: " + modalWidth + "px; height: " + modalHeight + "px; display: block;";
	$('#modal').attr('style', (style + " left: " + returnLeftAlignment(modalWidth) + "px;"));
}

function openModal(path, width, height) {
	if (width == null) width = 900;	
	if (height == null) height = 600;

	var style = "width: " + width + "px; height: " + height + "px;";
		
	var containerId = document.getElementById("modal");	
	containerId.setAttribute('style', (style + " left: " + returnLeftAlignment(width) + "px;"));

	var modalContent = document.getElementById("modal-content");
	containerId.appendChild(modalContent);
	modalContent.setAttribute('style', style);
	
	modalContent.innerHTML = "";	// remove any HTML in the modal div.
	
	var ifrm = document.createElement("iframe");
	ifrm.setAttribute("src", path);
	ifrm.setAttribute("frameBorder", "0");
	ifrm.setAttribute('border', '0');
	ifrm.setAttribute('width', width + "px");
	ifrm.setAttribute('height', height + "px");
	
	modalContent.appendChild(ifrm);	
	
	$("#modal").jqmShow();
	
	// set global width values for possible adjustment on resize.
	bodyWidth = $("body").css("width");
	modalWidth = width;
	modalHeight = height;
}

function returnLeftAlignment(modalWidth) {
	return Math.round(($('body').width() / 2) - (modalWidth / 2));
}


function imageSwap(id, imageUrl) {
	$('#configuration').attr('src', imageUrl);

	$('h2 span a').each(function() {
		$(this).removeClass('active');
	});

	$('#' + id).addClass('active');
}

function toCurrentNetworkMap()
{   
    //alert ($('#current-network-map').html());
    $('#network-map-title').html($('#current-network-title').html());
    $('#network-map-content').html($('#current-network-map').html());
}

function toFutureNetworkMap()
{
    $('#network-map-title').html($('#new-network-title').html());
    $('#network-map-content').html($('#new-network-map').html());
}

//SWF Detect from Adobe -----------------------

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion()
{
	var version;
	var axo;
	var e;
	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry
	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}
	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";
			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";
			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}
	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}
	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}
	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}

   function GetSwfVer(){
    // NS/Opera version >= 3 check for Flash plugin in plugin array
    var flashVer = -1;
    
    if (navigator.plugins != null && navigator.plugins.length > 0) {
        if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
            var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
            var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
            var descArray = flashDescription.split(" ");
            var tempArrayMajor = descArray[2].split(".");            
            var versionMajor = tempArrayMajor[0];
            var versionMinor = tempArrayMajor[1];
            var versionRevision = descArray[3];
            if (versionRevision == "") {
                versionRevision = descArray[4];
            }
            if (versionRevision[0] == "d") {
                versionRevision = versionRevision.substring(1);
            } else if (versionRevision[0] == "r") {
                versionRevision = versionRevision.substring(1);
                if (versionRevision.indexOf("d") > 0) {
                    versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
                }
            }
            var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
        }
    }
    // MSN/WebTV 2.6 supports Flash 4
    else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
    // WebTV 2.5 supports Flash 3
    else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
    // older WebTV supports Flash 2
    else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
    else if ( isIE && isWin && !isOpera ) {
        flashVer = ControlVersion();
    }    
    return flashVer;
}

function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];
        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}

$(function() {
	bodyWidth = $('body').width();
		   
	// DropDown Menus
	$('#jsddm > li').bind('mouseover', jsddm_open)
	$('#jsddm > li').bind('mouseout', jsddm_timer)

	$("#phone-content").show();

	$(".sales-menu #chat").mouseover(function() {
		ClearSalesMenu();

		$("#chat").addClass("chat-active");
		$("#email").addClass("email-inactive");
		$("#phone").addClass("phone-inactive");

		$("#contactbottom #chat-content").css("display", "block");
	});

	$(".sales-menu #email").mouseover(function() {
		ClearSalesMenu();

		$("#email").addClass("email-active");
		$("#chat").addClass("chat-inactive");
		$("#phone").addClass("phone-inactive");

		$("#contactbottom #email-content").css("display", "block");
	});

	$(".sales-menu #phone").mouseover(function() {
		ClearSalesMenu();

		$("#phone").addClass("phone-active");
		$("#email").addClass("email-inactive");
		$("#chat").addClass("chat-inactive");

		$("#contactbottom #phone-content").css("display", "block");
	});


	// bottom row additional resources links
	$("#download-datasheet").click(function() {
		location.href = "/resources/datasheets/";
	});

    $(".sales-menu #chat").click(function() {
		window.open("https://server.iad.liveperson.net/hc/50693523/?cmd=file&file=visitorWantsToChat&site=50693523&byhref=1&imageUrl=https://www.softlayer.com/img", "chat");
	});

	$("#softlayer-chat").click(function() {
		window.open("https://server.iad.liveperson.net/hc/50693523/?cmd=file&file=visitorWantsToChat&site=50693523&byhref=1&imageUrl=https://www.softlayer.com/img", "chat");
	});


	// resize even on the body to handle modal dialog height/scroll changes.
	$(window).resize(function() {
		adjustModal();
	});


    // Detect Flash for pages that need it
    haveReqFlash = (DetectFlashVer(8, 0, 0));
  
    
    // Network overview page: If required flash is present, show the flash map, else, show alternate content
    if (haveReqFlash){
        $('#flash-network-map').css('display', 'block');
    } else {
        $('#network-map-no-flash').css('display', 'block');  
    }
    
    //Link event banner to event page
    $('#home-event-banner').click(function() {
       location.href = '/about/event-schedule';
    });
   
	// sub page button hover/active functionality
	$(".button-container a").hover(
        function() {
            var id = $(this).attr("id");
            var isDefault = $(this).children("img").attr("id");

            if (isDefault != "default-button") {
                $(this).children("img").attr("src", "/images/button-tabs/navbox_" + id + "_on.jpg");
            }
        },
        function() {
            var id = $(this).attr("id");
            var isDefault = $(this).children("img").attr("id");

            if (isDefault != "default-button") {
                $(this).children("img").attr("src", "/images/button-tabs/navbox_" + id + "_off.jpg");
            } else {
                $(this).children("img").attr("src", "/images/button-tabs/navbox_" + id + "_on.jpg");
            }
    });
    
    // Modal Dialogs init
    $('.social-right').colorbox({width:"80%", height:"80%", iframe:true});
    $('#virtual-overview').colorbox({width:"80%", height:"80%", iframe:true});
    $('.datacenteroverview').colorbox({width:"800px", height:"550px", iframe:true});
    $('.simplifiednetworkdiagram').colorbox({width:"80%", height:"80%", iframe:true});
    $('.networkdiagramdallas').colorbox({width:"1124px", height:"800px", iframe:true});
    $('.seattle-details').colorbox({width:"1124px", height:"800px", iframe:true});
    $('.wdc-details').colorbox({width:"1124px", height:"800px", iframe:true});
    $('.magnify-iphone').colorbox({width:"65%", height:"80%", iframe:true});
    $('.wmp-demo').colorbox({width: "600px", height: "400px", iframe:true});
    $('.flash-demo').colorbox({width: "730px", height: "500px", iframe:true});
    $('.magnify-droid').colorbox({width: "600px", height: "710px", iframe:true});
});

document.onclick = jsddm_close;
