function bannerRotator (options) {
	this.startRotation = startRotation;
	this.stopRotation = stopRotation;
	this.generateButtons = generateButtons;
	this.getActiveSlidePos = getActiveSlidePos;
	this.changeSlide = changeSlide;
	this.doTransition = doTransition;

	this.options = options;

	// get params
	var defaults = {
		element_id									: '#bannerrotator',
		links_container_id					: '#rotator-links',
		link_id											: '.rotator-link',
		slide_duration							: 5,
		transition									: 'default',
		show_links									: true,
		link_colors									: {background : '#fff', font: '#000', border: '#000'},
		hover_link_colors					: {background : '#D9060D', font: 'yellow', border: '#0f0'},
		active_link_colors					: {background : 'white', font: '#000', border: '#00f'},
		preloaded_slide_no					: 0,
		rotator_width								: 655,
		rotator_height							: 306,
		rotujem											: false
	};
	// set defaults if not set in options
	for (var def_option in defaults) {
		if (this.options[def_option] == undefined) {
			this.options[def_option] = defaults[def_option];
		}
	}

	this.options.element = $(this.options.element_id);
	// set aditional params
	if (this.options.is_rotating == undefined) {
		this.options.is_rotating = false;
	}

	// load all slides that should rotate
	this.slides = document.getElementById(this.options.element_id.substr(1)).childNodes[0].childNodes;

	// generate buttons if option set true
	if (this.options.show_links == true) {
		this.generateButtons();
	}

	// set visible only last img tag
	for (var i=0; i<this.slides.length; i++) {
		if (i == this.slides.length-1) {
			$(this.slides[i]).fadeTo(0, 1);
//			$(this.slides[i]).css({'display':'block'});
		} else {
			$(this.slides[i]).fadeTo(0, 0);
//			$(this.slides[i]).css({'display':'none'});
		}

		// set general css for all slides
		$(this.slides[i]).css({'width'		:this.options.rotator_width+'px'});
		$(this.slides[i]).css({'height'		:this.options.rotator_height+'px'});
		$(this.slides[i]).css({'overflow'	:'hidden'});
	}

	// start rotating if not set
	if (!this.options.is_rotating) {
		this.startRotation();
	}

	// bind onhover effect for whole ad-rotator
//	alert(this.options.element.attr('class'));
//	this.options.element.
/*	this.options.element.hover(function(){
//		alert('on');
//		alert(this.id);
		this.rotator.stopRotation();
	}, function(){
//		alert('off');
		this.rotator.startRotation();
	});*/
}


function stopRotation() {
	this.options.is_rotating = false;
	$(this).stopTime(this.options.rotationEventName);
}

function startRotation() {
	this.options.is_rotating = true;
//	this.repeater();
	this.options.rotationEventName = 'adrotator-'+this.options.element_id.substr(1);
	$(this).everyTime(this.options.slide_duration*1000, this.options.rotationEventName, function() {

		this.changeSlide();

	});
}

function changeSlide(next_pos) {
//		this.options.rotujem = true;

		// get position of visible slide
		this.getActiveSlidePos();
		// next slide is actually last in position and it rotates bacwards
		if (next_pos == undefined) {
			this.next_slide = this.active_slide_pos-1;
			this.next_slide_no = this.slides.length-this.next_slide-1;
		} else {
			this.next_slide = this.slides.length-next_pos-1;
			this.next_slide_no = next_pos;
		}

		// if next slide doesn't exists, set next slide as the last one, to rotate again from last backwards to first
		if (this.next_slide < 0) { this.next_slide = this.slides.length-1; }
		if (this.next_slide_no > this.slides.length-1) { this.next_slide_no = 0; }

		// if the active slide is the same as the next slide, then do nothing
		if (this.next_slide != this.active_slide_pos) {

			// transition effect
			this.doTransition(this.active_slide_pos, this.next_slide);

			// show active number link
			this.options.element.find(this.options.links_container_id).children().css({'background':this.options.link_colors.background});
			this.options.element.find(this.options.links_container_id).children().css({'border-color':this.options.link_colors.border});
			this.options.element.find(this.options.links_container_id).children().css({'color':this.options.link_colors.font});

			if (this.numbers != undefined) {
				$(this.numbers[this.next_slide_no]).css({'background' : this.options.active_link_colors.background});
				$(this.numbers[this.next_slide_no]).css({'border-color' : this.options.active_link_colors.border});
				$(this.numbers[this.next_slide_no]).css({'color' : this.options.active_link_colors.font});
			}

			this.temp_hover_background_color = this.options.active_link_colors.background;
			this.temp_hover_border_color = this.options.active_link_colors.border;
			this.temp_hover_font_color = this.options.active_link_colors.font;

		}

//		this.options.rotujem = false;
//		alert(this.next_slide_no);
}

