function LaunchSynerge() { window.open('http://www.synergemarketing.com'); }

(function() {
var LANG_LINK = 'http://www.garofaloobgyn.com';
var videos = [
  { name: 'none', type: 'none', desc: 'Haga clic aquí para seleccionar un video' },
  { name: 'aden', type: 'swarm', code: 'adenomyosis', desc: 'Sobre Adenomiosis' },
  { name: 'cerv', type: 'swarm', code: 'cervicalcancer', desc: 'Sobre Cáncer de Cuello Uterino' },
  { name: 'dysp', type: 'swarm', code: 'cervicaldysplasia', desc: 'Sobre Displasia de Cuello Uterino' },
  { name: 'dysu', type: 'swarm', code: 'dysuterinebleeding', desc: 'Sobre Hemorragia Uterina Disfuncional (HUD)' },
  { name: 'endo', type: 'swarm', code: 'endometriosis', desc: 'Sobre Endometriosis' },
  { name: 'ovar', type: 'swarm', code: 'ovariancancer', desc: 'Sobre Cáncer de ovario' },
  { name: 'uter', type: 'swarm', code: 'uterinefibroids', desc: 'Sobre Fibroides Uterinos (Miomas uterinos)' },
  { name: 'vagi', type: 'swarm', code: 'vaginalprolapse', desc: 'Sobre Prolapso Vaginal' },
  { name: 'dvov', type: 'swarm', code: 'davincioverview', desc: 'Sobre el Sistema Quirúrgico da Vinci' },
  { name: 'dvhy', type: 'swarm', code: 'hysterectomy', desc: 'Sobre Histerectomía Asistida por el Sistema da Vinci' },
  { name: 'dvmy', type: 'swarm', code: 'myomectomyfibroid', desc: 'Sobre Miomectomía para Fibroides Uterinos asistida por el Sistema da Vinci' },
  //{ name: 'dvne', type: 'swarm', code: 'nephrectomy', desc: 'About daVinci Nephrectomy' },
  //{ name: 'dvpa', type: 'swarm', code: 'partialnephrectomy', desc: 'About daVinci Partial Nephrectomy' },
  //{ name: 'dvpr', type: 'swarm', code: 'prostatectomy', desc: 'About daVinci Prostatectomy' },
  //{ name: 'dvcy', type: 'swarm', code: 'cystectomy', desc: 'About daVinci Radical Cystectomy' },
  { name: 'dvsu', type: 'swarm', code: 'sacrouterineprolapse', desc: 'Sobre Sacrocolpopexia para Prolapso Uterino asistida por el Sistema da Vinci' },
  { name: 'dvsv', type: 'swarm', code: 'sacrovaginalprolapse', desc: 'Sobre Sacrocolpopexia para Prolapso de Cúpula Vaginal asistida por el Sistema da Vinci' }
  //{ name: 'essu', type: 'essure', desc: 'About Permanent Birth Control' }
];

var vidTypes = { none: { width: 580, height: 373 },
                 swarm: { width: 580, height: 373 },
                 essure: { width: 635, height: 520 } }

var videoPages = [
  { page: 'ADENOMYOSIS', video: ['aden'] },
  { page: 'ABNORMAL BLEEDING', video: ['dysu', 'uter'] },
  { page: 'ENDOMETRIAL ABLATION', video: ['dysu'] },
  { page: 'HYSTERECTOMY', video: ['dysu', 'uter','dvhy'] },
  { page: 'PELVIC PAIN', video: ['endo', 'uter'] },
  { page: 'ENDOMETRIOSIS', video: ['endo'] },
  { page: 'UTERINE FIBROIDS', video: ['uter','dvmy'] },
  { page: 'PELVIC ORGAN PROLAPSE', video: ['vagi'] },
  { page: 'DA VINCI SURGICAL SYSTEM', video: ['dvov'] },
  { page: 'SACROCOLPOPEXY', video: ['dvsu','dvsu'] }
  //{ page: 'PERMANENT BIRTH CONTROL', video: ['essu'] }
];

Garofalo = {
  menuNodes: null,
  overlay: null,

  // Find the menu entry representing this page and save menu nodes & alter the
  // class of the 'hits'
  LoadMenu: function( aMenuID, aMenuClass, aDropdownClass) {
    // Get all of the anchors in the menu hierarchy, iterate through them until 
    // the href attribute matches the page name
    var anchors = document.getElementById(aMenuID).getElementsByTagName('a');
    for ( var i = 0; i < anchors.length; ++i) {
      if ( window.location.href == anchors[i].href)
        break;
    }
    // If there wasn't a match, set the current page to the home page(first anchor). 
    // This happens when http:/www.example.com , i.e, no file name, is requested.
    var thePage = ( i < anchors.length) ? i : 0;
    // Now we go back up the tree and get the nodes and save them for later;
    Garofalo.menuNodes = [];
    var ptr = anchors[thePage];
    while( ptr && ptr.id != aMenuID) {
      if ( ptr.nodeName.toLowerCase() == 'li')
        Garofalo.menuNodes.push(ptr);
      ptr = ptr.parentNode;
    }

    // Put 'em in top-down order
    Garofalo.menuNodes.reverse();
    
    var theNode, theClass;
    for ( var i in Garofalo.menuNodes) {
      theNode = Garofalo.menuNodes[i].getElementsByTagName('a')[0];
      theClass = theNode.className;
      if ( theClass) theClass += ' ';
      theNode.className = theClass + ((i == 0) ? aMenuClass : aDropdownClass);
    }
  },
  
  // Populate the Section title on the left side of each page
  SetSectionTitle: function() {
    var theNodes = Garofalo.menuNodes;
    var len = theNodes.length;
    var theTag = len ? theNodes[len-1].getElementsByTagName('a')[0] : null;
    var theTitle = theTag ? (theTag.title || theTag.innerHTML).toUpperCase() : '';
    document.getElementById('left_title_bar').innerHTML = theTitle;
  },
  
  // Generate the breadcrumbs for each page
  SetBreadcrumbs: function() {
    var theFrag = document.createDocumentFragment();
    Garofalo.MakeBreadcrumb( theFrag, document.getElementById('header_nav'), true);

    for ( var i in Garofalo.menuNodes) 
      Garofalo.MakeBreadcrumb( theFrag, Garofalo.menuNodes[i]);
      
    var theBC = document.getElementById('breadcrumbs');
    if ( !theBC.firstChild)
      theBC.appendChild(theFrag);
    else
      theBC.replaceChild(theFrag, theBC.firstChild);
  },
  MakeBreadcrumb: function( aFrag, aNode, aFirst) {
    var theAnchor = aNode.getElementsByTagName('a')[0];
    if ( !aFirst)
      aFrag.appendChild(document.createTextNode(' > '));
    var theNode = theAnchor.cloneNode(true);
    theNode.removeAttribute('style');
    aFrag.appendChild(theNode);
  },
  

  InitVideoArea: function() {
    // Set up a listener on the left side for clicks in the video
    // selection area
    $ID('video_area').style.cursor = 'pointer';
    $EV('video_area', 'click', Garofalo.HandleVideoClick);    

    var theTitle = $ID('left_title_bar').innerHTML;
    for ( var i in videoPages) {
//alert( theTitle+', '+videoPages[i].page);
      if ( videoPages[i].page == theTitle) {
        Garofalo.CreateVideoLinks(i);
        break;
      }
    } 
  },
  LoadLeftNav: function() {   // Not used, but keep with left side stuff, just in case
    var menuLen = Garofalo.menuNodes.length - 1;
    if ( menuLen < 1) return;
    var theNav = Garofalo.menuNodes[menuLen-1].parentNode.cloneNode(true);
    document.getElementById('left_nav').appendChild( theNav);
  },

  CreateVideoLinks: function( aPage) {
    var theDiv, theDesc;  
    for ( var i in videoPages[aPage].video) {
      theDesc = "";
      for ( var j in videos) {
        if ( videos[j].name == videoPages[aPage].video[i]) {
          theDesc = videos[j].desc;
          break;
        }
      }
      if ( theDesc) { 
        theDiv = document.createElement('div');
        theDiv.id = videos[j].name;
        Neaux.Dom.SetStyle(theDiv, { paddingLeft: '28px' } );
        theDesc = theDesc.replace( /^about/gi, '>');
        theDiv.appendChild(document.createTextNode(theDesc));
        $ID('video_container').appendChild(theDiv);
      }
    }
  },
  
  CloseVideo: function() {
    Garofalo.overlay.style.display = 'none';
    $ID('ovMediaBox').innerHTML = '';
    $ID('ovSelect').selectedIndex = 0;
  },

  HandleVideoDropdown: function( aEvt) {
    var i = aEvt.target.selectedIndex;
    Garofalo.LoadVideo( i);
  },

  CreateVideoOverlay: function() {
    var theCanvas = document.createElement('div');
    Neaux.Dom.SetStyle( theCanvas, { 
          position: 'absolute', top: '0px', left: '0px', height: '100%', width: '100%', 
          zIndex: 100, backgroundImage: 'url(images/popup_bg.png)', backgroundRepeat: 'repeat', 
          display: 'none' } );

    // The popup window area
    var theFrame = document.createElement('div');
    theFrame.id = 'ovFrame';
    Neaux.Dom.SetStyle( theFrame, { position: 'absolute', background: '#ffffff', padding: '30px' } );
    
     // The dropdown of available videos
    var theDescription = document.createElement('div');
    Neaux.Dom.SetStyle( theDescription, { position: 'absolute', top: '5px', left: '30px', fontSize: '12px'} );
    
    var theDropdown = document.createElement('select');
    theDropdown.id = 'ovSelect';
    theDropdown.style.fontSize = '12px';
    var theOption = document.createElement('option');
    for ( var i in videos) {
      theOption = document.createElement('option');
      theOption.value = i;
      theOption.appendChild(document.createTextNode(videos[i].desc));
      theDropdown.appendChild(theOption);
    }
    theDescription.appendChild(theDropdown);
    theFrame.appendChild( theDescription);

    var theClose = document.createElement('div');
    Neaux.Dom.SetStyle( theClose, { position: 'absolute', top: '5px', right: '30px', 
                                    border: 'thin solid #000000', cursor: 'pointer', 
                                    fontSize: '12px', padding: '1px' } );
    theClose.appendChild(document.createTextNode('CERRAR'));
    theFrame.appendChild(theClose);
   
    // The Box into which the video plays
    var theMediaBox = document.createElement('div');
    theMediaBox.id = 'ovMediaBox';
    theMediaBox.style.border = '1px solid navy';
    theFrame.appendChild( theMediaBox);

    // The Doctor's name
    var theDoc = document.createElement('div');
    Neaux.Dom.SetStyle( theDoc, { position: 'absolute', bottom: '4px', left: '0px', 
                                  width: '100%', fontWeight: 'bold', color: '#aaaaaa', 
                                  textAlign: 'center', zIndex: 100 } );
    theDoc.appendChild(document.createTextNode('Dr. John M. Garofalo, M.D. 203.855.3535'));
    theFrame.appendChild(theDoc);

    theCanvas.appendChild( theFrame);
    document.body.appendChild(theCanvas);

    $EV(theDropdown, 'change', Garofalo.HandleVideoDropdown);
    $EV(theClose, 'click', Garofalo.CloseVideo);
    Garofalo.overlay = theCanvas;
  },

  LoadFlash: function( aVid) {
    var theHtml = '';
    switch( videos[aVid].type) {
      case 'swarm':
        var url = 'http://www.swarminteractive.com/subscriptions/viewer.swf?';
        var foo = 'client=2285&disclaimer=off&menuaccess=no&lang=spanish&openthis=';
        var bar = url + foo + videos[aVid].code;
        theHtml = 
          '<object type="application/x-shockwave-flash" ' +
          'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" ' +
          'width="580" height="373" data="'+bar+'">' +
          '<param name="movie" value="'+bar+'">' +
          '<param name="quality" value="high"></object>';
        break;
      case 'essure':
        theHtml = '<iframe src="http://www.essure.com/PopUps/FlashDemo.aspx" width="635" height="520" scrolling="no"></iframe>';
        break;
    }
    $ID('ovMediaBox').innerHTML = theHtml;
  },

  LoadVideo: function( aVid) {
    if ( !Garofalo.overlay)
      Garofalo.CreateVideoOverlay();
    $ID('ovSelect').selectedIndex = aVid;
    var type = videos[aVid].type;
    var w = vidTypes[type].width, h = vidTypes[type].height;
    var vh = window.innerHeight || document.documentElement.clientHeight;
    var vw = window.innerWidth || document.documentElement.clientWidth;
    var t = (vh - h)/2;
    var l = (vw - w)/2;
    
    Neaux.Dom.SetStyle($ID('ovMediaBox'), { width: w+'px', height: h+'px'} );
    Neaux.Dom.SetStyle($ID('ovFrame'), { top: t + 'px', left: l + 'px', width: w + 'px', 
                                         height: h + 'px' } );
                                         
    Garofalo.LoadFlash(aVid);
    Garofalo.overlay.style.display = 'block';
  },

  LookupVideo: function( aName) {
    for ( var i in videos) {
      if ( videos[i].name == aName) 
        return i;
    }
    return 0;
  },

  LaunchVideo: function( aName) {
    Garofalo.LoadVideo(Garofalo.LookupVideo( aName));
  },

  HandleVideoClick: function( aEvt) {
    switch ( aEvt.target.id) {
      case 'video_area':
      case 'video_text':
        Garofalo.LoadVideo(0);
        break;
      default:
        Garofalo.LaunchVideo( aEvt.target.id);
    }
  },


  // For links in a container having class = link_block_ext, set the
  // anchors such that a new browser window is launched
  CreateExternalLinks: function() {
    var theSections = $CLS('link_block_ext');
    for ( var s in theSections) {
      var theAnchors = theSections[s].getElementsByTagName('a');
      for ( var i=0; i < theAnchors.length; i++) {

        theAnchors[i].setAttribute('target', '_blank');
        //var theHref = theAnchors[i].href;
        //var theLink = 'javascript:Garofalo.LaunchWindow(theHref)';  // Save window id on first launch & reuse.
        //theAnchors[i].setAttribute('href',theLink);
      }
    }
  },
  LaunchWindow: function( href) {
    window.open(href);
  },

  ContactFormResults: function() {
    var theResponse = this.Text();
    var theStatus = theResponse.indexOf('ERR:');
    var theStatusText = 'Your request was submitted, Thank you!';
    var theNA = $ID('form_notification_area');

    if ( theStatus >= 0) {
      theStatus += 'ERR:'.length;
      theStatusText = 'The following error(s) were encounted:<br/>' + theResponse.substr(theStatus);
    }
    else {
      var fbody = $ID('form_body');
      fbody.style.display = 'none';
      theNA.style.margin = '8px 0';
      $ID('form_button_container').style.display = 'none';
      $ID('frmContact').reset();
    }
    theNA.innerHTML = theStatusText;
    theNA.style.color = theStatus < 0 ? 'green' : 'red';
    theNA.style.textAlign = 'center';
    theNA.style.display = 'block';
    $ID('btnSubmit').disabled = false;
  },
  
  DoContactForm: function() {
    $ID('btnSubmit').disabled = true;
    $ID('form_notification_area').style.display = 'none';
    var theElms = $ID('frmContact').elements;
    var theMap = [];
    for ( var i=0; i < theElms.length; ++i) {
      var elm = theElms[i];
      if ( !elm.name) continue;
      switch (elm.tagName.toLowerCase()) {
        case 'input':
        case 'textarea':
          if ( elm.type == 'radio') {
            if ( elm.checked) 
              theMap[elm.name] = elm.value;
          }
          else
            theMap[elm.name] = elm.value;
          break;
        case 'select':
          theMap[elm.name] = elm.value;
          break;
      }
    }
    var theAction = 'assets/garofalo.php?' + Neaux.Http.EncodeQueryString(theMap);
    var theOptions = { immediate: true, onload: Garofalo.ContactFormResults, action: theAction,
                       method: 'get' };
    var theRequest = new Neaux.Request( theOptions);
  },

  HandleLanguageClick: function() {
    var theURL = LANG_LINK + window.location.pathname;
    window.location = theURL;
  },

  Start: function() { 
    Garofalo.LoadMenu( 'header_nav', 'menu_selected', 'drop_selected');
    Garofalo.SetSectionTitle();
    Garofalo.SetBreadcrumbs();
    Garofalo.InitVideoArea();
    Garofalo.CreateExternalLinks();
    $EV('header_lang_link','click',Garofalo.HandleLanguageClick);
  },
  
  toString: function() { return '[Garofalo]'; }
}
Neaux.AddStartup( Garofalo.Start);
})();

