function clickAnchor(event) {
    var e = getEvent(event);
    var t = getEventSrc( e );
    if ( t.tagName == "A" ) {
        cancelEvent( e);
        document.location = t.getAttribute("href");
    } else {
        var allHrefs = t.getElementsByTagName("A");
        if ( allHrefs.length > 0 ) {
           cancelEvent( e);
           document.location = allHrefs[0].getAttribute("href");
        } else {
        }
    }
}
function refreshPage(action, params) {
    var p = parent?parent:this;
    if ( p.getPageType()=="buildababy" ) {
        refreshBabyList( action, params );
    } else {
        var frm = p.document.createElement("form");
        frm.setAttribute("method", "POST" );
        var pq = new PageQuery("?" + (action?"actionid=" + action:"") + (params?"&"+params:""));
        var keys = pq.getParameters();
        for ( var i=0;i<keys.length;i++ ) {
          var v = pq.getValue( keys[i] );
          var hi = p.document.createElement("input");
          hi.setAttribute("type", "hidden" );
          hi.setAttribute("name", keys[i] );
          hi.setAttribute("value", v );
          frm.appendChild( hi );
        }

        if ( p.getPageType()=="favorites" ) {
            frm.setAttribute("action", "favorites.html" );
        } else if ( p.getPageType()=="compare" ) {
            frm.setAttribute("action", "compare.html" );
        } else if ( p.getPageType()=="nameinfo" ) {
            frm.setAttribute("action", window.location.pathname + "?id=" + getQueryString("id") );
        } else if ( p.getPageType()=="home" ) {
            frm.setAttribute("action", "/" );
        }


        p.document.body.appendChild( frm );
        frm.submit();
        p.document.body.removeChild( frm );
    }
}
function showBabyInfo( id, name, gender ) {
    var infoEl = document.getElementById("babyinfo");
    if ( infoEl ) {
        var infoFrameEl = document.getElementById("babyinfoframe");
        if ( id ) {
            infoEl.style.display = "";
            document.body.setAttribute("currentName", name );
            if ( infoFrameEl )
            {
                var infoFrameDoc;
                if (infoFrameEl.contentDocument) {
                    // For NS6
                    infoFrameDoc = infoFrameEl.contentDocument;
                } else if (infoFrameEl.contentWindow) {
                    // For IE5.5 and IE6
                    infoFrameDoc = infoFrameEl.contentWindow.document;
                } else if (infoFrameEl.document) {
                    // For IE5
                    infoFrameDoc = infoFrameEl.document;
                }
                var infoFrameWasExpanded;
                if ( infoFrameDoc ) {
                    var el = infoFrameDoc.getElementById("infograph");
                    if ( el ) infoFrameWasExpanded = el.getAttribute("expanded") == "true";
                    else infoFrameWasExpanded = false;
                }
                var uri = "/babyinfo.html";
                if ( name && gender ) {
                    uri = ((gender=="M")?"/boy-names/":"/girl-names/") + name + ".html";
                }
                infoFrameEl.setAttribute( "src", uri + "?id=" + id + (infoFrameWasExpanded?"&expanded=true":"") +
                    (isPartialFinder()?"&partial=true":"") );
            }
        } else {
            infoEl.style.display = "none";
            if ( infoFrameEl ) infoFrameEl.setAttribute( "src", "about:blank" );
        }
        refreshAdFrames();
    }
}
function builderPageLoaded() {
    var id = getQueryString("loadbabyid");
    var n = getQueryString("loadbabyname");
    var g = getQueryString("loadbabygender");
    if ( id && n ) showBabyInfo( id, n, g );
}
function favoritesPageLoaded() {
    initialize();
}
function nameinfoPageLoaded() {
    initialize();
}
function comparePageLoaded() {
    initialize();
}
function homePageLoaded() {
    initialize();
}
function resizeGenderImg() {
    //document.getElementById("gendersym").style.height = "0px";
    var v = document.getElementById("nameinformation").offsetHeight;
    if ( v < 105 ) v = 105;
    document.getElementById("gendersym").style.height = ( v - 5 ) + "px";
}
function showFavorites() {
    var uri = "/favorites.html";
    document.location = uri;
}
function addToFavorites(event, index) {
    var primaryDoc = parent?parent:this;
    primaryDoc.doAddToFavorites(event, index );
}
function doAddToFavorites( event, index ) {
    if ( currentUsername ) {
        refreshBabyList("addtofavorites", "index="  + index );
    } else {
        var dlg = new LoginDialog();
        dlg.initialize(parent?parent:null);
        dlg.showLogin( refreshBabyList, "addtofavorites", "index="  + index );
    }
}
function favoritesFrameLoaded() {
    sizeParentFrame("babylist");
}
function infoFrameLoaded() {
    resizeGenderImg();
    if ( parent.frames['babylistframe'] && parent.frames['babylistframe'].getSelectedBabyIndex )
        setSelectedBaby(parent.frames['babylistframe'].getSelectedBabyIndex());
    sizeParentFrame("babyinfo");
    if ( typeof(parent.placeFooter) == "function" ) parent.placeFooter();
}
function babyInfoFrameLoaded() {
    sizeParentFrame("babynameinfo");
}
function babylistFrameLoaded() {
    sizeParentFrame("babylist");
    var instr = parent.document.getElementById("babyselectinstr");
    if ( instr ) {
        instr.style.display = (getBabyCount()>1)?"":"none";
    }
    instr = parent.document.getElementById("babybuildnameinstr");
    if ( instr ) {
        instr.innerHTML = (getBabyCount()>1)?"build your names":"build your name";
    }
    setSelectedBaby(getSelectedBabyIndex());
    if ( getBabyMessage() ) {
        showConfirm( getBabyMessage(), null, null, 5000 );
    }
    initialize();
}
function sizeParentFrame(name) {  // called from info frame onload
    var theHeight=document.body.offsetHeight;
    parent.document.getElementById(name).style.height=(theHeight + 20) + "px";
    var fel = parent.document.getElementById(name + "frame");
    if ( fel ) fel.style.height=(theHeight + 20) + "px";
}
function swapGraph(el, newSrc, onLoaded) {
    el.innerHTML = "<img src=\""+newSrc+"\" onload=\""+onLoaded+"\" />";
}
function swapInfoGraph() {
    var el = document.getElementById( "infograph" );
    var img = ( el.getAttribute("expanded") == "true" )?"/bbnimages/moc/graph-collapsed-flash.jpg":"/bbnimages/moc/graph-flash.jpg";
    swapGraph( el, img, "infoFrameLoaded()" );
    el.setAttribute( "expanded", ( el.getAttribute("expanded") == "true" )?"false":"true");
}
function swapNameInfoGraph(event, id, fl, letter ) {
    var el = document.getElementById( id );
    var img = ( el.getAttribute("expanded") == "true" )?"/bbnimages/moc/graph-collapsed-flash.jpg":"/buildnamegraph.jpg?letter="+letter+"&fl=" + fl;
    swapGraph( el, img, "babylistFrameLoaded()" );
    el.setAttribute( "expanded", ( el.getAttribute("expanded") == "true" )?"false":"true");
}
function swapNameInfoGraph2( event, id, nameid ) {
    var el = document.getElementById( id );
    var img = ( el.getAttribute("expanded") == "true" )?"/bbnimages/moc/graph-collapsed-flash.jpg":"/buildnamegraph.jpg?nameid="+nameid;
    swapGraph( el, img, "infoFrameLoaded()" );
    el.setAttribute( "expanded", ( el.getAttribute("expanded") == "true" )?"false":"true");
}
function addABaby() { // called from info frame on click
    refreshBabyList( "addbaby");
}
function setfirstname(index, name, id) { // called from info frame on click
    refreshBabyList( "setname&index=" +index + "&firstname=" + name + "&firstid="  + id);
}
function setmiddlename(index, name, id) { // called from info frame on click
    refreshBabyList( "setname&index=" +index + "&middlename=" + name + "&middleid="  + id);
}
function nameFieldKeyPressed(e, ix, name){
    e = getEvent(e);
    var key=e.keyCode || e.which;
    if (key==13){
        cancelEvent(e);
        nameChangedHandler( getEventSrc( e ), ix, name );
    }
}
function nameChanged( event, ix, name) {
    var el = getEventSrc(getEvent( event ) );
    nameChangedHandler( el, ix, name );
}
function getElem( name ) {
    var ret = document.getElementById(name);
    if ( !ret ) ret = parent.document.getElementById(name);
    return ret;
}
function showGray() {
    var d = parent?parent:this;
    var wh = d.getWindowWH();
    var xy = d.getScrollXY();
    var graybk = getElem("graybk");
    setAbsPosition( graybk, xy[0], xy[1], wh[0], wh[1] );
    graybk.style.display = "";
}
function hideGray() {
    var graybk = getElem("graybk");
    graybk.style.display = "none";
}
var nameChangeEnabled = true; // prevent trouble with onblur and confirm box
function nameChangedHandler( el, ix, name) {
    if ( !nameChangeEnabled ) return;
    nameChangeEnabled = false;
    if ( el.value != el.getAttribute("origval") )
    {
        var orig = el.getAttribute("origval" );
        var fromtxt = "";//(orig)?(" from " + el.getAttribute("origval" )):""; (keep it short)
        showConfirm( "Do you want to change Baby " + indexToLetter( ix ) +  "'s " + name + " name" + fromtxt + " to " +  el.value,
            function() {
                nameChangeEnabled = false;
                refreshBabyList( "setname&index="+ ix + "&" + name + "name=" + el.value );
                nameChangeEnabled = true;
            },
            function() {
                nameChangeEnabled = false;
                el.value = el.getAttribute("origval");
                nameChangeEnabled = true;
            });
    }
    nameChangeEnabled = true;
}
function removebaby(index) { // called from info frame on click
    showConfirm ("Do you want to remove Baby "+indexToLetter(index)+"?",
        function() {refreshBabyList( "removebaby&index=" +index + "&name=" + name);},
        function() {});
}
function selectBaby(index) { // called from info frame on click
    refreshBabyList( "selectbaby&index=" +index + "&name=" + name);
}
function refreshBabyList( action, params) {
    var mainPage = parent?parent:this;
    var listFrameEl = mainPage.document.getElementById("babylist");
    listFrameEl.style.display = "";
    var frm = mainPage.document.createElement("form");
    listFrameEl = mainPage.document.getElementById("babylistframe");
    frm.setAttribute("action", "babylist.html" );
    frm.setAttribute("target", "babylistframe" );
    frm.setAttribute("method", "POST" );
    var pq = new PageQuery("?" + (action?"actionid=" + action:"") + (params?"&"+params:""));
    var keys = pq.getParameters();
    for ( var i=0;i<keys.length;i++ ) {
      var v = pq.getValue( keys[i] );
      var hi = mainPage.document.createElement("input");
      hi.setAttribute("type", "hidden" );
      hi.setAttribute("name", keys[i] );
      hi.setAttribute("value", v );
      frm.appendChild( hi );
    }
    mainPage.document.body.appendChild( frm );
    frm.submit();
    mainPage.document.body.removeChild( frm );
    //listFrameEl.setAttribute( "src", uri);
    //listFrameEl.refresh();
}
function indexToLetter(ix) {
    var arr = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
    'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
    'X', 'Y', 'Z');
    if ( ix < 26 && ix >= 0) return arr[ix];
}
function setSelectedBaby( ix  ) {
    if ( typeof(parent.frames['babyinfoframe'].location) != "undefined" &&
        parent.frames['babyinfoframe'].location != "about:blank")
        parent.frames['babyinfoframe'].selectIndex( ix );
}
function returnToTopFromFrame() {
    var cl = parent.location.href;
    cl = cl.split("#")[0];
    parent.location.href = cl + "#topofpage";
}

