var $d = document,
    $w = window,
    ck = "SiteReturnUrl",
    ms = ".msn.",
    r = Atlas.GetCookieValue(ck);

if (r && r.length > 0)
{
    var i = r.indexOf("#"),
        j = r.indexOf("&", i + 1),
        k = $w.location.host.indexOf(ms);
    
    if (i > -1)
    {            
        // clear cookie to ensure 1 time use
        $d.cookie = ck + "=0; domain=" + window.location.host + "; path=/; expires=Mon, 01 Jan 1990 08:00:00 GMT;";
        
        // set location
        $w.location.href= r.substring(i, j > -1 ? j : r.length);
    }
}

// wire up swfaddress
SWFAddress.setId("ffobj");

// used to track dl script objects on page
var dlScripts = new Array();

// util functions 
function AddToFavorite(u)
{
      var w  = $w,
          bP = navigator.platform,
          bT = $d.title, 
          bU = u ? u : w.location.href;

      // Mozilla Firefox Bookmark
      if (w.sidebar)
          w.sidebar.addPanel(bT, bU, "");

      // Windows IE Favorite
      else if(bP.indexOf('Mac') < 0 && w.external && bP.indexOf('Netscape') < 0)
          w.external.AddFavorite(bU, bT);

      // other browsers
      else
          alert("Mac users: please use Command+D for Bookmarking this page\n\nPC users: please use Ctrl+D for Bookmarking this page.\n");
}

// register a DL event
function dl(c, e)
{
    // create new script elements
    var se = dlS(e);
        
    // cleanup existing script elements
    dlClean();
        
    // load control image
    Atlas.LoadImage(c);
    
    // append exposed JS to page head
    dlH().appendChild(se);

    // store in array for cleanup later
    dlScripts.push(se);    
}

// clean up
function dlClean()
{
    // remove all dl script tags
    while (dlScripts.length > 0)
    {
        // grab reference to existing script objects
        var o = dlScripts.pop();
        
        // remove from head
        dlH().removeChild(o);
    }
}

// create a script element and set the src
function dlS(u)
{
    var s = $d.createElement("script");    
    s.src = u;    
    return s;
}

// grab document head
function dlH()
{
    return $d.getElementsByTagName('head')[0];
}

// Converts query string to hash format
function convertQueryToHash()
{
	var convertedURL = decodeURIComponent(window.location.href);
	var queryDeepLinkKey = "?dl=";

	if(convertedURL.indexOf('#') != -1 || convertedURL.indexOf(queryDeepLinkKey) == -1) {
		return;
	}
	
	convertedURL = String(/\?dl=[^&\?]*/i.exec(convertedURL)).split(queryDeepLinkKey)[1];

	if (convertedURL.indexOf("|") > -1) {	
		convertedURL = convertedURL.replace(/\/*\|/, "[").replace(/\/*\|/g, "|");
		convertedURL = removeDuplicates(convertedURL, "\\/").replace(/\/$/, "") + "]";
	}

	SWFAddress.setValue(convertedURL);
}

// Removes duplicate characters specified by delimiters. '\' must be escaped (i.e. \\)
function removeDuplicates(string, delimiters) 
{
	return string.split('').join('').replace(new RegExp("([" + delimiters + "])\\1+", "gi"),'$1');
}

convertQueryToHash();