﻿// Set security domain
try {
    if (window.location.href.indexOf('ikbp.org') > 0)
        document.domain = "ikbp.org";
    else
        document.domain = "bmwusa.com";
} catch (e) { }

// Global  window.location
var $$G_Location = '';
var $$G_DeepFrame = null;
var $$G_NavHash = true;
var $$G_NavAuto = true;
var $$G_NavFade = false;
var $$G_NavHidden = null;
var $$G_FlashReady = false;
var $$G_MSIE = $.browser.msie;
var $$G_Browser_Version = $.browser.version;
var $$G_IE6 = $$G_MSIE && $$G_Browser_Version < 7;
var $$G_RoundelIMG = 'https://cache.bmwusa.com/image_7f79f065-0e57-4ddc-8ad1-7ae63aa0653b.arox'; /// roundel.png
var $$G_EmptyIMG = 'http://content.bmwusa.com/microsite/f10/_common/html/img/palette/1x1_trans.gif';
var $$G_NavShowIMG = 'http://content.bmwusa.com/microsite/f10/_common/html/img/standard_elements/bmwusa_navclosed.gif';
var $$G_dbg_max = 30;
var $$_dbg_current = 0;
var $$G_FrameHeight = 0;

function prepHash() {
    $$G_Location = window.location.href;
    try {
        if (window.ie) { // insert historyFrame for IE
            ///var x = new Element('iframe', {'id': 'historyFrame'}); // mootools is broken here for inserting iframes in certain versions of IE
            var x = document.createElement("iframe");
            x.setAttribute("id", "historyFrame");
            x.style.display = 'none';
            ///x.injectInside($E('body')); // mootools is broken for injecting iframes into the body in all versions of IE
            document.body.appendChild(x);
        }
    } catch (e) { }
    setTimeout('checkHash();', 300);
}

function ihash() {
    var l = $$G_DeepFrame.window.location.href;
    var h = l.indexOf('#');
    if (h > 0)
        return l.indexOf(h + 1);
    return '';
}

///var c = 0;
function checkHash(forceHash, gotimer) {
    forceHash = forceHash == null ? false : forceHash;
    gotimer = gotimer == null ? false : gotimer;
    var l = window.location.href;
    ///dbg($$G_Location +' :: '+ l);
    if ($$G_Location != l || forceHash) { // then the user has modified the url, inform the flash
        try {
            // if #navhide is added to the parent url the nav will hide
            if ($$G_NavHash) {
                var h = xhash();
                var i = ihash();
                if ((h.indexOf('navshow') >= 0 || i.indexOf('navshow') >= 0) && isNavHidden() && navOverframe)
                    navOverframe();
                else if ((h.indexOf('navhide') >= 0 || i.indexOf('navshow') >= 0) && !isNavHidden() && goFullFrame)
                    goFullFrame();
            }
        } catch (e) { }
        try {
            // passes hashes between the parent and the iframe
            if ($$G_DeepFrame && $$G_DeepFrame.contentWindow.deeplink) {
                if (window.ie) {
                    setTopHash(xhash());
                    $$G_DeepFrame.contentWindow.setHash(xhash());
                } else
                    $$G_DeepFrame.contentWindow.setHash(xhash());
            }
        } catch (e) { }
    }
    if (!forceHash || gotimer)
        setTimeout('checkHash();', 300); // recursive call
}

function xhash(hash) {
    try {
        var l = window.location.href;
        var h = l.indexOf('#');
        $$G_Location = window.location.href; // update global with new location
        if (h > 0 && l.length >= h && hash == null) // if hash is null, then we are requesting the current hash
            return l.slice(h + 1);
        else if (h > 0 && hash != null && hash != '') { // if h and hash exists, then we are requesting to replace it
            l = l.slice(0, h + 1) + hash;
            //			if (window.location.href != l)
            window.location = l;
        } else if (hash != null && hash != '') {
            l = window.location.href + '#' + hash;
            //			if (window.location.href != l)
            window.location = l;
        }
        $$G_Location = window.location.href; // update global with new location
    } catch (e) { }
    return '';
}

