// JavaScript Document

var indicationOptions = {   "0" : "Select an option",
          "1" : "Medical Thromboprophylaxis", 
          "2" : "Hip Replacement Surgery",
          "3" : "Abdominal Surgery",
          "4" : "Oncology: Symptomatic VTE",
          "5" : "UA and NQWMI"  };
          
var hipRegimenOptions = {   "0" : "Select an option",
          "1" : "Postoperative", 
          "2" : "Preoperative - Evening Before Surgery",
          "3" : "Preoperative - Day of Surgery"};

var abdominalOptions = {   "0" : "Select an option",
          "1" : "At-risk patients", 
          "2" : "High-risk patients",
          "3" : "Patients with malignancy"};
          
var indicationModalWindowTitles = { '53' : 'FRAGMIN at Home',
          '54' : 'Getting Ready to Take FRAGMIN',
          '55' : '<span class="injectionStepColor">Step 1:</span> Select the Injection Site',
          '56' : '<span class="injectionStepColor">Step 2:</span> Wash Hands',
          '57' : '<span class="injectionStepColor">Step 3:</span> Open the Pack',
          '58' : '<span class="injectionStepColor">Step 4:</span> Prepare the Injection Area',
          '59' : '<span class="injectionStepColor">Step 5:</span> Carefully Remove the Syringe From the Pack',
          '60' : '<span class="injectionStepColor">Step 6:</span> Make Sure That FRAGMIN Is Injected Into Fatty Tissue',
          '61' : '<span class="injectionStepColor">Step 7:</span> The Syringe Should Be Held with the Free Hand',
          '62' : '<span class="injectionStepColor">Step 8:</span> Carefully Remove the Needle From Your Skin',
          '63' : '<span class="injectionStepColor">Step 9:</span> Safely Dispose of the Used Syringe When Done'};
          
var ind = 0;
var reg = 0;
var wgt = 0;
var lk = 0;

var redirectURL = "http://us.eisai.com/product.asp?ID=236";

