function addOnLoadEvent(func, arg) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func(arg);
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func(arg);
        }
    }
}

function openSubNav(obj) {
    if (child = obj.getElementsByTagName("ul")[0]) child.style.display = "block";
}

function closeSubNav(obj) {
    if (child = obj.getElementsByTagName("ul")[0]) child.style.display = "none";
}
   
function initializeNav() {
	if( document.getElementById("nav") ){
		for( var i=0; (li = document.getElementById("nav").getElementsByTagName("li")[i]); i++ ){
				if ( li.getElementsByTagName("ul") ){
					li.onmouseover = function(){ openSubNav(this); }
					li.onmouseout = function(){ closeSubNav(this); }
			}
		}
	}

    var section = getActiveSection();

	if( section && document.getElementById("nav_"+section) )
		document.getElementById("nav_"+section).getElementsByTagName("a")[0].className = "selected";
}

function getActiveSection() {
	var urlPathName = window.top.location.pathname; // example: /en-us/buy/
	var activeTabRegexRules = 
	  {
        "home": [ /^\/[^\/]+\/?$/i ],
        "products": [ /^\/[^\/]+\/products/i ],
        "buy": [ /^\/[^\/]+\/buy/i , /store/i ],
        "buzz": [ /^\/[^\/]+\/buzz/i ],
        "support": [ /^\/[^\/]+\/support/i, /app/i, /cgi-bin/i ]
    };
    for (var section in activeTabRegexRules) {

        for (var regExRuleIndex in activeTabRegexRules[section]) {
            var regExRule = activeTabRegexRules[section][regExRuleIndex];
            if (urlPathName.match(regExRule)) return section;
        }
    }
    return null;
}

function initRegionSelector() {
    if (document.getElementById('regionSelector')) {
        var div = document.getElementById('regionSelector').getElementsByTagName('div');
        for (i = 0; i < div.length; i++) {
            div[i].onmouseover = function() {
                this.className = 'menuItem over';
            }
            div[i].onmouseout = function() {
                this.className = 'menuItem out';
            }
        }
    }
}

function onRegionSelected(culture, redirectPatternUrl) {
    var redirectUrl = redirectPatternUrl;

    var docLocation = document.location;
    var relativePath = docLocation.pathname + docLocation.search + docLocation.hash;
    var fullRelativePathWithoutCulture = relativePath.replace(/^\/en-us|^\/en-gb|^\/en-ca|^\/fr-ca|^\/fr-fr|^\/de-de|^\/es-mx|^\/en-au|^\/en-nz/i, "");
    fullRelativePathWithoutCulture = fullRelativePathWithoutCulture.replace(/^\//i, "");
    
    redirectUrl = redirectUrl.replace("{FullRelativePathWithoutCulture}", fullRelativePathWithoutCulture);
    redirectUrl = redirectUrl.replace("{SelectedCulture}", culture);

    set_cookie('region', culture, 30, '/', '.theflip.com', '');
    document.location = redirectUrl;
}