function ahash(hash) {
    try { // this is for IE 7
        if (window.ie) {
            xhash(hash); // set the location of the hash
            var doc = document.getElementById("historyFrame").contentWindow.document;
            doc.open("javascript:'<html></html>'");
            // this JS will get called when created and when navigated to via Back & Forward, at that point the hardcoded hash and current hash would be different
            doc.write("<html><head><scri" + "pt type=\"text/javascript\">if (\"" + hash + "\" != parent.xhash()) { parent.xhash(\"" + hash + "\"); parent.checkHash(true); }</scri" + "pt></head><body></body></html>");
            doc.close();

        } else
            xhash(hash);
    } catch (e) { }
}

function setTopAwareness(win) {
    if ($$G_DeepFrame == null || $$G_DeepFrame.contentWindow.deeplink != true) {
        $$G_DeepFrame = win;
        if ($$G_DeepFrame.contentWindow.deeplink && window.location.href.indexOf('#') > 0)
            checkHash(true);
    }
}

function setTopHash(hash) {
    // change the hash on top from the flash request
    return ahash(hash);
}

function setTopLocation(loc) {
    window.focus();
    window.location = loc;
}

function newRoundel() {
    $r = jQuery('#TopNavGrey IMG.Roundel');
    $R = $r.clone();
    $R.attr('id', 'Roundel');
    $R.attr('width', '187');
    $R.attr('height', '93');
    $R.css('width', '187px');
    $R.css('height', '93px');
    //$R.css('left', '50%');
    //$R.css('margin', '-1px 0 0 263px');
    $r.css('display', 'none');

    if ($$G_IE6) {
        $R.css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + png + "', sizingMethod='scale')");
        $R.attr('src', $$G_EmptyIMG);
    } else {
        $R.attr('src', $$G_RoundelIMG);
    }

    //	$R.appendTo('#TopNavNew');
    $R.insertBefore(document.body.firstChild);

    return $R;
}

function prepFullFrame(iframe) {
    try {
        if (iframe == null) iframe = '#T47xIframe';
        $I = jQuery(iframe);
        if ($I.length > 0) {
            newRoundel();
        }

        // hotspot for nav to return
        if ($$G_NavAuto) {
            jQuery('<div id="HotSpot" style="display: none; position: relative; top: 0; height: 36px; width: 998px; z-index: 9999; margin: 0 auto; padding: 0;"><a href="#" style="display: block; position: absolute; top: 0; left: 0; height: 36px; width: 998px; margin: 0; padding: 0; background: transparent url(' + $$G_NavShowIMG + ') no-repeat 8px 8px; border: 1px solid transparent;">&nbsp;</a></div>').insertBefore(document.body.firstChild);
            $H = jQuery('#HotSpot');
            $H.mouseover(function() {
                var $T = jQuery('#TopNavGrey');
                if (isNavHidden())
                    navOverframe();
            });
        }

        // FOR TESTING ONLY -----------------------------------------------
        if (window.location.href.indexOf("demo=1") > 0 || window.location.href.indexOf("demo=true") > 0) {
            jQuery('<div id="FullFrame" style="display: block; position: absolute; top: 0; left: 0; height: 20px; width: 20px; z-index: 9999; margin: 0; padding: 0;"><a href="javascript:goFullFrame();" style="display: block; position: absolute; top: 0; left: 0; height: 20px; width: 20px; margin: 0; padding: 0;background: none #fcc;"><b>^</b></a></div>').appendTo(document.body);
            jQuery('<div id="ShowTopNav" style="display: block; position: absolute; top: 0; left: 20px; height: 20px; width: 20px; z-index: 9999; margin: 0; padding: 0;"><a href="javascript:navOverframe();" style="display: block; position: absolute; top: 0; left: 0; height: 20px; width: 20px; margin: 0; padding: 0; background: none #cfc;"><b>v</b></a></div>').appendTo(document.body);
        }

        if (isNavHidden())
            navOverframe(); // show nav on default
    } catch (e) { }
}

