/**
* CPYB Homepage Ad Render
* Version 0.5 - 11/29/2007
* @author Warren Krewenki
*
* This package is distributed under the BSD license.
* For full license information, see LICENSE.TXT
*
* Based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
* Originally written to make use of the Prototype framework, and Script.acalo.us, now altered to use jQuery.
*
*
**/
$(function () {
    if (homePromo) {
        if ((homePromo.starts != null && homePromo.ends != null) || (homePromo.starts != '' && homePromo.ends != '')) {
            if (homePromo.starts < new Date() && homePromo.ends > new Date()) {
                window['homePromoAutoHide'] = null;

                // Create & Add the promo... 
                /*
                $(".homepage .header")
                .after(unescape("%3Cdiv%20class%3D%22home-promo%22%20style%3D%22display%3A%20none%3B%20z-index%3A%2010%3B%20position%3A%20absolute%3B%20width%3A%20" + homePromo.width + "%3B%20height%3A%20" + homePromo.height + "%3B%20left%3A%2050%25%3B%20margin-left%3A%20-25%25%3B%22%3E%3Cdiv%20class%3D%22promo-item%22%20style%3D%22display%3A%20block%3B%20position%3A%20relative%3B%22%3E%3Ca%20style%3D%22display%3A%20block%3B%20box-shadow%3A%200%200%2035px%20%23000%3B%22%20href%3D%22" + homePromo.href + "%22%20title%3D%22" + homePromo.title + "%22%" + ((homePromo.target) ? "%20target=%22" + homePromo.target + "%22" : "") + "3E%3Cimg%20src%3D%22" + homePromo.src + "%22%20/%3E%3C/a%3E%3Ca%20class%3D%22trigger%22%20title%3D%22Hide promo%22%20%href%3D%22%23%22%3E%3Cimg%20src%3D%22/lib/images/blank.gif%22%20style%3D%22display%3A%20block%3B%20top%3A%200px%3B%20width%3A%2027px%3B%20height%3A%2033px%3B%20background%3A%20url%28%27/lib/images/ads/home/ad-tab.png%27%29%20no-repeat%20scroll%200pt%200pt%20transparent%3B%20position%3A%20absolute%3B%20right%3A%20-27px%3B%22%20/%3E%3C/a%3E%3C/div%3E%3C/div%3E"));
                */
                $("body form")
                .append(unescape("%3Cdiv%20class%3D%22home-promo-container%22%20%20style%3D%22display%3A%20none%3B%20z-index%3A%2010%20%21important%3B%20position%3A%20relative%3B%20margin-top%3A%20-700px%3B%22%3E%3Cdiv%20style%3D%22width%3A%20960px%3B%20margin%3A%200%20auto%3B%20%22%3E%3Cdiv%20class%3D%22home-promo-wrapper%22%20style%3D%22display%3A%20block%3B%20position%3A%20relative%3B%20width%3A%20" + homePromo.width + "px%3B%20height%3A%20" + homePromo.height + "px%3B%20margin%3A%200%20auto%3B%20%22%3E%3Cdiv%20class%3D%22home-promo%22%20style%3D%22display%3A%20block%3B%20position%3A%20absolute%3B%20width%3A%20" + homePromo.width + "px%3B%20height%3A%20" + homePromo.height + "px%3B%20margin%3A%200%3B%22%3E%3Ca%20href%3D%22" + homePromo.href + "%22%20title%3D%22" + homePromo.title + "%22%" + ((homePromo.target) ? "%20target=%22" + homePromo.target + "%22" : "") + "%22%20style%3D%22display%3A%20block%3B%20box-shadow%3A%200%200%2035px%20%23000%3B%22%3E%3Cimg%20src%3D%22" + homePromo.src + "%22%3E%3C/a%3E%3Ca%20href%3D%22%23%22%20class%3D%22trigger%22%3E%3Cimg%20style%3D%22display%3A%20block%3B%20top%3A%200px%3B%20width%3A%2022px%3B%20height%3A%2026px%3B%20background%3A%20url%28%27/lib/images/ads/home/ad-tab.png%27%29%20no-repeat%20scroll%200pt%200pt%20transparent%3B%20position%3A%20absolute%3B%20right%3A%20-22px%3B%22%20src%3D%22/lib/images/blank.gif%22%3E%3C/a%3E%3C/div%3E%3C/div%3E%3C/div%3E%3C/div%3E"));

                $(".home-promo").data('isminimized',false);
                
                homePromo.minimize = function () {
                    window.clearTimeout(window['homePromoAutoHide']);
                    window['homePromoAutoHide'] = null;

                    $(".home-promo")
                    .animate({
                        "left": ((homePromo.offset.left+homePromo.width) * -1) + "px",
                    }, 1000, 'easeOutBack', function () {
                        $(this).data('isminimized',true);
                        $(".home-promo .trigger").attr('title', 'Show promo');
                        $(".home-promo .trigger img").css({ "background-position": "0 -26px" });
                    });
                }

                homePromo.maximize = function () {
                    $(".home-promo")
                    .animate({
                        "left": "0px",
                    }, 1000, 'easeOutExpo', function () {
                        $(this).data('isminimized',false);
                        $(".home-promo .trigger").attr('title', 'Hide promo');
                        $(".home-promo .trigger img").css({ "background-position": "0 0px" });
                        if (window['homePromoAutoHide'] == null) window['homePromoAutoHide'] = window.setTimeout(homePromo.minimize, homePromo.timeout);
                    });
                }

                $(".home-promo-wrapper .home-promo, .home-promo-wrapper .trigger").hover(
                    function (e) { if ($(".home-promo").data('isminimized') == false && $(".home-promo:animated").length == 0) { window.clearTimeout(window['homePromoAutoHide']); window['homePromoAutoHide'] = null; } }, //in
                    function (e) { if ($(".home-promo").data('isminimized') == false && window['homePromoAutoHide'] == null && $(".home-promo:animated").length == 0) window['homePromoAutoHide'] = window.setTimeout(homePromo.minimize, homePromo.timeout); } //out
                );

                $(".home-promo-wrapper .trigger").click(function (e) {
                    e.preventDefault();

                    if ($(".home-promo").css("left") == ((homePromo.offset.left+homePromo.width) * -1) + "px") homePromo.maximize();
                    else homePromo.minimize();
                });



                $(".home-promo-container")
                .delay(450)
                .fadeIn('normal', function () {
                    homePromo.offset = $(".home-promo").offset();
                    window['homePromoAutoHide'] = window.setTimeout(homePromo.minimize, homePromo.timeout);
                });


                // Store offset
                $(window).bind('resize',function(e) {
                    if ($(".home-promo").data('isminimized') == true) {
                        $(".home-promo").css({"left":"0px"});
                        homePromo.offset = $(".home-promo").offset();
                        $(".home-promo").css({"left":((homePromo.offset.left+homePromo.width) * -1) + "px"});
                    }
                });
            }
        }
    }
});
