// JavaScript Document

var continueSlideshow;
var pauseTime = 7000;
var initPauseTime = 5000;
var slideCount = 4;
var slideTransTime = 1000;

function initSlideTrans() {
    // initiallize the style values that are goign to be manipulated
    // this sets up slide 1 as the first slide
    page(1);
    continueSlideshow = true;
	var slideNum = 1;
	setTimeout(function () { startTrans(slideNum) }, initPauseTime);
}

function page(pageNum) {
    continueSlideshow = false;
    // reset everything
    var x = slideCount + 1;
    for (i = 1; i <= slideCount; i++) {
        $("#message-" + i).clearQueue();
        $("#image-" + i).clearQueue();
        $("#pageButton-" + i).clearQueue();
        if (i == pageNum) {
            $("#message-" + i).css("left", "380px");
            $("#image-" + i).show();
            $("#pageButton-" + i).css("backgroundImage", "url(images/pageOn.png)");
        } else {
            $("#message-" + i).css("left", "700px");
            $("#image-" + i).hide();
            $("#pageButton-" + i).css("backgroundImage", "url(images/pageOff.png)");
        }
    }
}

function startTrans(slideNum) {
    if (!continueSlideshow) return;
    nextSlideNum = slideNum + 1;
    if (nextSlideNum > slideCount) nextSlideNum = 1;
    $("#pageButton-" + slideNum).css("backgroundImage","url(images/pageOff.png)");
    setTimeout(function () { nextSlideButton(nextSlideNum) }, slideTransTime / 3);
    $("#image-" + slideNum).fadeOut(slideTransTime);
    $("#image-" + nextSlideNum).fadeIn(slideTransTime);
    $("#message-" + slideNum).animate({ left: "700px" }, slideTransTime, "swing", function () { endTrans(nextSlideNum) });
}

function nextSlideButton(slideNum) {
    if (!continueSlideshow) return;
    $("#pageButton-" + slideNum).css("backgroundImage", "url(images/pageOn.png)");
}

function endTrans(slideNum) {
    if (!continueSlideshow) return;
    $("#message-" + slideNum).animate({ left: "380px" }, slideTransTime, "swing", function () { nextTrans(slideNum) });
}

function nextTrans(slideNum) {
    if (!continueSlideshow) return;
    setTimeout(function () { startTrans(slideNum) }, pauseTime);
}

// This function can be used to parse the query string
function getValueFromQstring(theName, thePageObj) {
    if (thePageObj == "" || thePageObj == null) {
        var query = unescape(window.location.search);
    } else {
        var query = unescape(thePageObj.location.search);
    }
    if (query == "") return "not found";
    query = query.substr(1)
    var pairs = query.split('&');
    var keyVal;
    for (i = 0; i < pairs.length; i++) {
        keyVal = pairs[i].split('=');
        if (keyVal[0] == theName) {
            return keyVal[1];
        }
    }
    return "not found";
}

var sideBar;
var sideBarBox;
function scrolling() {
    if (document.documentElement.scrollTop > sideBarBox.offsetTop) {
        sideBar.style.top = document.documentElement.scrollTop - sideBarBox.offsetTop + "px";
    } else {
        sideBar.style.top = "0px";
    }
}

saveSource();

function saveSource() {
	var oldCookie = findCookie("scr");
	var ref = document.referrer;
	ref = ref.toLowerCase();
	// dont yet have a source or it is old
	if (oldCookie.indexOf("NoCookie") > -1) {
		// the user came from another website
		if (ref != "" && ref.indexOf("technicalprosource") == -1 && ref.indexOf("nps.com") == -1) {
			var code = "";
			for (var i=0; i < ref.length; i++) {
				//code += test.charCodeAt(i).toString(16);
				code += decToHex(ref.charCodeAt(i));
			}
			saveCookie("scr", code, 30);
		}
	}
	// the user did not come from our website
	if (ref.indexOf("technicalprosource") == -1) {
		oldCookie = findCookie("ep");
		var path;
		// dont yet have a start or it is old
		if (oldCookie.indexOf("NoCookie") > -1) {
			path = location.pathname;
		} else {
			// resave the cookie to update the expiration
			path = findCookie("ep");
		}
		saveCookie("ep", path, 30);
	}
}

// Find a Cookie value
function findCookie(cookieName) {
	if (document.cookie == "") return "NoCookies";
	var cookies = document.cookie
	var where = cookies.indexOf(cookieName,0)
	var i
	// the cookie is found so now find the value
	if (where == -1) return "NoCookie"; 
	var cookieArray = new Array()
	cookieArray = cookies.split("; ")
	for (i = 0; i < cookieArray.length; i++) {
		where = cookieArray[i].indexOf(cookieName,0)
		if (where != -1) {
			where = cookieArray[i].indexOf("=",0)
			// make sure you have the right cookie
			if (cookieName == cookieArray[i].substring(0,where)) {
				return cookieArray[i].substring(where + 1, cookieArray[i].length) } 
			}
		}
	return "NoCookie"
}

// Delete a Cookie
function deleteCookie(cookieName) {
	// change the exipery date and let the browser delete it
	var expireDate = new Date
	expireDate.setDate(expireDate.getDate()-1)
	if (document.cookie != "") { 
		// the cookies are found so now find the value
		var cookies = document.cookie
		var where = cookies.indexOf(cookieName,0)
		var i
		if (where != -1) {
			var cookieArray = new Array()
			cookieArray = cookies.split("; ")
			for (i = 0; i < cookieArray.length; i++) {
				where = cookieArray[i].indexOf(cookieName,0)
				if (where != -1) {
					where = cookieArray[i].indexOf("=",0)
					// make sure you have the right cookie
					if (cookieName == cookieArray[i].substring(0,where)) {
						document.cookie = cookieName + "=;expires=" + expireDate.toGMTString() } 
					}
				}
			}
		}
}

// Save a Cookie
// validDays must be 0 or more
function saveCookie(cookieName, cookieValue, validDays) {
	if (validDays == 0) {
		document.cookie = cookieName + "=" + cookieValue
	} else {
		var expireDate = new Date()
		expireDate.setDate(expireDate.getDate() + validDays)
		document.cookie = cookieName + "=" + cookieValue + "; expires=" + expireDate.toGMTString() + "; path=/"
	}
}

// Convert Decimal to Hex
function decToHex(decN) {
	var hexchart = '0123456789ABCDEF'
	var n = parseInt(decN/16)
	var hexN = hexchart.charAt(n)
	decN -= n*16
	hexN += hexchart.charAt(decN)
	return hexN
}

// Convert Hex to Decimal
function hexToDec(hexN) {
	var hexchart = '0123456789ABCDEF'
	var n = hexN.charAt(0)
	var decN = n.indexOf(n) * 16
	var n = hexN.charAt(1)
	decN += n.indexOf(n)
	return decN
}