function doTransition(active_s, next_s) {

	switch(this.options.transition)
	{

//	FUNCTIONALITY OF ALL TRANSITIONS
//	(FADE, SLIDE-LEFT, SLIDE-RIGHT, SLIDE-TOP, SLIDE-BOTTOM, SLIDE-IN-LEFT, SLIDE-IN-RIGHT, SLIDE-IN-TOP, SLIDE-IN-BOTTOM, SLIDE-OUT-LEFT, SLIDE-OUT-RIGHT, SLIDE-OUT-TOP, SLIDE-OUT-BOTTOM, DEFAULT)

	case 'fade':
		$(this.slides[this.active_slide_pos]).fadeTo("slow", 0);
		
		$(this.slides[this.active_slide_pos]).css({'z-index':'+1'});
		$(this.slides[this.next_slide]).css({'z-index':'+2'});
		
		$(this.slides[this.next_slide]).fadeTo("slow", 1);
		
	  break;



	case 'slide-left':
		// set starting positions
		$(this.slides[this.next_slide]).css({'left':this.options.rotator_width+'px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		// animate
		$(this.slides[this.active_slide_pos]).animate({
		    left: '-='+this.options.rotator_width+'px'
		  }, 500, function() {
		    $(this).fadeTo(0, 0);
		  });
		$(this.slides[this.next_slide]).animate({
		    left: '-='+this.options.rotator_width+'px'
		  }, 500, function() {
		  });
		break;

	case 'slide-right':
		// set starting positions
		$(this.slides[this.next_slide]).css({'left':'-'+this.options.rotator_width+'px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		// animate
		$(this.slides[this.active_slide_pos]).animate({
		    left: '+='+this.options.rotator_width+'px'
		  }, 500, function() {
		    $(this).fadeTo(0, 0);
		  });
		$(this.slides[this.next_slide]).animate({
		    left: '+='+this.options.rotator_width+'px'
		  }, 500, function() {
		  });
		break;

	case 'slide-top':
		// set starting positions
		$(this.slides[this.next_slide]).css({'top':this.options.rotator_height+'px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		// animate
		$(this.slides[this.active_slide_pos]).animate({
		    top: '-='+this.options.rotator_height+'px'
		  }, 500, function() {
		    $(this).fadeTo(0, 0);
		  });
		$(this.slides[this.next_slide]).animate({
		    top: '-='+this.options.rotator_height+'px'
		  }, 500, function() {
		  });
		break;

	case 'slide-bottom':
		// set starting positions
		$(this.slides[this.next_slide]).css({'top':'-'+this.options.rotator_height+'px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		// animate
		$(this.slides[this.active_slide_pos]).animate({
		    top: '+='+this.options.rotator_height+'px'
		  }, 500, function() {
		    $(this).fadeTo(0, 0);
		  });
		$(this.slides[this.next_slide]).animate({
		    top: '+='+this.options.rotator_height+'px'
		  }, 500, function() {
		  });
		break;







	case 'slide-in-left':
		// set starting positions
		$(this.slides[this.next_slide]).css({'left':'-'+this.options.rotator_width+'px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		$(this.slides[this.active_slide_pos]).css({'z-index':'+1'});
		$(this.slides[this.next_slide]).css({'z-index':'+2'});

		// animate
		var temp_active_slide = this.slides[this.active_slide_pos];
		$(this.slides[this.next_slide]).animate({
		    left: '+='+this.options.rotator_width+'px'
		  }, 500, function() {
		    $(temp_active_slide).fadeTo(0, 0);
		  });
		break;

	case 'slide-in-right':
		// set starting positions
		$(this.slides[this.next_slide]).css({'left':this.options.rotator_width+'px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		$(this.slides[this.active_slide_pos]).css({'z-index':'+1'});
		$(this.slides[this.next_slide]).css({'z-index':'+2'});

		// animate
		var temp_active_slide = this.slides[this.active_slide_pos];
		$(this.slides[this.next_slide]).animate({
		    left: '-='+this.options.rotator_width+'px'
		  }, 500, function() {
		    $(temp_active_slide).fadeTo(0, 0);
		  });
		break;

	case 'slide-in-top':
		// set starting positions
		$(this.slides[this.next_slide]).css({'top':'-'+this.options.rotator_height+'px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		$(this.slides[this.active_slide_pos]).css({'z-index':'+1'});
		$(this.slides[this.next_slide]).css({'z-index':'+2'});

		// animate
		var temp_active_slide = this.slides[this.active_slide_pos];
		$(this.slides[this.next_slide]).animate({
		    top: '+='+this.options.rotator_height+'px'
		  }, 500, function() {
		    $(temp_active_slide).fadeTo(0, 0);
		  });
		break;

	case 'slide-in-bottom':
		// set starting positions
		$(this.slides[this.next_slide]).css({'top':this.options.rotator_height+'px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		$(this.slides[this.active_slide_pos]).css({'z-index':'+1'});
		$(this.slides[this.next_slide]).css({'z-index':'+2'});

		// animate
		var temp_active_slide = this.slides[this.active_slide_pos];
		$(this.slides[this.next_slide]).animate({
		    top: '-='+this.options.rotator_height+'px'
		  }, 500, function() {
		    $(temp_active_slide).fadeTo(0, 0);
		  });
		break;






	case 'slide-out-left':
		// set starting positions
		$(this.slides[this.active_slide_pos]).css({'z-index':'+2'});
		$(this.slides[this.next_slide]).css({'z-index':'+1'});

		$(this.slides[this.next_slide]).css({'left':'0px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		// animate
		$(this.slides[this.active_slide_pos]).animate({
		    left: '-='+this.options.rotator_width+'px'
		  }, 500, function() {
		    $(this).fadeTo(0, 0);
		  });
		break;

	case 'slide-out-right':
		// set starting positions
		$(this.slides[this.active_slide_pos]).css({'z-index':'+2'});
		$(this.slides[this.next_slide]).css({'z-index':'+1'});

		$(this.slides[this.next_slide]).css({'left':'0px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		// animate
		$(this.slides[this.active_slide_pos]).animate({
		    left: '+='+this.options.rotator_width+'px'
		  }, 500, function() {
		    $(this).fadeTo(0, 0);
		  });
		break;

	case 'slide-out-top':
		// set starting positions
		$(this.slides[this.active_slide_pos]).css({'z-index':'+2'});
		$(this.slides[this.next_slide]).css({'z-index':'+1'});

		$(this.slides[this.next_slide]).css({'top':'0px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		// animate
		$(this.slides[this.active_slide_pos]).animate({
		    top: '-='+this.options.rotator_height+'px'
		  }, 500, function() {
		    $(this).fadeTo(0, 0);
		  });
		break;

	case 'slide-out-bottom':
		// set starting positions
		$(this.slides[this.active_slide_pos]).css({'z-index':'+2'});
		$(this.slides[this.next_slide]).css({'z-index':'+1'});

		$(this.slides[this.next_slide]).css({'top':'0px'});
		$(this.slides[this.next_slide]).fadeTo(0, 1);

		// animate
		$(this.slides[this.active_slide_pos]).animate({
		    top: '+='+this.options.rotator_height+'px'
		  }, 500, function() {
		    $(this).fadeTo(0, 0);
		  });
		break;





	default:
		$(this.slides[this.active_slide_pos]).fadeTo(0, 0);
		
		$(this.slides[this.active_slide_pos]).css({'z-index':'+1'});
		$(this.slides[this.next_slide]).css({'z-index':'+2'});
		
		$(this.slides[this.next_slide]).fadeTo(0, 1);
//		$(this.slides[this.active_slide_pos]).css({'display':'none'});
//		$(this.slides[this.next_slide]).css({'display':'block'});
	}


}
function generateButtons() {
	// generate html code for rotator links container
	rotator_links_html =  '<div id="'+this.options.links_container_id.substr(1)+'"></div>';
	this.options.element.append(rotator_links_html);

	// generate html code for links inside rotator links container
	for(var iteration=0; iteration<this.slides.length; iteration++) {
		link_html = '<a href="" class="'+this.options.link_id.substr(1)+'">'+(iteration+1)+'</a>';
		this.options.element.find(this.options.links_container_id).append(link_html);
	}
	this.options.element.find(this.options.links_container_id).append('<div style="clear:both; width:auto; height:auto;"></div>');

	// read all number-links into array
//	this.numbers = document.getElementById(this.options.links_container_id.substr(1)).childNodes;
	this.numbers = $(this.options.links_container_id, this.options.element).children();

	// set css for rotator-links container
	$(this.options.element_id+' '+this.options.links_container_id).css({
		'position' : 'absolute',
		'z-index' : '+3',
		'bottom' : '7px',
		'width' : 'auto',
		'height' : 'auto',
		'right' : '5px'
	});

	// set css for link
	$(this.options.element_id+' '+this.options.links_container_id+' '+this.options.link_id).css({
		'float'							: 'left',
		'margin-right'			: '2px',
		'padding-top'				: '1px',
		'width'							: '15px',
		'height'						: '15px',
		'text-align'				: 'center',
		'vertical-align'		: 'middle',
		'border-width'			: '1px',
		'border-style'			: 'solid',
		'border-border'			: this.options.link_colors.border,
		'background'				: this.options.link_colors.background,
		'color'							: this.options.link_colors.font,
		'text-decoration'		: 'none',
		'font-weight'				: 'bold',
		'font-size'				  : '12px',
		'opacity'				  : '0.9',
		'filter'				  : 'alpha(opacity = 90)',
		'-moz-border-radius': '3px',
    '-webkit-border-radius': '3px'
	});
	// set first number active
	if (this.numbers != undefined) {
		this.numbers[this.options.preloaded_slide_no].style.background = this.options.active_link_colors.background;
		this.numbers[this.options.preloaded_slide_no].style.borderColor = this.options.active_link_colors.border;
		this.numbers[this.options.preloaded_slide_no].style.color = this.options.active_link_colors.font;
	}

//	var rot = this.options.rotujem;
	$(this.options.links_container_id+" "+this.options.link_id, this.options.element).each(function (index) {
		$(this).bind('click', function () {
//			if (rot == false) {
//				rot = true;

				this.parentNode.parentNode.rotator.changeSlide(index);
//				rot = false;
//			}
//			alert(rot);
			return false;

		}).hover(function(){
			this.parentNode.parentNode.rotator.temp_hover_background_color = $(this).css('background');
			this.parentNode.parentNode.rotator.temp_hover_border_color = $(this).css('border-color');
			this.parentNode.parentNode.rotator.temp_hover_font_color = $(this).css('color');
			$(this).css({'background':this.parentNode.parentNode.rotator.options.hover_link_colors.background});
			$(this).css({'border-color':this.parentNode.parentNode.rotator.options.hover_link_colors.border});
			$(this).css({'color':this.parentNode.parentNode.rotator.options.hover_link_colors.font});
		}, function(){
			$(this).css({'background':this.parentNode.parentNode.rotator.temp_hover_background_color});
			$(this).css({'border-color':this.parentNode.parentNode.rotator.temp_hover_border_color});
			$(this).css({'color':this.parentNode.parentNode.rotator.temp_hover_font_color});
		});
	});
}

function getActiveSlidePos() {
	for (var i=0; i<this.slides.length; i++) {
		if ($(this.slides[i]).css('opacity') == 1) {
			this.active_slide_pos = i;
		}
	}
}
/*
// set time in sec for banner switcher
var banner_timer = 5;
var bannerrotator_active = false;

function adroller(id, transition_effect, start_position) {
	var banners = $(id+" > img");

	// if start position set then reset jquery timer
	if (start_position != undefined) {
		$(id).stopTime('bannerrotator');

	}

	if (img_src != '' && img_src != false && img_src != undefined) {

		$(id+" > img").each(function (index) {
			if(img_src == $(this).attr('src')) {
				$(this).fadeIn(1);
			} else {
				$(this).fadeOut(1);
			}
		});

	} else {



		if (!bannerrotator_active) {
			bannerrotator_active = !bannerrotator_active;
			$(id).everyTime(1000, 'bannerrotator', function() {
				$(this).append("<li>New One</li>");
			});
		}

		$('#'+id).end().find('.stop').css("cursor", "pointer").click(function() {
			if (bannerrotator_active) {
				bannerrotator_active = !bannerrotator_active;
				$(this).parents("div").find('ul').stopTime('controlled');
			}
		});




		var el = $("#"+id+" > img:visible");
//		alert(el.next().attr('src'));
//		alert($("."+id+" img:last-child").attr('src')+" == "+$(this).attr('src'));
		if (el.next().attr('src') == undefined || el.next().attr('src') == '' || el.next().attr('src') == false) {
			$("#"+id+" > img:first-child").fadeIn("fast");
		} else {
			el.next().fadeIn("fast");
		}
		el.fadeOut("slow");

	}
}
*/
$(document).ready(function() {
	$(".rotator").each(function(index, val){
		var size = new Array();
		size[0] = $(this).css('width').replace('px', '');
		size[1] = $(this).css('height').replace('px', '');

		var effect = 'fade';
		var time_out = 8;
		var showNumbers = true;

		var rotator = new bannerRotator({
			element_id									: '#'+$(this).attr('id'),
			links_container_id					: '#rotator-links',
			link_id											: '.rotator-link',
			slide_duration							: time_out,
			transition									: effect,
			show_links									: showNumbers,
			link_colors									: {background : '#303030', font: '#fff', border: '#999'},
			hover_link_colors						: {background : '#999999', font: '#000', border: '#999'},
			active_link_colors					: {background : '#999999', font: '#000', border: '#999'},
			preloaded_slide_no					: 0,
			rotator_width								: size[0],
			rotator_height							: size[1]
		});

		val.rotator = rotator;
		$(this).css({'position':'relative'});
		$(this).children("div:first-child").css({'position':'absolute', 'overflow':'hidden', 'width':size[0]+'px', 'height':size[1]+'px'});
		$(this).children("div:first-child").children().css({'position':'absolute', 'right':'0px', 'top':'0px'});
	});

});
