﻿/// <reference path="jquery.js" />
var AdPlay = function(el, width, height, imgs, urls, alts, time) {
    this.interval = 0;
    this._time = time * 1000;
    this.nowplay = -1;
    this._images = [];
    this._urls = [];
    this._alts = [];
    this._el = $(el);
    this._width = width;
    this._height = height;
    this.init(imgs, urls,alts);
};
jQuery.extend(AdPlay.prototype, {
    init: function(imgs, urls,alts) {
        this._images = jQuery.makeArray(imgs);
        this._urls = jQuery.makeArray(urls);
        this._alts = jQuery.makeArray(alts);
        var obj = this;
        this._el.css('width', this._width).css('height', this._height);
        $.each(this._images, function(i) {
        var img = $("<a href=\"" + obj._urls[i] + "\" target=_blank><img alt=\"" + obj._alts[i] + "\" width=" + obj._width + " height=" + obj._height + " src=\"" + obj._images[i] + "\"/></a>");
            img.hide();
            img.mouseover(function() { obj.stop(); });
            img.mouseout(function() { obj.play(); });
            img.appendTo(obj._el);
        });
        obj._playad(obj);
    },
    play: function() {
        var obj = this;        
        this.interval = setInterval(function() { obj._playad(obj); }, this._time);
    },
    stop: function() {
        clearInterval(this.interval);
    },
    _playad: function(adel) {
        //alert(0);
        var max = adel._el.children('A').size();
        if (max == 1) {
            adel._el.children('A').eq(0).show();
            return;
        }

        adel._el.children('A').eq(adel.nowplay).hide();

        adel.nowplay++;
        if (adel.nowplay == max) {
            adel.nowplay = 0;
            adel._el.children('A').eq(0).show();
            return;
        }
        else
            adel._el.children('A').eq(adel.nowplay).show();

    }
});