// Set security domain
try {
	document.domain = "bmwusa.com";
} catch(e) { }

// Global  window.location
var $$G_Location = '';
var $$G_DeepFrame = null;

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);
}

///var c = 0;
function checkHash(forceHash) {
	forceHash = forceHash == null? false: forceHash;
	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 ($$G_DeepFrame.deeplink)
				if (window.ie) {
					setTopHash(xhash());
					$$G_DeepFrame.setFlashHash(xhash());
				} else
					$$G_DeepFrame.setFlashHash(xhash());
		} catch(e) { }
	}
	if (!forceHash)
		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) // if h and hash exists, then we are requesting to replace it
			 window.location = l.slice(0, h+1) + hash;
		else if (hash != null)
			 window.location =  window.location.href +'#'+ hash;
		$$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 setTopHash(hash) {
	// change the hash on top from the flash request
	return ahash(hash);
}

function setTopLocation(loc) {
	window.focus();
	window.location = loc;
}

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

// Initiate prepatory functions when dom is ready
try {
	var when = navigator.userAgent.toLowercase().indexOf('msie')? 'load': 'domready';
	addEvent(window, when, function() { ///'domready', function() {
		var F = window.frames;
		if (F != null && F[0] != null)
			$$G_DeepFrame = F[0];
		prepHash();
	});
} catch (e) { }

function dbg(s) {
	document.getElementById('dbg').innerHTML += '<br />'+ s;
}