function showLogin() {
    var dlg = new LoginDialog();
    dlg.initialize( parent?parent:window);
    dlg.showLogin(refreshPage,  "signin", "" );
}


function showConfirm(msg, yes, no, tmout ) {
    if ( parent ) parent.showConfirm2( msg, yes, no, tmout );
    else showConfirm2( msg, yes, no, tmout );
}


function showConfirm2(msg, yes, no, tmout ) {
    if ( (parent?parent:window).currentDialog ) {
    } else {
        var dlg = new ConfirmDialog();
        dlg.initialize(parent?parent:window);
        dlg.showConfirm( msg, yes, no );
        if ( !yes && !no ) dlg.setTimeout( tmout );
    }
}

function blank( id ) {
    var e = document.getElementById(id);
    if ( e ) e.value = "";
}


var currentUsername = null;
function getCurrentUsername() { return currentUsername; }
function setCurrentUsername( un ) {
    currentUsername = un;
    document.getElementById("usersmenu").style.display = un?"":"none";
    document.getElementById("signinlink").innerHTML = un?"Sign out":"Sign in";
    document.getElementById("favoriteslink").innerHTML = un + "'s Favorites";
}
function signInLinkClicked() {
  if ( ! currentUsername ) {
    showLogin();
  } else {
    setCurrentUsername( null );
    refreshPage( "signout" );
  }
}


