// ExpandableBanners 2.0
// Fixed the errors when banner_ad does not exist on the web page.
// Additional Fix: Pass the pointer to jax only when banner_ad exists.
// Copyright (c) 2010 JaxCore.com
// Visit http://www.expandablebanners.com to learn how to make amazing advertisements for any website!

var ExpandableBanners = (function () {

    var extimer = null;
    var flashURL = null, flashWidth = 0, flashHeight = 0;
    var bannerId = 'banner_ad';  // this is the ID of the link that will activate the expanded ad
    var bannerAd, expandedAd, expandedImg, closeDiv, closeImg;
    var bannerTarget = "top";
    var link = "";  // this link will be automatically populated by the URL of the anchor tag whose ID is "banner_ad"
    var imageURL, closeURL, closeAlign;
    var altText = 'advertisement';
    var vertDirection = 'down';
    var horzDirection = 'right';
    var bannerFlash = false;
    var expandedFlash = false;
    var expandedSwf = null;
    var bannerFlashId = '';
    var bannerAdObject = '';
    var bannerAdEmbed = '';
    var extimer = null;
    var targetNew = true;

    function setBannerId(id) {
        bannerId = id;
    }
    function setLink(url) {
        link = url;
        //echo("banner link : "+url);
    }
    function setTargetNew() {
        targetNew = true;
    }
    function setExpandedImage(url) {
        imageURL = url;
    }
    function setDirection(vert, horz) {
        vertDirection = vert;
        horzDirection = horz;
    }
    function setCloseImage(url, alignV, alignH) {
        closeURL = url;
        closeAlignV = alignV;
        closeAlignH = alignH;
    }
    function setAlt(text) {
        altText = text;
    }

    function setBannerFlash() {
        bannerFlash = true;
    }

    function setExpandedFlash(swf, width, height) {
        expandedFlash = true;
        flashURL = swf;
        flashWidth = width;
        flashHeight = height;
    }

    function createElements() {
        //echo('creating elements');

        if (bannerFlash) {
            /*var ba = id("banner_ad");
            bannerAdObject = ba.getElementsByTagName("object")[0];
            bannerAdEmbed = ba.getElementsByTagName("embed")[0];*/
        }
        else {
            var b = dom.id(bannerId);
            if (isNode(b) && b.nodeName == 'A' && !!b.childNodes && isNode(b.childNodes[0]) && b.childNodes[0].nodeName == 'IMG' && isString(b.href)) {
                bannerAd = dom.id(bannerId);
                if (!bannerAd) {
                    //echo('no banner Ad? '+dom.id(bannerId)+' '+bannerId);
                    return; // error?
                }
                //echo('found anchor '+bannerAd.id);
            }
        }

        expandedAd = document.createElement('div');
        expandedAd.id = "expanded_ad";
        style.set(expandedAd, {
            position: 'absolute',
            visibility: 'hidden'
        });

        //echo('created '+expandedAd.id);

        var s = '';
        if (flashURL) {
            var attr = {
                id: 'expanded_swf',
                embedId: 'expanded_embed',
                width: flashWidth,
                height: flashHeight,
                codebase: 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0',
                classid: 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
                type: 'application/x-shockwave-flash',
                pluginspage: 'http://www.macromedia.com/go/getflashplayer'
            };
            param = {
                movie: flashURL,
                quality: 'high',
                wmode: 'transparent'
            };
            s = '<object id="' + attr.id + '" classid="' + attr.classid + '" codebase="' + attr.codebase + '" width="' + attr.width + '" height="' + attr.height + '">' +
			'<param name="movie" value="' + param.movie + '" />' +
			'<param name="quality" value="' + param.quality + '" />' +
			'<param name="wmode" value="' + param.wmode + '" />' +
			'<embed id="' + attr.embedId + '" src="' + param.movie + '" quality="' + param.quality + '" wmode="' + param.wmode + '" pluginspage="' + attr.pluginspage + '" type="' + attr.type + '" width="' + attr.width + '" height="' + attr.height + '" />' +
			'</object>';
        }
        else if (targetNew) {
            if (imageURL == undefined || imageURL == "") {
                s = '<a id="eb_expandedLink" href="' + link + '" target="_new"><img id="expanded_img" src="http://nursing.advanceweb.com/images/spacer.gif" alt="' + altText + '" border="0" /></a>';
            }
            else {
                s = '<a id="eb_expandedLink" href="' + link + '" target="_new"><img id="expanded_img" src="' + imageURL + '" alt="' + altText + '" border="0" /></a>';
            }


        }
        else {
            if (imageURL == undefined || imageURL == "") {
                s = '<a href="javascript://" onclick="ExpandableBanners.clickAd()"><img id="expanded_img" src="http://nursing.advanceweb.com/images/spacer.gif" alt="' + altText + '" border="0" /></a>';
            }
            else {
                s = '<a href="javascript://" onclick="ExpandableBanners.clickAd()"><img id="expanded_img" src="' + imageURL + '" alt="' + altText + '" border="0" /></a>';
            }
        }


        //echo(s);
        expandedAd.innerHTML = s;
        dom.prepend(expandedAd);

        if (flashURL) {
            expandedSwf = dom.id('expanded_swf');
            //echo('created '+expandedSwf.id);
        }
        else {
            if (expandedAd.childNodes[0].childNodes[0].nodeName == 'IMG') {
                expandedImg = expandedAd.childNodes[0].childNodes[0];
                //echo('created '+expandedImg.id);
            }
        }

        if (closeURL) {
            closeDiv = document.createElement('div');
            closeDiv.id = "expanded_close";
            closeDiv.style.position = 'absolute';
            closeDiv.style.cursor = 'pointer';
            closeDiv.style.backgroundColor = "red";
            closeDiv.innerHTML = '<a href="javascript://" onclick="ExpandableBanners.closeAd()"><img id="expanded_closeimg" src="' + closeURL + '" alt="Close" border="0" /></a>';
            dom.append(closeDiv, expandedAd);

            closeImg = dom.id("expanded_closeimg");
            if (!closeImg) return false;

            closeDiv.childNodes[0].style.outline = "none";
            //echo('created '+closeDiv+', '+closeImg.id);
        }
        return true;
    }

    function attachEvents() {
        if (expandedAd) { //bannerAd && 
            if (!bannerFlash) {

                dom.addEvent(bannerAd, "mouseover", function (e) {
                    //echo('mouseover event');
                    openAd();
                });

                dom.addEvent(expandedAd, "mouseover", function (e) {
                    //echo("close timer cancelled");
                    clearTimeout(extimer);
                });

                dom.addEvent(expandedAd, "mouseout", function (e) {
                    //echo('mouseout event');
                    if (!closeURL) {
                        //echo("close timer started");
                        extimer = setTimeout(function () {
                            closeAd();
                        }, 500);
                    }
                });

            }
            dom.addEvent(window, "resize", function (e) {
                resetAd();
            });
            return true;
        }
        return false;
    }

    function clickAd() {
        closeAd();
        if (bannerTarget == 'top') top.location = link;
        else document.location.href = link;
    }

    function openAd() {
        //echo('openAd()');
        resetAd();
        style.hide('banner_ad');
        style.show(expandedAd);
    }

    function closeAd() {
        //echo('closeAd()');
        style.show('banner_ad');
        style.hide(expandedAd);
    }

    function main() {
        //echo('ExpandableBanners main');
        var c = createElements();
        var a = attachEvents();
        if (c && a) {
            if (!bannerFlash) {
                //banner_ad may not be loaded due to rotation. So the check is needed  
                if (bannerAd != undefined) {
                    setLink(bannerAd.href);
                }
            }
        }

        if (targetNew) {
            var l = dom.id('eb_expandedLink');
            if (l && l.href) l.href = link;
        }

        // set size of div to same as movie
        if (bannerFlash) {
            var ba = dom.id('banner_ad');
            var obj, c;
            for (var i = 0; i < ba.childNodes.length; i++) {
                c = ba.childNodes[i];
                if (c.nodeName == 'EMBED' || c.nodeName == 'OBJECT') {
                    //echo(i+' '+c.nodeName);
                    obj = c;
                    break;
                }
            }
            if (obj && obj.width && obj.height) {
                //echo(obj.nodeName+' '+obj.width+' '+obj.height);
                style.size(ba, obj.width, obj.height);
            }
        }

        //uncomment the following line for testing
        //else alert('ExpandableBanners Error: failed to find elements '+c+' '+a);
    }

    function getElementPosition(e) {
        var x = y = 0;
        while (e != null) {
            x += e.offsetLeft;
            y += e.offsetTop;
            e = e.offsetParent;
        }
        return { x: x, y: y };
    }

    function resetAd() {
        var expandedItem;
        if (flashURL) {
            expandedItem = expandedSwf;
            expandedWidth = flashWidth;
            expandedHeight = flashHeight;
        }
        else {
            expandedItem = expandedAd;
            expandedWidth = expandedImg.width;
            expandedHeight = expandedImg.height;
        }
        style.maxZ(expandedAd);
        style.size(expandedAd, expandedWidth, expandedHeight);

        var img, bannerW, bannerH, x, y, dim;
        if (bannerFlash) {
            var ba = dom.id('banner_ad');

            dim = getElementPosition(ba);
            //echo('bannerFlash '+dim.x+' '+dim.y);
            bannerW = parseInt(ba.style.width);
            bannerH = parseInt(ba.style.height);
        }
        else {
            img = bannerAd.getElementsByTagName('img')[0];
            bannerW = img.width || 0;
            bannerH = img.height || 0;
            dim = dom.dimensions(img);
            //echo('banner '+dim.x+' '+dim.y);
        }
        var x = y = 0;

        if (horzDirection == 'center') {
            x = dim.x - expandedWidth / 2 + bannerW / 2;
        }
        else if (horzDirection == 'left') {
            x = dim.x - expandedWidth + bannerW - 2;
        }
        else if (horzDirection == 'right') {
            x = dim.x;
        }

        if (vertDirection == 'center') {
            y = dim.y - expandedHeight / 2 + bannerH / 2 - 2;
        }
        else if (vertDirection == 'up') {
            y = dim.y - expandedHeight + bannerH; // - expandedImg.height + bannerH;
        }
        else if (vertDirection == 'down') {
            y = dim.y;
        }
        style.move(expandedAd, x, y);

        if (!!closeURL && !!expandedImg && !!closeImg) {
            x = y = 0;
            if (closeAlignV == 'bottom') {
                y = expandedImg.height - closeImg.height;
            }
            if (closeAlignH == 'right' && !!expandedImg && !!closeImg) {
                x = expandedImg.width - closeImg.width;
            }
            style.move(closeDiv, x, y);
        }
    }

    return {
        main: main,
        clickAd: clickAd,
        openAd: openAd,
        closeAd: closeAd,
        setLink: setLink,
        setTargetNew: setTargetNew,
        setBannerFlash: setBannerFlash,
        setExpandedImage: setExpandedImage,
        setExpandedFlash: setExpandedFlash,
        setCloseImage: setCloseImage,
        setAlt: setAlt,
        setBannerId: setBannerId,
        setDirection: setDirection
    };

})();

//pass the pointer to jax only when banner_ad exists.
$(document).ready(function () {
   var b = dom.id("banner_ad");
    if (typeof b != 'undefined') {
        //alert("pointer is passed to jax");
        jax.run(function () {
            ExpandableBanners.main();
        });
    }

});




