function CBanner(name,id,w,h,slides,type){
	this.htmlid = name;
	this.id = id;
	this.width = w;
	this.height = h;
	this.slides = slides;
	this.index = 0;
	this.oTimeOut = null;
	this.type = type;
	
	return this;
}

CBanner.prototype.init = function(){
	$('#'+ this.htmlid +'.gallery').css({'height': this.height +'px'});
	$('#'+ this.htmlid + '.gallery .caption').css({'max-height': this.height +'px'});

	$('#'+ this.htmlid + '.gallery a').css({opacity: 0.0});
	if(this.slides.length > 1){
		$("#"+ this.htmlid + ".gallery div[class^='sidebar']").css({opacity: 0.5});
	}else{
		$("#"+ this.htmlid + ".gallery div[class^='sidebar']").css({display:'none'});
	}
	$('#'+ this.htmlid + '.gallery .caption').css({display:'none'});
}
	
CBanner.prototype.gallery = function(showId) {
 
  if(typeof(showId)=='undefined'){
		showId = (this.index + 1)% this.slides.length;
	}

	var current = $('#'+ this.htmlid + '.gallery a:eq('+this.index+')');
	var next = $('#'+ this.htmlid + '.gallery a:eq('+showId+')');
	
  var caption = this.slides[showId].text;
	
	next.css({opacity: 0.0}).animate({opacity: 1.0}, 1000).addClass('show');
	if(showId != this.index){
		//w = current.width();
		//h = current.height();
		current.animate({opacity:0.0},1000).removeClass('show');
		// current.animate(
			// {opacity:0.0,width:0,height:0}, 
			// 1000, 
			// function(){
				// $(this).width(w).height(h)
			// }
		// ).removeClass('show');
	}
	
	if(this.type==1){
		$(".gallery .sidebar"+ (showId+1)).animate({opacity: 1.0}, 300);
		if(showId != this.index){
			$(".gallery .sidebar"+ (this.index+1)).animate({opacity: 0.5},50);
		}
	}
		
   
	if (caption != '') {
		$('#'+ this.htmlid + '.gallery .content').html(caption);
	}
	
	this.index = showId;
	if(this.slides.length > 1){
		this.oTimeOut = setTimeout('bannersGallery('+ this.id + ')',this.slides[showId].time);
	}
}
CBanner.prototype.clearTimeout = function(){
	clearTimeout(this.oTimeOut);
}
CBanner.prototype.goStepNext = function(){
	this.gallery();
}
CBanner.prototype.goStepBack = function(){
	this.gallery((this.index-1 < 0)? this.slides.length-1 : this.index-1);
}
CBanner.prototype.goStep = function(idx){
	this.gallery(idx);
}

/*****************************************/

function bannersGallery(id){
	for(var i=0; i< banners.length; i++){
		if(banners[i].id == id){
			banners[i].gallery();
			break;
		}
	}
};

/*****************************************/
// main event
$(document).ready(function() {
	if(typeof(banners)!='undefined'){
		for(var i=0; i<banners.length; i++){
			banners[i].init();
			banners[i].gallery(0);
		}

		// events
    /*
		$(".gallery div[class^='sidebar']").hover(
			function(){
				$(this).animate({opacity: 1.0}, 300);
			},
			function(){
				var bid = $(this).parents("div[id^='gallery_']").attr("id").split("_")[1];
				var sid = $(this).attr("id");
        if(sid!=''){
					var bnr = null;
					for(var i=0; i< banners.length; i++){
						if(banners[i].id == bid){
							bnr = banners[i];
							break;
						}
					}
					if(bnr!=null && bnr.index != sid.split('_')[2]-1){
						$(this).animate({opacity: 0.5},50);
					}
				}else{
					$(this).animate({opacity: 0.5},50);
				}
			}
		);
    */
		$(".gallery .sidebarP").click(function() {
			var id = $(this).parents("div[id^='gallery_']").attr("id").split("_")[1];
			for(var i=0; i< banners.length; i++){
				if(banners[i].id == id){
					banners[i].clearTimeout();
					banners[i].goStepBack();
					break;
				}
			}
		});
    
    
    $(".gallery .ban1").click(function() {
		   	var id = $(this).parents("div[id^='gallery_']").attr("id").split("_")[1];
  			for(var i=0; i< banners.length; i++){
  				if(banners[i].id == id){
  					banners[i].clearTimeout();
  					banners[i].goStep(0);
  					break;
  				}
		  	}
		});
    
  $(".gallery .ban2").click(function() {
		   	var id = $(this).parents("div[id^='gallery_']").attr("id").split("_")[1];
  			for(var i=0; i< banners.length; i++){
  				if(banners[i].id == id){
  					banners[i].clearTimeout();
  					banners[i].goStep(1);
  					break;
  				}
		  	}
		});
    
        $(".gallery .ban3").click(function() {
		   	var id = $(this).parents("div[id^='gallery_']").attr("id").split("_")[1];
  			for(var i=0; i< banners.length; i++){
  				if(banners[i].id == id){
  					banners[i].clearTimeout();
  					banners[i].goStep(2);
  					break;
  				}
		  	}
		});
    
        $(".gallery .ban4").click(function() {
		   	var id = $(this).parents("div[id^='gallery_']").attr("id").split("_")[1];
  			for(var i=0; i< banners.length; i++){
  				if(banners[i].id == id){
  					banners[i].clearTimeout();
  					banners[i].goStep(3);
  					break;
  				}
		  	}
		});
    
		$(".gallery .sidebarN").click(function() {
			var id = $(this).parents("div[id^='gallery_']").attr("id").split("_")[1];
			for(var i=0; i< banners.length; i++){
				if(banners[i].id == id){
					banners[i].clearTimeout();
					banners[i].goStepNext();
					break;
				}
			}
		});
		$(".gallery div[class^='sidebar']").click(function() {
			var id = $(this).attr("id");
			if(id!=null){
				id = id.split('_');
				if(id.length>2){
					for(var i=0; i< banners.length; i++){
						if(banners[i].id == id[1]){
							banners[i].clearTimeout();
							banners[i].goStep(id[2]-1);
							break;
						}
					}
				}
			}
		});
		
		$('.gallery').hover(
			function (){
				$(this).children('.caption').fadeIn(200);
			},
			function (){
				$(this).children('.caption').fadeOut(600);
			}
		);
	}
});