var globalDlgId = 0;
var Dialog = function() {}

Dialog.prototype.initialize = function(theMainFrame) {
    this.mainFrame = theMainFrame;
    this.mainDocument = theMainFrame.document;
}

function closeDialogIfCurrent( dlgId ) {
   var p = this;
   if ( p.parent ) p = p.parent;
   if ( p.currentDialog ) {
       if ( p.currentDialog.dialogId == dlgId ) {
           p.currentDialog.close( "cancel" );
       }
   }
}

Dialog.prototype.setTimeout = function(timeout) {
    setTimeout( "closeDialogIfCurrent("+this.dialogId+");", timeout );
}

Dialog.prototype.showDialog = function( theDialogDivName, width, height, flashDivName ) {
    if ( !this.mainFrame.currentDialog ) {
        this.mainFrame.showGray();
        this.dialogDivName = theDialogDivName;
        var dlg = this.mainDocument.getElementById(this.dialogDivName);
        var wh = this.mainFrame.getWindowWH();
        var xy = this.mainFrame.getScrollXY();
        var x = Math.max( 0, (wh[0] - width)/2 + xy[0] );
        var y = Math.max( 0, (wh[1] - height)/2 + xy[1] );
        dlg.style.left = x + "px";
        dlg.style.top = y + "px";
        dlg.style.display = "";
        this.dialogId = globalDlgId++;
        dlg.style.zIndex = 500;
        if ( !supportsTransparentWmode() && flashDivName ) {
            var flashholder = this.mainDocument.getElementById(flashDivName);
            if ( flashholder ) flashholder.style.display = "none";
            this.flashDivName = flashDivName;
        }
        this.mainDocument.onkeypress = this.keyPressDialogHandler;
        this.mainFrame.currentDialog = this;
        this.mainFrame.focus();
    } else {
        alert("Unable to show dialog: " + theDialogDivName );
    }
}

 Dialog.prototype.keyPressDialogHandler = function(event) {
   var dialog = parent?parent.currentDialog:currentDialog;
   var conf = dialog.mainDocument.getElementById(dialog.dialogDivName);
   if ( conf.style.display == "" ) {
     event = getEvent(event);
     if (this.eatKeystrokes ) {
        cancelEvent( event );
     }
     var characterCode = 0;
     if(event && event.which){ //if which property of event object is supported (NN4)
        characterCode = event.which //character code is contained in NN4's which property
     }
     else{
        characterCode = event.keyCode //character code is contained in IE's keyCode property
     }
     if ( characterCode == 27 ) {
        dialog.close( "cancel" );
     } else if ( characterCode == 13 ) {
        //dialog.close( "enter" );
     }
   } else {
   }
}

