// JavaScript Document
var links = new Array();
var colors = new Array();
function InsertAdImage(image, link,color) {
    $('.img_reel').append('<div class="img_reel_ele" style="background-image:url(' + image + ');"></div>');
    links[links.length] = link;
    colors[colors.length] = color;
}
function AdRotator() {
    this.dir = 1;
    this.loc = 1;
    this.imageWidth = 1000; //$(".body_content").width();
    this.imageSum = $(".img_reel_ele").size();
    this.imageReelWidth = this.imageWidth * this.imageSum;
    this.i = -1;
    this.window_focus = true;
    this.init = function () {
        var me = this;
        $('.body_content_arrowL').mouseenter(function () {
            if (me.loc > 1) {
                $('.body_content_arrowL').children('div').stop(true, true);
                $('.body_content_arrowL').children('div').fadeIn(500);
            }
        });
        $('.body_content_arrowL').mouseleave(function () {
            $('.body_content_arrowL').children('div').stop(true, true);
            $('.body_content_arrowL').children('div').fadeOut(500);
        });
        $('.body_content_arrowR').mouseenter(function () {
            if (me.loc < me.imageSum) {
                $('.body_content_arrowR').children('div').stop(true, true);
                $('.body_content_arrowR').children('div').fadeIn(500);
            }
        });
        $('.body_content_arrowR').mouseleave(function () {
            $('.body_content_arrowR').children('div').stop(true, true);
            $('.body_content_arrowR').children('div').fadeOut(500);
        });
        $('.body_content_arrowR').click(function () {
            if (me.loc < me.imageSum) {
                me.loc++;
                $(".img_reel").stop(true, true);
                me.rotateL();
            }
        });
        $('.body_content_arrowL').click(function () {
            if (me.loc > 1) {
                me.loc--;
                $(".img_reel").stop(true, true);
                me.rotateR();
            }
        });
        $(window).focus(function () {
            me.window_focus = true;
            me.resetTimeout();
        })
        .blur(function () {
            me.window_focus = false;
        });
        $(".img_reel").css('width', me.imageReelWidth);
        $(".img_reel").css('left', 0);
        me.resetTimeout();
    };
    this.changeBackground = function () {
        var me = this;
        if (colors[me.loc - 1] == 'black')
            $('.body').css('background-image', 'url(images/cloth-dark.jpg)');
        else
            $('.body').css('background-image', 'url(images/cloth.jpg)');
    }
    this.rotateL = function () {
        var me = this;
        $(".img_reel").animate({
            left: ('-=' + me.imageWidth)
        }, 500);
        //me.changeBackground();
        me.resetTimeout();
    };
    this.rotateR = function () {
        var me = this;
        $(".img_reel").animate({
            left: ('+=' + me.imageWidth)
        }, 500);
        //me.changeBackground();
        me.resetTimeout();
    };
    this.resetTimeout = function () {
        var me = this;
        clearTimeout(me.i);
        if (!me.window_focus)
            return;
        me.i = window.setTimeout(function () {
            me.autoAdvance();
        }, 7500);
    }
    //setup an automatic timer...
    this.autoAdvance = function () {
        
        var me = this;
        if ((me.loc >= me.imageSum && me.dir > 0) || (me.loc <= 1 && me.dir < 0)) {
            me.dir *= -1;
        }
        me.loc += me.dir;
        if (me.dir > 0)
            me.rotateL();
        else
            me.rotateR();
    };
}

