/**

Utilz 

*/
function Class() { };

Class.prototype.construct = function() { };

Class.extend = function(def) {

    var classDef = function() {
        if (arguments[0] !== Class) {
            this.construct.apply(this, arguments);
        }
    };

    var proto = new this(Class);
    var superClass = this.prototype;

    for (var n in def) {
        var item = def[n];
        if (item instanceof Function) item.$ = superClass;
                else classDef[n] = item;
        proto[n] = item;
    }

    classDef.prototype = proto;
    classDef.extend = this.extend;

    return classDef;
};

var BannerRotator = Class.extend({
    
    construct:
        function(elementId, interval) {
            this._elementId = elementId;
            this._target = document.getElementById(this._elementId);
            this._properties = new Number();
            this.setPropertyValue('interval', interval);
            var d = new Date();
            this.setPropertyValue('lastBannerShown', this.getTime() - this.getPropertyValue('interval'));
            this.setPropertyValue('needBannerShow', false);
            this.setPropertyValue('nextBannerGetting', false);
            this.req = null;
            this.counter = 0;
            var img = new Image();
            img.src = 'images/loader.gif';
            this._target.innerHTML = this.loaderTemplate();
            this.history = new Array();
            this.forceShow = false;
            
        },
        
    setPropertyValue: 
        function(propertyId, propertyValue) {
            this._properties[propertyId] = propertyValue;
        },
    
    getPropertyValue:
        function (propertyId) {
            if (typeof this._properties[propertyId] !== 'undefined') {
                return  this._properties[propertyId]; 
            } else {
                return null;
            }
        },
        
    getBannerData:
        function() {
              
            if (this.counter > 0) {
                //alert('here');
                var i = Math.round(Math.random() * 100) % this.counter + 1;
                this.setPropertyValue('nextBannerReady', true);
                
                this.setPropertyValue('img', this.history[i]['img']);  
                this.setPropertyValue('id', this.history[i]['id']);  
                this.setPropertyValue('url', this.history[i]['url']);  
                this.setPropertyValue('title', this.history[i]['title']);  
                this.setPropertyValue('description', this.history[i]['description']);  
                
            } else {
                this.setPropertyValue('nextBannerGetting', true);
                $.ajax({
                    type: "GET",
                    url: "/banners.php",
                    data: "",
                    dataType: "json",
                    success: function(msg) {
                       // alert(msg.id);
                        this.owner.setPropertyValue('img', msg.img);  
                        this.owner.setPropertyValue('id', msg.id);  
                        this.owner.setPropertyValue('url', msg.url);  
                        this.owner.setPropertyValue('title', msg.title);  
                        this.owner.setPropertyValue('description', msg.desctiption);
                        this.owner.setPropertyValue('nextBannerReady', true);
                        this.owner.setPropertyValue('nextBannerGetting', false);
                        var i = new Image();
                        i.src = this.owner.getPropertyValue('img');
                        this.owner.addBanner();
                    },
                    error: function(request, settings){
                       // alert( "Error: " + settings);
                        isError = true;
                    },
                    owner: this
                });
            }
        },
        
    addBanner:
        function() {
            this.counter++;
            this.history[this.counter] = new Number();
            this.history[this.counter]['id'] = this.getPropertyValue('id');
            this.history[this.counter]['img'] = this.getPropertyValue('img');
            this.history[this.counter]['url'] = this.getPropertyValue('url');
            this.history[this.counter]['title'] = this.getPropertyValue('title');
            this.history[this.counter]['description'] = this.getPropertyValue('description');
            return;
            
        },
        
    
    
    displayBanner:
        function() {
            this._target.innerHTML = this.bannerTemplate();
            this.setPropertyValue('lastBannerShown', this.getTime());    
            this.setPropertyValue('needbannerShow', false);
        },
    
    smth:
        function() {
            if (this.getTime() - this.getPropertyValue('lastBannerShown') > this.getPropertyValue('interval')) {
               // alert(this.getTime() - this.getPropertyValue('lastBannerShown'));
                this.setPropertyValue('needbannerShow', true);
            }
            
            if (this.forceShow == true) {
                this.setPropertyValue('needbannerShow', true);
                this.forceShow = false;
                this._target.innerHTML = this.loaderTemplate();
            }
        
            if (this.getPropertyValue('needbannerShow')) {
                if (this._properties['nextBannerReady'] == true) {
                    this._properties['nextBannerReady'] = false;
                    this.displayBanner();
                    this.getBannerData();
                } else {
                    if (this._properties['nextBannerGetting'] == false) {
                        this.getBannerData();
                    }
                }
            }
        } ,
        
    bannerTemplate:
        function() { 
                                                          
            return '<img src="' + this._properties['img'] + '" />' + 
                    '<a href="' + this._properties['url'] + '">' + this._properties['title'] + '</a>' + 
                    '<a id="black" href="' + this._properties['url'] + '">' + this._properties['description'] + '</a>';
        },
        
    loaderTemplate:
        function() {
            return '<div style="width: 100%; text-align: center;"><img src="/bitrix/images/loader.gif" /></div>';
        },
    
    getTime:
        function() {
            var d = new Date();
            return d.getTime();    
        },
    
    getNextBanner:
        function() {
            
            this.forceShow = true;
            this.smth();
        },
    
    getPrevBanner:
        function() {
            this.forceShow = true;
            this.smth();
        }
    
    
    
});

function checkBanners()
{
    br.smth();    
}


var br = new BannerRotator('header-product', 7000);
checkBanners();
var i = setInterval(checkBanners, 1000);