var banners=[];
$(document).ready(function(){
		$('div.banner_container').each(function(index,element){
			banners.push(new Banner(element,0));
		});
});
$(window).load(function(){
	for(var index in banners){
		banners[index].start(index);
	};
});
function Banner(element,active){
	this.index = false;
	this.width = 0;
	this.timeout = false;
	this.slide_timeout = [];
	
	this.element=element;
	this.next_up=active;
	this.effect_duration = parseInt( $(element).attr('effect_duration') );
	this.effect_type = $(element).attr('effect_type');
	this.container=$(element).find('div.banner_slides_container');
	this.index_container=$(element).find('ul.banner_index');
	this.wrapper=$(this.container).find('div.banner_slides_wrapper');
	this.overlay = $(element).find('div.banner_overlay');
	this.slides = $(this.wrapper).find('div.banner_slide');
	var _this=this;
	$(this.slides).each(function(){
		_this.slide_timeout.push( parseInt( $(this).attr('timeout') ) );
	});
	this.max=this.slides.length -1;
	if( this.next_up > this.slides.length ){
		this.next_up = 0;
	}
	this.active=this.next_up;
	$(this.overlay).bind('click',function(){
		_this.link();
	});
	this.set_frame();
}
Banner.prototype = {
	set_frame: function(){
		this.width=$(this.container).innerWidth();
		var height=$(this.wrapper).find('img').innerHeight();
		
		$(this.wrapper).css({'height':height+'px','width':((this.max+1)*this.width)+'px','margin-left':'-'+(this.width*this.next_up)+'px','display':'block'});
		$(this.slides).css({'height':height+'px','width':this.width+'px'});
		$(this.element).animate({'height':(height+parseInt($(this.container).css('border-top-width'))+parseInt($(this.container).css('border-bottom-width')))+'px'},'fast','swing');
		$(this.container).css({'height':height+'px','width':this.width+'px'});
		
		var overlay_height=$(this.container).outerHeight(true);
		var overlay_paddingtop = (overlay_height-parseInt($(this.index_container).css('height'))-parseInt($(this.index_container).css('margin-bottom'))-parseInt($(this.container).css('margin-bottom'))-parseInt($(this.container).css('border-bottom-width')));
		var overlay_margintop = overlay_height;
		overlay_height -= overlay_paddingtop;
		
		$(this.overlay).css({'height':overlay_height+'px','margin-top':'-'+overlay_margintop+'px','behavior':'packs/css3pie/pie.htc','padding-top':overlay_paddingtop+'px'});
		$(this.index_container).css({});
	},
	start: function(index){
		this.index = index;
		
		this.set_frame();
		
		if( this.max < 1 ){
			$(this.index_container).css({'display':'none'});
			return;
		}
		
		this.build_index();
		
		switch(this.effect_type){
			default:
			case 'slide':
				this.timeout = setTimeout( 'banners['+this.index+'].slide()', this.slide_timeout[this.next_up] );
				break;
			case 'fade':
				$(this.element).find('div.banner_slide').css({'position':'absolute','z-index':'1','float':'none'});
				$(this.element).find('div.banner_slide').eq(this.next_up).css({'z-index':'2'});
				this.timeout = setTimeout( 'banners['+this.index+'].fade()', this.slide_timeout[this.next_up] );
		}
		this.increase();
	},
	build_index: function(){
		for( var i = 0; i <= this.max; i++){
			$(this.index_container).append('<li class="banner_index_button" index="'+i+'"></li>');
		}
		var _this = this;
		$(this.index_container).find('ul.banner_index').bind('click',function(event){
			event.stopPropagation();
		});
		$(this.index_container).find('li.banner_index_button').bind('click',function(event){
			event.stopPropagation();
			_this.go_to($(this).attr('index'));
		}).eq(this.next_up).addClass('banner_index_active');
	},
	increase: function(){
		this.active=this.next_up;
		this.next_up++;
		if(this.next_up>this.max){
			this.next_up=0;
		}
	},
	update_index: function(){
		$(this.index_container).find('li.banner_index_active').removeClass('banner_index_active');
		$(this.index_container).find('li.banner_index_button').eq(this.next_up).addClass('banner_index_active');
		
		if( $(this.slides).eq(this.next_up).find('a').size() == 1 ){
			$(this.overlay).addClass('banner_clickable');
		} else {
			$(this.overlay).removeClass('banner_clickable');
		}
	},
	link: function(){
		var a=$(this.slides).eq(this.active).find('a.banner_link');
		if( a.length == 1 ){
			if( $(a).attr('target') == '_blank' ){
				window.open($(a).attr('href'));
			}else{
				window.location = $(a).attr('href');
			}
		}
	},
	go_to: function(index){
		clearTimeout( this.timeout );
		this.next_up=index;
		switch(this.effect_type){
			default:
			case 'slide':
				this.slide();
				break;
			case 'fade':
				this.fade();
		}
	},
	
	slide: function(){
		var _this = this;
		$(this.wrapper).stop(true).animate({'marginLeft':'-'+(this.next_up*this.width)+'px'},this.effect_duration,'swing',function(){
			_this.update_index();
			_this.timeout=setTimeout('banners['+_this.index+'].slide()',_this.slide_timeout[_this.next_up]);
			_this.increase();
		});
	},
	fade: function(){
		var _this = this;
		_this.update_index();
		$(this.slides).stop(true).eq(this.next_up).css({'opacity':'0','z-index':'3'}).animate({'opacity':'1'},this.effect_duration,'linear',function(){
			$(_this.slides).css({'z-index':'1'}).eq(_this.next_up).css({'z-index':'2'});
			//_this.update_index();
			_this.timeout=setTimeout('banners['+_this.index+'].fade()',_this.slide_timeout[_this.next_up]);
			_this.increase();
		});
	}
};