Dialog.prototype.close = function( theAction ) {
    this.dialogId = -1;
    this.doclose( theAction );
}

Dialog.prototype.doclose = function( theAction ) {
   var dlg = this.mainDocument.getElementById(this.dialogDivName);
   if ( dlg.style.display == "" ) {
      this.action = theAction;
      dlg.style.display = "none";
      if ( this.mainFrame.currentDialog == this ) this.mainFrame.currentDialog = null;
      this.mainFrame.hideGray();
      if ( !supportsTransparentWmode() && this.flashDivName ) {
            var flashholder = document.getElementById(this.flashDivName);
            if ( !flashholder ) flashholder = parent.document.getElementById(this.flashDivName);
            if ( flashholder ) flashholder.style.display = "";
      }
   }
}



var LoginDialog = function() {
}

LoginDialog.prototype = new Dialog();

LoginDialog.prototype.showLogin = function( func, action, params ) {
    this.func = func;
    this.requestAction = action;
    this.refreshParams = params;
    this.mainFrame.blank( "password1");
    this.mainFrame.blank( "password2a");
    this.mainFrame.blank( "password2b");
    this.mainFrame.blank( "emailaddrs1");
    this.mainFrame.blank( "emailaddrs2");
    this.mainFrame.blank( "name2");
    this.mainDocument.getElementById("webuserpopupmessage").innerHTML = "";
    this.mainDocument.getElementById("password2astar").style.display = "none";
    this.mainDocument.getElementById("password2bstar").style.display = "none";
    this.mainDocument.getElementById("emailaddrs2star").style.display = "none";
    this.mainDocument.getElementById("name2star").style.display = "none";
    this.mainDocument.getElementById("emailaddrs1star").style.display = "none";
    this.mainDocument.getElementById("password1star").style.display = "none";
    this.showDialog( "webuserpopup", 800, 500, "flashholder" );
    this.mainDocument.getElementById("emailaddrs1").focus();
}


