var tunnel = new LanguageTunnel();
JQ(function() {
    JQ("a").each(function() {
        if (JQ(this).attr("href") == "#") {
            JQ(this).attr("href", "javascript:void(0);")
        }
    })
});
function LanguageTunnel() {
    var a = this;
    this.preInit = function() {
        a.path = "/";
        a.date = new Date();
        a.date.setTime(a.date.getTime() + 3600000 * 24 * 365);
        a.getParams();
        a.mergeConfigs();
        a.handleCookies()
    };
    this.init = function() {
        a.preloadImages();
        a.container = JQ("#tunnel_container");
        a.render();
        if (typeof afterGlobalGatewayRenders == "function") {
            afterGlobalGatewayRenders()
        }
    };
    this.preloadImages = function() {
        for (var b in a.config.images) {
            JQ("body").append('<img src="' + a.config.images[b] + '" style="display:none;"/>')
        }
    };
    this.render = function() {
        JQ("body").append('<div id="tunnel_floater"></div>');
        a.floater = JQ("div#tunnel_floater");
        a.container.append('<div id="tunnel_logo"><img src="' + a.config.images.logo + '" /></div>');
        a.container.append('<div id="language_wrapper"></div>');
        a.language_wrapper = a.container.find("#language_wrapper");
        JQ.each(a.config.region,
        function() {
            var d = "lang_bg_" + Math.floor(Math.random() * 10000000);
            var b = "";
            if (typeof this._class == "string") {
                b = " " + this._class + "_region"
            }
            var c = "";
            c += '<div class="language_button_wrapper' + b + '">';
            c += '	<img src="' + ((this.countries.length == 1) ? a.config.images.lang_button_bg_arrowless: a.config.images.lang_button_bg) + '" class="language_button_bg" id="' + d + '" />';
            c += '	<div class="language_button">' + this.display + "</div>";
            c += "</div>";
            a.language_wrapper.append(c);
            a.language_wrapper.find("#" + d).parent().data("countries", this.countries)
        });
        a.container.find("div.language_button_wrapper").each(function() {
            var c = JQ(this).find("img.language_button_bg");
            c.css("opacity", "0");
            var d = JQ(this).find("img.language_button_bg").not("#" + JQ(this).attr("id")).parent();
            var b = 150;
            JQ(this).css("cursor", "pointer");
            if (JQ(this).data("countries").length == 1) {
                JQ(this).click(function() {
                    a.loadCookies(JQ(this).data("countries")[0])
                });
                JQ(this).hover(function() {
                    JQ(this).addClass("hover");
                    a.container.find("div.language_button_wrapper.active").removeClass("active");
                    JQ(this).addClass("active");
                    c.css({
                        opacity: 1
                    });
                    a.container.find("div.language_button_wrapper").not(".active").find("img.language_button_bg").css({
                        opacity: 0
                    });
                    a.floater.html("")
                },
                function() {
                    JQ(this).removeClass("hover")
                })
            } else {
                JQ(this).hover(function() {
                    JQ(this).addClass("hover");
                    a.container.find("div.language_button_wrapper.active").removeClass("active");
                    JQ(this).addClass("active");
                    c.css({
                        opacity: 1
                    });
                    a.container.find("div.language_button_wrapper").not(".active").find("img.language_button_bg").css({
                        opacity: 0
                    });
                    a.renderFlyOut(JQ(this))
                },
                function() {
                    JQ(this).removeClass("hover")
                })
            }
        })
    };
    this.renderFlyOut = function(e) {
        var d = e.data("countries");
        a.floater.css({
            position: "absolute",
            top: e.offset().top,
            left: e.offset().left + 220
        });
        if (JQ(d).length > 10) {
            var g = "";
            g += '<div class="floater_wrapper active wide" style="width:400px;background:url(' + a.config.images.floater_bg_wide + ') repeat-y;" >';
            g += '	<div class="floater_bottom_wrapper" style="width:400px;background:url(' + a.config.images.floater_bottom_wide + ') no-repeat left bottom;">';
            g += '		<div class="floater_top" style="width:400px;background:url(' + a.config.images.floater_top_wide + ') no-repeat left top;">';
            g += '			<div class="floater_content" style="float:left;" ></div>';
            g += '			<div class="floater_content2" ></div>';
            g += '			<div class="floater_content_clear" >&nbsp;</div>';
            g += "		</div>";
            g += "	</div>";
            g += "</div>";
            a.floater.html(g);
            var f = Math.ceil(JQ(d).length / 2);
            var c = "div.floater_content";
            JQ.each(d,
            function() {
                var h = "";
                if (this.display.length == 0) {
                    h = " style='visibility:hidden;'"
                }
                if (--f == -1) {
                    c = "div.floater_content2"
                }
                var i = '<div class="country"' + h + ">";
                i += '	<a href="#" >' + this.display + "</a>";
                i += "</div>";
                a.floater.find(c).append(i);
                a.floater.find(c + " div.country:last a").data("country", this)
            })
        } else {
            var g = "";
            g += '<div class="floater_wrapper narrow active" style="width:200px;background:url(' + a.config.images.floater_bg + ') repeat-y;" >';
            g += '	<div class="floater_bottom_wrapper" style="width:200px; background:url(' + a.config.images.floater_bottom + ') no-repeat left bottom;">';
            g += '		<div class="floater_top" style="width:200px;background:url(' + a.config.images.floater_top + ') no-repeat left top;">';
            g += '			<div class="floater_content" ></div>';
            g += "		</div>";
            g += "	</div>";
            g += "</div>";
            a.floater.html(g);
            JQ.each(d,
            function() {
                var h = '<div class="country">';
                h += '	<a href="#" >' + this.display + "</a>";
                h += "</div>";
                a.floater.find("div.floater_content").append(h);
                a.floater.find("div.floater_content div.country:last a").data("country", this)
            })
        }
        var b = 50;
        a.floater.find("div.country a").css("color", a.config.style.countryColor);
        a.floater.find("div.country img").css("opacity", 0);
        a.floater.find("div.country").each(function() {
            JQ(this).find("a").click(function() {
                var h = JQ(this).data("country");
                a.loadCookies(h);
                return false
            })
        })
    };
    this.mergeConfigs = function() {
        var b = {
            cookies: ["NIKE_LANG_LOCALE", "NIKE_COUNTRY", "NIKE_COMMERCE_COUNTRY", "NIKE_COMMERCE_LANG_LOCALE"],
            style: {
                width: 999,
                height: 666,
                floater_top: 22,
                countryColor: "e9e6e2",
                countryHoverColor: "605e5b",
                langColor: "ffffff"
            },
            images: {
                logo: "/nikeos/global/image/global_gateway/tunnel_logo.jpg",
                lang_button_bg: "/nikeos/global/image/global_gateway/button_background.jpg",
                tunnel_bg: "/nikeos/global/image/global_gateway/tunnel_bg.jpg",
                floater_bg: "/nikeos/global/image/global_gateway/tunnel_floater_bg.jpg",
                floater_top: "/nikeos/global/image/global_gateway/tunnel_floater_top.jpg",
                floater_bottom: "/nikeos/global/image/global_gateway/tunnel_floater_bottom.jpg",
                country_hover_bg: "/nikeos/global/image/global_gateway/country_hover_bg.jpg"
            },
            region: []
        };
        if (NIKE_TUNNEL_CONFIG.region.constructor != Array) {
            NIKE_TUNNEL_CONFIG.region = [NIKE_TUNNEL_CONFIG.region]
        }
        if (typeof NIKE_TUNNEL_CONFIG != "undefined") {
            a.config = JQ.extend(true, {},
            b, NIKE_TUNNEL_CONFIG)
        } else {
            a.config = b
        }
        var d,
        c;
        for (var e = 0; e < a.config.region.length; e++) {
            if (a.config.region[e].countries.constructor != Array) {
                c = a.config.region[e].countries;
                a.config.region[e].countries = [];
                a.config.region[e].countries[0] = c
            }
        }
    };
    this.handleCookies = function() {
        a.hasAllCookies = true;
        a.cookies = [];
        var c;
        JQ.each(a.config.cookies,
        function() {
            c = JQ.cookie(this);
            if (!c) {
                a.hasAllCookies = false
            } else {
                a.cookies[this] = c
            }
        });
        if (a.hasAllCookies) {
            var e,
            d,
            b = false;
            for (e = 0; e < a.config.region.length; e++) {
                for (d = 0; d < a.config.region[e].countries.length; d++) {
                    if (a.config.region[e].countries[d].NIKE_COMMERCE_COUNTRY == JQ.cookie("NIKE_COMMERCE_COUNTRY") && a.config.region[e].countries[d].NIKE_COMMERCE_LANG_LOCALE == JQ.cookie("NIKE_COMMERCE_LANG_LOCALE") && a.config.region[e].countries[d].NIKE_COUNTRY == JQ.cookie("NIKE_COUNTRY") && a.config.region[e].countries[d].NIKE_LANG_LOCALE == JQ.cookie("NIKE_LANG_LOCALE")) {
                        b = true
                    }
                }
            }
            if (!b) {
                a.hasAllCookies = false
            }
        }
        if (a.hasAllCookies && location.search.indexOf("?change") == -1) {
            return a.leave()
        }
        JQ(function() {
            setTimeout(a.init, 0)
        })
    };
    this.loadCookies = function(b) {
        if (typeof b.LINK != "undefined") {
            a.cookies = b;
            if (typeof a.cookies != "undefined") {
                for (var c in b) {
                    if (c != "display" && c != "tracking_value" && c != "COOKIES") {
                        JQ.cookie(c, b[c], {
                            expires: a.date,
                            path: a.path,
                            domain: a.getDomain()
                        })
                    }
                }
            }
            a.leave();
            return
        }
        for (var c in b) {
            if (c != "display" && c != "tracking_value" && c != "COOKIES") {
                JQ.cookie(c, b[c], {
                    expires: a.date,
                    path: a.path,
                    domain: a.getDomain()
                })
            }
        }
        a.cookies = b;
        a.trackClick(b.tracking_value);
        a.leave()
    };
    this.leave = function() {
        var f = a.config.region;
        var c,
        b,
        e = false;
        for (var d = 0; d < f.length; d++) {
            b = f[d].countries;
            for (c = 0; c < b.length; c++) {
                if (b[c].NIKE_COUNTRY == a.cookies.NIKE_COUNTRY && b[c].NIKE_LANG_LOCALE == a.cookies.NIKE_LANG_LOCALE) {
                    if (typeof b[c].LINK != "undefined") {
                        e = b[c].LINK;
                        if (typeof leaveGatewayOverwrite == "function") {
                            return leaveGatewayOverwrite(e)
                        }
                        location.href = addParams(e, [getNonNikeReferrer(document.referrer)]);
                        return
                    }
                }
            }
        }
        if (typeof leaveGatewayOverwrite == "function") {
            return leaveGatewayOverwrite(a.getURL())
        }
        document.location.href = addParams(a.getURL(), [getNonNikeReferrer(document.referrer)])
    };
    this.trackClick = function(b) {
        tracking.click({
            pageName: track_site + ">language_tunnel>" + b
        })
    };
    this.getParams = function() {
        a.continueURL = getParam("continueURL", "query_string");
        // hack the continueURL until we reimplement swfAddress - team#/wake
        if ( a.continueURL.indexOf('team#/') >= 0 ){
            var sport = a.continueURL.substring(a.continueURL.indexOf('team#/')+6,a.continueURL.length)
            a.continueURL = 'team?sport='+sport;
        }
        if ( a.continueURL.indexOf('feature#/') >= 0 ){
            var feature = a.continueURL.substring(a.continueURL.indexOf('feature#/')+9,a.continueURL.length)
            a.continueURL = 'feature='+feature;
        }
				if ( getParam('feature', 'query_string') !== "" ) {
				 		a.continueURL = a.continueURL + '?feature=' + getParam('feature', 'query_string');
				}

    };
    this.getURL = function(d) {
        var c = "/nikeos/p/" + a.config.site + "/" + a.cookies.NIKE_LANG_LOCALE + "/";
        var b = c;
        if (a.continueURL != "undefined" && a.continueURL != "") {
            b = b + a.continueURL;
            if (b.indexOf(c) < 0) {
                b = c
            }
        }
        if (document.referrer.length > 0) {
            b = addParams(b, [getNonNikeReferrer(document.referrer)])
        }
        return b
    };
    this.getDomain = function() {
        var b = "";
        if (location.href.toString().indexOf("nike.com") >= 0) {
            b = "nike.com"
        } else {
            if (location.href.toString().indexOf("rga.com") >= 0) {
                b = "rga.com"
            }
        }
        return b
    };
    a.preInit()
}
function getParam(a, b) {
	
    var d = window.location.href;
    	if (d.indexOf('?') >= 0) {
			d=d.substring(d.indexOf('?'),d.length);
		} else {
            if (b == "hash") {
                d = d.substring(d.indexOf("#"), d.length)
            }
        }
    
    a = a.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    
		var c = "[\\?&]" + a + "=([^&]*)";
    var f = new RegExp(c);
    var e = f.exec(d);
    if (e == null) {
        return ""
    } else {
        return e[1]
    }
}
function addParams(b, e) {
	
    var d = b.split("?");
    if (e) {
        for (var c = 0; c < e.length; c++) {
            if ((d.length > 1) || (c != 0)) {
                for (var a in e[c]) {
                    b += "&" + a + "=" + encodeURIComponent(e[c][a])
                }
            } else {
                for (var a in e[c]) {
                    b += "?" + a + "=" + encodeURIComponent(e[c][a])
                }
            }
        }
    }
    return b
}
function getNonNikeReferrer(a) {
    var c = /^((http|https)\:\/\/)([a-zA-Z0-9]+\.)*nike\.com(\/.*)?$/i;
    var b = !(c.test(a));
    if (b) {
        return {
            ref: a
        }
    } else {
        return null
    }
};