$(document).ready(function() {    

  // Check for cookie
  if ($.cookies.get("siteSelected") == undefined)
  {  
    // Show site selection modal
    showSiteSelectionModal();
    
  }
  else
  {
    var cookieValue = $.cookies.get("siteSelected");
    
    switch (cookieValue)
    {
      case "hcp":
        // Do something very special
        break;
      case "pc":
        showSiteSelectionModal();
        break;
    }
  };

//Initial load of first chapter and calling movie function
  if (document.getElementById("videoList")) { 
    var i = $('#videoList').val();
      moviePlayer(i);
  };
  
//Calls movie function for appropriate chapter when list is changed
  $("#videoList").change(function() {
    var i = $('#videoList').val();
    moviePlayer(i);  
  });


//Call function for links leaving site
interstitial();
  

$("#topnav li > a").each (function(index) {                

$(this).hover (
function () {
        $('#headerPic0').hide();
        $('#headerPic' + (index + 1) ).css("z-index" , "2" ).fadeIn(500);
},
function () {
        $('#headerPic' + (index + 1) ).hide().css( "z-index" , "0" );
        $('#headerPic0').fadeIn();
  
});

  });
  
  
  
//Animation for leaf fade in on left navigation for occupation content pages  
  $("#leftNav li a:not(li li li a)").prepend("<b></b>");
    $("#leftNav li a:not(li li li a):not(li li.active a)").hover(
        function(){
            $(this).animate({"padding-left":"20px"},{duration:200,queue:false}).addClass("cur");
            $(">b",this).animate({opacity:1,"margin-left":"-20px"},{duration:200,queue:false});
        },
        function(){
            $(this).animate({"padding-left":"0px"},{duration:200,queue:false}).removeClass("cur");
      $("leftNav li.active a").animate({"padding-left":"20px"},{duration:200,queue:false}).removeClass("cur");
            $(">b",this).animate({opacity:0,"margin-left":"10px"},{duration:200,queue:false});
        }
    );
  

  // Indications Modal Window
  $("a.modalLink1").fancybox({
    'padding'    :  0,
    'autoDimensions':  false,
    'width'     :   850,
    'height'    :  450,
    'transitionIn'  :  'fade',
    'transitionOut'  :  'fade',
    'speedIn'    :  200, 
    'speedOut'    :  200,
    'onComplete'  :  prepModalContent_indications
  });  
  
  // Injections Modal Window
  $("a.modalLink2").click(function() {
    
    $('#fragmin_video').fadeOut(200);
    
    $.fancybox({
      'href'      :  '/index.php?id=66',
      'padding'    :  0,
      'autoDimensions':  false,
      'width'     :   850,
      'height'    :  450,
      'transitionIn'  :  'fade',
      'transitionOut'  :  'fade',
      'speedIn'    :  200, 
      'speedOut'    :  200,
      'onComplete'  :  prepModalContent_injection,
      'onClosed'    :  injectionModalClosed
    });  
    

  });  

  // Dosing Calculator Modal Window
  $("a.modalLink3").fancybox({
    'padding'    :  0,
    'autoDimensions':  false,
    'width'     :   650,
    'height'    :  550,
    'transitionIn'  :  'fade',
    'transitionOut'  :  'fade',
    'speedIn'    :  200, 
    'speedOut'    :  200,
    'onComplete'  :  prepModalContent_dosing
  });  
  
  $("#slider").easySlider({
    continuous: true,
    auto:      true,
    pause:      5000
  });

   $('div#indicationModalFlyoutMask2').toggle(
   function() {
        $('div#indicationModalFlyout').animate({
      'width'     :   560
    })
        $(this).animate({
      'width'     :   595
    })
    $(this).css({
      'background-image' : 'url(assets/templates/base/css/images/bkg_openFlyout.png)',
      'background-repeat' : 'no-repeat',
      'background-position' : '100% 0'
    });
  }, 
  function() {
       $('div#indicationModalFlyout').animate({
      'width'     :   160
    })
        $(this).animate({
      'width'     :   195
    })
    $(this).css({
      'background-image' : 'url(assets/templates/base/css/images/bkg_closedFlyout.png)',
      'background-repeat' : 'no-repeat',
      'background-position' : '100% 0'
    });
  });

});

function showSiteSelectionModal() 
{
  $.fancybox({
    'href'      :  '/index.php?id=69',
    'padding'    :  0,
    'autoDimensions':  false,
    'width'     :   800,
    'height'    :  450,
    'transitionIn'  :  'fade',
    'transitionOut'  :  'fade',
    'speedIn'    :  200, 
    'speedOut'    :  200,
    'showCloseButton'  : false,
    'hideOnOverlayClick' : false,
    'onComplete'  :  prepModalContent_siteSelection
  });
}

function hideAllModalContent()
{
  $('#modalWindowContent').children().each(
       function(){
         $(this).hide();
       }
  );
  
  $('#modalWindowReferences').children().each(
       function(){
         $(this).hide();
       }
  );
}

function resetLinks()
{  
  $('#contentLinks li').children().each(
       function(){
         $(this).css({
           'padding' : '0',
        'background-image' : 'none'
      });
       }
  );
}

function showContainer(contentContainer, contentLinkID, referenceContainer)
{
  hideAllModalContent();
  resetLinks();
  
  $("#" + contentContainer).show();
  $("#" + referenceContainer).show();
  $("#" + contentLinkID).css({
    'padding' : '0 0 0 20px',
    'background-image' : 'url(assets/templates/base/css/images/leaf.gif)',
    'background-position' : '0 0',
    'background-repeat' : 'no-repeat'
  });
}

function showContainer_injection(contentContainer, contentLinkID, contentID)
{
  var headerColor = "";
  var htmlString = "";
  
  hideAllModalContent();
  resetLinks();
  
  // Figure out where to get the header title
  if ( indicationModalWindowTitles[contentID] != undefined)
  {
    // Pull title from the hard-coded array which is used to override default
    htmlString = indicationModalWindowTitles[contentID];  
  }
  else
  {
    // Get the title attribute of the content container and use in header
    htmlString = $("#" + contentContainer).attr("title");
  }
  $('#modalWindowHeader_Injection h1').html(htmlString);
  
  // Show the appropriate contentContainer div
  $("#" + contentContainer).show();
  $("#" + contentLinkID).css({
    'padding' : '0 0 0 20px',
    'background-image' : 'url(assets/templates/base/css/images/leaf.gif)',
    'background-position' : 'left',
    'background-repeat' : 'no-repeat'
  });
}

