function AdRotator(element) {
    this.rotation_delay = 5000; // Miliseconds
    this.animation_speed = 700; // Miliseconds

    this.cur_image = 0;
    this.skip_one = 0;  // Indicates to skip doing a rotation at next interval
    this.element = element;
    this.ads = this.element.children('[id!=dots]').not('map');

    this.ads.each(function() {
        $('#dots').append('<img class="inactive_dot" src="' + WEB_IMAGE_ROOT + 'Homepage/position_indicator_inactive.png">');
    });

    this.active_dot = $('#dots .active_dot');

    if (this.ads.length > 0) {
        $(this.ads[this.cur_image]).show();
    }

    if (this.ads.length > 1) {
        var that = this;

        // Add the click functions to the inactive dots
        $.each($('#dots .inactive_dot'), function (index, node) {
            $(node).click(function() {
                that.UserSelect(index)
            });
        });

        this.DrawDots();
        $('#dots').show();
        window.setInterval(function() {
            that.NextImage();
        }, this.rotation_delay);
    }
}

AdRotator.prototype.NextImage = function() {
    if (this.skip_one > 0) {
        this.skip_one -= 1;
        return;
    }

    var next_image_num = this.cur_image + 1;
    if (next_image_num >= this.ads.length) {
        next_image_num = 0;
    }

    this.GoToImage(next_image_num);
}

AdRotator.prototype.RotateImages = function(old_image, active_image) {
    active_image.css('left', '169px').show().animate({
        'left': '0px'
    }, this.animation_speed);
    old_image.animate({
        'left': '-170px'
    }, this.animation_speed, function() {
        $(this).hide()
    });
}

AdRotator.prototype.UserSelect = function(image_num) {
    this.GoToImage(image_num);
    this.skip_one = 1;
}

AdRotator.prototype.GoToImage = function(image_num) {
    var old_image = $(this.ads[this.cur_image]);
    this.cur_image = image_num;
    var active_image = $(this.ads[this.cur_image]);
    this.RotateImages(old_image, active_image);

    this.DrawDots();
}

// Move the active dot to the correct position and hide the inactive dot for current rotator
AdRotator.prototype.DrawDots = function() {
    $('#dots .inactive_dot').show();
    $('#dots .inactive_dot').eq(this.cur_image)
        .hide().after(this.active_dot);
}

// build world hunger popup showing link to WH site in iframe
function wh_popup(title, content_id, content_width, content_height, html ) {

    //  webtrends
//    dcsMultiTrack('DCS.dcssip', 'www.pizzahut.com', 'DCS.dcsuri', '/world_hunger_relief.asp', 'WT.ti', 'Please Give to World Hunger Relief 2010', 'WT.ac', 'World Hunger Relief Donation 2010', 'DCSext.whrd2010', 'World Hunger Relief - Home Page - Thanks');

    // build World Hunger
    var ieversion = 0;
    var ie_fix_left_margin;
    var ie_fix_top_margin;
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
        var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
    }
    //alert(ieversion);
    if (ieversion !== 0 && ieversion < 8) {
        ie_fix_left_margin = 6;
        ie_fix_top_margin = 0;
    } else {
        ie_fix_left_margin = 6;
        ie_fix_top_margin = 10;
    }


    html = '<div id="wfp-iframe" style="border:0px;overflow:hidden;top:60px;left:10px;width:860px;height:485px;">' +
    '<iframe scrolling="auto" frameborder="0" src="http://www.wfpwidgets.org/sideshow/go/MjY0LTIx" hspace="0" id="wfpbox-frame" style="width:860px;height:490px;"></iframe>' +
    '</div>';

    var divs = '<div style="width: '+content_width+'px;">' +
    '   <div style="position:relative; height: 41px;">' +
    '       <div style="z-index: 5;position: absolute; top: 0px; left: 0px; width: 17px; height: 41px; background: url(\'http://quikorder.pizzahut.com/QOcontent2/images/borders/tbar/bkgdContainerTopLeft_trans.png\') no-repeat;"></div>' +
    '       <div style="z-index: 6;position: absolute; top: 0px; left: 17px; width: '+(content_width - 34)+'px; background: url(\'http://quikorder.pizzahut.com/QOcontent2/images/borders/tbar/bkgdContainerTopFill_trans.png\'); height: 41px;">' +
    '           <div class="TitleBarBoxTitle"><h2 style="margin-top:'+ie_fix_top_margin+'px">' + title + '</h2></div>' +
    '       </div>' +
    '       <div style="z-index: 5;position: absolute; top: 0px; right: 0px; width: 17px;height:41px;background: url(\'http://quikorder.pizzahut.com/QOcontent2/images/borders/tbar/bkgdContainerTopRight_trans.png\') no-repeat;"></div>' +
    '       <div style="z-index: 6; position: absolute; top: 12px; right: 12px;">' +
    '           <a href="javascript:{}" class="WindowClose">' +
    '               <img src="http://quikorder.pizzahut.com/QOcontent2/images/buttons/btnClose.png" style="display: block; border: 0px;"/>' +
    '           </a>' +
    '       </div>' +
    '   </div>' +
    '   <div style="position: relative; width: '+(content_width)+'; ">' +
    '       <div style="background: url(\'http://quikorder.pizzahut.com/QOcontent2/images/borders/tbar/bkgdContainerLeftFill_trans.png\') repeat-y; height: 100%; width: 6px; position: absolute; left: 0; top: 0;"></div>' +
    '       <div style="position: relative; margin-left:'+ie_fix_left_margin+'px; width:'+(content_width - 12)+'px;">' +
    '           <div id="' + content_id + '" class="TitleBarBox"><div style="text-align: center; height: ' + content_height + 'px;"><img style="text-align: center;" src="http://quikorder.pizzahut.com/QOcontent2/images/order-summary-ajax-loader.gif" alt="" /></div></div>' +
    '       </div>' +
    '       <div style="background: url(\'http://quikorder.pizzahut.com/QOcontent2/images/borders/tbar/bkgdContainerRightFill_trans.png\') repeat-y; height: 100%; width: 6px; position: absolute; right: 0; top: 0;"></div>' +
    '   </div>' +
    '   <div style="position:relative; height: 15px;">' +
    '       <div style="position: absolute; top: 0px; left: 0px; width: 14px; height: 14px;background: url(\'http://quikorder.pizzahut.com/QOcontent2/images/borders/tbar/bkgdContainerBtmLeft_trans.png\') no-repeat;"></div>' +
    '       <div style="position: absolute; top: 0px; left: 14px; width: '+(content_width - 28)+'px; background: url(\'http://quikorder.pizzahut.com/QOcontent2/images/borders/tbar/bkgdContainerBtmFill_trans.png\'); height: 14px;"></div>' +
    '       <div style="position: absolute; top: 0px; right: 0px; width: 14px;height:14px;background: url(\'http://quikorder.pizzahut.com/QOcontent2/images/borders/tbar/bkgdContainerBtmRight_trans.png\') no-repeat;"></div>' +
    '   </div>' +
    '</div>';


    changeLocationPopup = new Popup( divs,
    {
        closeSelector: '.WindowClose',
        zIndex: 495,
        offset: {
            top: mainPageGetScrollY() + 100
            }
    }
    );
    changeLocationPopup.show();
    $('#WH-Hunger-Detail').html( html);
    changeLocationPopup.centerPopup();

}

