
/** init 
   * initialize all the main features
   */

function init() {
  this.initializeLinks();
}

/* =Text-Zoom
--------------------------------------------------------------*/

// text zoom control

textzoom = {};
textzoom.LEVELS = [62.5, 77, 85, 93];
textzoom.DEFAULT_INDEX = 1;
textzoom.index = Number(getCookie('zoom') || textzoom.DEFAULT_INDEX);
textzoom.zoom = function(n) {
    switch (n) {
        case +1: textzoom.index = Math.min(textzoom.index+1, textzoom.LEVELS.length-1); break;
        case -1: textzoom.index = Math.max(textzoom.index-1, 0); break;
        default: textzoom.index = textzoom.DEFAULT_INDEX; break;
    }
    document.body.style.fontSize = textzoom.LEVELS[textzoom.index] + '%';
    setCookie('zoom', textzoom.index, 365, '/');
};
document.write('<style type="text/css" media="screen">body { font-size: ' + textzoom.LEVELS[textzoom.index] + '%; }</style>');

// utility functions

function getCookie(name) {
    var pattern = new RegExp('(^|; )' + name + '=([^;]*)');
    var m = document.cookie.match(pattern);
    return m && unescape(m[2]);
}

function setCookie(name, value, days, path, domain, secure) {
    var c = name + '=' + escape(value);
    var expires = null;
    if (days)
        expires = new Date(new Date().getTime() + (days * 24 * 60 * 60 * 1000));
    if (expires)
        c += '; expires=' + expires.toUTCString();
    if (path)
        c += '; path=' + path;
    if (domain)
        c += '; domain=' + domain;
    if (secure)
        c += '; secure';
    document.cookie = c;
}

/* =Specific-Browser-Selection
--------------------------------------------------------------*/

var css_browser_selector = function() {
  var 
    ua=navigator.userAgent.toLowerCase(),
    is=function(t){ return ua.indexOf(t) != -1; },
    h=document.getElementsByTagName('html')[0],
    b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
    os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
  var c=b+os+' js';
  h.className += h.className?' '+c:c;
}();


/* =Clear-Search-Box
--------------------------------------------------------------*/

function clearText(thefield){
  if (thefield.defaultValue==thefield.value)
  thefield.value = "";
};


var Questions = Class.create({
 
  answers: [],
  
  curDisplayId: "state-1",
 
  initialize: function(){
    this.initQuestion();
  },
  
  initQuestion: function(){
    this.answers = $$(".answers"); 
    this.answers.each(function(a){
      var yes = a.select(".btn1")[0];
      var no = a.select(".btn2")[0];
      yes.observe("click", this.onItemClick.bindAsEventListener(this,a,"yes"));
      no.observe("click", this.onItemClick.bindAsEventListener(this,a,"no"));
    }.bind(this)); 
  },
  
  onItemClick: function(ev){ 
    ev.stop();
    var args = $A(arguments);
    var ans = args[1];
    ans.removeClassName("no");
    ans.removeClassName("yes");
    ans.addClassName(args[2]);
    if($$(".yes",".no").length==this.answers.length){
      var newId = ""
      if($$(".yes").length==this.answers.length){
        newId = "state-3";
      }else{
        newId = "state-2";
      };
      if(newId!=this.curDisplayId){
        $(this.curDisplayId).style.display = "none";
        $(newId).style.display = "block";
        this.curDisplayId = newId;
      }
    }    
  }

});

/* =PI Append
--------------------------------------------------------------*/

function initializeLinks() {
  var links = $$("a.pi-download");
  for (var i=0;i<links.length;i++) {
    links[i].observe('click', this.downloadPi.bind(this));
  }
  
  $$('a.hcp-link').each(function(hcplink) {

    hcplink.observe('click', warnApop);
  
  });
  
}
  
function downloadPi(ev) {
  var el = ev.findElement("a");
  var piUrl = "http://www.gene.com/gene/products/information/pdf/rituxan-prescribing.pdf";
  var url = el.href;
  if(url!=piUrl && url.endsWith(".pdf")){
    if(isIe6() || Prototype.Browser.WebKit){
      window.open(piUrl, "PI");
    } else {
      location.href = piUrl;
    }
  }
}

function getClasses(element) {
    return element.className.trim().split(/\s+/);
}

function hasClass(element, c) {
    return getClasses(element).indexOf(c) != -1;
}

function addClass(element,newCs){
  if(!(hasClass(element,newCs))) element.className += (" "+newCs);
}

function removeClass(element, rmCs){
    if(hasClass(element,rmCs)){
     var cs=element.className.match(' '+rmCs)?' '+rmCs:rmCs;
     element.className = element.className.replace(cs,""); 
    }
}


function getTarget(e) {
    var e = e || window.event;
    return e.target || e.srcElement;
}

function rewriteHref(href) {
    var m;
    if (m = location.href.match(/\/(apop)\/([^\/]+)\//)) {
        href = href.replace(/\/(apop)\/.*/, '/' + '$1' + '/' + m[2] + '/');
    }
    return href;
}

function confirmRedirect(e, disclaimer) {
    var target = getTarget(e);
    if(target.tagName == "IMG" || target.tagName == "STRONG" || target.tagName == "SPAN") target = target.parentNode;
    var href = target.href;
    cancelEvent(e);

    function handleOK() {
        dialog.hide();
        if (hasClass(target, 'external'))
            window.open(href);
        else
            location = href;
    }

    function handleCancel() {
        dialog.hide();
        dialog.destroy();
    }

    var dialog = new YAHOO.widget.SimpleDialog('dialog',
                                              {
                                                  buttons: [ { text: 'OK', handler: handleOK },
                                                             { text: 'Cancel',  handler: handleCancel } ],
                                                  close: false,
                                                  constraintoviewport: true,
                                                  draggable: false,
                                                  fixedcenter: true,
                                                  modal: true,
                                                  icon: YAHOO.widget.SimpleDialog.ICON_WARN,
                                                  text: disclaimer,
                                                  underlay: 'none',
                                                  visible: false,
                                                  width: '315px'
                                              });
    dialog.setHeader('Rituxan.com/lymphoma');
    dialog.render(document.body);
    dialog.show();
}

function cancelEvent(e) {
    var e = e || window.event;
    e.cancelBubble = true;
    if (typeof e.stopPropagation == 'function')
        e.stopPropagation();
    e.returnValue = false;
    if (typeof e.preventDefault == 'function')
        e.preventDefault();
}

RituxanLymphoma_WARNING = '<div class="warning">The information contained in this section of the site is intended for U.S. healthcare professionals only. Click "OK" if you are a healthcare professional.</div>';

function warnApop(e) {
    if (!location.pathname.match(/\/apop\//)) {
        var target = getTarget(e);
        target.href = rewriteHref(target.href);
        confirmRedirect(e, RituxanLymphoma_WARNING);
    }
}

function isIe6() {
  return document.getElementById && document.all&&(navigator.appVersion.indexOf("MSIE 6.")>=0);
}

document.observe("dom:loaded",function(){
  var questions = new Questions();
});