// Hide Navigation
function goFullFrame(iframe) {
    try {
        if (iframe == null) iframe = '#T47bIframe';

        // If using T47b with a splash/launch
        var $I = jQuery(iframe);
        if ($I.length > 0) {
            $I.css('position', 'absolute');
            $I.css('width', '100%');
            $I.css('height', '660px');
            $I.css('z-index', '900');
            $I.css('left', '0');
            $I.css('top', '0');
        }

        // New Roundel for topmost layer if using T47b with a splash/launch
        $R = jQuery('#Roundel');
        if ($R.length == 0) {
            $R = newRoundel();
            $R.css('position', 'absolute');
            $R.css('top', '0');
            $R.css('z-index', '1000');
            $R.css('left', '50%');
        }
        $R.css('margin', '-1px 0 0 273px');

        // Hide Top Nav
        var $T = jQuery('#TopNavGrey');
        var $H = jQuery('#HotSpot');
        $T.css('position', 'relative');
        if ($$G_NavFade)
            $T.fadeOut("slow", function() {
                jQuery('#TopNavGrey').css('z-index', '10');
                //$H.css('visibility', 'visible');
                $H.fadeIn("fast", function() { navHidden__(); });
            });
        else {
            $T.css('z-index', '10');
            $T.css('display', 'none');
            $H.css('visibility', 'visible');
            $H.css('display', 'block');
            navHidden__();
        }

        if ($$G_NavAuto)
            $R.mouseover(function() {
                var $T = jQuery('#TopNavGrey');
                if (isNavHidden())
                    navOverframe();
            });

        // Call Flash to let it know to expand if using T47b with a splash/launch
        if ($I.length > 0) {
            document.getElementById('deeplink_swf').flashExpand('100%', '660px');
        }
    } catch (e) { }
}

// Show Navigation
function navOverframe(iframe) {
    try {
        // Reveal Top Nav
        var $T = jQuery('#TopNavGrey');
        var $H = jQuery('#HotSpot');
        $T.css('z-index', '999');
        if ($$G_NavFade) {
            $H.fadeOut("fast", function() {
                $T.fadeIn("slow", function() { navReady__(); });
            });
        } else {
            $T.css('display', 'block');
            $H.css('visibility', 'hidden');
            $H.css('display', 'none');
            navReady__();
        }
    } catch (e) { }
}

// Register Flash Existence for navigation controls
function flashIsReady() {
    // sets vars to true that flash is available, then navOverframe and goFullFrame can activate callevents at end of nav control
    $$G_FlashReady = true;
    return $$G_FlashReady;
}

function navReady__() {
    try {
        if ($$G_FlashReady && $$G_DeepFrame) {
            $$G_NavHidden = false;
            $$G_DeepFrame.contentWindow.navReady_();
        }
    } catch (e) { }
}

function navHidden__() {
    try {
        if ($$G_FlashReady && $$G_DeepFrame) {
            $$G_NavHidden = true;
            $$G_DeepFrame.contentWindow.navHidden_();
        }
    } catch (e) { }
}

function isNavHidden() {
    var $T = jQuery('#TopNavGrey');
    if (parseInt($T.css('z-index')) <= 10 || $T.css('display') == 'none')
        $$G_NavHidden = true;
    else
        $$G_NavHidden = false;
    return $$G_NavHidden;
}

jQuery(function() {
    $$G_DeepFrame = document.getElementById('T47xIframe');
    prepHash();

    if (xhash() != '')
        checkHash(true, true);
    checkHash();

    prepFullFrame();

    // Placing #navhide in the iframe src will hide the nav on load
    var framesrc = document.getElementById('T47xIframe').src;
    if ((framesrc.indexOf('navhide') > 0 || window.location.href.indexOf('navhide') > 0) && !isNavHidden())
        goFullFrame();
    //	else if (isNavHidden())
    //		navOverframe();

    // Allows for the iFrame to have a fluid height with a minimum height based on the initial iframe height
    $$G_FrameHeight = $$G_DeepFrame.height;
    if ($$G_FrameHeight > 0 && parseInt($$G_FrameHeight) == $$G_FrameHeight) {
        $(window).resize(function() {
            var $f = $('#T47xIframe');
            $f.attr('minheight', $$G_FrameHeight);
            var h = parseInt($(window).height());

            $f.css('height', parseInt(h > $$G_FrameHeight ? h : $$G_FrameHeight) + 'px');
        });
    }
});

function getURL() {
    return self.location.href;
}

function dbg(s) {
    if (document.getElementById('dbg')) {
        if ($$G_dbg_max > 0)
            $$_dbg_current++;
        if ($$_dbg_current > $$G_dbg_max) {
            document.getElementById('dbg').innerHTML = '';
            $$_dbg_current = 0;
        }
        document.getElementById('dbg').innerHTML += '<br />' + s;
    } else
        alert(s);
}