// For Ad ending on 11/20/2010
function StartCountdown() {
    if ($('#flashPlaceholder a img').attr('src') == 'http://quikorder.pizzahut.com/QOcontent2/images/rotator/custappreciation.jpg' ) {
        $('#flashPlaceholder a').append(
            '<div id="countdown" style="position:absolute;top:165px;left:438px;">' +

            '<div class="countdown_item" style="left:266px"></div>' +
            '<div class="countdown_item" style="left:234px"></div>' +
            '<div class="countdown_colon" style="left:218px"></div>' +
            '<div class="countdown_item" style="left:189px"></div>' +
            '<div class="countdown_item" style="left:157px"></div>' +
            '<div class="countdown_colon" style="left:141px"></div>' +
            '<div class="countdown_item" style="left:112px"></div>' +
            '<div class="countdown_item" style="left:81px"></div>' +
            '<div class="countdown_item" style="left:32px"></div>' +
            '<div class="countdown_item" style="left:0px"></div>' +
            '</div>'
        );

        AdCountdown.start(2010, 11, 20)
    }
}

var AdCountdown = {
    stop_time: false,
    days: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,
    digits: null,
    digit_width: 35,

    start:function(year, month, day) {
        // Get all the digit positions and store them
        // 0-1 = days, 2-3 = hrs, 4-5 = mins 6-7 = secs
        this.digits = $('div.countdown_item');

        month = month - 1;
        day = day + 1;
        this.stop_time = new Date(year, month, day);
        this.loop();
    },

    loop:function(year, month, day) {
        var amount = Math.floor((this.stop_time - new Date()) / 1000);

        if (amount < 0) {
            $('div.countdown_item').css('background-position', '0 0');
            return;
        }

        if (this.days != Math.floor(amount / 86400)) {
            this.days = Math.floor(amount / 86400);
            $(this.digits[7]).css('background-position', -Math.floor(this.days/10) * this.digit_width + 'px 0');
            $(this.digits[6]).css('background-position', -Math.floor(this.days%10) * this.digit_width + 'px 0');
        }
        if (this.hours != Math.floor((amount % 86400) / 3600)) {
            this.hours = Math.floor((amount % 86400) / 3600);
            $(this.digits[5]).css('background-position', -Math.floor(this.hours/10) * this.digit_width + 'px 0');
            $(this.digits[4]).css('background-position', -Math.floor(this.hours%10) * this.digit_width + 'px 0');
        }
        if (this.minutes != Math.floor((amount % 3600) / 60)) {
            this.minutes = Math.floor((amount % 3600) / 60);
            $(this.digits[3]).css('background-position', -Math.floor(this.minutes/10) * this.digit_width + 'px 0');
            $(this.digits[2]).css('background-position', -Math.floor(this.minutes%10) * this.digit_width + 'px 0');
        }
        this.seconds = Math.floor(amount % 60);
        $(this.digits[1]).css('background-position', -Math.floor(this.seconds/10) * this.digit_width + 'px 0');
        $(this.digits[0]).css('background-position', -Math.floor(this.seconds%10) * this.digit_width + 'px 0');

        this.timeout_id = setTimeout('AdCountdown.loop(' + year + ', ' + month + ', ' + day + ')', 1000);
    }
}

