function Banner(args) 
{
	this.instanceId = Banner.instances.length;
	Banner.instances.push(this);
	document.getElementById('slides').innerHTML = '<div id="' + args.divId + '"></div>';
	this.hostId = args.divId;
	this.host =  jQuery('#' + args.divId);
	this.host.width(args.width).height(args.height);
	this.width = args.width;
	this.height = args.height;
	this.slides = args.slides;
	this.nextDelay = args.nextDelay;
	this.fadeDelay = args.fadeDelay;
	this.loaded = [];
	var out = '';
	for (var i = 0, max = this.slides.length; i < max; i++) {
		this.loaded[i] = false;
        onclickImg = '';
        styleImg = 'style="width:'+this.width+'px;height:'+this.height+'px;"';
        useMap = '';
        mapHtml = '';
        if(this.slides[i].link!=""){
            onclickImg = 'onclick="window.location=\''+this.slides[i].link+'\'"';
        }else{
            nameMap = 'useMap'+(i);
            useMap = 'usemap="#'+nameMap+'"';
            mapHtml = '<map name="'+nameMap+'">';
            mapHtml += this.slides[i].map;
            mapHtml += '</map>';
        }
        $('#'+args.divId).append('<a id="' + this.hostId + i + '" class="aBanner"><img class="imgBanner" ' + onclickImg + styleImg + ' src="' + this.slides[i].image + '" onload="Banner.instances[' + this.instanceId + '].onLoad(' + i + ')" ' + useMap + '>' + mapHtml + '</a>');
        var slide = $("#"+this.hostId + i).css({'width': 0, 'height': args.height, 'display': 'none'});
        out += '<div id="slideCtrl' + i + '" onclick="Banner.instances[0].next('+i+')"></div>';
    }
    $('#slideShowControls').html(out);
    $('#message').html(out);
    this.current = 0;
    //this.current = getRandomNumber(0, this.slides.length-1);
    //this.restart();
    this.next();
    
}

// used for soft references to a banner instance
Banner.instances = [];
Banner.updateDimensions = function(args)
{
    this.width = args.width;
    this.height = args.height;
    images = $.find("img.imgBanner");
    for(i = 0; i<images.length;i++)
    {
        images[i].style.width = this.width+"px";
        images[i].style.height = this.height+"px";
        images[i].parentNode.style.width = this.width+"px";
        images[i].parentNode.style.height = this.height+"px";
        images[i].parentNode.parentNode.style.height = this.height+"px";
        images[i].parentNode.parentNode.style.height = this.height+"px";
    }
}
Banner.prototype = {
    instanceId: null,
    width: null,
    height: null,
    nextDelay: null,
    fadeDelay: null,
    slides: null,
    hostId: null,
    host: null,
    current_group:0,
    current: 0,
    last: 0,
    zIndex: 0,
    timer: null,
    resumeTimerId: null,
    pending: false, // image is pending but not loaded yet
    loaded: null, // array of: int (slide index) => bool (loaded or not)
    onLoad: function (id) {
        this.loaded[id] = true;
        if (this.pending && id == this.current) {
            this.pending = false;
            this.next();
        }
    },
    stop: function (resume) {
        var that = this;
        if (this.timer != 'off')
            clearInterval(this.timer);
        this.timer = 'off';
        if (this.resumeTimerId !== null) {
            clearTimeout(this.resumeTimerId);
            this.resumeTimerId = null;
        }
        if (resume) {
            this.resumeTimerId = setTimeout(function () {that.next()}, this.nextDelay);
        }
    },
    next: function (current) {
        if(swUpdateSizes){
            datos = resizeImgWindow();
            swUpdateSizes = false;
        }
        this.width = datos.width;
        var that = this;
        if (typeof current != "undefined") {
            last = this.last;
            this.current = current;
            this.stop(true);
        } else {
            last = (this.current > 0 ? this.current - 1 : this.slides.length - 1) % this.slides.length;
        }
        if (this.loaded[this.current] && this.current != last) {
            for (var i = 0, max = this.slides.length; i < max; i++) {
                if(i!=this.current&&i!=last){
                    $('#' + this.hostId + i).css({'zIndex': 0});
                }
            }
            var slide = jQuery('#' + this.hostId + this.current);
            // TRICKY: animating irrelevant margin-left as a stub for modified width anim.
            // (see step() handler)
            var lastSlide = jQuery('#' + this.hostId + last);
            slide
				.css({'width': 0, 'margin-left': 0, 'display': 'none', 'zIndex': this.zIndex})
				.css({'display': 'block'})
				.animate({'margin-left': this.width}, {
				    'duration': this.fadeDelay,
				    'callback': function (current) {
				        slide.css('margin-left', 0);
				    },
				    'step': function (current) {
				        // avoid IE6 jitter by using only even widths (rounding error)
				        slide.css('width', 2 * Math.ceil(current / 2));
				    }
				});
            lastSlide.css({'zIndex': 1});
            this.zIndex = 2;
            $('#slideShowControls').css({'zIndex': this.zIndex});
            $('#slideCtrl' + this.current).css('backgroundPosition', 'left top');
            if (last != this.current) {
                $('#slideCtrl' + last).css('backgroundPosition', 'left -14px');
            }
            this.last = this.current;
            $('#message').html(this.slides[this.current].message);
            this.current = (this.current + 1) % this.slides.length;
            this.stop();
            this.timer = setInterval(function () {that.next()}, this.nextDelay);
            /*
            if (this.start) {
            setTimeout(function () { that.next() }, this.nextDelay);
            }*/
        } else {
            this.pending = true;
        }
    },
    restart:function(){
        this.stop();
        clearInterval(this.timer);
        clearTimeout(this.resumeTimerId);
    }
};
function getRandomNumber(start, end)
{
  if ((start >= 0) && (end >= 0)){
    return Math.round(Math.abs(start) + (Math.random() * (Math.abs(end) - Math.abs(start))));
  }
}
function resizeImgWindow()
{
    data = {height:$(window).height(),width:$(window).width()};
    var ratio = 700 / 1200;
    width = Math.max(1000, data.width);
    var imgWidth = Math.max(1000, Math.min((data.height - 73) / ratio, data.width));
    var imgHeight = Math.ceil(imgWidth * ratio);

    this._lastImgWidth = imgWidth;
    this._lastImgHeight = imgHeight;
    
    $('#slide img.imgBanner,#slide a.aBanner, #slide #slides,#banner,#banner img,#slide #content').css({
            'width': imgWidth,
            'height': imgHeight
    });
    winH = $(window).height() + 100; // 100px approx toolbar/tabs height

    winW  = $(window).width();

    var avL = screen.availLeft ? screen.availLeft : 0;
    var avT = screen.availTop ? screen.availTop : 0;

    if (winH + 70 < screen.availHeight || winW + 70 < screen.availWidth) {
            if (screen.availWidth > 1175) {
                    var w = Math.max(1175, winW);
                    var h = Math.max(Math.min(875, screen.availHeight), winH);

                    //window.moveTo(avL + Math.round((screen.availWidth - w)/2), avT + Math.round((screen.availHeight - h)/2));
                    //window.resizeTo(w, h);
            } else {
                    //window.moveTo(avL, avT);
                    //window.resizeTo(screen.availWidth, screen.availHeight);
            }
    }
    if(imgWidth>1420){
        dimImg = "large";
    }else if(imgWidth>1024){
        dimImg = "medium";
    }else{
        dimImg = "small";
    }
    return {width:imgWidth,height:imgHeight,dimImg:dimImg};
}