function prepModalContent_indications()
{  
  $('#modalWindowContent div:first').show();
  $('#modalWindowReferences div:first').show();

  $('#contentLinks li a:first').css({
    'padding' : '0 0 0 20px',
    'background-image' : 'url(assets/templates/base/css/images/leaf.gif)',
    'background-position' : 'left',
    'background-repeat' : 'no-repeat'
  });
}

function prepModalContent_injection()
{  
  $('#modalWindowContent div:first').fadeIn(400);

  $('#modalWindowHeader_Injection h1').html( $('#contentLinks li a:first').html() );

  $('#contentLinks li a:first').css({
    'padding' : '0 0 0 20px',
    'background-image' : 'url(assets/templates/base/css/images/leaf.gif)',
    'background-position' : 'left',
    'background-repeat' : 'no-repeat'
  });
}

function injectionModalClosed()
{
  $('#fragmin_video').show();
}

function prepModalContent_siteSelection()
{  
  $("#siteSelectionLeftButton").click(function() {  
    setSiteSelctionCookie("hcp");
  });

  $("#siteSelectionRightButton").click(function() {  
    setSiteSelctionCookie("pc");
    window.location = redirectURL;
  });
}

function setSiteSelctionCookie(userType)
{
  var cookieOptions = {
    expiresAt: new Date( 2012, 12, 1 )
  };
  
  $.cookies.set("siteSelected", userType, cookieOptions);
  
  $.fancybox.close();
}

function prepModalContent_dosing()
{  
  $("#indicationSelect").addOption(indicationOptions, false);
  
  $('#indicationSelect').change(function() {
      
      var selectedIndication = document.getElementById("indicationSelect").selectedIndex;
      
      switch (selectedIndication)
      {
        case 0:
          $('#weightInputContainer').hide();
          $('#regimenSelectContainer').hide();
          $('#dosingCalculateButton').hide();
          document.getElementById("indicationSelect").selectedIndex = 0;
          document.getElementById("regimenSelect").selectedIndex = 0;
          return;
        case 1:
          $('#weightInputContainer').hide();
          $('#regimenSelectContainer').hide();
          $('#dosingCalculateButton').show();
          document.getElementById("regimenSelect").selectedIndex = 0;
          break;
        case 2:
          $("#regimenSelect").addOption(hipRegimenOptions, false);
          $('#weightInputContainer').hide();
          $('#regimenSelectContainer').show();
          $('#dosingCalculateButton').hide();
          document.getElementById("dosingPounds").value = "";
          document.getElementById("dosingKilograms").value = "";
          break;
        case 3:
          $("#regimenSelect").addOption(abdominalOptions, false);
          $('#weightInputContainer').hide();
          $('#regimenSelectContainer').show();
          $('#dosingCalculateButton').hide();
          document.getElementById("dosingPounds").value = "";
          document.getElementById("dosingKilograms").value = "";
          break;
        default:
          $('#regimenSelectContainer').hide();
          $('#weightInputContainer').show();
          document.getElementById("regimenSelect").selectedIndex = 0;
      }
      
  });
  
  $('#regimenSelect').change(function() {
      
      var selectedRegimen = document.getElementById("regimenSelect").selectedIndex;
      
      switch (selectedRegimen)
      {
        case 0:
          return;
        default:
          $('#dosingCalculateButton').show();
      }
  });
  
  $("input").numeric();
  
  $('#dosingCalculateButton').click(function() {
    
    ind = document.getElementById("indicationSelect").selectedIndex;
    reg = document.getElementById("regimenSelect").selectedIndex;
    wgt = 0;
    lk = 0;
    loadUrl ="";
    
    if (document.getElementById("dosingPounds").value == "" && document.getElementById("dosingKilograms").value == "")
    {
      wgt = 0;
    } 
    else if (document.getElementById("dosingPounds").value != "")
    {
      wgt = document.getElementById("dosingPounds").value;
      lk = 1;
    }
    else if (document.getElementById("dosingKilograms").value != "")
    {
      wgt = document.getElementById("dosingKilograms").value;
      lk = 2;
    }
    else
    {
      wgt = 0;
    };
    
    loadUrl = "/index.php?id=68&ind=" + ind + "&reg=" + reg + "&wgt=" + wgt + "&lk=" + lk;
    
    $('#dosingCalculatorContainer').load(loadUrl, calculationScreenPrep);
    
  });
  
  $('#dosingPounds').keyup(function() {
    
    document.getElementById("dosingKilograms").value = "";
    
    if ( document.getElementById("dosingPounds").value.length > 0 )
    {
      $('#dosingCalculateButton').show();
    }
    else
    {
      $('#dosingCalculateButton').hide();
    };
  });
    
  $('#dosingKilograms').keyup(function() {
    
    document.getElementById("dosingPounds").value = "";

    if ( document.getElementById("dosingKilograms").value.length > 0 )
    {
      $('#dosingCalculateButton').show();
    }
    else
    {
      $('#dosingCalculateButton').hide();
    };
    
  });  
  
}

