var banner = {
	
	offset: 0,
	size: null,
	i: 0,
	interval: null,
	tipos: new Array(),
	timeout: null,
	flashs: new Array(),
	
	start: function(){
		if ($('#banner').length < 1) return false;
		$('#banner a.left').bind('click',this.move_right);
		$('#banner a.right').bind('click',this.move_left);
		$('#banner .balls a').bind('click',function(){
			return banner.move_to($(this).attr('name'));
		});
		this.size = $('#banner .slice').length;
		var x = 0;
		$('#banner .slice').each(function(){
			if ($(this).hasClass('flash')) banner.tipos[x] = 'flash'; else banner.tipos[x] = 'image';
			banner.flashs[x] = $(this).find('object').attr('id') ? $(this).find('object').attr('id') : 0;
			x++;
		});
		setTimeout(function(){
			if (banner.tipos[banner.i] != 'flash') {
				banner.timer();
			} else {
				banner.playmovie(banner.flashs[banner.i]);
			}
		},2000);
	},
	
	thisMovie: function(movieName){
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window.document[movieName];
		} else {
			return document[movieName];
		}
	},
	
	playmovie: function(obj){
		if (obj != '' && obj != null) {
			if (document.getElementById(obj).tagName == 'OBJECT') {
				try {
					this.thisMovie(obj).start_animation();
				} catch(e) {
					setTimeout(function(){
						banner.playmovie(obj);
					},1000);
				}
			}
		}
	},
	
	stopmovie: function(obj){
		if (obj != '' && obj != null) {
			if (document.getElementById(obj).tagName == 'OBJECT') {
				try {
					this.thisMovie(obj).stop_animation();
				} catch(e) {
					setTimeout(function(){
						banner.stopmovie(obj);
					},1000);
				}
			}
		}
	},
	
	timer: function(){
		this.interval = setInterval(this.move_left, 20000);
	},
	
	move_banner: function(){
		$('#banner .balls a').removeClass('selected');
		$($('#banner .balls a')[banner.i]).addClass('selected');
		$('#banner .scroller').animate({ 'left': banner.offset + 'px' }, 400, function(){
			clearInterval(banner.interval);
			if (banner.tipos[banner.i] != 'flash') {
				banner.timer();
			} else {
				banner.playmovie(banner.flashs[banner.i]);
			}
		});
	},
	
	move_left: function(){
		clearInterval(this.timeout);
		banner.stopmovie(banner.flashs[banner.i]);
		if (banner.i < banner.size - 1) {
			banner.offset -= 960;
			banner.i++;
		} else {
			banner.offset = 0;
			banner.i = 0;
		}
		banner.move_banner();
		return false;
	},
	
	move_right: function(){
		clearInterval(this.timeout);
		if (banner.i > 0) {
			banner.stopmovie(banner.flashs[banner.i]);
			banner.offset += 960;
			banner.i--;
			banner.move_banner();
		}
		return false;
	},
	
	move_to: function(i){
		clearInterval(this.timeout);
		banner.stopmovie(banner.flashs[banner.i]);
		banner.i = i - 1;
		banner.offset = banner.i * 960 * (-1);
		banner.move_banner();
		return false;
	},
	
	finish: function(){
		this.timeout = setTimeout(function(){
			banner.move_left();
		},2000);
	}

};
