(function () {
    var trim = function (s) {
        return s.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
    };
    var get_cookie = function (cookie_name, unset) {
        var allCookies = document.cookie.split(";");
        var target = null;
        for (var i = 0; i < allCookies.length; i = i + 1) {
            var next = trim(allCookies[i]);
            if ((next.indexOf(cookie_name) === 0) && next.indexOf("=") === cookie_name.length) {
                target = next;
            }
        }
        // Target now contains exactly the cookie we want or is empty
        if (target !== null) {
            var index1 = target.indexOf("=");
            var index2 = target.indexOf(";");
            if (index1 === -1) {
                // Malformed cookie - useless so attempt to delete it
                unset_cookie(cookie_name);
                return null;
            }
            if (index2 === -1) {
                index2 = target.length;
            }
            var value = unescape(target.substring(index1 + 1, index2));
            if (unset) {
                unset_cookie(cookie_name);
            }
            return value;
        }
        return null;
    };

    var isIE = false;
    try {
        isIE = navigator.userAgent.match(/MSIE/) !== null;
    } catch (e) {}

    window.startup = function () {
		
		var tabs = document.getElementsByName('section-tab');
		for (var i = 0; i < tabs.length; i += 1) {
			tabs[i].setAttribute('href', 'javascript:void(0);');
		}
				
        var countdownContainer = document.getElementById('countdown-container');
        if (countdownContainer === null) {
            var countdownContainer = document.getElementById('ie-countdown-container');
        }
        if (countdownContainer) {
            countdownContainer.style.display = "";
			countdownContainer.style.visibility = "visible";
            var doTimer = get_cookie("timer") !== "1";
            var countdownEl = document.getElementById('int-days-remaining');
            var r = Math.max(0, Math.floor((new Date(1278892800000) - new Date()) / 86400000));
            var x = 0;
            if (doTimer && !isIE) {
                document.cookie = "timer=1; expires=session";
                var i = setInterval(function () {
                    countdownEl.innerHTML = x;
                    if (x === r) {
                        clearInterval(i);
                    }
                    x += 1;
                }, Math.max(0, 100 / Math.log(r / 2)));
            } else {
                countdownEl.innerHTML = r;
            }
        }
    };

    // Tabs Script
    window.changeTabs = function (newTab, liIndex) {
        var parent = document.getElementById('column-content');
        var children = parent.childNodes;
        for(var i=0; i<children.length; i++){
            if(children[i].id != newTab){
                children[i].className = "section";
            }
            else{
                children[i].className ="section section-selected";
            };
        };
        var lis = document.getElementById('article-nav').getElementsByTagName("li");
        for(var i=0; i<lis.length; i++){
            lis[i].className = (i === liIndex) ? "initialselected" : "";
        }
    };
	
	// IE Tabs Script
    window.ieChangeTabs = function (newTab, liIndex) {
        var parent = document.getElementById('ie-column-content');
        var children = parent.childNodes;
        for(var i=0; i<children.length; i++){
            if(children[i].id != newTab){
                children[i].className = "ie-section";
            }
            else{
                children[i].className ="ie-section ie-section-selected";
            };
        };
        var lis = document.getElementById('ie-article-nav').getElementsByTagName("li");
        for(var i=0; i<lis.length; i++){
            lis[i].className = (i === liIndex) ? "ie-initialselected" : "";
        }
    };
})();
