var contextPath;

// text zoom control

textzoom = {};
textzoom.LEVELS = [51.5, 62.5, 74.5, 90.0];
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>');

// special case link initialization

APOPTOSIS_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>';
LINK_DISCLAIMER = '<div class="warning">The link you have selected will take you away from this site to one that is not owned or controlled by Genentech, Inc. Genentech, Inc. makes no representation as to the accuracy of the information contained on sites we do not own or control.\n\n Genentech does not recommend and does not endorse the content on any third-party websites. Your use of third-party websites is at your own risk and subject to the terms and conditions of use for such sites.</div>';

function initializeLinks() {
    var anchor, i = 0;
    while ((anchor = document.getElementsByTagName('a')[i++]) != null) {
        if (hasClass(anchor, 'external'))
            anchor.setAttribute('target', '_blank');
        if (hasClass(anchor, 'third-party'))
            addEvent(anchor, 'click', disclaimLink);
        if (hasClass(anchor.parentNode, 'apop-link'))
            addEvent(anchor, 'click', warnApop);
    }
}

function disclaimLink(e) {
    confirmRedirect(e, LINK_DISCLAIMER);
}

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

function goPat(e) {
    var target = getTarget(e);
    location = rewriteHref(target.href);
    cancelEvent(e);
}

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 = 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('ResearchApoptosis.com');
    dialog.render(document.body);
    dialog.show();
}

addEvent(window, 'load', initializeLinks);

// 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;
}

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

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

function addEvent(obj, type, fn) {
    if (obj.addEventListener)
        obj.addEventListener(type, fn, false);
    else if (obj.attachEvent) {
        obj['e' + type + fn] = fn;
        obj[type + fn] = function() { obj['e' + type + fn](window.event); }
        obj.attachEvent('on' + type, obj[type + fn]);
    }
}

function removeEvent(obj, type, fn) {
    if (obj.removeEventListener)
        obj.removeEventListener(type, fn, false);
    else if (obj.detachEvent) {
        obj.detachEvent('on' + type, obj[type + fn]);
        obj[type + fn] = null;
        obj['e' + type + fn] = null;
    }
}

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();
}

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

function getTransport() {
    if (typeof window.XMLHttpRequest != 'undefined')
        return new XMLHttpRequest();
    try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (ex) {}
    try { return new ActiveXObject('MSXML2.XMLHTTP'); } catch (ex) {}
    return null;
}

function getMatchingTextNodes(node, pattern) {
    if (node == null || node.nodeType != 1)
        return;
    var nodes = [];
    var root = node;
    while (node != null) {
        if (node.hasChildNodes())
            node = node.firstChild;
        else if (node != root && null != (next = node.nextSibling))
            node = next;
        else {
            next = null;
            for ( ; node != root; node = node.parentNode) {
                next = node.nextSibling;
                if (next != null) break;
            }
            node = next;
        }
        if (node != null && node.nodeType == 3) {
            while ((match = pattern.exec(node.data)) != null)
                nodes.push([node, match]);
        }
    }
    return nodes;
}

function urlencode(str) {
    var encode = (typeof encodeURIComponent == 'function')
        ? encodeURIComponent
        : escape;
    return encode(str).replace(/%20/g,'+');
}

Array.prototype.indexOf = function(item, start) {
    for (var i = (start || 0); i < this.length; i++) {
        if (this[i] == item) return i;
    }
    return -1;
}

String.prototype.trim = function() {
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}


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

function setContextPath(path) {
  contextPath = path;
}

function init_slidedeck() {
    meltmedia.example.slidedeck = new meltmedia.widget.SlideDeck("slidedeck", 
    { 
        fixedcenter:true,
        constraintoviewport:true,
        underlay:"none",
        close:true,
        visible:false,
        draggable:false,
        modal:true,
        slides:[{src:contextPath+"/images/slidedecks/slidedeck1/Slide1.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide2.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide3.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide4.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide5.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide6.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide7.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide8.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide9.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide10.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide11.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide12.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide13.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide14.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide15.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide16.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide17.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide18.jpg",title:"Apoptosis mode of action slide deck",description:""},
                {src:contextPath+"/images/slidedecks/slidedeck1/Slide19.jpg",title:"Apoptosis mode of action slide deck",description:""}], 
        fulldeck:contextPath+"/content/Apoptosis-MOA-slide-kit-2007.ppt",
        count:true,
        width:"442px"
    } );
    meltmedia.example.slidedeck.render();
}

function showSlideDeck() {
    meltmedia.example.slidedeck.show();
}