function calculationScreenPrep()
{
  $('#dosingBackButton').click(function() {
    
    loadUrl = "/index.php?id=67";
    
    $('#dosingCalculatorContainer').load(loadUrl, prepModalContent_dosing);
    
  });
  
  $('#indicationValue').html(indicationOptions[ind]);
  
  optionArray = [];
  
  switch (ind)
  {
    case 2:
      optionArray = hipRegimenOptions;
      $('#regimenValue').html(optionArray[reg]);
      break;
    case 3:
      optionArray = abdominalOptions;
      $('#regimenValue').html(optionArray[reg]);
      break;
    default:
      $('#regimenRow').hide();
  }  
  
  if (wgt > 0)
  {
    var unit = 0;
    
    if (lk == 1)
    {
      unit = "LBS";
    }
    else
    {
      unit = "KG"
    }
    
    $('#weightValue').html(wgt + " " + unit);
  }
  else
  {
    $('#weightRow').hide();
  }
  
}

//loads the different chapters for the video
function moviePlayer(i)
{
  var htmlText = '';
  
    if ($.browser.safari) 
    { // If no, use Flash.  
      htmlText += '<video width="517" height="295" controls poster="assets/videos/videoPoster.jpg">';
      htmlText += '<source src="assets/videos/PATIENT_INJECTION_INSTRUCTIONS_' + i + '.mp4" type="video/mp4" /></video>';
    $('#fragmin_video').html(htmlText);
  }
  else
  {           
    flowplayer("fragmin_video", { src: "assets/videos/flowplayer-3.2.4.swf", wmode: "transparent" }, {
        clip: {
             autoPlay: true
         },
        playlist: [
        {url: '/assets/videos/videoPoster.jpg', scaling: 'orig'},
        {url: '/assets/videos/PATIENT_INJECTION_INSTRUCTIONS_' + i + '.f4v', autoPlay: false}
      ]
    });  
  };
}


function interstitial() {



 
   $("a[target=_blank]").click(function (){
   
   var current = $(this).attr("href");

   $.fancybox({
    'padding'    :  35,
    'autoDimensions':  false,
    'width'     :   300,
    'height'    :  200,
    'transitionIn'  :  'fade',
    'transitionOut'  :  'fade',
    'speedIn'    :  200, 
    'speedOut'    :  200,
'content'    :   '<p>You have selected a link that will take you away from Fragmin.com.</p><br /><a style="border: 1px solid #333; padding: 5px; margin: 0 auto;" href="' + current + '" target="_blank">Proceed</a>'
  });  
   
   return false;
//   window.open(http://fragmin.stg4.gsw-w.com/73)
//   alert('You are now leaving FRAGMIN.  Click OK');
   
   });

}