LoginDialog.prototype.encodeRequestParam = function(txt) {
     txt = txt.replace(/ /g,"%20");
     txt = txt.replace(/\//g,"%2F");
     txt = txt.replace(/\?/g,"%3F");
     txt = txt.replace(/=/g,"%3D");
     txt = txt.replace(/&/g,"%26");
     txt = txt.replace(/@/g,"%40");
     return txt;
}

LoginDialog.prototype.goodPw = function( pw ) {
     return pw.length > 4 && pw.indexOf(" ") == -1;
}

LoginDialog.prototype.close =  function( action ) {
    var allowclose = true;
    if ( action == "signin" ) {
        var email = this.mainDocument.getElementById("emailaddrs1").value;
        var password = this.mainDocument.getElementById("password1").value;
        this.mainDocument.getElementById("emailaddrs1star").style.display = email?"none":"";
        this.mainDocument.getElementById("password1star").style.display = password?"none":"";
        if ( !password || !email ) {
          allowclose = false;
          this.mainDocument.getElementById("webuserpopupmessage").innerHTML = "Please enter an email address and password.";
        } else {
          this.mainDocument.getElementById("webuserpopupmessage").innerHTML = "";
          this.refreshParams += "&signin=true&email=" + this.encodeRequestParam(email) + "&password=" + this.encodeRequestParam(password);
        }
    } else if ( action == "signup" ) {
        var email = this.mainDocument.getElementById("emailaddrs2").value;
        var name = this.mainDocument.getElementById("name2").value;
        var pw1 = this.mainDocument.getElementById("password2a").value;
        var pw2 = this.mainDocument.getElementById("password2b").value;
        this.mainDocument.getElementById("emailaddrs2star").style.display = email?"none":"";
        this.mainDocument.getElementById("name2star").style.display = name?"none":"";
        this.mainDocument.getElementById("password2astar").style.display = pw1?"none":"";
        this.mainDocument.getElementById("password2bstar").style.display = pw2?"none":"";
        this.mainDocument.getElementById("webuserpopupmessage").innerHTML = "";
        if ( !email || !name || !pw1 || !pw2 ) {
          allowclose = false;
          this.mainDocument.getElementById("webuserpopupmessage").innerHTML = "Please fill in all fields.";
        } else if ( pw1 != pw2 ) {
          allowclose = false;
          this.mainDocument.getElementById("password2astar").style.display = "";
          this.mainDocument.getElementById("password2bstar").style.display = "";
          this.mainDocument.getElementById("webuserpopupmessage").innerHTML = "The passwords do not match.";
        } else if ( !this.goodPw( pw1 ) ) {
          allowclose = false;
          this.mainDocument.getElementById("password2astar").style.display = "";
          this.mainDocument.getElementById("password2bstar").style.display = "";
          this.mainDocument.getElementById("webuserpopupmessage").innerHTML = "The passwords are not valid. They must be 5 letters or numbers long without any spaces.";
        } else {
          this.refreshParams += "&signup=true&email=" + this.encodeRequestParam(email) + "&password=" + this.encodeRequestParam(pw1) +
            "&name=" + this.encodeRequestParam( name );
        }
    }

    if ( allowclose ) {
        this.doclose( action );
        if ( action && action != "cancel" && this.func ) {
            this.func( this.requestAction, this.refreshParams );
        }
    }
    //alert( action );
}


var ConfirmDialog = function() {
    this.eatKeystrokes = true;
}

ConfirmDialog.prototype = new Dialog();

ConfirmDialog.prototype.showConfirm = function ( message, yesFunc, noFunc ) {
    this.yesFunc = yesFunc;
    this.noFunc = noFunc;
    if ( !supportsTransparentWmode() ) {
        if ( noFunc ) {
            if ( confirm( message + "?" ) ) {
               yesFunc();
            } else {
                noFunc();
            }
        } else {
            alert( message );
            if ( yesFunc ) yesFunc();
        }
    } else {
        var babyconfirmmsgbox = this.mainDocument.getElementById("babyconfirmmsgbox");
        this.mainDocument.getElementById("confirmnobutton").style.display = ( this.noFunc )?"":"none";
        this.mainDocument.getElementById("confirmyesbutton").style.display = ( this.noFunc )?"":"none";
        this.mainDocument.getElementById("confirmquestionmark").style.display = ( this.noFunc )?"":"none";
        this.mainDocument.getElementById("confirmokbutton").style.display = ( !this.noFunc )?"":"none";
        babyconfirmmsgbox.innerHTML = message;
        this.showDialog( "babyconfirm", 400, 200, "flashholder" );
        this.mainDocument.getElementById("babyconfirm").focus();
    }
}
ConfirmDialog.prototype.close =  function( action ) {
    if ( ( action == "yes" || action == "ok" ) && this.yesFunc ) {
        this.yesFunc();
    }
    if ( ( action == "no" || action == "cancel" ) && this.noFunc ) {
        this.noFunc();
    }
    this.doclose( action );
}

function tabClicked( e ) {
  e = getEvent( e );
  var el = getEventSrc(e);
  var tabs;
  while ( !(tabs = el.tabs) ) {
    if ( el.parentNode ) el = el.parentNode;
  }
  tabs.click( el );
}



var Tabs = function() {}

Tabs.prototype.initialize = function(tabList, mainFrame, onclick) {
    this.tabList = tabList;
    this.mainFrame = mainFrame;
    this.mainDocument = mainFrame.document;
    this.onclick = onclick;
    this.initTabs();
}

Tabs.prototype.click = function( el ) {
    var tabIx = -1;
    for ( var i=0;i<this.tabList.length;i++ ) {
        if ( el == this.tabList[i] ) {
            tabIx = i;
            break;
        }
    }
    if ( tabIx != -1 ) {
        if ( this.canShowTab( tabIx ) ) {
            this.showTab( tabIx );
            this.tabShown( tabIx );
            if ( this.onclick ) this.onclick( tabIx );
        }
    }
}

Tabs.prototype.canShowTab = function( ix ) {
    return true;
}

Tabs.prototype.tabShown = function( ix ) {
    return true;
}

Tabs.prototype.showTab = function( ix ) {
    var six = -1;
    var x = 0;
    for ( var i=0;i<this.tabList.length;i++ ) {
        var s = ix == i;
        setClass( this.tabList[i], s?"selTab":"unselTab" );
        this.tabList[i].style.left = (x) + "px";
        this.tabList[i].style.zIndex = s?100:1;
        x += ((s?118:123) - 8);
        if ( s ) six = ix;
    }
    return true;
}

Tabs.prototype.initTabs = function() {
    var s = true;
    var x = 0;
    for ( var i=0;i<this.tabList.length;i++ ) {
        this.tabList[i].tabs = this;
        this.tabList[i].onclick = tabClicked;
        setClass( this.tabList[i], s?"selTab":"unselTab" );
        this.tabList[i].style.left = (x) + "px";
        x += ((s?118:123) - 8);
        s = false;
    }
}
function moveFavorite( gender, letter, moveamt ) {
    refreshPage("moveitem", "gender=" + gender + "&letter=" + letter + "&move=" + moveamt );
}
function submitcomments() {
 var comment = document.getElementById("questionnairetxtbox").value;
 if ( comment && comment.length > 0 ) {
   document.forms['commentsform'].submit();
 }

}




/**
 * SWFObject v1.4.4: Flash Player detection and embed - http://blog.deconcept.com/swfobject/
 *
 * SWFObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * **SWFObject is the SWF embed script formerly known as FlashObject. The name was changed for
 *   legal reasons.
 */
if(typeof deconcept == "undefined") var deconcept = new Object();
if(typeof deconcept.util == "undefined") deconcept.util = new Object();
if(typeof deconcept.SWFObjectUtil == "undefined") deconcept.SWFObjectUtil = new Object();
deconcept.SWFObject = function(swf, id, w, h, ver, c, useExpressInstall, quality, xiRedirectUrl, redirectUrl, detectKey){
    if (!document.getElementById) { return; }
    this.DETECT_KEY = detectKey ? detectKey : 'detectflash';
    this.skipDetect = deconcept.util.getRequestParameter(this.DETECT_KEY);
    this.params = new Object();
    this.variables = new Object();
    this.attributes = new Array();
    if(swf) { this.setAttribute('swf', swf); }
    if(id) { this.setAttribute('id', id); }
    if(w) { this.setAttribute('width', w); }
    if(h) { this.setAttribute('height', h); }
    if(ver) { this.setAttribute('version', new deconcept.PlayerVersion(ver.toString().split("."))); }
    this.installedVer = deconcept.SWFObjectUtil.getPlayerVersion();
    if(c) { this.addParam('bgcolor', c); }
    var q = quality ? quality : 'high';
    this.addParam('quality', q);
    this.setAttribute('useExpressInstall', useExpressInstall);
    this.setAttribute('doExpressInstall', false);
    var xir = (xiRedirectUrl) ? xiRedirectUrl : window.location;
    this.setAttribute('xiRedirectUrl', xir);
    this.setAttribute('redirectUrl', '');
    if(redirectUrl) { this.setAttribute('redirectUrl', redirectUrl); }
}
deconcept.SWFObject.prototype = {
    setAttribute: function(name, value){
        this.attributes[name] = value;
    },
    getAttribute: function(name){
        return this.attributes[name];
    },
    addParam: function(name, value){
        this.params[name] = value;
    },
    getParams: function(){
        return this.params;
    },
    addVariable: function(name, value){
        this.variables[name] = value;
    },
    getVariable: function(name){
        return this.variables[name];
    },
    getVariables: function(){
        return this.variables;
    },
    getVariablePairs: function(){
        var variablePairs = new Array();
        var key;
        var variables = this.getVariables();
        for(key in variables){
            variablePairs.push(key +"="+ variables[key]);
        }
        return variablePairs;
    },
    getSWFHTML: function() {
        var swfNode = "";
        if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // netscape plugin architecture
            if (this.getAttribute("doExpressInstall")) { this.addVariable("MMplayerType", "PlugIn"); }
            swfNode = '<embed type="application/x-shockwave-flash" src="'+ this.getAttribute('swf') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'"';
            swfNode += ' id="'+ this.getAttribute('id') +'" name="'+ this.getAttribute('id') +'" ';
            var params = this.getParams();
             for(var key in params){ swfNode += [key] +'="'+ params[key] +'" '; }
            var pairs = this.getVariablePairs().join("&");
             if (pairs.length > 0){ swfNode += 'flashvars="'+ pairs +'"'; }
            swfNode += '/>';
        } else { // PC IE
            if (this.getAttribute("doExpressInstall")) { this.addVariable("MMplayerType", "ActiveX"); }
            swfNode = '<object id="'+ this.getAttribute('id') +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'">';
            swfNode += '<param name="movie" value="'+ this.getAttribute('swf') +'" />';
            var params = this.getParams();
            for(var key in params) {
             swfNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
            }
            var pairs = this.getVariablePairs().join("&");
            if(pairs.length > 0) {swfNode += '<param name="flashvars" value="'+ pairs +'" />';}
            swfNode += "</object>";
        }
        return swfNode;
    },
    write: function(elementId){
        if(this.getAttribute('useExpressInstall')) {
            // check to see if we need to do an express install
            var expressInstallReqVer = new deconcept.PlayerVersion([6,0,65]);
            if (this.installedVer.versionIsValid(expressInstallReqVer) && !this.installedVer.versionIsValid(this.getAttribute('version'))) {
                this.setAttribute('doExpressInstall', true);
                this.addVariable("MMredirectURL", escape(this.getAttribute('xiRedirectUrl')));
                document.title = document.title.slice(0, 47) + " - Flash Player Installation";
                this.addVariable("MMdoctitle", document.title);
            }
        }
        if(this.skipDetect || this.getAttribute('doExpressInstall') || this.installedVer.versionIsValid(this.getAttribute('version'))){
            var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
            n.innerHTML = this.getSWFHTML();
            return true;
        }else{
            if(this.getAttribute('redirectUrl') != "") {
                document.location.replace(this.getAttribute('redirectUrl'));
            }
        }
        return false;
    }
}

/* ---- detection functions ---- */
deconcept.SWFObjectUtil.getPlayerVersion = function(){
    var PlayerVersion = new deconcept.PlayerVersion([0,0,0]);
    if(navigator.plugins && navigator.mimeTypes.length){
        var x = navigator.plugins["Shockwave Flash"];
        if(x && x.description) {
            PlayerVersion = new deconcept.PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
        }
    }else{
        // do minor version lookup in IE, but avoid fp6 crashing issues
        // see http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
        try{
            var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
        }catch(e){
            try {
                var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
                PlayerVersion = new deconcept.PlayerVersion([6,0,21]);
                axo.AllowScriptAccess = "always"; // throws if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
            } catch(e) {
                if (PlayerVersion.major == 6) {
                    return PlayerVersion;
                }
            }
            try {
                axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
            } catch(e) {}
        }
        if (axo != null) {
            PlayerVersion = new deconcept.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
        }
    }
    return PlayerVersion;
}
deconcept.PlayerVersion = function(arrVersion){
    this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
    this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
    this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
}
deconcept.PlayerVersion.prototype.versionIsValid = function(fv){
    if(this.major < fv.major) return false;
    if(this.major > fv.major) return true;
    if(this.minor < fv.minor) return false;
    if(this.minor > fv.minor) return true;
    if(this.rev < fv.rev) return false;
    return true;
}
/* ---- get value of query string param ---- */
deconcept.util = {
    getRequestParameter: function(param) {
        var q = document.location.search || document.location.hash;
        if(q) {
            var pairs = q.substring(1).split("&");
            for (var i=0; i < pairs.length; i++) {
                if (pairs[i].substring(0, pairs[i].indexOf("=")) == param) {
                    return pairs[i].substring((pairs[i].indexOf("=")+1));
                }
            }
        }
        return "";
    }
}
/* fix for video streaming bug */
deconcept.SWFObjectUtil.cleanupSWFs = function() {
    if (window.opera || !document.all) return;
    var objects = document.getElementsByTagName("OBJECT");
    for (var i=0; i < objects.length; i++) {
        objects[i].style.display = 'none';
        for (var x in objects[i]) {
            if (typeof objects[i][x] == 'function') {
                objects[i][x] = function(){};
            }
        }
    }
}
// fixes bug in fp9 see http://blog.deconcept.com/2006/07/28/swfobject-143-released/
deconcept.SWFObjectUtil.prepUnload = function() {
    __flash_unloadHandler = function(){};
    __flash_savedUnloadHandler = function(){};
    if (typeof window.onunload == 'function') {
        var oldUnload = window.onunload;
        window.onunload = function() {
            deconcept.SWFObjectUtil.cleanupSWFs();
            oldUnload();
        }
    } else {
        window.onunload = deconcept.SWFObjectUtil.cleanupSWFs;
    }
}
if (typeof window.onbeforeunload == 'function') {
    var oldBeforeUnload = window.onbeforeunload;
    window.onbeforeunload = function() {
        deconcept.SWFObjectUtil.prepUnload();
        oldBeforeUnload();
    }
} else {
    window.onbeforeunload = deconcept.SWFObjectUtil.prepUnload;
}
/* add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}

/* add some aliases for ease of use/backwards compatibility */
var getQueryParamValue = deconcept.util.getRequestParameter;
var FlashObject = deconcept.SWFObject; // for legacy support
var SWFObject = deconcept.SWFObject;



